Skip to content

Commit 68317c1

Browse files
奇淼(piexlmaxSliverHorncg831344
authored
调整角色ID为uint格式,增加embed打包,更改日志格式。 (flipped-aurora#1144)
* feature: 根据 flipped-aurora#377 pr进行修改embed, 打包静态文件夹与配置文件 * 修改角色id为uint * 修改日志格式 Co-authored-by: SliverHorn <503551462@qq.com> Co-authored-by: cg81344 <cg831344@126.com>
1 parent 78e6271 commit 68317c1

File tree

30 files changed

+223
-76
lines changed

30 files changed

+223
-76
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,5 @@ yarn-error.log*
2727
/server/latest_log
2828

2929
*.iml
30+
web/.pnpm-debug.log
31+
web/pnpm-lock.yaml

server/core/internal/zap.go

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
package internal
22

33
import (
4+
"bytes"
45
"fmt"
56
"github.com/flipped-aurora/gin-vue-admin/server/global"
67
"go.uber.org/zap"
8+
"go.uber.org/zap/buffer"
79
"go.uber.org/zap/zapcore"
10+
"runtime"
11+
"strings"
812
"time"
913
)
1014

@@ -35,7 +39,7 @@ func (z *_zap) GetEncoderConfig() zapcore.EncoderConfig {
3539
EncodeLevel: global.GVA_CONFIG.Zap.ZapEncodeLevel(),
3640
EncodeTime: z.CustomTimeEncoder,
3741
EncodeDuration: zapcore.SecondsDurationEncoder,
38-
EncodeCaller: zapcore.FullCallerEncoder,
42+
EncodeCaller: CallerEncoder,
3943
}
4044
}
4145

@@ -48,7 +52,7 @@ func (z *_zap) GetEncoderCore(l zapcore.Level, level zap.LevelEnablerFunc) zapco
4852
return nil
4953
}
5054

51-
return zapcore.NewCore(z.GetEncoder(), writer, level)
55+
return zapcore.NewCore(&EscapeSeqJSONEncoder{z.GetEncoder()}, writer, level)
5256
}
5357

5458
// CustomTimeEncoder 自定义日志输出时间格式
@@ -105,3 +109,41 @@ func (z *_zap) GetLevelPriority(level zapcore.Level) zap.LevelEnablerFunc {
105109
}
106110
}
107111
}
112+
113+
// FuncName 返回调用本函数的函数名称
114+
// pc runtime.Caller 返回的第一个值
115+
func FuncName(pc uintptr) string {
116+
funcName := runtime.FuncForPC(pc).Name()
117+
sFuncName := strings.Split(funcName, ".")
118+
return sFuncName[len(sFuncName)-1]
119+
}
120+
121+
// CallerEncoder serializes a caller in package/file:funcname:line format
122+
func CallerEncoder(caller zapcore.EntryCaller, enc zapcore.PrimitiveArrayEncoder) {
123+
shortCaller := caller.TrimmedPath()
124+
shortCallerSplited := strings.Split(shortCaller, ":")
125+
funcName := FuncName(caller.PC)
126+
result := shortCallerSplited[0] + ":" + funcName + ":" + shortCallerSplited[1]
127+
enc.AppendString(result)
128+
}
129+
130+
type EscapeSeqJSONEncoder struct {
131+
zapcore.Encoder
132+
}
133+
134+
// EncodeEntry 将方法zap.error中的errorVerbose的堆栈换行符修改
135+
func (enc *EscapeSeqJSONEncoder) EncodeEntry(entry zapcore.Entry, fields []zapcore.Field) (*buffer.Buffer, error) {
136+
b, err := enc.Encoder.EncodeEntry(entry, fields)
137+
if err != nil {
138+
return nil, err
139+
}
140+
newb := buffer.NewPool().Get()
141+
142+
b1 := bytes.Replace(b.Bytes(), []byte("\\n"), []byte("\n"), -1)
143+
b2 := bytes.Replace(b1, []byte("\\t"), []byte("\t"), -1)
144+
_, err = newb.Write(b2)
145+
if err != nil {
146+
return nil, err
147+
}
148+
return newb, nil
149+
}

