diff --git a/server/api/v1/sys_initdb.go b/server/api/v1/sys_initdb.go index 7a35819b..9cbe974c 100644 --- a/server/api/v1/sys_initdb.go +++ b/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" @@ -18,8 +19,8 @@ import ( // @Router /init/initdb [post] func InitDB(c *gin.Context) { if global.GVA_DB != nil { - global.GVA_LOG.Error("非法访问!") - response.FailWithMessage("非法访问", c) + global.GVA_LOG.Error("已存在数据库配置!") + response.FailWithMessage("已存在数据库配置", c) return } var dbInfo request.InitDB @@ -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) diff --git a/server/api/v1/sys_user.go b/server/api/v1/sys_user.go index a3d738d7..1d29ff40 100644 --- a/server/api/v1/sys_user.go +++ b/server/api/v1/sys_user.go @@ -23,15 +23,15 @@ import ( // @Success 200 {string} string "{"success":true,"data":{},"msg":"登陆成功"}" // @Router /base/login [post] func Login(c *gin.Context) { - var L request.Login - _ = c.ShouldBindJSON(&L) - if err := utils.Verify(L, utils.LoginVerify); err != nil { + var l request.Login + _ = c.ShouldBindJSON(&l) + if err := utils.Verify(l, utils.LoginVerify); err != nil { response.FailWithMessage(err.Error(), c) return } - if store.Verify(L.CaptchaId, L.Captcha, true) { - U := &model.SysUser{Username: L.Username, Password: L.Password} - if err, user := service.Login(U); err != nil { + if store.Verify(l.CaptchaId, l.Captcha, true) { + u := &model.SysUser{Username: l.Username, Password: l.Password} + if err, user := service.Login(u); err != nil { global.GVA_LOG.Error("登陆失败! 用户名不存在或者密码错误!", zap.Any("err", err)) response.FailWithMessage("用户名不存在或者密码错误", c) } else { @@ -112,13 +112,13 @@ func tokenNext(c *gin.Context, user model.SysUser) { // @Success 200 {string} string "{"success":true,"data":{},"msg":"注册成功"}" // @Router /user/register [post] func Register(c *gin.Context) { - var R request.Register - _ = c.ShouldBindJSON(&R) - if err := utils.Verify(R, utils.RegisterVerify); err != nil { + var r request.Register + _ = c.ShouldBindJSON(&r) + if err := utils.Verify(r, utils.RegisterVerify); err != nil { response.FailWithMessage(err.Error(), c) return } - user := &model.SysUser{Username: R.Username, NickName: R.NickName, Password: R.Password, HeaderImg: R.HeaderImg, AuthorityId: R.AuthorityId} + user := &model.SysUser{Username: r.Username, NickName: r.NickName, Password: r.Password, HeaderImg: r.HeaderImg, AuthorityId: r.AuthorityId} err, userReturn := service.Register(*user) if err != nil { global.GVA_LOG.Error("注册失败!", zap.Any("err", err)) @@ -142,8 +142,8 @@ func ChangePassword(c *gin.Context) { response.FailWithMessage(err.Error(), c) return } - U := &model.SysUser{Username: user.Username, Password: user.Password} - if err, _ := service.ChangePassword(U, user.NewPassword); err != nil { + u := &model.SysUser{Username: user.Username, Password: user.Password} + if err, _ := service.ChangePassword(u, user.NewPassword); err != nil { global.GVA_LOG.Error("修改失败!", zap.Any("err", err)) response.FailWithMessage("修改失败,原密码与当前账户不符", c) } else { diff --git a/server/config.yaml b/server/config.yaml index 00be24cb..9d969a7d 100644 --- a/server/config.yaml +++ b/server/config.yaml @@ -53,6 +53,7 @@ captcha: img-height: 80 # mysql connect configuration +# 未初始化之前请勿手动修改数据库信息!!!如果一定要手动初始化请看(https://www.gin-vue-admin.com/docs/first) mysql: path: '' config: '' diff --git a/server/model/sys_auto_code.go b/server/model/sys_auto_code.go index 6e9e0bde..9e030f5d 100644 --- a/server/model/sys_auto_code.go +++ b/server/model/sys_auto_code.go @@ -7,6 +7,7 @@ type AutoCodeStruct struct { StructName string `json:"structName"` // Struct名称 TableName string `json:"tableName"` // 表名 PackageName string `json:"packageName"` // 文件名称 + HumpPackageName string `json:"humpPackageName"` // go文件名称 Abbreviation string `json:"abbreviation"` // Struct简称 Description string `json:"description"` // Struct中文名称 AutoCreateApiToSql bool `json:"autoCreateApiToSql"` // 是否自动创建api diff --git a/server/resource/template/server/model.go.tpl b/server/resource/template/server/model.go.tpl index 6ec46518..bbc7331b 100644 --- a/server/resource/template/server/model.go.tpl +++ b/server/resource/template/server/model.go.tpl @@ -9,9 +9,9 @@ import ( type {{.StructName}} struct { global.GVA_MODEL {{- range .Fields}} {{- if eq .FieldType "bool" }} - {{.FieldName}} *{{.FieldType}} `json:"{{.FieldJson}}" form:"{{.FieldJson}}" gorm:"column:{{.ColumnName}};comment:{{.Comment}}{{- if .DataType -}};type:{{.DataType}}{{- if eq .FieldType "string" -}}{{- if .DataTypeLong -}}({{.DataTypeLong}}){{- end -}}{{- end -}};{{- if .DataTypeLong -}}size:{{.DataTypeLong}};{{- end -}}{{- end -}}"` + {{.FieldName}} *{{.FieldType}} `json:"{{.FieldJson}}" form:"{{.FieldJson}}" gorm:"column:{{.ColumnName}};comment:{{.Comment}}{{- if .DataType -}};type:{{.DataType}}{{- end }}"` {{- else }} - {{.FieldName}} {{.FieldType}} `json:"{{.FieldJson}}" form:"{{.FieldJson}}" gorm:"column:{{.ColumnName}};comment:{{.Comment}}{{- if .DataType -}};type:{{.DataType}}{{- if eq .FieldType "string" -}}{{- if .DataTypeLong -}}({{.DataTypeLong}}){{- end -}}{{- end -}};{{- if .DataTypeLong -}}size:{{.DataTypeLong}};{{- end -}}{{- end -}}"` + {{.FieldName}} {{.FieldType}} `json:"{{.FieldJson}}" form:"{{.FieldJson}}" gorm:"column:{{.ColumnName}};comment:{{.Comment}}{{- if .DataType -}};type:{{.DataType}}{{- if eq .FieldType "string" -}}{{- if .DataTypeLong -}}({{.DataTypeLong}}){{- end -}}{{- end -}};{{- if ne .FieldType "string" -}}{{- if .DataTypeLong -}}size:{{.DataTypeLong}};{{- end -}}{{- end -}}{{- end -}}"` {{- end }} {{- end }} } diff --git a/server/router/sys_base.go b/server/router/sys_base.go index da4159b8..20da80dc 100644 --- a/server/router/sys_base.go +++ b/server/router/sys_base.go @@ -2,12 +2,11 @@ package router import ( "gin-vue-admin/api/v1" - "gin-vue-admin/middleware" "github.com/gin-gonic/gin" ) func InitBaseRouter(Router *gin.RouterGroup) (R gin.IRoutes) { - BaseRouter := Router.Group("base").Use(middleware.NeedInit()) + BaseRouter := Router.Group("base") { BaseRouter.POST("login", v1.Login) BaseRouter.POST("captcha", v1.Captcha) diff --git a/server/service/jwt_black_list.go b/server/service/jwt_black_list.go index db625f03..507a76ff 100644 --- a/server/service/jwt_black_list.go +++ b/server/service/jwt_black_list.go @@ -26,7 +26,8 @@ func JsonInBlacklist(jwtList model.JwtBlacklist) (err error) { //@return: bool func IsBlacklist(jwt string) bool { - isNotFound := errors.Is(global.GVA_DB.Where("jwt = ?", jwt).First(&model.JwtBlacklist{}).Error, gorm.ErrRecordNotFound) + err := global.GVA_DB.Where("jwt = ?", jwt).First(&model.JwtBlacklist{}).Error + isNotFound := errors.Is(err, gorm.ErrRecordNotFound) return !isNotFound } diff --git a/server/service/sys_auto_code.go b/server/service/sys_auto_code.go index eda86476..679120af 100644 --- a/server/service/sys_auto_code.go +++ b/server/service/sys_auto_code.go @@ -360,8 +360,15 @@ func getNeedList(autoCode *model.AutoCodeStruct) (dataList []tplData, fileList [ origFileName := strings.TrimSuffix(trimBase[lastSeparator+1:], ".tpl") firstDot := strings.Index(origFileName, ".") if firstDot != -1 { + var fileName string + if origFileName[firstDot:] !=".go"{ + fileName = autoCode.PackageName+origFileName[firstDot:] + }else{ + fileName = autoCode.HumpPackageName+origFileName[firstDot:] + } + dataList[index].autoCodePath = filepath.Join(autoPath, trimBase[:lastSeparator], autoCode.PackageName, - origFileName[:firstDot], autoCode.PackageName+origFileName[firstDot:]) + origFileName[:firstDot], fileName) } } diff --git a/server/service/sys_initdb.go b/server/service/sys_initdb.go index 3880cb05..44e4ec70 100644 --- a/server/service/sys_initdb.go +++ b/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("..") diff --git a/web/.env.development b/web/.env.development index 93aab90b..6e1d0340 100644 --- a/web/.env.development +++ b/web/.env.development @@ -1,6 +1,6 @@ ENV = 'development' -VUE_APP_CLI_PORT = '8080' -VUE_APP_SERVER_PORT = '8888' -VUE_APP_BASE_API = '/api' -VUE_APP_BASE_PATH = 'http://127.0.0.1' +VUE_APP_CLI_PORT = 8080 +VUE_APP_SERVER_PORT = 8888 +VUE_APP_BASE_API = /api +VUE_APP_BASE_PATH = http://127.0.0.1 diff --git a/web/.env.production b/web/.env.production index f8929e13..9e015c86 100644 --- a/web/.env.production +++ b/web/.env.production @@ -1,7 +1,7 @@ ENV = 'production' -VUE_APP_CLI_PORT = '8080' -VUE_APP_SERVER_PORT = '8888' -VUE_APP_BASE_API = '/api' +VUE_APP_CLI_PORT = 8080 +VUE_APP_SERVER_PORT = 8888 +VUE_APP_BASE_API = /api #下方修改为你的线上ip -VUE_APP_BASE_PATH = 'http://8.141.61.63' \ No newline at end of file +VUE_APP_BASE_PATH = http://8.141.61.63 \ No newline at end of file diff --git a/web/src/App.vue b/web/src/App.vue index a864f38f..89a2d2b9 100644 --- a/web/src/App.vue +++ b/web/src/App.vue @@ -5,12 +5,8 @@ diff --git a/web/src/core/config.js b/web/src/core/config.js new file mode 100644 index 00000000..08d58028 --- /dev/null +++ b/web/src/core/config.js @@ -0,0 +1,15 @@ +/* +* 网站配置文件 +* */ + + + +const config ={ + + appName : 'Gin-Vue-Admin', + + appLogo : 'https://www.gin-vue-admin.com/img/logo.png' + +} + +export default config \ No newline at end of file diff --git a/web/src/core/gin-vue-admin.js b/web/src/core/gin-vue-admin.js index 2b5f178c..5163ebfd 100644 --- a/web/src/core/gin-vue-admin.js +++ b/web/src/core/gin-vue-admin.js @@ -10,6 +10,10 @@ import APlayer from '@moefe/vue-aplayer' import '../../node_modules/timeline-vuejs/dist/timeline-vuejs.css' // 路由守卫 import Bus from '@/utils/bus' +//加载网站配置文件夹 +import config from './config' +Vue.prototype.$GIN_VUE_ADMIN = config +console.log(config) Vue.use(Bus) Vue.use(APlayer, { defaultCover: 'https://github.com/u3u.png', diff --git a/web/src/utils/page.js b/web/src/utils/page.js index 8c07b9fd..42e4a65f 100644 --- a/web/src/utils/page.js +++ b/web/src/utils/page.js @@ -1,8 +1,7 @@ -const title = 'GIN-VUE-ADMIN' - +import config from '@/core/config' export default function getPageTitle(pageTitle) { if (pageTitle) { - return `${pageTitle} - ${title}` + return `${pageTitle} - ${config.appName}` } - return `${title}` + return `${config.appName}` } diff --git a/web/src/utils/request.js b/web/src/utils/request.js index 025d6439..884a5838 100644 --- a/web/src/utils/request.js +++ b/web/src/utils/request.js @@ -2,7 +2,7 @@ import axios from 'axios' // 引入axios import { Message } from 'element-ui' import { store } from '@/store' import context from '@/main' -import router from '@/router/index' +import { MessageBox } from 'element-ui' const service = axios.create({ baseURL: process.env.VUE_APP_BASE_API, @@ -64,16 +64,6 @@ service.interceptors.response.use( if (response.headers['new-token']) { store.commit('user/setToken', response.headers['new-token']) } - if (response.data.code === 0) { - if (response.data.data?.needInit) { - Message({ - type: 'info', - message: '您是第一次使用,请初始化' - }) - store.commit('user/NeedInit') - router.push({ name: 'Init' }) - } - } if (response.data.code === 0 || response.headers.success === 'true') { return response.data } else { @@ -90,11 +80,14 @@ service.interceptors.response.use( }, error => { closeLoading() - Message({ - showClose: true, - message: error, - type: 'error' + MessageBox.confirm(`检测到接口错误${error},此类错误内容常见于后台panic,如果影响您正常使用可强制登出清理缓存`, '接口报错', { + distinguishCancelAndClose: true, + confirmButtonText: '清理缓存', + cancelButtonText: '取消' }) + .then(() => { + store.commit('user/LoginOut') + }) return error } ) diff --git a/web/src/view/init/index.vue b/web/src/view/init/index.vue index 68362069..b87d200b 100644 --- a/web/src/view/init/index.vue +++ b/web/src/view/init/index.vue @@ -1,11 +1,21 @@