github.com/kotovmak/go-admin@v1.1.1/plugins/admin/modules/response/response.go (about) 1 package response 2 3 import ( 4 "net/http" 5 6 "github.com/kotovmak/go-admin/context" 7 "github.com/kotovmak/go-admin/modules/auth" 8 "github.com/kotovmak/go-admin/modules/config" 9 "github.com/kotovmak/go-admin/modules/db" 10 "github.com/kotovmak/go-admin/modules/errors" 11 "github.com/kotovmak/go-admin/modules/language" 12 "github.com/kotovmak/go-admin/modules/menu" 13 "github.com/kotovmak/go-admin/template" 14 "github.com/kotovmak/go-admin/template/types" 15 ) 16 17 func Ok(ctx *context.Context) { 18 ctx.JSON(http.StatusOK, map[string]interface{}{ 19 "code": http.StatusOK, 20 "msg": "ok", 21 }) 22 } 23 24 func OkWithMsg(ctx *context.Context, msg string) { 25 ctx.JSON(http.StatusOK, map[string]interface{}{ 26 "code": http.StatusOK, 27 "msg": msg, 28 }) 29 } 30 31 func OkWithData(ctx *context.Context, data map[string]interface{}) { 32 ctx.JSON(http.StatusOK, map[string]interface{}{ 33 "code": http.StatusOK, 34 "msg": "ok", 35 "data": data, 36 }) 37 } 38 39 func BadRequest(ctx *context.Context, msg string) { 40 ctx.JSON(http.StatusBadRequest, map[string]interface{}{ 41 "code": http.StatusBadRequest, 42 "msg": language.Get(msg), 43 }) 44 } 45 46 func Alert(ctx *context.Context, desc, title, msg string, conn db.Connection, btns *types.Buttons, 47 pageType ...template.PageType) { 48 user := auth.Auth(ctx) 49 50 pt := template.Error500Page 51 if len(pageType) > 0 { 52 pt = pageType[0] 53 } 54 55 pageTitle, description, content := template.GetPageContentFromPageType(title, desc, msg, pt) 56 57 tmpl, tmplName := template.Default().GetTemplate(ctx.IsPjax()) 58 buf := template.Execute(&template.ExecuteParam{ 59 User: user, 60 TmplName: tmplName, 61 Tmpl: tmpl, 62 Panel: types.Panel{ 63 Content: content, 64 Description: description, 65 Title: pageTitle, 66 }, 67 Config: config.Get(), 68 Menu: menu.GetGlobalMenu(user, conn, ctx.Lang()).SetActiveClass(config.URLRemovePrefix(ctx.Path())), 69 Animation: true, 70 Buttons: *btns, 71 IsPjax: ctx.IsPjax(), 72 Iframe: ctx.IsIframe(), 73 }) 74 ctx.HTML(http.StatusOK, buf.String()) 75 } 76 77 func Error(ctx *context.Context, msg string, datas ...map[string]interface{}) { 78 res := map[string]interface{}{ 79 "code": http.StatusInternalServerError, 80 "msg": language.Get(msg), 81 } 82 if len(datas) > 0 { 83 res["data"] = datas[0] 84 } 85 ctx.JSON(http.StatusInternalServerError, res) 86 } 87 88 func Denied(ctx *context.Context, msg string) { 89 ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ 90 "code": http.StatusForbidden, 91 "msg": language.Get(msg), 92 }) 93 } 94 95 var OffLineHandler = func(ctx *context.Context) { 96 if config.GetSiteOff() { 97 if ctx.WantHTML() { 98 ctx.HTML(http.StatusOK, `<html><body><h1>The website is offline</h1></body></html>`) 99 } else { 100 ctx.JSON(http.StatusForbidden, map[string]interface{}{ 101 "code": http.StatusForbidden, 102 "msg": language.Get(errors.SiteOff), 103 }) 104 } 105 ctx.Abort() 106 } 107 }