Skip to content

Commit 9b23ef9

Browse files
author
pixel
committed
自动初始化整体调试完成
1 parent d9a9b4e commit 9b23ef9

File tree

14 files changed

+273
-32
lines changed

14 files changed

+273
-32
lines changed

server/api/v1/sys_initdb.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"gin-vue-admin/model/request"
66
"gin-vue-admin/model/response"
77
"gin-vue-admin/service"
8-
98
"go.uber.org/zap"
109

1110
"github.com/gin-gonic/gin"
@@ -31,7 +30,7 @@ func InitDB(c *gin.Context) {
3130
}
3231
if err := service.InitDB(dbInfo); err != nil {
3332
global.GVA_LOG.Error("自动创建数据库失败", zap.Any("err", err))
34-
response.FailWithMessage("自动创建数据库失败", c)
33+
response.FailWithMessage("自动创建数据库失败,请查看后台日志", c)
3534
return
3635
}
3736
response.OkWithData("自动创建数据库成功", c)
@@ -43,17 +42,17 @@ func InitDB(c *gin.Context) {
4342
// @Success 200 {string} string "{"code":0,"data":{},"msg":"探测完成"}"
4443
// @Router /init/checkdb [post]
4544
func CheckDB(c *gin.Context) {
46-
if global.GVA_DB == nil {
45+
if global.GVA_DB != nil {
4746
global.GVA_LOG.Info("数据库无需初始化")
4847
response.OkWithDetailed(gin.H{
49-
"needInit":false,
50-
},"数据库无需初始化", c)
48+
"needInit": false,
49+
}, "数据库无需初始化", c)
5150
return
52-
}else{
51+
} else {
5352
global.GVA_LOG.Info("前往初始化数据库")
5453
response.OkWithDetailed(gin.H{
55-
"needInit":true,
56-
},"前往初始化数据库", c)
54+
"needInit": true,
55+
}, "前往初始化数据库", c)
5756
return
5857
}
5958
}

server/config.yaml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,11 @@ captcha:
5454

5555
# mysql connect configuration
5656
mysql:
57-
path: '127.0.0.1:3306'
58-
config: 'charset=utf8mb4&parseTime=True&loc=Local'
59-
db-name: 'qmPlus'
60-
username: 'root'
61-
password: 'Aa@6447985'
57+
path: ''
58+
config: ''
59+
db-name: ''
60+
username: ''
61+
password: ''
6262
max-idle-conns: 10
6363
max-open-conns: 100
6464
log-mode: false

server/middleware/need_init.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package middleware
2+
3+
import (
4+
"gin-vue-admin/global"
5+
"gin-vue-admin/model/response"
6+
"github.com/gin-gonic/gin"
7+
)
8+
9+
// 处理跨域请求,支持options访问
10+
func NeedInit() gin.HandlerFunc {
11+
return func(c *gin.Context) {
12+
if global.GVA_DB == nil {
13+
response.OkWithDetailed(gin.H{
14+
"needInit": true,
15+
}, "前往初始化数据库", c)
16+
c.Abort()
17+
} else {
18+
c.Next()
19+
}
20+
// 处理请求
21+
}
22+
}

server/model/request/sys_init.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package request
33
type InitDB struct {
44
Host string `json:"host"`
55
Port string `json:"port"`
6-
UserName string `json:"user_name" binding:"required"`
6+
UserName string `json:"userName" binding:"required"`
77
Password string `json:"password"`
8-
DBName string `json:"db_name" binding:"required"`
8+
DBName string `json:"dbName" binding:"required"`
99
}

server/router/sys_initdb.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
func InitInitRouter(Router *gin.RouterGroup) {
99
ApiRouter := Router.Group("init")
1010
{
11-
ApiRouter.POST("initbd", v1.InitDB) // 创建Api
12-
ApiRouter.POST("checkdb", v1.CheckDB) // 创建Api
11+
ApiRouter.POST("initdb", v1.InitDB) // 创建Api
12+
ApiRouter.POST("checkdb", v1.CheckDB) // 创建Api
1313
}
1414
}

