77 lines
1.7 KiB
Go
77 lines
1.7 KiB
Go
package user
|
|
|
|
import (
|
|
"fmt"
|
|
"go_dreamfactory/lego/core"
|
|
"go_dreamfactory/lego/sys/log"
|
|
"go_dreamfactory/modules"
|
|
"go_dreamfactory/pb"
|
|
"time"
|
|
|
|
uuid "github.com/satori/go.uuid"
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
)
|
|
|
|
const ( //Redis
|
|
TableUser core.SqlTable = "user" //用户表
|
|
)
|
|
|
|
type ModelUser struct {
|
|
modules.MCompModel
|
|
}
|
|
|
|
func (this *ModelUser) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) {
|
|
err = this.MCompModel.Init(service, module, comp, options)
|
|
this.TableName = string(TableUser)
|
|
return
|
|
}
|
|
|
|
func (this *ModelUser) FindByAccount(sid int32, account string) (*pb.DBUser, error) {
|
|
filter := bson.M{
|
|
"sid": sid,
|
|
"binduid": account,
|
|
}
|
|
sr := this.DB.FindOne(TableUser, filter)
|
|
var nd *pb.DBUser
|
|
err := sr.Decode(&nd)
|
|
return nd, err
|
|
}
|
|
|
|
//查询昵称
|
|
func (this *ModelUser) NickNameIsExist(name string) bool {
|
|
if err := this.DB.FindOne(TableUser, bson.M{"name": name}).Err(); err != nil {
|
|
if err == mongo.ErrNoDocuments { //无记录
|
|
return true
|
|
}
|
|
|
|
}
|
|
return false
|
|
}
|
|
|
|
func (this *ModelUser) User_Create(user *pb.DBUser) (err error) {
|
|
_id := primitive.NewObjectID().Hex()
|
|
user.Id = _id
|
|
user.Uid = fmt.Sprintf("%d_%s", user.Sid, _id)
|
|
user.Uuid = uuid.NewV4().String()
|
|
user.Lv = 1 //初始等级
|
|
user.Ctime = time.Now().Unix()
|
|
return this.Add(user.Uid, user)
|
|
}
|
|
|
|
//获取用户
|
|
func (this *ModelUser) GetUser(uid string) (user *pb.DBUser) {
|
|
user = &pb.DBUser{}
|
|
if err := this.Get(uid, user); err != nil {
|
|
log.Errorf("getUser err:%v", err)
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
//设置属性
|
|
func (this *ModelUser) updateUserAttr(uid string, data map[string]interface{}) error {
|
|
return this.Change(uid, data)
|
|
}
|