118 lines
3.0 KiB
Go
118 lines
3.0 KiB
Go
package cms
|
|
|
|
import (
|
|
"errors"
|
|
"sync"
|
|
"time"
|
|
|
|
"git.rosy.net.cn/baseapi/utils"
|
|
"git.rosy.net.cn/jx-callback/business/auth2"
|
|
"git.rosy.net.cn/jx-callback/business/jxutils/jxcontext"
|
|
"git.rosy.net.cn/jx-callback/business/model"
|
|
"git.rosy.net.cn/jx-callback/business/model/dao"
|
|
)
|
|
|
|
var (
|
|
ErrUserIDAndNameMustGiven = errors.New("用户ID2和用户名必须不为空")
|
|
)
|
|
|
|
var (
|
|
userProvider = &UserProvider{}
|
|
authTypeFieldMap = map[string]string{
|
|
auth2.UserIDID: "user_id",
|
|
auth2.UserIDID2: "user_id2",
|
|
auth2.UserIDMobile: "mobile",
|
|
auth2.UserIDEmail: "email",
|
|
}
|
|
jdUsersStruct GetJdUsersStruct
|
|
titleListJdUser = []string{
|
|
"用户名",
|
|
"关联门店",
|
|
"所属角色",
|
|
"状态",
|
|
}
|
|
)
|
|
|
|
type GetJdUsersStruct struct {
|
|
locker sync.RWMutex
|
|
userMap []JdUserStruct
|
|
}
|
|
|
|
type JdUserStruct struct {
|
|
UserName string `json:"用户名"`
|
|
StoreIDs string `json:"关联门店"`
|
|
RoleName string `json:"所属角色"`
|
|
Status string `json:"状态"`
|
|
}
|
|
|
|
type UserProvider struct {
|
|
}
|
|
|
|
func (*UserProvider) UpdateUserMobile(userID string, mobile string) (err error) {
|
|
_, err = dao.UpdateEntityLogically(dao.GetDB(), &model.User{}, map[string]interface{}{
|
|
"Mobile": mobile,
|
|
}, userID, map[string]interface{}{
|
|
"UserID": userID,
|
|
})
|
|
return err
|
|
}
|
|
|
|
func (*UserProvider) UpdateUserEmail(userID string, email string) (err error) {
|
|
_, err = dao.UpdateEntityLogically(dao.GetDB(), &model.User{}, map[string]interface{}{
|
|
"Email": email,
|
|
}, userID, map[string]interface{}{
|
|
"UserID": userID,
|
|
})
|
|
return err
|
|
}
|
|
|
|
func (*UserProvider) UpdateUserType(userID string, userTypeMask int8, updateType int) (err error) {
|
|
db := dao.GetDB()
|
|
user := &model.User{
|
|
UserID: userID,
|
|
}
|
|
if err = dao.GetEntity(db, user, "UserID"); err == nil {
|
|
if updateType == auth2.UpdateUserTypeAdd {
|
|
user.Type |= userTypeMask
|
|
} else if updateType == auth2.UpdateUserTypeDelete {
|
|
user.Type &= ^userTypeMask
|
|
} else {
|
|
user.Type = userTypeMask
|
|
}
|
|
dao.WrapUpdateULEntity(user, userID)
|
|
_, err = dao.UpdateEntity(db, user, "Type", model.FieldUpdatedAt, model.FieldLastOperator)
|
|
}
|
|
return err
|
|
}
|
|
|
|
func (*UserProvider) UpdateLastLogin(userID string, lastLoginType, fromIP string) (err error) {
|
|
_, err = dao.UpdateEntityLogically(dao.GetDB(), &model.User{}, map[string]interface{}{
|
|
"LastLoginAt": utils.Time2Pointer(time.Now()),
|
|
"LastLoginType": lastLoginType,
|
|
"LastLoginIP": fromIP,
|
|
}, userID, map[string]interface{}{
|
|
"UserID": userID,
|
|
})
|
|
return err
|
|
}
|
|
|
|
func GetUserBindAuthInfo(ctx *jxcontext.Context) (authList []*model.AuthBind, err error) {
|
|
authInfo, err := ctx.GetV2AuthInfo()
|
|
if err == nil {
|
|
return auth2.GetUserBindAuthInfo(authInfo.GetID())
|
|
}
|
|
return nil, err
|
|
}
|
|
|
|
func DisableUser(ctx *jxcontext.Context, userID string) (err error) {
|
|
userName := ctx.GetUserName()
|
|
if _, err = dao.UpdateEntityLogically(dao.GetDB(), &model.User{}, map[string]interface{}{
|
|
"Status": model.UserStatusDisabled,
|
|
}, userName, map[string]interface{}{
|
|
"UserID": userID,
|
|
}); err == nil {
|
|
auth2.DisableUser(userID, userName)
|
|
}
|
|
return err
|
|
}
|