goframe开发一个企业网站 统一返回响应码 18
响应码的logic
package returncode
import (
"context"
"gf_new_web/internal/service"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/frame/g"
)
type sReturncode struct {
}
var (
insReturncode = sReturncode{}
codeSuccessDefault = gcode.CodeOK.Code()
MessageSuccessDefault = "操作成功"
)
func init() {
service.RegisterReturncode(New())
}
func New() *sReturncode {
return &sReturncode{}
}
func Returncode() *sReturncode {
return &insReturncode
}
// SuccessJson 返回成功json
func (s *sReturncode) SuccessJson(ctx context.Context, code int, message string, data interface{}) {
g.RequestFromCtx(ctx).Response.WriteJson(g.Map{
"code": code,
"message": message,
"data": data,
})
}
// SuccessJsonDefault 返回默认成功json
func (s *sReturncode) SuccessJsonDefault(ctx context.Context) {
service.Returncode().SuccessJson(ctx, service.Returncode().SuccessCodeDefault(), service.Returncode().SuccessMessageDefault(), g.Map{})
}
// SuccessJsonDefaultMessage 返回自定义提示语的成功json
func (s *sReturncode) SuccessJsonDefaultMessage(ctx context.Context, message string) {
service.Returncode().SuccessJson(ctx, service.Returncode().SuccessCodeDefault(), message, g.Map{})
}
// SuccessCodeDefault 获取默认成功code码
func (s *sReturncode) SuccessCodeDefault() int {
return codeSuccessDefault
}
// SuccessMessageDefault 获取默认成功提示语
func (s *sReturncode) SuccessMessageDefault() string {
return MessageSuccessDefault
}
// Status404 状态码404
func (s *sReturncode) Status404(ctx context.Context) {
g.RequestFromCtx(ctx).Response.WriteStatusExit(404, " ")
return
}
// Status500 http状态码500
func (s *sReturncode) Status500(ctx context.Context) {
g.RequestFromCtx(ctx).Response.WriteStatusExit(500, "")
return
}
service.go
// ================================================================================
// Code generated and maintained by GoFrame CLI tool. DO NOT EDIT.
// You can delete these comments if you wish manually maintain this interface file.
// ================================================================================
package service
import (
"context"
)
type (
IReturncode interface {
// SuccessJson 返回成功json
SuccessJson(ctx context.Context, code int, message string, data interface{})
// SuccessJsonDefault 返回默认成功json
SuccessJsonDefault(ctx context.Context)
// SuccessJsonDefaultMessage 返回自定义提示语的成功json
SuccessJsonDefaultMessage(ctx context.Context, message string)
// SuccessCodeDefault 获取默认成功code码
SuccessCodeDefault() int
// SuccessMessageDefault 获取默认成功提示语
SuccessMessageDefault() string
// Status404 状态码404
Status404(ctx context.Context)
// Status500 http状态码500
Status500(ctx context.Context)
}
)
var (
localReturncode IReturncode
)
func Returncode() IReturncode {
if localReturncode == nil {
panic("implement not found for interface IReturncode, forgot register?")
}
return localReturncode
}
func RegisterReturncode(i IReturncode) {
localReturncode = i
}
view的处理
logic
package returnview
import (
"context"
"gf_new_web/internal/service"
"github.com/gogf/gf/v2/frame/g"
)
type sReturnview struct {
}
var (
insReturnview = sReturnview{}
)
func init() {
service.RegisterReturnview(New())
}
func New() *sReturnview {
return &sReturnview{}
}
func Returnview() *sReturnview {
return &insReturnview
}
// WebView 网站页面渲染
func (s *sReturnview) WebView(ctx context.Context, template string, data map[string]interface{}) (err error) {
err = g.RequestFromCtx(ctx).Response.WriteTpl(template, data)
if err != nil {
return err
}
return
}
// ErrorView 错误页面渲染
func (s *sReturnview) ErrorView(ctx context.Context, code int, message string) (err error) {
template := "backend/error.html"
data := g.Map{
"code": code,
"message": message,
}
err = g.RequestFromCtx(ctx).Response.WriteTpl(template, data)
if err != nil {
return err
}
return
}
这里的Webview还要再修改,将一前端一些重启的代码写入。