feat: rebind mobile
This commit is contained in:
@@ -0,0 +1,111 @@
|
||||
package analysislogic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"lone-services/pkg/modelbase"
|
||||
"lone-services/pkg/redis"
|
||||
"lone-services/pkg/utils"
|
||||
"lone-services/pkg/validate"
|
||||
pkgws "lone-services/pkg/ws"
|
||||
analysis "lone-services/rpc/analysis/pb"
|
||||
"lone-services/services/analysis/internal/dao"
|
||||
"lone-services/services/analysis/internal/logic/common"
|
||||
"lone-services/services/analysis/internal/model"
|
||||
"lone-services/services/analysis/internal/svc"
|
||||
"lone-services/services/analysis/validator"
|
||||
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
const imageTimeLayout = "2006-01-02 15:04"
|
||||
|
||||
type QrcodeInfoLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewQrcodeInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *QrcodeInfoLogic {
|
||||
return &QrcodeInfoLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
func (l *QrcodeInfoLogic) QrcodeInfo(in *analysis.QrcodeInfoReq) (*analysis.Response, error) {
|
||||
var req validator.QrcodeInfoValidator
|
||||
if msg := validate.ValidateFromProto(in, &req); msg != utils.StringEmpty {
|
||||
return common.OutResponse(utils.ErrorParams, msg), nil
|
||||
}
|
||||
|
||||
idStr := strconv.FormatInt(req.Id, utils.NumberTen)
|
||||
key := pkgws.RedisKeyQRShare + utils.MD5Encrypt(idStr)
|
||||
exists, err := redis.Client.Exists(l.ctx, key).Result()
|
||||
if err != nil {
|
||||
l.Errorf("qrcode info redis exists: %v", err)
|
||||
return common.FailResponse(utils.Fail), nil
|
||||
}
|
||||
if exists == 0 {
|
||||
return common.FailResponse(utils.ErrorNotFund), nil
|
||||
}
|
||||
|
||||
var detail dao.Image
|
||||
err = model.ImageModel{}.Init().GetOne(modelbase.Params{
|
||||
Eq: map[string]string{"id": idStr},
|
||||
}, &detail)
|
||||
if err != nil {
|
||||
l.Errorf("qrcode info get image: %v", err)
|
||||
return common.FailResponse(utils.Fail), nil
|
||||
}
|
||||
if detail.Id < utils.NumberOne {
|
||||
return common.FailResponse(utils.ErrorNotFund), nil
|
||||
}
|
||||
|
||||
return common.OkResponse(buildImageDetail(&detail)), nil
|
||||
}
|
||||
|
||||
func buildImageDetail(detail *dao.Image) dao.ImageDetailResource {
|
||||
var images map[string]interface{}
|
||||
if detail.Images != utils.StringEmpty {
|
||||
_ = jsoniter.UnmarshalFromString(detail.Images, &images)
|
||||
}
|
||||
|
||||
var contents map[string]string
|
||||
if detail.Contents != utils.StringEmpty {
|
||||
var raw map[string]string
|
||||
if err := jsoniter.UnmarshalFromString(detail.Contents, &raw); err == nil {
|
||||
contents = formatImageContents(raw)
|
||||
} else {
|
||||
contents = map[string]string{}
|
||||
}
|
||||
}
|
||||
|
||||
return dao.ImageDetailResource{
|
||||
Id: detail.Id,
|
||||
UserId: detail.UserId,
|
||||
TaskId: detail.TaskId,
|
||||
Images: images,
|
||||
Contents: contents,
|
||||
Cover: detail.Cover,
|
||||
CreateTime: detail.CreateTime.Format(imageTimeLayout),
|
||||
}
|
||||
}
|
||||
|
||||
func formatImageContents(data map[string]string) map[string]string {
|
||||
result := make(map[string]string, len(data))
|
||||
for k, v := range data {
|
||||
switch k {
|
||||
case "health_score":
|
||||
parts := strings.Split(v, "/")
|
||||
result[k] = strings.TrimSpace(parts[0])
|
||||
default:
|
||||
result[k] = strings.TrimSuffix(v, "%")
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
Reference in New Issue
Block a user