/* * @desc:路由绑定 * * * @Date: 2022/2/18 16:23 */ package router import ( "github.com/gogf/gf/v2/errors/gcode" "github.com/gogf/gf/v2/errors/gerror" "github.com/gogf/gf/v2/net/ghttp" "net/http" "tyj_admin/internal/controller" "tyj_admin/internal/service" ) func BindController(group *ghttp.RouterGroup) { group.Group("/api/v1", func(group *ghttp.RouterGroup) { group.Middleware(ghttp.MiddlewareHandlerResponse) // 绑定后台路由 SystemBindController(group) // 绑定测试路由 DemoBindController(group) // 绑定公共路由 CommonBindController(group) GameBindController(group) }) } func BindAnotherController(group *ghttp.RouterGroup) { group.Group("/frontApi", func(group *ghttp.RouterGroup) { group.Middleware(MiddlewareHandlerResponse) GameBindFrontController(group) }) } func MiddlewareHandlerResponse(r *ghttp.Request) { r.Middleware.Next() // There's custom buffer content, it then exits current handler. if r.Response.BufferLength() > 0 { return } var ( msg string err = r.GetError() res = r.GetHandlerResponse() code = gerror.Code(err) ) if err != nil { if code == gcode.CodeNil { code = gcode.CodeInternalError } msg = err.Error() } else { if r.Response.Status > 0 && r.Response.Status != http.StatusOK { msg = http.StatusText(r.Response.Status) switch r.Response.Status { case http.StatusNotFound: code = gcode.CodeNotFound case http.StatusForbidden: code = gcode.CodeNotAuthorized default: code = gcode.CodeUnknown } // It creates error as it can be retrieved by other middlewares. err = gerror.NewCode(code, msg) r.SetError(err) } else { code = gcode.CodeOK } } if msg != "" { res = msg } if res == nil { res = "" } //fmt.Println("WriteJson ", err, msg, gjson.MustEncodeString(res)) r.Response.WriteJson(res) } func CommonBindController(group *ghttp.RouterGroup) { group.Group("/pub", func(group *ghttp.RouterGroup) { group.Middleware(service.Middleware().MiddlewareCORS) group.Group("/captcha", func(group *ghttp.RouterGroup) { group.Bind( controller.Captcha, ) }) // 文件上传 group.Group("/upload", func(group *ghttp.RouterGroup) { group.Bind( controller.Upload, ) }) }) } func DemoBindController(group *ghttp.RouterGroup) { group.Group("/demo", func(group *ghttp.RouterGroup) { group.Middleware(service.Middleware().MiddlewareCORS) group.Bind( controller.Demo, ) }) } func SystemBindController(group *ghttp.RouterGroup) { group.Group("/system", func(group *ghttp.RouterGroup) { group.Middleware(service.Middleware().MiddlewareCORS, service.Middleware().OperLog) // 系统初始化 group.Bind( controller.DbInit, ) group.Bind( //登录 controller.Login, ) //登录验证拦截 service.SysGfToken().Middleware(group) //context拦截器 group.Middleware(service.Middleware().Ctx, service.Middleware().Auth) group.Bind( controller.User, controller.Menu, controller.Role, controller.Dept, controller.Post, controller.DictType, controller.DictData, controller.Config, controller.Monitor, controller.LoginLog, ) }) } func GameBindController(group *ghttp.RouterGroup) { group.Group("/game", func(group *ghttp.RouterGroup) { //group.Bind(controller.GamePub) group.Middleware(service.Middleware().MiddlewareCORS) service.SysGfToken().Middleware(group) //context拦截器 group.Middleware(service.Middleware().Ctx, service.Middleware().Auth, service.Middleware().OperLog) group.Bind( controller.GameRole, controller.GameMail, controller.GameOrder, controller.GameManage, controller.GameNotice, controller.GameCorn, controller.GameCustom, controller.GameBazaar, controller.GameMonthlyLottery, controller.GamePropExchange, controller.GameModel, controller.GameCCD, controller.GameVersion, controller.GameRank, controller.GameWhiteList, controller.Config, controller.GameLoginUrl, controller.GameCdKey, controller.GameSet, controller.Advertisement, controller.GameBug, ) }) } func GameBindFrontController(group *ghttp.RouterGroup) { group.Group("/game", func(group *ghttp.RouterGroup) { group.Bind( controller.GamePub, controller.OutCCD, controller.OutId, controller.GameRebate) }) }