feat: wecom curd
This commit is contained in:
@@ -0,0 +1,127 @@
|
||||
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 && (req.UserId < utils.NumberOne || req.Type < utils.NumberOne) {
|
||||
return outResponse(utils.ErrorParams, "请传入 id 或 user_id+type"), nil
|
||||
}
|
||||
|
||||
m := model.UserModel{}.Init()
|
||||
var row dao.WecomUser
|
||||
w := modelbase.Params{Order: "id desc"}
|
||||
if req.Id > utils.NumberZero {
|
||||
w.Eq = map[string]string{"id": strconv.FormatInt(req.Id, utils.NumberTen)}
|
||||
} else {
|
||||
w.Eq = map[string]string{
|
||||
"user_id": strconv.FormatInt(req.UserId, utils.NumberTen),
|
||||
"type": strconv.FormatUint(uint64(req.Type), utils.NumberTen),
|
||||
}
|
||||
}
|
||||
if err := m.GetOne(w, &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
|
||||
}
|
||||
|
||||
name := row.Name
|
||||
if req.Name != utils.StringEmpty {
|
||||
name = req.Name
|
||||
}
|
||||
avatar := row.Avatar
|
||||
if req.Avatar != utils.StringEmpty {
|
||||
avatar = req.Avatar
|
||||
}
|
||||
typeVal := row.Type
|
||||
if req.Type > utils.NumberZero {
|
||||
typeVal = row.Type
|
||||
}
|
||||
userStatus := resolveStatus(req.Status, row.Status)
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
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{}{
|
||||
"name": name,
|
||||
"mobile": encryptMobile,
|
||||
"avatar": avatar,
|
||||
"status": userStatus,
|
||||
"type": typeVal,
|
||||
"updated_at": utils.Now(),
|
||||
}); err != nil {
|
||||
l.Errorf("wecom update user db: %v", err)
|
||||
return failResponse(utils.Fail), nil
|
||||
}
|
||||
|
||||
return okResponse(nil), nil
|
||||
}
|
||||
Reference in New Issue
Block a user