server/embed.go

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package main
2+
3+
import (
4+
"embed"
5+
"fmt"
6+
"io"
7+
"io/fs"
8+
"os"
9+
"path/filepath"
10+
)
11+
12+
var (
13+
//go:embed resource
14+
//go:embed config.yaml
15+
//go:embed config.docker.yaml
16+
resource embed.FS
17+
)
18+
19+
var Embed = new(_embed)
20+
21+
type _embed struct{}
22+
23+
// RestoreFolder
24+
// Author [SliverHorn](https://github.com/SliverHorn)
25+
// Author [WangLeonard](https://github.com/WangLeonard)
26+
func (e *_embed) RestoreFolder(dir string) {
27+
entries, err := resource.ReadDir(dir)
28+
if err != nil {
29+
fmt.Println("[embed restore resource file]: err:", err)
30+
return
31+
}
32+
for i := 0; i < len(entries); i++ {
33+
if entries[i].IsDir() {
34+
e.RestoreFile(filepath.Join(dir, entries[i].Name()), entries[i])
35+
continue
36+
}
37+
e.RestoreFile(entries[i].Name(), entries[i])
38+
}
39+
}
40+
41+
// RestoreFile
42+
// Author [SliverHorn](https://github.com/SliverHorn)
43+
// Author [WangLeonard](https://github.com/WangLeonard)
44+
func (e *_embed) RestoreFile(path string, entry fs.DirEntry) {
45+
_, err := os.Stat(path)
46+
if entry.IsDir() { // 文件夹
47+
if os.IsNotExist(err) { // 判断 path 变量的文件夹存在, 不存在则创建文件夹
48+
fmt.Printf("[embed restore mkdir] dir:%s\n", path)
49+
err = os.Mkdir(path, os.ModePerm) // 创建文件夹, 权限为 os.ModePerm 可自行修改
50+
if err != nil {
51+
fmt.Printf("[embed restore mkdir] err:%v\n", err)
52+
return
53+
}
54+
}
55+
var entries []fs.DirEntry
56+
entries, err = resource.ReadDir(path) // 读取文件夹的文件和文件夹数据
57+
if err != nil {
58+
return
59+
}
60+
for i := 0; i < len(entries); i++ {
61+
_, err = os.Stat(entries[i].Name()) // 获取子文件夹的信息
62+
dirPath := filepath.Join(path, entries[i].Name())
63+
if os.IsNotExist(err) && entries[i].IsDir() { // 判断子文件夹是否存在, 这里有可能是文件,所以要加上是否为文件夹
64+
fmt.Println("[embed restore mkdir] dir:", dirPath)
65+
err = os.Mkdir(dirPath, os.ModePerm) // 创建文件夹, 权限为 os.ModePerm 可自行修改
66+
if err != nil {
67+
fmt.Println("[embed restore mkdir] err:", err)
68+
return
69+
}
70+
}
71+
e.RestoreFile(dirPath, entries[i])
72+
}
73+
}
74+
75+
if os.IsNotExist(err) && !entry.IsDir() { // 文件
76+
var src fs.File
77+
src, err = resource.Open(path) // 根据path从embed的到文件数据
78+
if err != nil {
79+
fmt.Println("[embed restore resource open file] open embed file failed, err:", err)
80+
return
81+
}
82+
var dst *os.File
83+
dst, err = os.Create(path) // 创建本地文件的 writer
84+
if err != nil {
85+
fmt.Println("[embed restore os create file] write err:", err)
86+
return
87+
}
88+
_, err = io.Copy(dst, src) // 把embed的数据复制到本地
89+
if err != nil {
90+
fmt.Println("[embed restore io copy file] writer file failed, err:", err)
91+
return
92+
}
93+
defer func() { // 关闭文件流
94+
_ = src.Close()
95+
_ = dst.Close()
96+
}()
97+
return
98+
}
99+
fmt.Println("[embed restore resource file] file exist, path:", path)
100+
}

