1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- package frame
- import (
- "github.com/gin-gonic/gin"
- "github.com/go-playground/validator/v10"
- )
- // 重新封装上下文对象
- type Context struct {
- *gin.Context
- }
- type Response struct {
- Status int `json:"status"` // 返回状态值
- Msg string `json:"msg"` //返回的提示语
- Data interface{} `json:"data"` //返回数据
- }
- func (this *Context) Success(data ...interface{}) interface{} {
- response := Response{
- Status: 200,
- Msg: "操作成功",
- Data: nil,
- }
- for _, datum := range data {
- switch datum.(type) {
- case string:
- response.Msg = datum.(string)
- case interface{}:
- response.Data = datum.(interface{})
- }
- }
- return response
- }
- func (this *Context) Ok(data ...interface{}) {
- response := Response{
- Status: 200,
- Msg: "操作成功",
- Data: nil,
- }
- for _, datum := range data {
- switch datum.(type) {
- case string:
- response.Msg = datum.(string)
- case interface{}:
- response.Data = datum.(interface{})
- }
- }
- this.JSON(200, response)
- }
- const FAST_BODY_COLNUM = "fast_body_colnum"
- func (this *Context) Cols() []string {
- cols, bl := this.Get(FAST_BODY_COLNUM)
- /* rts := make([]string, 0)*/
- if bl {
- /*old := cols.([]string)
- for i := 0; i < st.NumField(); i++ {
- if collection.Collect(old).Contains(st.Field(i).Name) {
- rts = append(rts, st.Field(i).Name)
- }
- }
- return rts*/
- return cols.([]string)
- }
- return make([]string, 0)
- }
- func (this *Context) Error(data ...interface{}) {
- response := Response{
- Status: BUSINESS_CODE,
- Msg: "操作失败",
- Data: nil,
- }
- for _, datum := range data {
- switch datum.(type) {
- case string:
- response.Msg = datum.(string)
- case interface{}:
- response.Data = datum.(interface{})
- }
- }
- this.JSON(200, response)
- }
- func (this *Context) ValidteError(err error, req interface{}) {
- if err != nil {
- Throw(BUSINESS_CODE, GetError(err.(validator.ValidationErrors), req))
- }
- }
|