package controllers import ( "github.com/go-playground/validator/v10" "github.com/kataras/iris/v12" "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) 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", "") 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 }