192 lines
3.7 KiB
Go
192 lines
3.7 KiB
Go
package handlers
|
|
|
|
import (
|
|
"base-service/app/dao"
|
|
"base-service/app/models"
|
|
"base-service/app/services/wecom/message/builder"
|
|
"base-service/app/services/wecom/message/task"
|
|
util "base-service/app/services/wecom/message/utils"
|
|
"base-service/pkg/wecom/message"
|
|
"base-service/utils"
|
|
"encoding/json"
|
|
"fmt"
|
|
"strconv"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
type AddressReviewHandler struct {
|
|
}
|
|
|
|
func (h AddressReviewHandler) Build(job dao.WeComJob) ([]*message.SendRequest, error) {
|
|
|
|
logInfo := dao.EditLogInfo{}
|
|
|
|
models.EditLogModel{}.Init().GetOne(models.Params{
|
|
Eq: map[string]string{
|
|
"obj_id": strconv.Itoa(job.ObjId),
|
|
"obj_type": strconv.Itoa(int(job.ObjType)),
|
|
},
|
|
Order: "id desc",
|
|
}, &logInfo)
|
|
|
|
if logInfo.Id == dao.NumberZero {
|
|
return nil, fmt.Errorf("edit log not found")
|
|
}
|
|
|
|
// 需要返回的字段
|
|
fields := []dao.FieldConfig{
|
|
{
|
|
Field: "type_name",
|
|
Label: "门店名称",
|
|
},
|
|
{
|
|
Field: "name",
|
|
Label: "收货人",
|
|
},
|
|
{
|
|
Field: "address",
|
|
Label: "地址",
|
|
},
|
|
{
|
|
Field: "district",
|
|
Label: "地区",
|
|
},
|
|
{
|
|
Field: "mobile",
|
|
Label: "收货人电话",
|
|
},
|
|
}
|
|
|
|
content, err := BuildAddressDiff(logInfo.Content, fields)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
toUser := utils.GetConfigString("wecom.address_reviewer_ids")
|
|
|
|
taskId := uuid.NewString()
|
|
|
|
detailUrl := fmt.Sprintf("https://baidu.com/xxx/review?id=%d", job.ObjId)
|
|
|
|
err = task.Service{}.Create(job, taskId, detailUrl)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
req, err := builder.ButtonCard(
|
|
builder.ButtonCardParams{
|
|
ToUser: toUser,
|
|
TaskId: taskId,
|
|
Title: "地址信息变更审核",
|
|
Content: content,
|
|
Buttons: []message.Button{
|
|
{
|
|
Text: "拒绝",
|
|
Style: dao.NumberTwo,
|
|
Key: fmt.Sprintf("%s:reject:%s", util.GetExtString(job.Ext, "key"), taskId),
|
|
},
|
|
{
|
|
Text: "通过",
|
|
Style: dao.NumberOne,
|
|
Key: fmt.Sprintf("%s:agree:%s", util.GetExtString(job.Ext, "key"), taskId),
|
|
},
|
|
},
|
|
},
|
|
)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return []*message.SendRequest{
|
|
req,
|
|
}, nil
|
|
}
|
|
|
|
func BuildAddressDiff(content string, fields []dao.FieldConfig) ([]message.HorizontalContent, error) {
|
|
|
|
var data struct {
|
|
Old map[string]interface{} `json:"old"`
|
|
New map[string]interface{} `json:"new"`
|
|
}
|
|
|
|
if err := json.Unmarshal([]byte(content), &data); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
result := make([]message.HorizontalContent, 0)
|
|
|
|
districtOld := util.GetMapString(data.Old, "district")
|
|
districtNew := util.GetMapString(data.New, "district")
|
|
|
|
for _, field := range fields {
|
|
|
|
// district 不单独显示
|
|
if field.Field == "district" {
|
|
continue
|
|
}
|
|
|
|
oldValue := util.GetMapString(data.Old, field.Field)
|
|
newValue := util.GetMapString(data.New, field.Field)
|
|
|
|
// type_name 新数据可能不存在
|
|
if field.Field == "type_name" && newValue == dao.StringEmpty {
|
|
newValue = oldValue
|
|
}
|
|
|
|
// 地址拼接地区
|
|
if field.Field == "address" {
|
|
|
|
if districtOld != dao.StringEmpty {
|
|
oldValue = districtOld + oldValue
|
|
}
|
|
|
|
if districtNew != dao.StringEmpty {
|
|
newValue = districtNew + newValue
|
|
}
|
|
}
|
|
|
|
// old 数据,手机号解密
|
|
if field.Field == "mobile" {
|
|
|
|
if oldValue != dao.StringEmpty {
|
|
|
|
decryptOld, err := utils.Decrypt(oldValue)
|
|
if err == nil {
|
|
oldValue = decryptOld
|
|
}
|
|
}
|
|
|
|
// if newValue != dao.StringEmpty {
|
|
|
|
// decryptNew, err := utils.Decrypt(newValue)
|
|
// if err == nil {
|
|
// newValue = decryptNew
|
|
// }
|
|
// }
|
|
}
|
|
|
|
value := newValue
|
|
|
|
if oldValue != dao.StringEmpty &&
|
|
oldValue != dao.StringZero &&
|
|
oldValue != newValue {
|
|
|
|
value = fmt.Sprintf(
|
|
"%s → %s",
|
|
oldValue,
|
|
newValue,
|
|
)
|
|
}
|
|
|
|
result = append(result, message.HorizontalContent{
|
|
KeyName: field.Label,
|
|
Value: value,
|
|
})
|
|
}
|
|
|
|
return result, nil
|
|
}
|