Browse Source

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

main
pixel 4 years ago
parent
commit
5c45744745
  1. 2
      server/api/v1/sys_user.go
  2. 2
      server/config.yaml
  3. 2
      server/config/gorm.go
  4. 58
      server/initialize/gorm.go
  5. 33
      server/initialize/logger.go
  6. 11
      server/utils/cmd_Task.go
  7. 2
      server/utils/verify.go
  8. 9
      web/src/components/upload/image.vue
  9. 8
      web/src/utils/stringFun.js
  10. 10
      web/src/view/systemTools/autoCode/component/fieldDialog.vue

2
server/api/v1/sys_user.go

@ -240,7 +240,7 @@ func DeleteUser(c *gin.Context) {
func SetUserInfo(c *gin.Context) {
var user model.SysUser
_ = c.ShouldBindJSON(&user)
if err := utils.Verify(user, utils.SetUserVerify); err != nil {
if err := utils.Verify(user, utils.IdVerify); err != nil {
response.FailWithMessage(err.Error(), c)
return
}

2
server/config.yaml

@ -60,7 +60,7 @@ mysql:
max-idle-conns: 10
max-open-conns: 100
log-mode: false
log-zap: false
log-zap: ""
# local configuration
local:

2
server/config/gorm.go

@ -9,5 +9,5 @@ type Mysql struct {
MaxIdleConns int `mapstructure:"max-idle-conns" json:"maxIdleConns" yaml:"max-idle-conns"`
MaxOpenConns int `mapstructure:"max-open-conns" json:"maxOpenConns" yaml:"max-open-conns"`
LogMode bool `mapstructure:"log-mode" json:"logMode" yaml:"log-mode"`
LogZap bool `mapstructure:"log-zap" json:"logZap" yaml:"log-zap"`
LogZap string `mapstructure:"log-zap" json:"logZap" yaml:"log-zap"`
}

58
server/initialize/gorm.go

@ -10,7 +10,11 @@ import (
"os"
)
// Gorm 初始化数据库并产生数据库全局变量
//@author: SliverHorn
//@function: Gorm
//@description: 初始化数据库并产生数据库全局变量
//@return: *gorm.DB
func Gorm() *gorm.DB {
switch global.GVA_CONFIG.System.DbType {
case "mysql":
@ -20,7 +24,12 @@ func Gorm() *gorm.DB {
}
}
// MysqlTables 注册数据库表专用
// MysqlTables
//@author: SliverHorn
//@function: MysqlTables
//@description: 注册数据库表专用
//@param: db *gorm.DB
func MysqlTables(db *gorm.DB) {
err := db.AutoMigrate(
model.SysUser{},
@ -52,7 +61,12 @@ func MysqlTables(db *gorm.DB) {
global.GVA_LOG.Info("register table success")
}
// GormMysql 初始化Mysql数据库
//
//@author: SliverHorn
//@function: GormMysql
//@description: 初始化Mysql数据库
//@return: *gorm.DB
func GormMysql() *gorm.DB {
m := global.GVA_CONFIG.Mysql
dsn := m.Username + ":" + m.Password + "@tcp(" + m.Path + ")/" + m.Dbname + "?" + m.Config
@ -76,23 +90,45 @@ func GormMysql() *gorm.DB {
}
}
// gormConfig 根据配置决定是否开启日志
//@author: SliverHorn
//@function: gormConfig
//@description: 根据配置决定是否开启日志
//@param: mod bool
//@return: *gorm.Config
func gormConfig(mod bool) *gorm.Config {
if global.GVA_CONFIG.Mysql.LogZap {
switch global.GVA_CONFIG.Mysql.LogZap {
case "Silent":
return &gorm.Config{
Logger: Default.LogMode(logger.Info),
Logger: Default.LogMode(logger.Silent),
DisableForeignKeyConstraintWhenMigrating: true,
}
}
if mod {
case "Error":
return &gorm.Config{
Logger: logger.Default.LogMode(logger.Info),
Logger: Default.LogMode(logger.Error),
DisableForeignKeyConstraintWhenMigrating: true,
}
} else {
case "Warn":
return &gorm.Config{
Logger: logger.Default.LogMode(logger.Silent),
Logger: Default.LogMode(logger.Warn),
DisableForeignKeyConstraintWhenMigrating: true,
}
case "Info":
return &gorm.Config{
Logger: Default.LogMode(logger.Info),
DisableForeignKeyConstraintWhenMigrating: true,
}
default:
if mod {
return &gorm.Config{
Logger: logger.Default.LogMode(logger.Info),
DisableForeignKeyConstraintWhenMigrating: true,
}
} else {
return &gorm.Config{
Logger: logger.Default.LogMode(logger.Silent),
DisableForeignKeyConstraintWhenMigrating: true,
}
}
}
}

33
server/initialize/logger.go

@ -14,8 +14,8 @@ import (
)
var (
Discard = New(log.New(ioutil.Discard, "", log.LstdFlags), GormConfig{})
Default = New(log.New(os.Stdout, "\r\n", log.LstdFlags), GormConfig{
Discard = New(log.New(ioutil.Discard, "", log.LstdFlags), log.New(ioutil.Discard, "", log.LstdFlags), GormConfig{})
Default = New(log.New(os.Stdout, "\r\n", log.LstdFlags), log.New(os.Stdout, "\r\n", log.LstdFlags), GormConfig{
SlowThreshold: 200 * time.Millisecond,
LogLevel: logger.Warn,
Colorful: true,
@ -31,7 +31,7 @@ type traceRecorder struct {
Err error
}
func New(writer Writer, config GormConfig) logger.Interface {
func New(writer Writer, gormWriter logger.Writer, config GormConfig) logger.Interface {
var (
infoStr = "%s\n[info] "
warnStr = "%s\n[warn] "
@ -52,6 +52,7 @@ func New(writer Writer, config GormConfig) logger.Interface {
return &GormLogger{
Writer: writer,
gormWriter: gormWriter,
GormConfig: config,
infoStr: infoStr,
warnStr: warnStr,
@ -75,6 +76,7 @@ type GormConfig struct {
type GormLogger struct {
Writer
gormWriter logger.Writer
GormConfig
infoStr, warnStr, errStr string
traceStr, traceErrStr, traceWarnStr string
@ -135,16 +137,31 @@ func (g *GormLogger) Trace(ctx context.Context, begin time.Time, fc func() (stri
}
func (g *GormLogger) Printf(message string, data ...interface{}) {
if global.GVA_CONFIG.Mysql.LogZap == "Info" && !global.GVA_CONFIG.Mysql.LogMode {
switch len(data) {
case 0:
global.GVA_LOG.Info(message)
case 1:
global.GVA_LOG.Info("gorm", zap.Any("src", data[0]))
case 2:
global.GVA_LOG.Info("gorm", zap.Any("src", data[0]), zap.Any("duration", data[1]))
case 3:
global.GVA_LOG.Info("gorm", zap.Any("src", data[0]), zap.Any("duration", data[1]), zap.Any("rows", data[2]))
case 4:
global.GVA_LOG.Info("gorm", zap.Any("src", data[0]), zap.Any("duration", data[1]), zap.Any("rows", data[2]), zap.Any("sql", data[3]))
}
return
}
switch len(data) {
case 0:
global.GVA_LOG.Info(message)
g.gormWriter.Printf(message, "")
case 1:
global.GVA_LOG.Info("gorm", zap.Any("src", data[0]))
g.gormWriter.Printf(message, data[0].(string))
case 2:
global.GVA_LOG.Info("gorm", zap.Any("src", data[0]), zap.Any("duration", data[1]))
g.gormWriter.Printf(message, data[0].(string), data[1].(float64))
case 3:
global.GVA_LOG.Info("gorm", zap.Any("src", data[0]), zap.Any("duration", data[1]), zap.Any("rows", data[2]))
g.gormWriter.Printf(message, data[0].(string), data[1].(float64), data[2].(string))
case 4:
global.GVA_LOG.Info("gorm", zap.Any("src", data[0]), zap.Any("duration", data[1]), zap.Any("rows", data[2]), zap.Any("sql", data[3]))
g.gormWriter.Printf(message, data[0].(string), data[1].(float64), data[2].(string), data[3].(string))
}
}

11
server/utils/cmd_Task.go

@ -73,17 +73,12 @@ func newT(f func(chan struct{}) error) *T {
//@description: 添加任务
func (t *T) AddTask() {
if len(t.ch) == 1 {
return
}
t.Lock()
defer t.Unlock()
if len(t.ch) == 1 {
select {
case t.ch <- struct{}{}:
default:
// 代表已经有任务了
// 直接丢弃这次任务
return
}
t.ch <- struct{}{}
}
//@author: [songzhibin97](https://github.com/songzhibin97)

2
server/utils/verify.go

@ -6,12 +6,10 @@ var (
MenuVerify = Rules{"Path": {NotEmpty()}, "ParentId": {NotEmpty()}, "Name": {NotEmpty()}, "Component": {NotEmpty()}, "Sort": {Ge("0")}}
MenuMetaVerify = Rules{"Title": {NotEmpty()}}
LoginVerify = Rules{"CaptchaId": {NotEmpty()}, "Captcha": {NotEmpty()}, "Username": {NotEmpty()}, "Password": {NotEmpty()}}
SetUserVerify = Rules{"ID": {NotEmpty()}, "Username": {NotEmpty()}, "NickName": {NotEmpty()}, "HeaderImg": {NotEmpty()}}
RegisterVerify = Rules{"Username": {NotEmpty()}, "NickName": {NotEmpty()}, "Password": {NotEmpty()}, "AuthorityId": {NotEmpty()}}
PageInfoVerify = Rules{"Page": {NotEmpty()}, "PageSize": {NotEmpty()}}
CustomerVerify = Rules{"CustomerName": {NotEmpty()}, "CustomerPhoneData": {NotEmpty()}}
AutoCodeVerify = Rules{"Abbreviation": {NotEmpty()}, "StructName": {NotEmpty()}, "PackageName": {NotEmpty()}, "Fields": {NotEmpty()}}
WorkFlowVerify = Rules{"WorkflowNickName": {NotEmpty()}, "WorkflowName": {NotEmpty()}, "WorkflowDescription": {NotEmpty()}, "WorkflowStepInfo": {NotEmpty()}}
AuthorityVerify = Rules{"AuthorityId": {NotEmpty()}, "AuthorityName": {NotEmpty()}, "ParentId": {NotEmpty()}}
AuthorityIdVerify = Rules{"AuthorityId": {NotEmpty()}}
OldAuthorityVerify = Rules{"OldAuthorityId": {NotEmpty()}}

9
web/src/components/upload/image.vue

@ -13,18 +13,20 @@
<el-upload
class="image-uploader"
:action="`${path}/fileUploadAndDownload/upload`"
:headers="{ 'x-token': token }"
:show-file-list="false"
:on-success="handleImageSuccess"
:before-upload="beforeImageUpload"
:multiple="false"
>
<img v-if="imageUrl" :src="imageUrl" class="image" />
<img v-if="imageUrl" :src="path + imageUrl" class="image" />
<i v-else class="el-icon-plus image-uploader-icon"></i>
</el-upload>
</div>
</template>
<script>
const path = process.env.VUE_APP_BASE_API;
import { mapGetters } from "vuex";
import ImageCompress from "@/utils/image.js";
export default {
name: "upload-image",
@ -51,6 +53,9 @@ export default {
path: path,
};
},
computed: {
...mapGetters("user", ["userInfo", "token"]),
},
methods: {
beforeImageUpload(file) {
let isRightSize = file.size / 1024 < this.fileSize;
@ -97,4 +102,4 @@ export default {
height: 178px;
display: block;
}
</style>
</style>

8
web/src/utils/stringFun.js

@ -7,6 +7,14 @@ export const toUpperCase = (str) => {
}
}
export const toLowerCase = (str) => {
if (str[0]) {
return str.replace(str[0], str[0].toLowerCase())
} else {
return ""
}
}
// 驼峰转换下划线
export const toSQLLine = (str) => {
if (str == "ID") return "ID"

10
web/src/view/systemTools/autoCode/component/fieldDialog.vue

@ -12,6 +12,9 @@
<el-col :span="6">
<el-input v-model="dialogMiddle.fieldName" autocomplete="off"></el-input>
</el-col>
<el-col :offset="1" :span="2">
<el-button @click="autoFill">自动填充</el-button>
</el-col>
</el-form-item>
<el-form-item label="Field中文名" prop="fieldDesc">
<el-col :span="6">
@ -103,6 +106,7 @@
</template>
<script>
import { getDict } from "@/utils/dictionary";
import { toSQLLine , toLowerCase } from "@/utils/stringFun.js";
import { getSysDictionaryList } from "@/api/sysDictionary";
export default {
name: "FieldDialog",
@ -183,6 +187,10 @@ export default {
};
},
methods: {
autoFill(){
this.dialogMiddle.fieldJson = toLowerCase(this.dialogMiddle.fieldName)
this.dialogMiddle.columnName = toSQLLine(this.dialogMiddle.fieldJson)
},
async getDbfdOptions() {
this.dialogMiddle.dataType = ""
this.dialogMiddle.dataTypeLong = ""
@ -201,7 +209,7 @@ export default {
});
this.dictOptions = dictRes.data.list
}
},
};
</script>
<style lang="scss">

Loading…
Cancel
Save