package logic import ( "encoding/json" "fmt" "fusenapi/utils/auth" "fusenapi/utils/basic" "fusenapi/utils/check" "strings" "context" "fusenapi/server/info/internal/svc" "fusenapi/server/info/internal/types" "github.com/zeromicro/go-zero/core/logx" "gorm.io/gorm" ) type InfoLogic struct { logx.Logger ctx context.Context svcCtx *svc.ServiceContext } func NewInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *InfoLogic { return &InfoLogic{ Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx, } } // 处理进入前逻辑w,r // func (l *InfoLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) { // } // 这个与表名强关联 var ModuleTable map[string]string = map[string]string{ "userinfo": "fs_user_info", "material": "fs_user_material", } type ModuleQuery struct { TableName string ModuleName string Cond string ModuleQuery map[string]struct{} } func (mquery *ModuleQuery) EncodeQuery(field string) string { var qstr []byte = []byte("JSON_OBJECT(") for query := range mquery.ModuleQuery { if query != "" { query = "." + query } qstr = append(qstr, []byte(fmt.Sprintf("'%s%s', JSON_EXTRACT(%s,'$%s'),", mquery.ModuleName, query, field, query))...) } if qstr[len(qstr)-1] == ',' { qstr[len(qstr)-1] = ')' } else { qstr = append(qstr, ')') } return string(qstr) } func (l *InfoLogic) Info(req *types.UserInfoRequest, userinfo *auth.UserInfo) (resp *basic.Response) { // 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data) // userinfo 传入值时, 一定不为null var condUser string switch userinfo.GetIdType() { case auth.IDTYPE_User: condUser = fmt.Sprintf("user_id = %d", userinfo.UserId) case auth.IDTYPE_Guest: condUser = fmt.Sprintf("guest_id = %d", userinfo.GuestId) default: condUser = "user_id = 0 and guest_id = 0" } var mquerys map[string]*ModuleQuery = make(map[string]*ModuleQuery) var metadict map[string]any = make(map[string]any) for _, module := range req.Module { if !check.CheckModuleQuery(module) { return resp.SetStatusWithMessage(basic.CodeApiErr, fmt.Sprintf("%s format is error", module)) } mlist := strings.Split(module, ".") if len(mlist) < 2 { return resp.SetStatusWithMessage(basic.CodeApiErr, fmt.Sprintf("%s format error", module)) } mtable := mlist[0] tname, ok := ModuleTable[mtable] if !ok { return resp.SetStatusWithMessage(basic.CodeApiErr, fmt.Sprintf("%s format error, table %s not found", module, tname)) } moduleName := mlist[1] cond := fmt.Sprintf("module = '%s' and %s", moduleName, condUser) if mquery, ok := mquerys[mtable]; ok { mquery.ModuleQuery[strings.Join(mlist[2:], ",")] = struct{}{} } else { mquery := &ModuleQuery{ TableName: tname, ModuleName: mtable + "." + moduleName, Cond: cond, ModuleQuery: map[string]struct{}{strings.Join(mlist[2:], ","): {}}} mquerys[mtable] = mquery } } for _, mquery := range mquerys { sqlstr := fmt.Sprintf("select id, module, %s as querydata from %s where %s order by ctime asc limit 1", mquery.EncodeQuery("metadata"), mquery.TableName, mquery.Cond) raw := l.svcCtx.MysqlConn.Raw(sqlstr) if raw.Error != nil { if raw.Error == gorm.ErrRecordNotFound { continue } else { logx.Error(raw.Error) return resp.SetStatusWithMessage(basic.CodeApiErr, raw.Error.Error()) } } var info map[string]any = make(map[string]any) err := raw.Scan(&info).Error if err == gorm.ErrRecordNotFound { continue } if err != nil { logx.Error(err, mquery.EncodeQuery("metadata")) return resp.SetStatusWithMessage(basic.CodeApiErr, err.Error()) } queryjson, ok := info["querydata"].(string) if !ok { return resp.SetStatusWithMessage(basic.CodeDbSqlErr, mquery.ModuleName) } var querydata map[string]any = make(map[string]any) err = json.Unmarshal([]byte(queryjson), &querydata) if err != nil { logx.Error(info) return resp.SetStatusWithMessage(basic.CodeApiErr, err.Error()) } for k, v := range querydata { metadict[k] = v } } return resp.SetStatus(basic.CodeOK, metadict) } // 处理逻辑后 w,r 如:重定向, resp 必须重新处理 // func (l *InfoLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) { // // httpx.OkJsonCtx(r.Context(), w, resp) // }