ad4bc7de85
CI / changes (push) Successful in 13s
CI / ad (push) Successful in 0s
CI / admin (push) Successful in 0s
CI / bff (push) Successful in 0s
CI / chore (push) Successful in 0s
CI / express (push) Successful in 0s
CI / product (push) Successful in 0s
CI / sale (push) Successful in 0s
CI / user (push) Successful in 0s
CI / wecom (push) Successful in 32s
155 lines
4.1 KiB
Go
155 lines
4.1 KiB
Go
package logic
|
|
|
|
import (
|
|
"context"
|
|
"strconv"
|
|
|
|
"lone-services/pkg/modelbase"
|
|
"lone-services/pkg/utils"
|
|
"lone-services/pkg/validate"
|
|
wecomuser "lone-services/pkg/wecom/user"
|
|
wecom "lone-services/rpc/wecom/pb"
|
|
"lone-services/services/wecom/internal/client"
|
|
"lone-services/services/wecom/internal/dao"
|
|
"lone-services/services/wecom/internal/model"
|
|
"lone-services/services/wecom/internal/svc"
|
|
"lone-services/services/wecom/validator"
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
)
|
|
|
|
type UpdateUserLogic struct {
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
logx.Logger
|
|
}
|
|
|
|
func NewUpdateUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateUserLogic {
|
|
return &UpdateUserLogic{
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
Logger: logx.WithContext(ctx),
|
|
}
|
|
}
|
|
|
|
func (l *UpdateUserLogic) UpdateUser(in *wecom.UpdateUserReq) (*wecom.Response, error) {
|
|
var req validator.UpdateUserValidator
|
|
if msg := validate.ValidateFromProto(in, &req); msg != utils.StringEmpty {
|
|
return outResponse(utils.ErrorParams, msg), nil
|
|
}
|
|
if req.Id < utils.NumberOne {
|
|
return outResponse(utils.ErrorParams, "请传入 id"), nil
|
|
}
|
|
|
|
m := model.UserModel{}.Init()
|
|
var row dao.WecomUser
|
|
if err := m.GetOne(modelbase.Params{
|
|
Eq: map[string]string{"id": strconv.FormatInt(req.Id, utils.NumberTen)},
|
|
}, &row); err != nil {
|
|
l.Errorf("wecom update user get: %v", err)
|
|
return failResponse(utils.Fail), nil
|
|
}
|
|
if row.Id < utils.NumberOne {
|
|
return failResponse(utils.ErrorNotFund), nil
|
|
}
|
|
|
|
userId := row.UserId
|
|
if req.UserId > utils.NumberZero {
|
|
userId = req.UserId
|
|
}
|
|
typeVal := row.Type
|
|
if req.Type > utils.NumberZero {
|
|
typeVal = uint8(req.Type)
|
|
}
|
|
name := row.Name
|
|
if req.Name != utils.StringEmpty {
|
|
name = req.Name
|
|
}
|
|
avatar := row.Avatar
|
|
if req.Avatar != utils.StringEmpty {
|
|
avatar = req.Avatar
|
|
}
|
|
userStatus := resolveStatus(req.Status, row.Status)
|
|
|
|
// 更换 user_id / type 时校验是否与已有启用记录冲突
|
|
if userId != row.UserId || typeVal != row.Type {
|
|
var existing dao.WecomUser
|
|
if err := m.GetOne(modelbase.Params{
|
|
Eq: map[string]string{
|
|
"user_id": strconv.FormatInt(userId, utils.NumberTen),
|
|
"type": strconv.FormatUint(uint64(typeVal), utils.NumberTen),
|
|
"status": strconv.Itoa(int(dao.StatusEnabled)),
|
|
},
|
|
}, &existing); err != nil {
|
|
l.Errorf("wecom update user unique check: %v", err)
|
|
return failResponse(utils.Fail), nil
|
|
}
|
|
if existing.Id > utils.NumberZero && existing.Id != row.Id {
|
|
return failResponse(utils.ErrorDataIsExist), nil
|
|
}
|
|
}
|
|
|
|
plainMobile := utils.StringEmpty
|
|
encryptMobile := row.Mobile
|
|
if req.Mobile != utils.StringEmpty {
|
|
enc, err := utils.EncryptPhone(req.Mobile)
|
|
if err != nil {
|
|
l.Errorf("wecom update encrypt mobile: %v", err)
|
|
return failResponse(utils.Fail), nil
|
|
}
|
|
encryptMobile = enc
|
|
plainMobile = req.Mobile
|
|
} else {
|
|
dec, err := utils.DecryptPhone(row.Mobile)
|
|
if err != nil {
|
|
l.Errorf("wecom update decrypt mobile: %v", err)
|
|
return failResponse(utils.Fail), nil
|
|
}
|
|
plainMobile = dec
|
|
}
|
|
|
|
agentsJSON := row.AgentIds
|
|
if len(req.AgentIds) > utils.NumberZero {
|
|
built, aErr := buildAgentsJSON(req.AgentIds)
|
|
if aErr != nil {
|
|
l.Errorf("wecom update agents: %v", aErr)
|
|
return outResponse(utils.ErrorParams, aErr.Error()), nil
|
|
}
|
|
agentsJSON = built
|
|
}
|
|
|
|
if !utils.GetConfigBool("wecom.skip") {
|
|
updateReq := wecomuser.UpdateRequest{
|
|
UserId: row.WecomUserId,
|
|
Name: name,
|
|
Mobile: plainMobile,
|
|
Enable: statusEnable(userStatus),
|
|
}
|
|
if avatar != utils.StringEmpty {
|
|
updateReq.AvatarMediaID = avatar
|
|
}
|
|
if err := client.NewContactClient().Update(l.ctx, updateReq); err != nil {
|
|
l.Errorf("wecom http update user: %v", err)
|
|
return failWecomHTTP(), nil
|
|
}
|
|
}
|
|
|
|
if _, err := m.Edit(modelbase.Params{
|
|
Eq: map[string]string{"id": strconv.FormatInt(row.Id, utils.NumberTen)},
|
|
}, map[string]interface{}{
|
|
"user_id": userId,
|
|
"type": typeVal,
|
|
"name": name,
|
|
"mobile": encryptMobile,
|
|
"avatar": avatar,
|
|
"status": userStatus,
|
|
"agent_ids": agentsJSON,
|
|
"updated_at": utils.Now(),
|
|
}); err != nil {
|
|
l.Errorf("wecom update user db: %v", err)
|
|
return failResponse(utils.Fail), nil
|
|
}
|
|
|
|
return okResponse(nil), nil
|
|
}
|