package controllers import ( "github.com/go-playground/validator/v10" "github.com/kataras/iris/v12" "github.com/stretchr/objx" "io" "log" "xps/cache" "xps/viewmodels" ) var ( validate *validator.Validate ) func init() { validate = validator.New() } func errorData(errs ...error) { var s string for _, err := range errs { if err != nil { s += err.Error() + "
" } } } type Base struct { Ctx iris.Context } func (b *Base) GetCurOcId() int64 { ocId, _ := b.Ctx.Values().GetInt64("ocId") return ocId } func (b *Base) GetCurUserId() int64 { userId, _ := b.Ctx.Values().GetInt64("userId") return userId } func (b *Base) GetOcTypeId() int64 { ocTypeId, _ := b.Ctx.Values().GetInt64("ocTypeId") return ocTypeId } func (b *Base) GetAppKey() string { appKey := b.Ctx.Values().GetString("appKey") return appKey } func (b *Base) GetAppSecret() string { appSecret := b.Ctx.Values().GetString("appSecret") return appSecret } func (b *Base) GetJsonData() (map[string]interface{}, error) { data, err := io.ReadAll(b.Ctx.Request().Body) if err != nil { return nil, err } m, err := objx.FromJSON(string(data)) if err != nil { return nil, err } return m, nil } func (b *Base) buildParams() map[string]interface{} { ocId, err := b.Ctx.Values().GetInt64("ocId") if err != nil { _ = b.Ctx.JSON(ResultErr(err.Error(), nil)) b.Ctx.Next() } userId, err := b.Ctx.Values().GetInt64("userId") if err != nil { _ = b.Ctx.JSON(ResultErr(err.Error(), nil)) b.Ctx.Next() } params := make(map[string]interface{}) params["ocId"] = ocId params["userId"] = userId groupId := b.Ctx.URLParamInt64Default("groupId", 0) if groupId > 0 { params["groupId"] = groupId } limit := b.Ctx.URLParamIntDefault("limit", 0) if limit <= 0 { limit = 10 } params["limit"] = limit page := b.Ctx.URLParamIntDefault("page", 0) if page <= 0 { page = 1 } params["page"] = page keywords := b.Ctx.URLParamDefault("keywords", "") if keywords != "" { params["keywords"] = keywords } return params } // RetCode Result Code type RetCode int // Result Code List const ( RetOk RetCode = 0 RetErr RetCode = 500 ) type QueryConditions struct { OcId int64 `url:"ocId"` GroupId int64 `url:"groupId"` UserId int64 `url:"userId"` Page int `url:"page"` Limit int `url:"limit"` Keywords string `url:"keywords"` } type JsonResult struct { Code RetCode `json:"code"` Msg interface{} `json:"msg"` Data interface{} `json:"data"` } func ResultOk(msg string, objects interface{}) (r *JsonResult) { r = &JsonResult{Code: RetOk, Msg: msg, Data: objects} return } func ResultErr(msg string, objects interface{}) (r *JsonResult) { r = &JsonResult{Code: RetErr, Msg: msg, Data: objects} return } func GetCurUser(userId int64) *viewmodels.UserData { userData, err := cache.GetUserData(userId) if err != nil { log.Fatal(err.Error()) } return userData }