Browse Source

Merge branch 'gva_gormv2_dev' of https://github.com/flipped-aurora/gin-vue-admin into gva_gormv2_dev

main
pixel 3 years ago
parent
commit
a23e51db56
  1. 3
      server/api/v1/sys_initdb.go
  2. 4
      server/service/exa_excel_parse.go
  3. 33
      server/service/sys_initdb.go
  4. 2
      server/utils/upload/aliyun_oss.go
  5. 1
      server/utils/upload/qiniu.go
  6. 1
      server/utils/upload/tencent_cos.go

3
server/api/v1/sys_initdb.go

@ -5,6 +5,7 @@ import (
"gin-vue-admin/model/request"
"gin-vue-admin/model/response"
"gin-vue-admin/service"
"go.uber.org/zap"
"github.com/gin-gonic/gin"
@ -30,7 +31,7 @@ func InitDB(c *gin.Context) {
}
if err := service.InitDB(dbInfo); err != nil {
global.GVA_LOG.Error("自动创建数据库失败!", zap.Any("err", err))
response.FailWithMessage("自动创建数据库失败,请查看后台日志", c)
response.FailWithMessage("自动创建数据库失败,请查看后台日志,检查后在进行初始化", c)
return
}
response.OkWithData("自动创建数据库成功", c)

4
server/service/exa_excel_parse.go

@ -24,8 +24,8 @@ func ParseInfoList2Excel(infoList []model.SysBaseMenu, filePath string) error {
menu.Component,
})
}
excel.SaveAs(filePath)
return nil
err := excel.SaveAs(filePath)
return err
}
func ParseExcel2InfoList() ([]model.SysBaseMenu, error) {

33
server/service/sys_initdb.go

@ -9,10 +9,11 @@ import (
"gin-vue-admin/model/request"
"gin-vue-admin/source"
"gin-vue-admin/utils"
"path/filepath"
"github.com/spf13/viper"
"gorm.io/driver/mysql"
"gorm.io/gorm"
"path/filepath"
)
//@author: [songzhibin97](https://github.com/songzhibin97)
@ -71,13 +72,6 @@ func initDB(InitDBFunctions ...model.InitDBFunc) (err error) {
//@return: error
func InitDB(conf request.InitDB) error {
BaseMysql := config.Mysql{
Path: "",
Dbname: "",
Username: "",
Password: "",
Config: "charset=utf8mb4&parseTime=True&loc=Local",
}
if conf.Host == "" {
conf.Host = "127.0.0.1"
@ -100,15 +94,11 @@ func InitDB(conf request.InitDB) error {
Config: "charset=utf8mb4&parseTime=True&loc=Local",
}
if err := writeConfig(global.GVA_VP, MysqlConfig); err != nil {
return err
}
m := global.GVA_CONFIG.Mysql
if m.Dbname == "" {
if MysqlConfig.Dbname == "" {
return nil
}
linkDns := m.Username + ":" + m.Password + "@tcp(" + m.Path + ")/" + m.Dbname + "?" + m.Config
linkDns := MysqlConfig.Username + ":" + MysqlConfig.Password + "@tcp(" + MysqlConfig.Path + ")/" + MysqlConfig.Dbname + "?" + MysqlConfig.Config
mysqlConfig := mysql.Config{
DSN: linkDns, // DSN data source name
DefaultStringSize: 191, // string 类型字段的默认长度
@ -118,15 +108,11 @@ func InitDB(conf request.InitDB) error {
SkipInitializeWithVersion: false, // 根据版本自动配置
}
if db, err := gorm.Open(mysql.New(mysqlConfig), &gorm.Config{DisableForeignKeyConstraintWhenMigrating: true}); err != nil {
//global.GVA_LOG.Error("MySQL启动异常!", zap.Any("err", err))
//os.Exit(0)
//return nil
_ = writeConfig(global.GVA_VP, BaseMysql)
return nil
} else {
sqlDB, _ := db.DB()
sqlDB.SetMaxIdleConns(m.MaxIdleConns)
sqlDB.SetMaxOpenConns(m.MaxOpenConns)
sqlDB.SetMaxIdleConns(MysqlConfig.MaxIdleConns)
sqlDB.SetMaxOpenConns(MysqlConfig.MaxOpenConns)
global.GVA_DB = db
}
@ -147,7 +133,7 @@ func InitDB(conf request.InitDB) error {
model.SysOperationRecord{},
)
if err != nil {
_ = writeConfig(global.GVA_VP, BaseMysql)
global.GVA_DB = nil
return err
}
err = initDB(
@ -163,7 +149,10 @@ func InitDB(conf request.InitDB) error {
source.File,
source.BaseMenu)
if err != nil {
_ = writeConfig(global.GVA_VP, BaseMysql)
global.GVA_DB = nil
return err
}
if err = writeConfig(global.GVA_VP, MysqlConfig); err != nil {
return err
}
global.GVA_CONFIG.AutoCode.Root, _ = filepath.Abs("..")

2
server/utils/upload/aliyun_oss.go

@ -25,7 +25,7 @@ func (*AliyunOSS) UploadFile(file *multipart.FileHeader) (string, string, error)
global.GVA_LOG.Error("function file.Open() Failed", zap.Any("err", openError.Error()))
return "", "", errors.New("function file.Open() Failed, err:" + openError.Error())
}
defer f.Close() // 创建文件 defer 关闭
// 上传阿里云路径 文件名格式 自己可以改 建议保证唯一性
yunFileTmpPath := filepath.Join("uploads", time.Now().Format("2006-01-02")) + "/" + file.Filename

1
server/utils/upload/qiniu.go

@ -38,6 +38,7 @@ func (*Qiniu) UploadFile(file *multipart.FileHeader) (string, string, error) {
return "", "", errors.New("function file.Open() Filed, err:" + openError.Error())
}
defer f.Close() // 创建文件 defer 关闭
fileKey := fmt.Sprintf("%d%s", time.Now().Unix(), file.Filename) // 文件名格式 自己可以改 建议保证唯一性
putErr := formUploader.Put(context.Background(), &ret, upToken, fileKey, f, file.Size, &putExtra)
if putErr != nil {

1
server/utils/upload/tencent_cos.go

@ -24,6 +24,7 @@ func (*TencentCOS) UploadFile(file *multipart.FileHeader) (string, string, error
global.GVA_LOG.Error("function file.Open() Filed", zap.Any("err", openError.Error()))
return "", "", errors.New("function file.Open() Filed, err:" + openError.Error())
}
defer f.Close() // 创建文件 defer 关闭
fileKey := fmt.Sprintf("%d%s", time.Now().Unix(), file.Filename)
_, err := client.Object.Put(context.Background(), global.GVA_CONFIG.TencentCOS.PathPrefix+"/"+fileKey, f, nil)

Loading…
Cancel
Save