mirror of
https://gitcode.com/flipped-aurora/gin-vue-admin.git
synced 2026-03-20 10:30:21 +00:00
* feat: ai相关调用调整 * feat: ai请求模式变更 * feat: ai自动化完善 * feat: 添加环境变量支持,条件渲染开发环境特定元素 * feat: 2.5版本插件,不再冗余进主项目,可以做到完全自动安装。 * feat: 添加插件管理功能,包括获取插件列表和删除插件的API接口 * feat: 同步测试插件的id调整 * feat: 增加插件相关初始化api * feat: 增加登录日志 * feat: 增加签发token功能 * feat: 更安全的目录清理功能,确保仅在无Go文件时删除目录 * feat: 增加skills定义能力 * feat: 优化技能管理界面,增强用户体验和视觉效果 * feat: 更新授权链接,确保用户获取最新的授权信息 * feat: 修改技能定义器名称为“Skills管理”,并重命名文件为sys_skills.go * feat: 增加全局约束的获取和保存功能,更新相关API和前端实现 * feat: 增加skills全局约束管理 * feat: 添加 vite-check-multiple-dom 插件,支持路由多根检测 (#2173) --------- Co-authored-by: piexlMax(奇淼 <qimiaojiangjizhao@gmail.com> Co-authored-by: Azir-11 <2075125282@qq.com> Co-authored-by: 青菜白玉汤 <79054161+Azir-11@users.noreply.github.com>
139 lines
3.6 KiB
Go
139 lines
3.6 KiB
Go
package utils
|
|
|
|
import (
|
|
"github.com/pkg/errors"
|
|
"go.uber.org/zap"
|
|
"gorm.io/gorm"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
"sync"
|
|
|
|
"github.com/flipped-aurora/gin-vue-admin/server/global"
|
|
"github.com/flipped-aurora/gin-vue-admin/server/model/system"
|
|
)
|
|
|
|
var (
|
|
ApiMap = make(map[string][]system.SysApi)
|
|
MenuMap = make(map[string][]system.SysBaseMenu)
|
|
DictMap = make(map[string][]system.SysDictionary)
|
|
rw sync.Mutex
|
|
)
|
|
|
|
func getPluginName() string {
|
|
_, file, _, ok := runtime.Caller(2)
|
|
pluginName := ""
|
|
if ok {
|
|
file = filepath.ToSlash(file)
|
|
const key = "server/plugin/"
|
|
if idx := strings.Index(file, key); idx != -1 {
|
|
remain := file[idx+len(key):]
|
|
parts := strings.Split(remain, "/")
|
|
if len(parts) > 0 {
|
|
pluginName = parts[0]
|
|
}
|
|
}
|
|
}
|
|
return pluginName
|
|
}
|
|
|
|
func RegisterApis(apis ...system.SysApi) {
|
|
name := getPluginName()
|
|
if name != "" {
|
|
rw.Lock()
|
|
ApiMap[name] = apis
|
|
rw.Unlock()
|
|
}
|
|
|
|
err := global.GVA_DB.Transaction(func(tx *gorm.DB) error {
|
|
for _, api := range apis {
|
|
err := tx.Model(system.SysApi{}).Where("path = ? AND method = ? AND api_group = ? ", api.Path, api.Method, api.ApiGroup).FirstOrCreate(&api).Error
|
|
if err != nil {
|
|
zap.L().Error("注册API失败", zap.Error(err), zap.String("api", api.Path), zap.String("method", api.Method), zap.String("apiGroup", api.ApiGroup))
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
zap.L().Error("注册API失败", zap.Error(err))
|
|
}
|
|
}
|
|
|
|
func RegisterMenus(menus ...system.SysBaseMenu) {
|
|
name := getPluginName()
|
|
if name != "" {
|
|
rw.Lock()
|
|
MenuMap[name] = menus
|
|
rw.Unlock()
|
|
}
|
|
|
|
parentMenu := menus[0]
|
|
otherMenus := menus[1:]
|
|
err := global.GVA_DB.Transaction(func(tx *gorm.DB) error {
|
|
err := tx.Model(system.SysBaseMenu{}).Where("name = ? ", parentMenu.Name).FirstOrCreate(&parentMenu).Error
|
|
if err != nil {
|
|
zap.L().Error("注册菜单失败", zap.Error(err))
|
|
return errors.Wrap(err, "注册菜单失败")
|
|
}
|
|
pid := parentMenu.ID
|
|
for i := range otherMenus {
|
|
otherMenus[i].ParentId = pid
|
|
err = tx.Model(system.SysBaseMenu{}).Where("name = ? ", otherMenus[i].Name).FirstOrCreate(&otherMenus[i]).Error
|
|
if err != nil {
|
|
zap.L().Error("注册菜单失败", zap.Error(err))
|
|
return errors.Wrap(err, "注册菜单失败")
|
|
}
|
|
}
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
zap.L().Error("注册菜单失败", zap.Error(err))
|
|
}
|
|
|
|
}
|
|
|
|
func RegisterDictionaries(dictionaries ...system.SysDictionary) {
|
|
name := getPluginName()
|
|
if name != "" {
|
|
rw.Lock()
|
|
DictMap[name] = dictionaries
|
|
rw.Unlock()
|
|
}
|
|
|
|
err := global.GVA_DB.Transaction(func(tx *gorm.DB) error {
|
|
for _, dict := range dictionaries {
|
|
details := dict.SysDictionaryDetails
|
|
dict.SysDictionaryDetails = nil
|
|
err := tx.Model(system.SysDictionary{}).Where("type = ?", dict.Type).FirstOrCreate(&dict).Error
|
|
if err != nil {
|
|
zap.L().Error("注册字典失败", zap.Error(err), zap.String("type", dict.Type))
|
|
return err
|
|
}
|
|
for _, detail := range details {
|
|
detail.SysDictionaryID = int(dict.ID)
|
|
err = tx.Model(system.SysDictionaryDetail{}).Where("sys_dictionary_id = ? AND value = ?", dict.ID, detail.Value).FirstOrCreate(&detail).Error
|
|
if err != nil {
|
|
zap.L().Error("注册字典详情失败", zap.Error(err), zap.String("value", detail.Value))
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
zap.L().Error("注册字典失败", zap.Error(err))
|
|
}
|
|
}
|
|
|
|
func Pointer[T any](in T) *T {
|
|
return &in
|
|
}
|
|
|
|
func GetPluginData(pluginName string) ([]system.SysApi, []system.SysBaseMenu, []system.SysDictionary) {
|
|
rw.Lock()
|
|
defer rw.Unlock()
|
|
return ApiMap[pluginName], MenuMap[pluginName], DictMap[pluginName]
|
|
}
|
|
|