|
|
package controllers
import ( "fmt" "go.uber.org/zap" "hudongzhuanjia/logger" "hudongzhuanjia/utils/code" "hudongzhuanjia/utils/define" "net/http" "strconv"
"github.com/ouxuanserver/osmanthuswine/src/core" )
//解析page、pageSize
type BaseCtl struct { core.Controller Page int PageSize int }
func (t *BaseCtl) Prepare() { t.OriginResponseWriter.Header().Set("Access-Control-Allow-Origin", "*") t.OriginResponseWriter.Header().Set("Access-Control-Allow-Credentials", "true") t.OriginResponseWriter.Header().Set("Access-Control-Allow-Methods", "*") t.OriginResponseWriter.Header().Set("Access-Control-Allow-Headers", "Content-Type,Access-Token") t.OriginResponseWriter.Header().Set("Access-Control-Expose-Headers", "*") if t.Request.OriginRequest.Method == "OPTIONS" { t.OriginResponseWriter.WriteHeader(http.StatusOK) t.OriginResponseWriter.Write(nil) return } t.Page, _ = t.GetInt("page") if t.Page <= 0 { t.Page = 0 } else { t.Page = t.Page - 1 } t.PageSize, _ = t.GetInt("page_size") if t.PageSize == 0 { t.PageSize = 10 } }
func (t *BaseCtl) Bind(obj interface{}) error { return t.RequestToStruct(obj) }
func (t *BaseCtl) Get(key string) (value string, exist bool) { value, exist = t.Request.REQUEST[key] return }
func (t *BaseCtl) MustGet(key string) string { value, exist := t.Get(key) t.Assert(exist, code.MSG_ERR_Param, fmt.Sprintf("%s不能为空", key)) return value }
func (t *BaseCtl) Default(key string, def string) string { value, exist := t.Get(key) if exist { return value } return def }
func (t *BaseCtl) GetInt64(key string) (int64, bool) { v, ok := t.Get(key) if !ok { return 0, false } value, err := strconv.ParseInt(v, 10, 64) if err != nil { logger.Error("get int64 from request error", err) t.ERROR(fmt.Sprintf("%v的数据类型不为int", key), code.MSG_ERR_Param) return value, false }
return value, true }
func (t *BaseCtl) MustGetInt64(key string) int64 { value, exist := t.GetInt64(key) t.Assert(exist, code.MSG_ERR_Param, fmt.Sprintf("%v不能为空", key)) return value }
func (t *BaseCtl) DefaultInt64(key string, def int64) int64 { value, exist := t.GetInt64(key) if exist { return value } return def }
func (t *BaseCtl) GetInt(key string) (int, bool) { value, exist := t.GetInt64(key) return int(value), exist }
func (t *BaseCtl) MustGetInt(key string) int { value, exist := t.GetInt(key) t.Assert(exist, code.MSG_ERR_Param, fmt.Sprintf("%s不能为空", key)) return value }
func (t *BaseCtl) DefaultInt(key string, def int) int { value, exist := t.GetInt(key) if exist { return value } return def }
func (t *BaseCtl) GetBool(key string) (bool, bool) { v, ok := t.Get(key) if !ok { return false, false } value, err := strconv.ParseBool(v) if err != nil { logger.Error("get bool from request error", err) t.ERROR(fmt.Sprintf("%v的数据类型不为bool", key), code.MSG_ERR_Param) } return value, true }
func (t *BaseCtl) MustGetBool(key string) bool { value, exist := t.GetBool(key) t.Assert(exist, code.MSG_ERR_Param, fmt.Sprintf("%s不能为空", key)) return value }
func (t *BaseCtl) DefaultBool(key string, def bool) bool { value, exist := t.GetBool(key) if exist { return value } return def }
func (t *BaseCtl) GetDouble(key string) (float64, bool) { v, ok := t.Get(key) if !ok { return 0, false } value, err := strconv.ParseFloat(v, 64) if err != nil { logger.Error("get double from request error", err) t.ERROR(fmt.Sprintf("%v的数据类型不为double", key), code.MSG_ERR_Param) } return value, true }
func (t *BaseCtl) MustGetDouble(key string) float64 { value, exist := t.GetDouble(key) t.Assert(exist, code.MSG_ERR_Param, fmt.Sprintf("%s不能为空", key)) return value }
func (t *BaseCtl) DefaultDouble(key string, def float64) float64 { value, exist := t.GetDouble(key) if exist { return value } return def }
func (t *BaseCtl) GetFloat(key string) (float32, bool) { value, exist := t.GetDouble(key) return float32(value), exist }
func (t *BaseCtl) MustGetFloat(key string) float32 { value, exist := t.GetFloat(key) t.Assert(exist, code.MSG_ERR_Param, fmt.Sprintf("%s不能为空", key)) return value }
func (t *BaseCtl) DefaultFloat(key string, def float32) float32 { value, exist := t.GetFloat(key) if exist { return value } return def }
func (t BaseCtl) XML(data []byte) { t.OriginResponseWriter.Header().Add("Content-Type", "application/xml; charset=utf-8") t.OriginResponseWriter.Write(data) panic(nil) }
func (t *BaseCtl) ERROR(errStr string, code int, data ...string) { t.DisplayByError(errStr, code, data...) }
func (t *BaseCtl) SUCCESS(str string) { t.Display(nil, str, 0) }
func (t *BaseCtl) JSON(data interface{}) { t.DisplayByData(data) }
func (t *BaseCtl) CheckErr(err error) { if err != nil { logger.Error("check err", zap.Error(err)) t.CheckErrDisplayByError(err) } }
// false
func (t *BaseCtl) Assert(b bool, code int, msg string) { if !b { t.ERROR(msg, code) } return }
func (t *BaseCtl) CheckRunning(status string) { if status != define.StatusRunning { t.ERROR(fmt.Sprintf("该活动%s", status), code.MSG_MODULE_STATUS_NOT_RUNNING) } }
|