123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- package controllers
- import (
- "encoding/json"
- "fmt"
- "github.com/beego/beego/v2/server/web"
- "hongze/hongze_mobile_admin/utils"
- "net/url"
- )
- //不需要授权的基类
- type BaseCommon struct {
- web.Controller
- Response
- }
- type Response struct {
- Code int `json:"code"`
- Data interface{} `json:"data"`
- Msg string `json:"msg"`
- ErrMsg string `json:"errMsg" description:"给开发工程师看的错误信息"`
- }
- const (
- SUCCESS = 200 //成功
- ERROR = 400 //代表业务处理失败,前端同学需要做额外逻辑处理
- TOKEN_ERROR = 401 //代表token异常,用户需要重新静默授权,获取最新的token
- BIND_ERROR = 403 //403代表用户需要进行绑定操作,需要跳转到输入账号密码绑定页面
- SIGN_ERROR = 3 //签名异常
- WX_CODE_USED_ERROR = 4011 //待表微信授权code已使用
- )
- //返回数据
- func (c BaseCommon) Result() {
- var content []byte
- var err error
- content, err = json.Marshal(c.Response)
- ip := c.Ctx.Input.IP()
- requestBody, err := url.QueryUnescape(string(c.Ctx.Input.RequestBody))
- if err != nil {
- fmt.Println("base auth url.QueryUnescape Err:", err.Error())
- requestBody = string(c.Ctx.Input.RequestBody)
- }
- utils.ApiLog.Println("请求地址:", c.Ctx.Input.URI(), "Authorization:", c.Ctx.Input.Header("Authorization"), "RequestBody:", requestBody, "ResponseBody", string(content), "IP:", ip)
- c.Controller.Data["json"] = c.Response
- c.Controller.ServeJSON()
- c.StopRun()
- }
- //没有任何信息的返回
- func (c BaseCommon) Ok() {
- c.Response.Code = SUCCESS
- c.Response.Msg = "操作成功"
- c.Response.Data = map[string]interface{}{}
- c.Result()
- }
- func (c BaseCommon) OkWithMessage(message string) {
- c.Response.Code = SUCCESS
- c.Response.Msg = message
- c.Response.Data = map[string]interface{}{}
- c.Result()
- }
- func (c BaseCommon) OkWithData(data interface{}) {
- c.Response.Code = SUCCESS
- c.Response.Msg = "操作成功"
- c.Response.Data = data
- c.Result()
- }
- func (c BaseCommon) OkDetailed(data interface{}, message string) {
- c.Response.Code = SUCCESS
- c.Response.Msg = message
- c.Response.Data = data
- c.Result()
- }
- func (c BaseCommon) Fail() {
- c.Response.Code = ERROR
- c.Response.Msg = "操作失败"
- c.Response.Data = map[string]interface{}{}
- c.Result()
- }
- func (c BaseCommon) FailWithMessage(message, errMessage string) {
- c.Response.Code = ERROR
- c.Response.Msg = message
- c.Response.ErrMsg = errMessage
- c.Response.Data = map[string]interface{}{}
- c.Result()
- }
- func (c BaseCommon) FailWithCodeUsed(message, errMessage string) {
- c.Response.Code = WX_CODE_USED_ERROR
- c.Response.Msg = message
- c.Response.ErrMsg = errMessage
- c.Response.Data = map[string]interface{}{}
- c.Result()
- }
- //token异常
- func (c BaseCommon) TokenError(data interface{}, message string) {
- c.Response.Code = TOKEN_ERROR
- c.Response.Msg = message
- c.Response.Data = data
- c.Result()
- }
- //token异常
- func (c BaseCommon) TokenMsgError(message, errMessage string) {
- c.Response.Code = TOKEN_ERROR
- c.Response.Msg = message
- c.Response.ErrMsg = errMessage
- //c.Response.Data = data
- c.Result()
- }
- //账户绑定异常
- func (c BaseCommon) BindError(data interface{}, message string) {
- c.Response.Code = BIND_ERROR
- c.Response.Msg = message
- c.Response.Data = data
- c.Result()
- }
- //账户绑定异常
- func (c BaseCommon) BindMsgError(message, errMessage string) {
- c.Response.Code = BIND_ERROR
- c.Response.Msg = message
- c.Response.ErrMsg = errMessage
- //c.Response.Data = data
- c.Result()
- }
- //签名异常
- func (c BaseCommon) SignError(message string) {
- c.Response.Code = SIGN_ERROR
- c.Response.Msg = message
- c.Response.Data = map[string]interface{}{}
- c.Result()
- }
- func (c BaseCommon) FailWithDetailed(code int, data interface{}, message string) {
- c.Response.Code = code
- c.Response.Msg = message
- c.Response.Data = data
- c.Result()
- }
|