Skip to content

Commit c028ea3

Browse files
authored
Merge pull request #2145 from bypanghu/main
fix: 更新优化插件注册 api 和注册 menus 逻辑。
2 parents 287cb6e + f357c67 commit c028ea3

File tree

8 files changed

+54
-51
lines changed

8 files changed

+54
-51
lines changed

server/api/v1/system/sys_user.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ func (b *BaseApi) SetUserAuthority(c *gin.Context) {
264264
}
265265
c.Header("new-token", token)
266266
c.Header("new-expires-at", strconv.FormatInt(claims.ExpiresAt.Unix(), 10))
267-
utils.SetToken(c, token, int((claims.ExpiresAt.Unix()-time.Now().Unix())/60))
267+
utils.SetToken(c, token, int(claims.ExpiresAt.Unix()-time.Now().Unix()))
268268
response.OkWithMessage("修改成功", c)
269269
}
270270

server/model/system/sys_base_menu.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ type SysBaseMenu struct {
1313
Hidden bool `json:"hidden" gorm:"comment:是否在列表隐藏"` // 是否在列表隐藏
1414
Component string `json:"component" gorm:"comment:对应前端文件路径"` // 对应前端文件路径
1515
Sort int `json:"sort" gorm:"comment:排序标记"` // 排序标记
16-
Meta `json:"meta" gorm:"embedded;comment:附加属性"` // 附加属性
16+
Meta `json:"meta" gorm:"embedded"` // 附加属性
1717
SysAuthoritys []SysAuthority `json:"authoritys" gorm:"many2many:sys_authority_menus;"`
1818
Children []SysBaseMenu `json:"children" gorm:"-"`
1919
Parameters []SysBaseMenuParameter `json:"parameters"`
Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,53 @@
11
package utils
22

33
import (
4-
"fmt"
4+
"github.com/pkg/errors"
5+
"go.uber.org/zap"
6+
"gorm.io/gorm"
57

68
"github.com/flipped-aurora/gin-vue-admin/server/global"
79
"github.com/flipped-aurora/gin-vue-admin/server/model/system"
810
)
911

10-
func RegisterApis(apis ...system.SysApi) {
11-
var count int64
12-
var apiPaths []string
13-
for i := range apis {
14-
apiPaths = append(apiPaths, apis[i].Path)
15-
}
16-
global.GVA_DB.Find(&[]system.SysApi{}, "path in (?)", apiPaths).Count(&count)
17-
if count > 0 {
18-
return
19-
}
20-
err := global.GVA_DB.Create(&apis).Error
12+
func RegisterApis( apis ...system.SysApi) {
13+
err := global.GVA_DB.Transaction(func(tx *gorm.DB) error {
14+
for _, api := range apis {
15+
err := tx.Model(system.SysApi{}).Where("path = ? AND method = ? AND api_group = ? ", api.Path, api.Method, api.ApiGroup).FirstOrCreate(&api).Error
16+
if err != nil {
17+
zap.L().Error("注册API失败", zap.Error(err), zap.String("api", api.Path), zap.String("method", api.Method), zap.String("apiGroup", api.ApiGroup))
18+
return err
19+
}
20+
}
21+
return nil
22+
})
2123
if err != nil {
22-
fmt.Println(err)
24+
zap.L().Error("注册API失败", zap.Error(err))
2325
}
2426
}
2527

26-
func RegisterMenus(menus ...system.SysBaseMenu) {
27-
var count int64
28-
var menuNames []string
28+
func RegisterMenus( menus ...system.SysBaseMenu) {
2929
parentMenu := menus[0]
3030
otherMenus := menus[1:]
31-
for i := range menus {
32-
menuNames = append(menuNames, menus[i].Name)
33-
}
34-
global.GVA_DB.Find(&[]system.SysBaseMenu{}, "name in (?)", menuNames).Count(&count)
35-
if count > 0 {
36-
return
37-
}
38-
err := global.GVA_DB.Create(&parentMenu).Error
39-
if err != nil {
40-
fmt.Println(err)
41-
}
42-
for i := range otherMenus {
31+
err := global.GVA_DB.Transaction(func(tx *gorm.DB) error {
32+
err := tx.Model(system.SysBaseMenu{}).Where("name = ? ", parentMenu.Name).FirstOrCreate(&parentMenu).Error
33+
if err != nil {
34+
zap.L().Error("注册菜单失败", zap.Error(err))
35+
return errors.Wrap(err, "注册菜单失败")
36+
}
4337
pid := parentMenu.ID
44-
otherMenus[i].ParentId = pid
45-
}
46-
err = global.GVA_DB.Create(&otherMenus).Error
38+
for i := range otherMenus {
39+
otherMenus[i].ParentId = pid
40+
err = tx.Model(system.SysBaseMenu{}).Where("name = ? ", otherMenus[i].Name).FirstOrCreate(&otherMenus[i]).Error
41+
if err != nil {
42+
zap.L().Error("注册菜单失败", zap.Error(err))
43+
return errors.Wrap(err, "注册菜单失败")
44+
}
45+
}
46+
47+
return nil
48+
})
4749
if err != nil {
48-
fmt.Println(err)
50+
zap.L().Error("注册菜单失败", zap.Error(err))
4951
}
52+
5053
}

server/utils/autocode/template_funcs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ func GenerateSearchFormItem(field systemReq.AutoCodeField) string {
219219
if field.FieldType == "array" {
220220
multipleAttr = "multiple "
221221
}
222-
result += fmt.Sprintf(` <el-tree-select v-model="formData.%s" placeholder="请选择%s" :data="%sOptions" style="width:100%%" filterable :clearable="%v" check-strictly %s></el-tree-select>
222+
result += fmt.Sprintf(` <el-tree-select v-model="searchInfo.%s" placeholder="请选择%s" :data="%sOptions" style="width:100%%" filterable :clearable="%v" check-strictly %s></el-tree-select>
223223
`,
224224
field.FieldJson, field.FieldDesc, field.DictType, field.Clearable, multipleAttr)
225225
} else if field.CheckDataSource {

server/utils/claims.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func GetToken(c *gin.Context) string {
4949
global.GVA_LOG.Error("重新写入cookie token失败,未能成功解析token,请检查请求头是否存在x-token且claims是否为规定结构")
5050
return token
5151
}
52-
SetToken(c, token, int((claims.ExpiresAt.Unix()-time.Now().Unix())/60))
52+
SetToken(c, token, int(claims.ExpiresAt.Unix()-time.Now().Unix()))
5353
}
5454
return token
5555
}

web/src/core/config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const greenText = (text) => `\x1b[32m${text}\x1b[0m`
88
export const config = {
99
appName: 'Gin-Vue-Admin',
1010
showViteLogo: true,
11-
KeepAliveTabs: true,
11+
keepAliveTabs: false,
1212
logs: []
1313
}
1414

web/src/pinia/modules/router.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export const useRouterStore = defineStore('router', () => {
5656

5757
// 1. 首先添加原有的keepAlive配置
5858
keepArrTemp.push(...keepAliveRoutersArr)
59-
if (config.KeepAliveTabs) {
59+
if (config.keepAliveTabs) {
6060
history.forEach((item) => {
6161
// 2. 为所有history中的路由强制启用keep-alive
6262
// 通过routeMap获取路由信息,然后通过pathInfo获取组件名

web/src/view/superAdmin/user/user.vue

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@
174174
</div>
175175
</template>
176176
</el-dialog>
177-
177+
178178
<el-drawer
179179
v-model="addUserDialog"
180180
:size="appStore.drawerSize"
@@ -356,6 +356,12 @@
356356
}
357357
)
358358
359+
const authOptions = ref([])
360+
const setOptions = (authData) => {
361+
authOptions.value = []
362+
setAuthorityOptions(authData, authOptions.value)
363+
}
364+
359365
const initPage = async () => {
360366
getTableData()
361367
const res = await getAuthorityList()
@@ -373,7 +379,7 @@
373379
nickName: '',
374380
password: ''
375381
})
376-
382+
377383
// 生成随机密码
378384
const generateRandomPassword = () => {
379385
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*'
@@ -395,7 +401,7 @@
395401
})
396402
})
397403
}
398-
404+
399405
// 打开重置密码对话框
400406
const resetPasswordFunc = (row) => {
401407
resetPwdInfo.value.ID = row.ID
@@ -404,7 +410,7 @@
404410
resetPwdInfo.value.password = ''
405411
resetPwdDialog.value = true
406412
}
407-
413+
408414
// 确认重置密码
409415
const confirmResetPassword = async () => {
410416
if (!resetPwdInfo.value.password) {
@@ -414,12 +420,12 @@
414420
})
415421
return
416422
}
417-
423+
418424
const res = await resetPassword({
419425
ID: resetPwdInfo.value.ID,
420426
password: resetPwdInfo.value.password
421427
})
422-
428+
423429
if (res.code === 0) {
424430
ElMessage({
425431
type: 'success',
@@ -433,7 +439,7 @@
433439
})
434440
}
435441
}
436-
442+
437443
// 关闭重置密码对话框
438444
const closeResetPwdDialog = () => {
439445
resetPwdInfo.value.password = ''
@@ -450,12 +456,6 @@
450456
})
451457
}
452458
453-
const authOptions = ref([])
454-
const setOptions = (authData) => {
455-
authOptions.value = []
456-
setAuthorityOptions(authData, authOptions.value)
457-
}
458-
459459
const deleteUserFunc = async (row) => {
460460
ElMessageBox.confirm('确定要删除吗?', '提示', {
461461
confirmButtonText: '确定',

0 commit comments

Comments
 (0)