253 lines
7.8 KiB
Go
253 lines
7.8 KiB
Go
package cms
|
||
|
||
import (
|
||
"errors"
|
||
"fmt"
|
||
|
||
"git.rosy.net.cn/baseapi/utils/errlist"
|
||
|
||
"git.rosy.net.cn/baseapi/platformapi/dingdingapi"
|
||
"git.rosy.net.cn/baseapi/utils"
|
||
"git.rosy.net.cn/jx-callback/business/auth2"
|
||
"git.rosy.net.cn/jx-callback/business/auth2/authprovider/dingding"
|
||
"git.rosy.net.cn/jx-callback/business/authz"
|
||
"git.rosy.net.cn/jx-callback/business/authz/autils"
|
||
"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"
|
||
"git.rosy.net.cn/jx-callback/globals"
|
||
"git.rosy.net.cn/jx-callback/globals/api"
|
||
"git.rosy.net.cn/jx-callback/globals/api2"
|
||
)
|
||
|
||
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",
|
||
}
|
||
)
|
||
|
||
type UserProvider struct {
|
||
}
|
||
|
||
func (*UserProvider) GetUser(authID, authIDType string) (user auth2.IUser) {
|
||
globals.SugarLogger.Debugf("GetUser, authID:%s, authIDType:%s", authID, authIDType)
|
||
fieldName := authTypeFieldMap[authIDType]
|
||
if fieldName != "" {
|
||
user2, err := dao.GetUserByID(dao.GetDB(), fieldName, authID)
|
||
if err == nil {
|
||
user = user2 // 这样写的原因是golang nil的比较问题
|
||
}
|
||
}
|
||
return user
|
||
}
|
||
|
||
func (*UserProvider) UpdateUserMobile(userID string, mobile string) (err error) {
|
||
_, err = dao.UpdateEntityLogically(dao.GetDB(), &model.User{}, map[string]interface{}{
|
||
"Mobile": mobile,
|
||
}, model.AdminName, 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,
|
||
}, model.AdminName, map[string]interface{}{
|
||
"UserID": userID,
|
||
})
|
||
return err
|
||
}
|
||
|
||
// func (*UserProvider) CreateUser(userID2, mobile, email, name string) (user auth2.IUser, err error) {
|
||
// realUser := &model.User{
|
||
// UserID2: userID2,
|
||
// Mobile: mobile,
|
||
// Email: email,
|
||
// Name: name,
|
||
// }
|
||
// return realUser, CreateUser(realUser)
|
||
// }
|
||
|
||
func init() {
|
||
auth2.Init(userProvider)
|
||
}
|
||
|
||
func RegisterUser(user *model.User, mobileVerifyCode string, inAuthInfo *auth2.AuthInfo) (outAuthInfo *auth2.AuthInfo, errCode string, err error) {
|
||
errCode = model.ErrCodeGeneralFailed
|
||
mobileAuth, err2 := auth2.Login(auth2.AuthTypeMobile, user.Mobile, auth2.UserIDMobile, mobileVerifyCode)
|
||
if err = err2; err == nil {
|
||
if !mobileAuth.IsUserEmpty() {
|
||
return nil, model.ErrCodeUserAlreadyExist, auth2.ErrUserMobileAlreadyExist
|
||
}
|
||
user.Type = model.UserTypeConsumer
|
||
if inAuthInfo.AuthBindInfo.Type == dingding.AuthTypeStaff {
|
||
user.Type |= model.UserTypeOperator
|
||
}
|
||
if err = CreateUser(user, "RegisterUser"); err == nil {
|
||
if outAuthInfo, err = auth2.BindUser(mobileAuth, user); err == nil && inAuthInfo != nil {
|
||
err = auth2.AddAuthBind(&outAuthInfo.UserBasic, inAuthInfo)
|
||
}
|
||
} else if dao.IsDuplicateError(err) {
|
||
errCode = model.ErrCodeUserAlreadyExist
|
||
err = auth2.ErrUserID2AlreadyExist
|
||
}
|
||
}
|
||
if err == nil {
|
||
errCode = model.ErrCodeSuccess
|
||
}
|
||
return outAuthInfo, errCode, 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 CreateUser(user *model.User, creatorName string) (err error) {
|
||
if user == nil || user.UserID2 == "" || user.Name == "" || user.Mobile == "" {
|
||
return ErrUserIDAndNameMustGiven
|
||
}
|
||
dao.WrapAddIDCULDEntity(user, creatorName)
|
||
user.UserID = utils.GetUUID()
|
||
user.Status = model.UserStatusNormal
|
||
return dao.CreateEntity(nil, user)
|
||
}
|
||
|
||
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
|
||
}
|
||
|
||
func OnDingDingMsg(msg map[string]interface{}) (callbackResponse *dingdingapi.CallbackResponse) {
|
||
eventType := utils.Interface2String(msg[dingdingapi.KeyEventType])
|
||
if eventType == dingdingapi.CBTagUserLeaveOrg {
|
||
var (
|
||
authBind *model.AuthBind
|
||
err error
|
||
)
|
||
db := dao.GetDB()
|
||
for _, userID := range msg[dingdingapi.KeyUserID].([]interface{}) {
|
||
userIDStr := utils.Interface2String(userID)
|
||
globals.SugarLogger.Debugf("OnDingDingMsg dingding user:%s left company", userIDStr)
|
||
if authBind, err = dao.GetAuthBind(db, "", dingding.AuthTypeStaff, userIDStr); err == nil { // 直接找到了
|
||
globals.SugarLogger.Debugf("OnDingDingMsg dingding user:%s, userID:%s left company", userIDStr, authBind.UserID)
|
||
if err = DisableUser(jxcontext.AdminCtx, authBind.UserID); err != nil {
|
||
globals.SugarLogger.Errorf("OnDingDingMsg failed with error:%v", err)
|
||
}
|
||
}
|
||
}
|
||
}
|
||
return api.DingDingAPI.Err2CallbackResponse(nil)
|
||
}
|
||
|
||
func GetUsers(ctx *jxcontext.Context, userType int, keyword string, userIDs []string, userID2, mobile string) (userList []*model.User, err error) {
|
||
return dao.GetUsers(dao.GetDB(), userType, keyword, userIDs, userID2, mobile)
|
||
}
|
||
|
||
func GetStoreList4User(ctx *jxcontext.Context, mobileNum, userID string) (storeList []*dao.StoreWithCityName, err error) {
|
||
roleList, err := api2.RoleMan.GetUserRoleList(userID)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
var (
|
||
storeIDs []int
|
||
shortRoleNameList []string
|
||
)
|
||
for _, v := range roleList {
|
||
if v.StoreID == 0 {
|
||
shortRoleNameList = append(shortRoleNameList, v.Name)
|
||
} else {
|
||
storeIDs = append(storeIDs, v.StoreID)
|
||
}
|
||
}
|
||
storeList, err = dao.GetStoreListByMobileOrStoreIDs(dao.GetDB(), mobileNum, shortRoleNameList, storeIDs)
|
||
return storeList, err
|
||
}
|
||
|
||
func GetMyStoreListNew(ctx *jxcontext.Context) (storeList []*dao.StoreWithCityName, err error) {
|
||
mobileNum, userID := ctx.GetMobileAndUserID()
|
||
if mobileNum == "" {
|
||
return nil, fmt.Errorf("不能得到用户手机号")
|
||
}
|
||
return GetStoreList4User(ctx, mobileNum, userID)
|
||
}
|
||
|
||
func GetStoreRoleList(ctx *jxcontext.Context) (roleList []*authz.RoleInfo, err error) {
|
||
return authz.StoreRoleList, nil
|
||
}
|
||
|
||
func GetUserRoleList(ctx *jxcontext.Context, userID string) (roleList []*authz.RoleInfo, err error) {
|
||
return api2.RoleMan.GetUserRoleList(userID)
|
||
}
|
||
|
||
func GetRoleUserList(ctx *jxcontext.Context, r *authz.RoleInfo) (userIDList []string, err error) {
|
||
return api2.RoleMan.GetRoleUserList(r)
|
||
}
|
||
|
||
func AddRoles4User(ctx *jxcontext.Context, userID string, rList []*authz.RoleInfo) (err error) {
|
||
errList := errlist.New()
|
||
for _, v := range rList {
|
||
if err = autils.ValidateRole(v.Name, v.StoreID); err == nil {
|
||
if err = api2.RoleMan.AddRole4User(userID, v); err != nil {
|
||
errList.AddErr(err)
|
||
}
|
||
} else {
|
||
errList.AddErr(err)
|
||
}
|
||
}
|
||
return errList.GetErrListAsOne()
|
||
}
|
||
|
||
func DeleteRoles4User(ctx *jxcontext.Context, userID string, rList []*authz.RoleInfo) (err error) {
|
||
errList := errlist.New()
|
||
for _, v := range rList {
|
||
if err = api2.RoleMan.DeleteRole4User(userID, v); err != nil {
|
||
errList.AddErr(err)
|
||
}
|
||
}
|
||
return errList.GetErrListAsOne()
|
||
}
|
||
|
||
func AddUsers4Role(ctx *jxcontext.Context, r *authz.RoleInfo, userIDList []string) (err error) {
|
||
if err = autils.ValidateRole(r.Name, r.StoreID); err != nil {
|
||
return err
|
||
}
|
||
|
||
errList := errlist.New()
|
||
for _, v := range userIDList {
|
||
if err = api2.RoleMan.AddRole4User(v, r); err != nil {
|
||
errList.AddErr(err)
|
||
}
|
||
}
|
||
return errList.GetErrListAsOne()
|
||
}
|
||
|
||
func DeleteUsers4Role(ctx *jxcontext.Context, r *authz.RoleInfo, userIDList []string) (err error) {
|
||
errList := errlist.New()
|
||
for _, v := range userIDList {
|
||
if err = api2.RoleMan.DeleteRole4User(v, r); err != nil {
|
||
errList.AddErr(err)
|
||
}
|
||
}
|
||
return errList.GetErrListAsOne()
|
||
}
|