135 lines
2.9 KiB
Go
135 lines
2.9 KiB
Go
package user
|
|
|
|
import (
|
|
"fmt"
|
|
"go_dreamfactory/comm"
|
|
"go_dreamfactory/lego/core"
|
|
"go_dreamfactory/lego/sys/redis"
|
|
"go_dreamfactory/modules"
|
|
"go_dreamfactory/pb"
|
|
"go_dreamfactory/sys/configure"
|
|
"go_dreamfactory/utils"
|
|
"time"
|
|
|
|
"github.com/spf13/cast"
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
"go.mongodb.org/mongo-driver/x/bsonx"
|
|
)
|
|
|
|
type ModelSetting struct {
|
|
modules.MCompModel
|
|
module *User
|
|
}
|
|
|
|
func (this *ModelSetting) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) {
|
|
this.TableName = comm.TableSetting
|
|
err = this.MCompModel.Init(service, module, comp, options)
|
|
this.module = module.(*User)
|
|
this.DB.CreateIndex(core.SqlTable(this.TableName), mongo.IndexModel{
|
|
Keys: bsonx.Doc{{Key: "uid", Value: bsonx.Int32(1)}},
|
|
})
|
|
return
|
|
}
|
|
|
|
// 初始用户设置
|
|
func (this *ModelSetting) InitSetting(uid string) {
|
|
setting := &pb.DBUserSetting{
|
|
Uid: uid,
|
|
Huazhi: 0,
|
|
Kangjuchi: 2,
|
|
Gaoguang: false,
|
|
Wuli: false,
|
|
Guaji: true,
|
|
Fuben: true,
|
|
Huodong: true,
|
|
Tansuo: true,
|
|
Saiji: true,
|
|
Xuanshang: true,
|
|
}
|
|
if err := this.Add(uid, setting); err != nil {
|
|
this.module.Errorf("InitSetting err:%v", err)
|
|
}
|
|
}
|
|
|
|
// 用户设置获取
|
|
func (this *ModelSetting) GetSetting(uid string) *pb.DBUserSetting {
|
|
setting := &pb.DBUserSetting{}
|
|
if err := this.Get(uid, setting); err != nil {
|
|
return nil
|
|
}
|
|
return setting
|
|
}
|
|
|
|
//更新设置
|
|
func (this *ModelSetting) UpdateSetting(uid string, data map[string]interface{}) error {
|
|
if len(data) == 0 {
|
|
return nil
|
|
}
|
|
return this.Change(uid, data)
|
|
}
|
|
|
|
//校验时间和初始次数
|
|
func (this *ModelSetting) checkInitCount(uid string) bool {
|
|
ue, err := this.module.modelExpand.GetUserExpand(uid)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
if ue != nil {
|
|
//验证时间
|
|
tt := time.Unix(ue.LastInitdataTime, 0)
|
|
if !configure.Now().After(tt) {
|
|
return false
|
|
}
|
|
//判断上次初始的时间
|
|
if utils.IsToday(ue.LastInitdataTime) {
|
|
if ue.InitdataCount >= 3 {
|
|
return false
|
|
}
|
|
ue.InitdataCount++
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
//验证码
|
|
func (this *ModelSetting) checkVeriCode(uid string) (int32, bool) {
|
|
key := fmt.Sprintf("code:%s", uid)
|
|
var code int32
|
|
err := this.Redis.Get(key, &code)
|
|
if err != nil {
|
|
if err == redis.RedisNil {
|
|
return 0, false
|
|
} else {
|
|
this.module.Errorf("%v", err)
|
|
}
|
|
return 0, false
|
|
}
|
|
|
|
return code, true
|
|
}
|
|
|
|
// 刷新验证码
|
|
func (this *ModelSetting) refresh(uid string) (code int32) {
|
|
var ok bool
|
|
key := fmt.Sprintf("code:%s", uid)
|
|
if code, ok = this.checkVeriCode(uid); ok {
|
|
return
|
|
} else {
|
|
code = cast.ToInt32(utils.GenValidateCode(6))
|
|
if err := this.Redis.Set(key, code, time.Second*60); err != nil {
|
|
this.module.Errorf("%v", err)
|
|
return 0
|
|
}
|
|
return
|
|
}
|
|
}
|
|
|
|
// 清空设置
|
|
func (this *ModelSetting) cleanData(uid string) {
|
|
if err := this.DelByUId(uid); err != nil {
|
|
this.module.Errorf("cleanData err:%v", err)
|
|
}
|
|
}
|