419 lines
13 KiB
Go
419 lines
13 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"
|
||
"git.rosy.net.cn/jx-callback/business/jxutils/jsonerr"
|
||
"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) 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
|
||
}
|
||
_, err = dao.UpdateEntity(db, user, "Type")
|
||
}
|
||
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 RegisterUserWithMobile(ctx *jxcontext.Context, user *model.User, mobileVerifyCode string, inAuthInfo *auth2.AuthInfo) (outAuthInfo *auth2.AuthInfo, err error) {
|
||
mobileAuth, err2 := auth2.Login(auth2.AuthTypeMobile, user.GetMobile(), auth2.UserIDMobile, mobileVerifyCode)
|
||
if err = err2; err == nil {
|
||
if !mobileAuth.IsUserEmpty() {
|
||
return nil, jsonerr.New(mobileAuth, model.ErrCodeJsonUserAlreadyExist)
|
||
}
|
||
user.Type = model.UserTypeConsumer
|
||
if inAuthInfo.AuthBindInfo.Type == dingding.AuthTypeStaff {
|
||
user.Type |= model.UserTypeOperator
|
||
} else {
|
||
user.Type |= model.UserTypeStoreBoss
|
||
}
|
||
if err = CreateUser(user, utils.LimitUTF8StringLen(ctx.GetRealRemoteIP()+","+inAuthInfo.GetAuthID(), 32)); err == nil {
|
||
if outAuthInfo, err = auth2.BindUser(mobileAuth, user); err == nil && inAuthInfo != nil {
|
||
err = auth2.AddAuthBind(&outAuthInfo.UserBasic, inAuthInfo)
|
||
}
|
||
} else if dao.IsDuplicateError(err) {
|
||
err = auth2.ErrUserID2AlreadyExist
|
||
}
|
||
}
|
||
return outAuthInfo, err
|
||
}
|
||
|
||
func HandleOrder4Consignee(order *model.GoodsOrder) (err error) {
|
||
var user *model.User
|
||
authType := jxutils.GetAuthType4Vendor(order.VendorID)
|
||
if authType == "" {
|
||
msg := fmt.Sprintf("平台ID:%d当前不被支持,请联系开发", order.VendorID)
|
||
globals.SugarLogger.Warn(msg)
|
||
return fmt.Errorf(msg)
|
||
}
|
||
|
||
oeratorName := order.VendorOrderID
|
||
mobileNumber := jxutils.GetRealMobile4Order(order)
|
||
db := dao.GetDB()
|
||
if mobileNumber != "" {
|
||
userList, _, err2 := dao.GetUsers(db, 0, "", nil, "", mobileNumber, 0, 0)
|
||
if err = err2; err != nil {
|
||
return err
|
||
}
|
||
if len(userList) > 0 {
|
||
user = userList[0]
|
||
}
|
||
}
|
||
|
||
vendorUserID := order.VendorUserID
|
||
if err == nil && vendorUserID != "" {
|
||
authInfo, err2 := dao.GetAuthBind(db, "", model.AuthBindTypeID, authType, vendorUserID)
|
||
if err = err2; err != nil && !dao.IsNoRowsError(err) {
|
||
return err
|
||
}
|
||
if err == nil {
|
||
vendorUserID = ""
|
||
if user == nil {
|
||
if user, err = dao.GetUserByID(db, "UserID", authInfo.UserID); err != nil {
|
||
return err
|
||
}
|
||
}
|
||
} else {
|
||
err = nil
|
||
}
|
||
}
|
||
|
||
if user == nil {
|
||
user = &model.User{
|
||
UserID2: mobileNumber,
|
||
Name: order.ConsigneeName,
|
||
Mobile: &mobileNumber,
|
||
Type: model.UserTypeConsumer,
|
||
Address: order.ConsigneeAddress,
|
||
Remark: order.VendorOrderID,
|
||
}
|
||
setUserAddress(db, user, order)
|
||
|
||
if user.GetID2() == "" {
|
||
user.UserID2 = vendorUserID
|
||
}
|
||
if user.GetID2() == "" {
|
||
user.UserID2 = order.VendorUserID
|
||
}
|
||
|
||
user.Type = model.UserTypeConsumer
|
||
err = CreateUser(user, oeratorName)
|
||
globals.SugarLogger.Debug(err)
|
||
} else {
|
||
if user.GetMobile() == "" && mobileNumber != "" {
|
||
user.Mobile = &mobileNumber
|
||
dao.UpdateEntity(db, user, "Mobile")
|
||
}
|
||
}
|
||
if err == nil {
|
||
order.UserID = user.GetID()
|
||
if vendorUserID != "" {
|
||
authBind := &model.AuthBind{
|
||
UserID: user.GetID(),
|
||
BindType: model.AuthBindTypeID,
|
||
AuthID: vendorUserID,
|
||
Type: authType,
|
||
}
|
||
dao.WrapAddIDCULDEntity(authBind, oeratorName)
|
||
authBind.Status = model.AuthBindStatusNormal
|
||
err = dao.CreateEntity(nil, authBind)
|
||
}
|
||
}
|
||
return err
|
||
}
|
||
|
||
func setUserAddress(db *dao.DaoDB, user *model.User, order *model.GoodsOrder) {
|
||
user.Address = order.ConsigneeAddress
|
||
store := &model.Store{}
|
||
store.ID = jxutils.GetSaleStoreIDFromOrder(order)
|
||
if err := dao.GetEntity(db, store); err == nil {
|
||
user.CityCode = store.CityCode
|
||
user.DistrictCode = store.DistrictCode
|
||
} else if dao.IsNoRowsError(err) {
|
||
if order.ConsigneeLng != 0 && order.ConsigneeLat != 0 {
|
||
if user.DistrictCode = api.AutonaviAPI.GetCoordinateDistrictCode(jxutils.IntCoordinate2Standard(order.ConsigneeLng), jxutils.IntCoordinate2Standard(order.ConsigneeLat)); user.DistrictCode > 0 {
|
||
if placeInfo, err := dao.GetPlaceByCode(db, user.DistrictCode); err == nil {
|
||
user.CityCode = placeInfo.ParentCode
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
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) {
|
||
globals.SugarLogger.Debugf("CreateUser user:%s, creatorName:%s", utils.Format4Output(user, true), creatorName)
|
||
|
||
if user == nil || user.UserID2 == "" || user.Name == "" {
|
||
return ErrUserIDAndNameMustGiven
|
||
}
|
||
if user.GetMobile() == "" {
|
||
user.Mobile = nil
|
||
}
|
||
if user.GetEmail() == "" {
|
||
user.Email = nil
|
||
}
|
||
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, "", model.AuthBindTypeAuth, 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, offset, pageSize int) (pagedInfo *model.PagedInfo, err error) {
|
||
userList, totalCount, err := dao.GetUsers(dao.GetDB(), userType, keyword, userIDs, userID2, mobile, offset, pageSize)
|
||
if err == nil {
|
||
pagedInfo = &model.PagedInfo{
|
||
TotalCount: totalCount,
|
||
Data: userList,
|
||
}
|
||
}
|
||
return pagedInfo, err
|
||
}
|
||
|
||
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, errCode string, err error) {
|
||
if !auth2.IsV2Token(ctx.GetToken()) {
|
||
return nil, model.ErrCodeTokenIsInvalid, model.ErrTokenIsInvalid
|
||
}
|
||
mobileNum, userID := ctx.GetMobileAndUserID()
|
||
if mobileNum == "" {
|
||
return nil, "", fmt.Errorf("不能得到用户手机号")
|
||
}
|
||
storeList, err = GetStoreList4User(ctx, mobileNum, userID)
|
||
return storeList, "", err
|
||
}
|
||
|
||
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 checkUserType(userID string, userType int8) (err error) {
|
||
userList, _, err := dao.GetUsers(dao.GetDB(), 0, "", []string{userID}, "", "", 0, 0)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
if len(userList) == 0 {
|
||
return fmt.Errorf("找不到用户:%s", userID)
|
||
}
|
||
if userList[0].Type&userType == 0 {
|
||
return fmt.Errorf("用户:%s不能用于当前操作", userID)
|
||
}
|
||
return nil
|
||
}
|
||
|
||
func AddRoles4User(ctx *jxcontext.Context, userID string, rList []*authz.RoleInfo) (err error) {
|
||
errList := errlist.New()
|
||
if err = checkUserType(userID, model.UserTypeNonConsumer); err != nil {
|
||
return err
|
||
}
|
||
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 if v.StoreID > 0 {
|
||
jxutils.HandleUserWXRemark(dao.GetDB(), userID, true)
|
||
}
|
||
} 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)
|
||
} else if v.StoreID > 0 {
|
||
jxutils.HandleUserWXRemark(dao.GetDB(), userID, true)
|
||
}
|
||
}
|
||
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 = checkUserType(v, model.UserTypeNonConsumer); err != nil {
|
||
return err
|
||
}
|
||
if err = api2.RoleMan.AddRole4User(v, r); err != nil {
|
||
errList.AddErr(err)
|
||
} else if r.StoreID > 0 {
|
||
jxutils.HandleUserWXRemark(dao.GetDB(), v, true)
|
||
}
|
||
}
|
||
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)
|
||
} else if r.StoreID > 0 {
|
||
jxutils.HandleUserWXRemark(dao.GetDB(), v, true)
|
||
}
|
||
}
|
||
return errList.GetErrListAsOne()
|
||
}
|