Skip to content

Commit

Permalink
result
Browse files Browse the repository at this point in the history
  • Loading branch information
baowk committed Aug 21, 2023
1 parent 6fbcc2d commit 44c0cfc
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 18 deletions.
8 changes: 8 additions & 0 deletions cmd/start/start.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package start

import (
"dilu/common/codes"
"fmt"

"github.com/baowk/dilu-core/core"
"github.com/baowk/dilu-core/core/i18n"

"github.com/fsnotify/fsnotify"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -52,5 +54,11 @@ func run() {
//fmt.Println(core.Cfg)

core.Init()

i18n.Register(&codes.Code{
EnableI18N: core.Cfg.Server.I18n,
Lang: core.Cfg.Server.Lang,
})

core.Run(&AppRouters)
}
34 changes: 32 additions & 2 deletions common/codes/code.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package codes

import (
"github.com/gin-gonic/gin"
"golang.org/x/text/language"
)

const (
SUCCESS = 200
FAILURE = 500
Expand All @@ -19,8 +24,33 @@ const (
LANG_EN = "en"
)

func GetMsg(code int, lang string) (str string) {
type Code struct {
EnableI18N bool
Lang string
}

func (e *Code) Enable() bool {
return e.EnableI18N
}

func (e *Code) DefLang() string {
return e.Lang
}

func (e *Code) GetMsg(code int, c *gin.Context) string {
var lang string
if e.EnableI18N {
acceptLanguate := c.GetHeader("Accept-Language")
tags, _, _ := language.ParseAcceptLanguage(acceptLanguate)
if len(tags) > 0 {
lang = tags[0].String()
}
} else {
lang = e.Lang
}

var ok bool
var str string
switch lang {
case LANG_ZH_CN, LANG_ZH:
str, ok = zhCNText[code]
Expand All @@ -32,5 +62,5 @@ func GetMsg(code int, lang string) (str string) {
if !ok {
return "unknown error"
}
return
return str
}
11 changes: 10 additions & 1 deletion docs/docs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 10 additions & 1 deletion docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/base.ReqIds"
"$ref": "#/definitions/base.ReqId"
}
}
],
Expand Down Expand Up @@ -312,6 +312,15 @@
}
}
},
"base.ReqId": {
"type": "object",
"properties": {
"id": {
"description": "主键ID",
"type": "integer"
}
}
},
"base.ReqIds": {
"type": "object",
"properties": {
Expand Down
8 changes: 7 additions & 1 deletion docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ definitions:
description: 总条数
type: integer
type: object
base.ReqId:
properties:
id:
description: 主键ID
type: integer
type: object
base.ReqIds:
properties:
ids:
Expand Down Expand Up @@ -181,7 +187,7 @@ paths:
name: data
required: true
schema:
$ref: '#/definitions/base.ReqIds'
$ref: '#/definitions/base.ReqId'
responses:
"200":
description: '{"code": 200, "data": [...]}'
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.20
require (
github.com/aliyun/aliyun-oss-go-sdk v2.2.8+incompatible
github.com/aws/aws-sdk-go v1.44.319
github.com/baowk/dilu-core v0.0.0-20230819025016-0d5ea3a238b1
github.com/baowk/dilu-core v0.0.0-20230821064921-4597dba0ab27
github.com/fsnotify/fsnotify v1.6.0
github.com/gin-gonic/gin v1.9.1
github.com/golang-jwt/jwt/v5 v5.0.0
Expand All @@ -20,6 +20,7 @@ require (
github.com/swaggo/swag v1.16.1
github.com/tencentyun/cos-go-sdk-v5 v0.7.42
go.uber.org/zap v1.25.0
golang.org/x/text v0.9.0
gorm.io/gorm v1.25.2
)

Expand Down Expand Up @@ -95,7 +96,6 @@ require (
golang.org/x/net v0.10.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.10.0 // indirect
golang.org/x/text v0.9.0 // indirect
golang.org/x/time v0.1.0 // indirect
golang.org/x/tools v0.7.0 // indirect
google.golang.org/protobuf v1.30.0 // indirect
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
)

//go:generate go env -w GO111MODULE=on
//go:generate go env -w GOPROXY=https://goproxy.cn,direct
//go:generate go env -w GOPROXY=https://goproxy.io,direct
//go:generate go mod tidy
//go:generate go mod download
//go:generate swag init --parseDependency --parseDepth=6
Expand Down
2 changes: 1 addition & 1 deletion modules/demo/apis/demo_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (e *DemoApi) QueryPage(c *gin.Context) {
// @Tags Demo
// @Accept application/json
// @Product application/json
// @Param data body base.ReqIds true "body"
// @Param data body base.ReqId true "body"
// @Success 200 {object} base.Resp{data=models.Demo} "{"code": 200, "data": [...]}"
// @Router /api/v1/demo/get [post]
func (e *DemoApi) Get(c *gin.Context) {
Expand Down
18 changes: 9 additions & 9 deletions resources/template/v4/no_actions/apis.go.template
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package apis

import (
"dilu/modules/demo/models"
"dilu/modules/demo/service"
"dilu/modules/demo/service/dto"
"dilu/modules/{{.PackageName}}/models"
"dilu/modules/{{.PackageName}}/service"
"dilu/modules/{{.PackageName}}/service/dto"

"github.com/baowk/dilu-core/core/base"
"github.com/gin-gonic/gin"
Expand All @@ -21,7 +21,7 @@ type {{.ClassName}}Api struct {
// @Product application/json
// @Param data body dto.DemePageReq true "body"
// @Success 200 {object} base.Resp{data=base.PageResp{list=[]models.{{.ClassName}}}} "{"code": 200, "data": [...]}"
// @Router /api/v1/demo/page [post]
// @Router /api/v1/{{.PackageName}}/page [post]
func (e *{{.ClassName}}Api) QueryPage(c *gin.Context) {
var req dto.DemePageReq
if err := c.ShouldBind(&req); err != nil {
Expand All @@ -42,9 +42,9 @@ func (e *{{.ClassName}}Api) QueryPage(c *gin.Context) {
// @Tags {{.ClassName}}
// @Accept application/json
// @Product application/json
// @Param data body base.ReqIds true "body"
// @Param data body base.ReqId true "body"
// @Success 200 {object} base.Resp{data=models.{{.ClassName}}} "{"code": 200, "data": [...]}"
// @Router /api/v1/demo/get [post]
// @Router /api/v1/{{.PackageName}}/get [post]
func (e *{{.ClassName}}Api) Get(c *gin.Context) {
var req base.ReqId
if err := c.ShouldBind(&req); err != nil {
Expand All @@ -66,7 +66,7 @@ func (e *{{.ClassName}}Api) Get(c *gin.Context) {
// @Product application/json
// @Param data body dto.{{.ClassName}}Dto true "body"
// @Success 200 {object} base.Resp{data=models.{{.ClassName}}} "{"code": 200, "data": [...]}"
// @Router /api/v1/demo/create [post]
// @Router /api/v1/{{.PackageName}}/create [post]
func (e *{{.ClassName}}Api) Create(c *gin.Context) {
var req dto.{{.ClassName}}Dto
if err := c.ShouldBind(&req); err != nil {
Expand All @@ -89,7 +89,7 @@ func (e *{{.ClassName}}Api) Create(c *gin.Context) {
// @Product application/json
// @Param data body dto.{{.ClassName}}Dto true "body"
// @Success 200 {object} base.Resp{data=models.{{.ClassName}}} "{"code": 200, "data": [...]}"
// @Router /api/v1/demo/update [post]
// @Router /api/v1/{{.PackageName}}/update [post]
func (e *{{.ClassName}}Api) Update(c *gin.Context) {
var req dto.{{.ClassName}}Dto
if err := c.ShouldBind(&req); err != nil {
Expand All @@ -112,7 +112,7 @@ func (e *{{.ClassName}}Api) Update(c *gin.Context) {
// @Product application/json
// @Param data body base.ReqIds true "body"
// @Success 200 {object} base.Resp{data=models.{{.ClassName}}} "{"code": 200, "data": [...]}"
// @Router /api/v1/demo/del [post]
// @Router /api/v1/{{.PackageName}}/del [post]
func (e *{{.ClassName}}Api) Del(c *gin.Context) {
var req base.ReqIds
if err := c.ShouldBind(&req); err != nil {
Expand Down

0 comments on commit 44c0cfc

Please sign in to comment.