server/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
// @name x-token
2121
// @BasePath /
2222
func main() {
23+
Embed.RestoreFolder(".")
2324
global.GVA_VP = core.Viper() // 初始化Viper
2425
global.GVA_LOG = core.Zap() // 初始化zap日志库
2526
zap.ReplaceGlobals(global.GVA_LOG)

server/middleware/casbin_rbac.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/flipped-aurora/gin-vue-admin/server/service"
77
"github.com/flipped-aurora/gin-vue-admin/server/utils"
88
"github.com/gin-gonic/gin"
9+
"strconv"
910
)
1011

1112
var casbinService = service.ServiceGroupApp.SystemServiceGroup.CasbinService
@@ -19,9 +20,8 @@ func CasbinHandler() gin.HandlerFunc {
1920
// 获取请求方法
2021
act := c.Request.Method
2122
// 获取用户的角色
22-
sub := waitUse.AuthorityId
23-
e := casbinService.Casbin()
24-
// 判断策略中是否存在
23+
sub := strconv.Itoa(int(waitUse.AuthorityId))
24+
e := casbinService.Casbin() // 判断策略中是否存在
2525
success, _ := e.Enforce(sub, obj, act)
2626
if global.GVA_CONFIG.System.Env == "develop" || success {
2727
c.Next()

server/model/common/request/common.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package request
22

33
// PageInfo Paging common input parameter structure
44
type PageInfo struct {
5-
Page int `json:"page" form:"page"` // 页码
6-
PageSize int `json:"pageSize" form:"pageSize"` // 每页大小
5+
Page int `json:"page" form:"page"` // 页码
6+
PageSize int `json:"pageSize" form:"pageSize"` // 每页大小
77
Keyword string `json:"keyword" form:"keyword"` //关键字
88
}
99

@@ -22,7 +22,7 @@ type IdsReq struct {
2222

2323
// GetAuthorityId Get role by id structure
2424
type GetAuthorityId struct {
25-
AuthorityId string `json:"authorityId" form:"authorityId"` // 角色ID
25+
AuthorityId uint `json:"authorityId" form:"authorityId"` // 角色ID
2626
}
2727

2828
type Empty struct{}

server/model/example/exa_customer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ type ExaCustomer struct {
1010
CustomerName string `json:"customerName" form:"customerName" gorm:"comment:客户名"` // 客户名
1111
CustomerPhoneData string `json:"customerPhoneData" form:"customerPhoneData" gorm:"comment:客户手机号"` // 客户手机号
1212
SysUserID uint `json:"sysUserId" form:"sysUserId" gorm:"comment:管理ID"` // 管理ID
13-
SysUserAuthorityID string `json:"sysUserAuthorityID" form:"sysUserAuthorityID" gorm:"comment:管理角色ID"` // 管理角色ID
13+
SysUserAuthorityID uint `json:"sysUserAuthorityID" form:"sysUserAuthorityID" gorm:"comment:管理角色ID"` // 管理角色ID
1414
SysUser system.SysUser `json:"sysUser" form:"sysUser" gorm:"comment:管理详情"` // 管理详情
1515
}

server/model/system/request/jwt.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@ type BaseClaims struct {
1717
ID uint
1818
Username string
1919
NickName string
20-
AuthorityId string
20+
AuthorityId uint
2121
}

server/model/system/request/sys_authority_btn.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ package request
22

33
type SysAuthorityBtnReq struct {
44
MenuID uint `json:"menuID"`
5-
AuthorityId string `json:"authorityId"`
5+
AuthorityId uint `json:"authorityId"`
66
Selected []uint `json:"selected"`
77
}

server/model/system/request/sys_casbin.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ type CasbinInfo struct {
88

99
// Casbin structure for input parameters
1010
type CasbinInReceive struct {
11-
AuthorityId string `json:"authorityId"` // 权限id
11+
AuthorityId uint `json:"authorityId"` // 权限id
1212
CasbinInfos []CasbinInfo `json:"casbinInfos"`
1313
}
1414

server/model/system/request/sys_menu.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
// Add menu authority info structure
99
type AddMenuAuthorityInfo struct {
1010
Menus []system.SysBaseMenu `json:"menus"`
11-
AuthorityId string `json:"authorityId"` // 角色ID
11+
AuthorityId uint `json:"authorityId"` // 角色ID
1212
}
1313

1414
func DefaultMenu() []system.SysBaseMenu {

server/model/system/request/sys_user.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ import model "github.com/flipped-aurora/gin-vue-admin/server/model/system"
44

55
// User register structure
66
type Register struct {
7-
Username string `json:"userName"`
8-
Password string `json:"passWord"`
9-
NickName string `json:"nickName" gorm:"default:'QMPlusUser'"`
10-
HeaderImg string `json:"headerImg" gorm:"default:'https://qmplusimg.henrongyi.top/gva_header.jpg'"`
11-
AuthorityId string `json:"authorityId" gorm:"default:888"`
12-
Enable int `json:"enable"`
13-
AuthorityIds []string `json:"authorityIds"`
7+
Username string `json:"userName"`
8+
Password string `json:"passWord"`
9+
NickName string `json:"nickName" gorm:"default:'QMPlusUser'"`
10+
HeaderImg string `json:"headerImg" gorm:"default:'https://qmplusimg.henrongyi.top/gva_header.jpg'"`
11+
AuthorityId uint `json:"authorityId" gorm:"default:888"`
12+
Enable int `json:"enable"`
13+
AuthorityIds []uint `json:"authorityIds"`
1414
}
1515

1616
// User login structure
@@ -30,20 +30,20 @@ type ChangePasswordStruct struct {
3030

3131
// Modify user's auth structure
3232
type SetUserAuth struct {
33-
AuthorityId string `json:"authorityId"` // 角色ID
33+
AuthorityId uint `json:"authorityId"` // 角色ID
3434
}
3535

3636
// Modify user's auth structure
3737
type SetUserAuthorities struct {
3838
ID uint
39-
AuthorityIds []string `json:"authorityIds"` // 角色ID
39+
AuthorityIds []uint `json:"authorityIds"` // 角色ID
4040
}
4141

4242
type ChangeUserInfo struct {
4343
ID uint `gorm:"primarykey"` // 主键ID
4444
NickName string `json:"nickName" gorm:"default:系统用户;comment:用户昵称"` // 用户昵称
4545
Phone string `json:"phone" gorm:"comment:用户手机号"` // 用户角色ID
46-
AuthorityIds []string `json:"authorityIds" gorm:"-"` // 角色ID
46+
AuthorityIds []uint `json:"authorityIds" gorm:"-"` // 角色ID
4747
Email string `json:"email" gorm:"comment:用户邮箱"` // 用户邮箱
4848
HeaderImg string `json:"headerImg" gorm:"default:https://qmplusimg.henrongyi.top/gva_header.jpg;comment:用户头像"` // 用户头像
4949
SideMode string `json:"sideMode" gorm:"comment:用户侧边主题"` // 用户侧边主题

server/model/system/response/sys_authority.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ type SysAuthorityResponse struct {
88

99
type SysAuthorityCopyResponse struct {
1010
Authority system.SysAuthority `json:"authority"`
11-
OldAuthorityId string `json:"oldAuthorityId"` // 旧角色ID
11+
OldAuthorityId uint `json:"oldAuthorityId"` // 旧角色ID
1212
}

server/model/system/sys_authority.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ type SysAuthority struct {
88
CreatedAt time.Time // 创建时间
99
UpdatedAt time.Time // 更新时间
1010
DeletedAt *time.Time `sql:"index"`
11-
AuthorityId string `json:"authorityId" gorm:"not null;unique;primary_key;comment:角色ID;size:90"` // 角色ID
11+
AuthorityId uint `json:"authorityId" gorm:"not null;unique;primary_key;comment:角色ID;size:90"` // 角色ID
1212
AuthorityName string `json:"authorityName" gorm:"comment:角色名"` // 角色名
13-
ParentId string `json:"parentId" gorm:"comment:父角色ID"` // 父角色ID
13+
ParentId uint `json:"parentId" gorm:"comment:父角色ID"` // 父角色ID
1414
DataAuthorityId []*SysAuthority `json:"dataAuthorityId" gorm:"many2many:sys_data_authority_id;"`
1515
Children []SysAuthority `json:"children" gorm:"-"`
1616
SysBaseMenus []SysBaseMenu `json:"menus" gorm:"many2many:sys_authority_menus;"`

server/model/system/sys_authority_btn.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package system
22

33
type SysAuthorityBtn struct {
4-
AuthorityId string `gorm:"comment:角色ID"`
4+
AuthorityId uint `gorm:"comment:角色ID"`
55
SysMenuID uint `gorm:"comment:菜单ID"`
66
SysBaseMenuBtnID uint `gorm:"comment:菜单按钮ID"`
77
SysBaseMenuBtn SysBaseMenuBtn ` gorm:"comment:按钮详情"`

server/model/system/sys_authority_menu.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ package system
33
type SysMenu struct {
44
SysBaseMenu
55
MenuId string `json:"menuId" gorm:"comment:菜单ID"`
6-
AuthorityId string `json:"-" gorm:"comment:角色ID"`
6+
AuthorityId uint `json:"-" gorm:"comment:角色ID"`
77
Children []SysMenu `json:"children" gorm:"-"`
88
Parameters []SysBaseMenuParameter `json:"parameters" gorm:"foreignKey:SysBaseMenuID;references:MenuId"`
9-
Btns map[string]string `json:"btns" gorm:"-"`
9+
Btns map[string]uint `json:"btns" gorm:"-"`
1010
}
1111

1212
type SysAuthorityMenu struct {

server/model/system/sys_user.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ type SysUser struct {
1515
HeaderImg string `json:"headerImg" gorm:"default:https://qmplusimg.henrongyi.top/gva_header.jpg;comment:用户头像"` // 用户头像
1616
BaseColor string `json:"baseColor" gorm:"default:#fff;comment:基础颜色"` // 基础颜色
1717
ActiveColor string `json:"activeColor" gorm:"default:#1890ff;comment:活跃颜色"` // 活跃颜色
18-
AuthorityId string `json:"authorityId" gorm:"default:888;comment:用户角色ID"` // 用户角色ID
18+
AuthorityId uint `json:"authorityId" gorm:"default:888;comment:用户角色ID"` // 用户角色ID
1919
Authority SysAuthority `json:"authority" gorm:"foreignKey:AuthorityId;references:AuthorityId;comment:用户角色"`
2020
Authorities []SysAuthority `json:"authorities" gorm:"many2many:sys_user_authority;"`
2121
Phone string `json:"phone" gorm:"comment:用户手机号"` // 用户手机号

server/model/system/sys_user_authority.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package system
22

33
type SysUseAuthority struct {
4-
SysUserId uint `gorm:"column:sys_user_id"`
5-
SysAuthorityAuthorityId string `gorm:"column:sys_authority_authority_id"`
4+
SysUserId uint `gorm:"column:sys_user_id"`
5+
SysAuthorityAuthorityId uint `gorm:"column:sys_authority_authority_id"`
66
}
77

88
func (s *SysUseAuthority) TableName() string {

server/service/example/exa_customer.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func (exa *CustomerService) GetExaCustomer(id uint) (customer example.ExaCustome
6060
//@param: sysUserAuthorityID string, info request.PageInfo
6161
//@return: list interface{}, total int64, err error
6262

63-
func (exa *CustomerService) GetCustomerInfoList(sysUserAuthorityID string, info request.PageInfo) (list interface{}, total int64, err error) {
63+
func (exa *CustomerService) GetCustomerInfoList(sysUserAuthorityID uint, info request.PageInfo) (list interface{}, total int64, err error) {
6464
limit := info.PageSize
6565
offset := info.PageSize * (info.Page - 1)
6666
db := global.GVA_DB.Model(&example.ExaCustomer{})
@@ -70,7 +70,7 @@ func (exa *CustomerService) GetCustomerInfoList(sysUserAuthorityID string, info
7070
if err != nil {
7171
return
7272
}
73-
var dataId []string
73+
var dataId []uint
7474
for _, v := range auth.DataAuthorityId {
7575
dataId = append(dataId, v.AuthorityId)
7676
}

0 commit comments

Comments
 (0)