server/service/sys_initdb.go

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"gin-vue-admin/model/request"
99
"gin-vue-admin/source"
1010
"github.com/spf13/viper"
11+
"gorm.io/driver/mysql"
12+
"gorm.io/gorm"
1113
)
1214

1315
//@author: [songzhibin97](https://github.com/songzhibin97)
@@ -62,6 +64,7 @@ func InitDB(conf request.InitDB) error {
6264
if conf.Host == "" {
6365
conf.Host = "127.0.0.1"
6466
}
67+
6568
if conf.Port == "" {
6669
conf.Port = "3306"
6770
}
@@ -76,11 +79,65 @@ func InitDB(conf request.InitDB) error {
7679
"mysql.db-name": conf.DBName,
7780
"mysql.username": conf.UserName,
7881
"mysql.password": conf.Password,
82+
"mysql.config": "charset=utf8mb4&parseTime=True&loc=Local",
7983
}
8084
if err := writeConfig(global.GVA_VP, setting); err != nil {
8185
return err
8286
}
83-
err := initDB(source.Admin,
87+
m := global.GVA_CONFIG.Mysql
88+
if m.Dbname == "" {
89+
return nil
90+
}
91+
92+
linkDns := m.Username + ":" + m.Password + "@tcp(" + m.Path + ")/" + m.Dbname + "?" + m.Config
93+
mysqlConfig := mysql.Config{
94+
DSN: linkDns, // DSN data source name
95+
DefaultStringSize: 191, // string 类型字段的默认长度
96+
DisableDatetimePrecision: true, // 禁用 datetime 精度,MySQL 5.6 之前的数据库不支持
97+
DontSupportRenameIndex: true, // 重命名索引时采用删除并新建的方式,MySQL 5.7 之前的数据库和 MariaDB 不支持重命名索引
98+
DontSupportRenameColumn: true, // 用 `change` 重命名列,MySQL 8 之前的数据库和 MariaDB 不支持重命名列
99+
SkipInitializeWithVersion: false, // 根据版本自动配置
100+
}
101+
if db, err := gorm.Open(mysql.New(mysqlConfig), &gorm.Config{DisableForeignKeyConstraintWhenMigrating: true}); err != nil {
102+
//global.GVA_LOG.Error("MySQL启动异常", zap.Any("err", err))
103+
//os.Exit(0)
104+
//return nil
105+
return nil
106+
} else {
107+
sqlDB, _ := db.DB()
108+
sqlDB.SetMaxIdleConns(m.MaxIdleConns)
109+
sqlDB.SetMaxOpenConns(m.MaxOpenConns)
110+
global.GVA_DB = db
111+
}
112+
113+
err := global.GVA_DB.AutoMigrate(
114+
model.SysUser{},
115+
model.SysAuthority{},
116+
model.SysApi{},
117+
model.SysBaseMenu{},
118+
model.SysBaseMenuParameter{},
119+
model.JwtBlacklist{},
120+
model.SysDictionary{},
121+
model.SysDictionaryDetail{},
122+
model.ExaFileUploadAndDownload{},
123+
model.ExaFile{},
124+
model.ExaFileChunk{},
125+
model.ExaSimpleUploader{},
126+
model.ExaCustomer{},
127+
model.SysOperationRecord{},
128+
model.WorkflowProcess{},
129+
model.WorkflowNode{},
130+
model.WorkflowEdge{},
131+
model.WorkflowStartPoint{},
132+
model.WorkflowEndPoint{},
133+
model.WorkflowMove{},
134+
model.ExaWfLeave{},
135+
)
136+
if err != nil {
137+
return err
138+
}
139+
err = initDB(
140+
source.Admin,
84141
source.Api,
85142
source.AuthorityMenu,
86143
source.Authority,
@@ -92,7 +149,7 @@ func InitDB(conf request.InitDB) error {
92149
source.File,
93150
source.BaseMenu,
94151
source.Workflow)
95-
if err!=nil {
152+
if err != nil {
96153
return err
97154
}
98155
return nil

server/source/casbin.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ var carbines = []gormadapter.CasbinRule{
176176
//@author: [SliverHorn](https://github.com/SliverHorn)
177177
//@description: casbin_rule 表数据初始化
178178
func (c *casbin) Init() error {
179+
global.GVA_DB.AutoMigrate(gormadapter.CasbinRule{})
179180
return global.GVA_DB.Transaction(func(tx *gorm.DB) error {
180181
if tx.Find(&[]gormadapter.CasbinRule{}).RowsAffected == 154 {
181182
color.Danger.Println("\n[Mysql] --> casbin_rule 表的初始数据已存在!")

web/src/App.vue

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,20 @@
55
</template>
66

77
<script>
8+
import { checkDB } from "@/api/initdb"
89
export default {
910
name: 'app',
10-
components: {}
11+
async created(){
12+
const res = await checkDB()
13+
if(res.code == 0 && res.data.needInit){
14+
this.$message({
15+
type:"info",
16+
message:"您是第一次使用,请初始化"
17+
})
18+
this.$store.commit("user/NeedInit")
19+
this.$router.push({name:"init"})
20+
}
21+
}
1122
}
1223
</script>
1324

web/src/api/initdb.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import service from '@/utils/request'
2+
3+
// @Tags InitDB
4+
// @Summary 初始化用户数据库
5+
// @Produce application/json
6+
// @Param data body request.InitDB true "初始化数据库参数"
7+
// @Success 200 {string} string "{"code":0,"data":{},"msg":"自动创建数据库成功"}"
8+
// @Router /init/initdb [post]
9+
export const initDB = (data) => {
10+
return service({
11+
url: "/init/initdb",
12+
method: 'post',
13+
data
14+
})
15+
}
16+
17+
18+
// @Tags CheckDB
19+
// @Summary 初始化用户数据库
20+
// @Produce application/json
21+
// @Success 200 {string} string "{"code":0,"data":{},"msg":"探测完成"}"
22+
// @Router /init/checkdb [post]
23+
24+
export const checkDB = () => {
25+
return service({
26+
url: "/init/checkdb",
27+
method: 'post',
28+
})
29+
}

web/src/permission.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import getPageTitle from '@/utils/page'
44

55
let asyncRouterFlag = 0
66

7-
const whiteList = ['login']
7+
const whiteList = ['login','init']
88
router.beforeEach(async(to, from, next) => {
99
const token = store.getters['user/token']
1010
// 在白名单中的判断情况

web/src/router/index.js

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,25 @@ Vue.use(Router)
77
const originalPush = Router.prototype.push
88
//修改原型对象中的push方法
99
Router.prototype.push = function push(location) {
10-
return originalPush.call(this, location).catch(err => err)
10+
return originalPush.call(this, location).catch(err => err)
1111
}
1212

1313
const baseRouters = [{
14-
path: '/',
15-
redirect: '/login'
16-
},
17-
{
18-
path: '/login',
19-
name: 'login',
20-
component: () =>
21-
import ('@/view/login/login.vue')
22-
}
14+
path: '/',
15+
redirect: '/login'
16+
},
17+
{
18+
path: "/init",
19+
name: 'init',
20+
component: () =>
21+
import('@/view/init/init.vue')
22+
},
23+
{
24+
path: '/login',
25+
name: 'login',
26+
component: () =>
27+
import('@/view/login/login.vue')
28+
}
2329
]
2430

2531
// 需要通过后台数据来生成的组件

web/src/store/module/router.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@ export const router = {
4242
children: []
4343
}]
4444
const asyncRouterRes = await asyncMenu()
45-
const asyncRouter = asyncRouterRes.data.menus
45+
if(asyncRouterRes.code !=0){
46+
return
47+
}
48+
const asyncRouter = asyncRouterRes.data&&asyncRouterRes.data.menus
4649
asyncRouter.push({
4750
path: "404",
4851
name: "404",

web/src/store/module/user.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ export const user = {
2121
// 这里的 `state` 对象是模块的局部状态
2222
state.token = token
2323
},
24+
NeedInit(state){
25+
state.userInfo = {}
26+
state.token = ""
27+
sessionStorage.clear()
28+
router.push({ name: 'init', replace: true })
29+
30+
},
2431
LoginOut(state) {
2532
state.userInfo = {}
2633
state.token = ""

0 commit comments

Comments
 (0)