模板生成优化

This commit is contained in:
xiangheng
2024-08-16 02:51:43 +08:00
parent edd6d9df0e
commit 64e676572b
19 changed files with 174 additions and 488 deletions

View File

@@ -26,10 +26,10 @@ type {{{ toUpperCamelCase .ModuleName }}}Handler struct {
{{{- range .Columns }}} {{{- range .Columns }}}
{{{- if .IsQuery }}} {{{- if .IsQuery }}}
{{{- if eq .HtmlType "datetime" }}} {{{- if eq .HtmlType "datetime" }}}
// @Param {{{toUpperCamelCase .GoField }}}Start query {{{ .GoType }}} false "{{{ .ColumnComment }}}" // @Param {{{toUpperCamelCase .GoField }}}Start query {{{goToTsType .GoType }}} false "{{{ .ColumnComment }}}"
// @Param {{{toUpperCamelCase .GoField }}}End query {{{ .GoType }}} false "{{{ .ColumnComment }}}" // @Param {{{toUpperCamelCase .GoField }}}End query {{{goToTsType .GoType }}} false "{{{ .ColumnComment }}}"
{{{- else }}} {{{- else }}}
// @Param {{{toUpperCamelCase .GoField }}} query {{{ .GoType }}} false "{{{ .ColumnComment }}}" // @Param {{{toUpperCamelCase .GoField }}} query {{{goToTsType .GoType }}} false "{{{ .ColumnComment }}}"
{{{- end }}} {{{- end }}}
{{{- end }}} {{{- end }}}
{{{- end }}} {{{- end }}}
@@ -54,10 +54,10 @@ func (hd *{{{ toUpperCamelCase .ModuleName }}}Handler) List(c *gin.Context) {
{{{- range .Columns }}} {{{- range .Columns }}}
{{{- if .IsQuery }}} {{{- if .IsQuery }}}
{{{- if eq .HtmlType "datetime" }}} {{{- if eq .HtmlType "datetime" }}}
// @Param {{{toUpperCamelCase .GoField }}}Start query {{{ .GoType }}} false "{{{ .ColumnComment }}}" // @Param {{{toUpperCamelCase .GoField }}}Start query {{{goToTsType .GoType }}} false "{{{ .ColumnComment }}}"
// @Param {{{toUpperCamelCase .GoField }}}End query {{{ .GoType }}} false "{{{ .ColumnComment }}}" // @Param {{{toUpperCamelCase .GoField }}}End query {{{goToTsType .GoType }}} false "{{{ .ColumnComment }}}"
{{{- else }}} {{{- else }}}
// @Param {{{toUpperCamelCase .GoField }}} query {{{ .GoType }}} false "{{{ .ColumnComment }}}" // @Param {{{toUpperCamelCase .GoField }}} query {{{goToTsType .GoType }}} false "{{{ .ColumnComment }}}"
{{{- end }}} {{{- end }}}
{{{- end }}} {{{- end }}}
{{{- end }}} {{{- end }}}
@@ -78,7 +78,7 @@ func (hd *{{{ toUpperCamelCase .ModuleName }}}Handler) ListAll(c *gin.Context)
// @Param Token header string true "token" // @Param Token header string true "token"
{{{- range .Columns }}} {{{- range .Columns }}}
{{{- if .IsPk }}} {{{- if .IsPk }}}
// @Param {{{toUpperCamelCase .GoField }}} query {{{ .GoType }}} false "{{{ .ColumnComment }}}" // @Param {{{toUpperCamelCase .GoField }}} query {{{goToTsType .GoType }}} false "{{{ .ColumnComment }}}"
{{{- end }}} {{{- end }}}
{{{- end }}} {{{- end }}}
// @Success 200 {object} response.Response{ data={{{ toUpperCamelCase .EntityName }}}Resp} "成功" // @Success 200 {object} response.Response{ data={{{ toUpperCamelCase .EntityName }}}Resp} "成功"
@@ -103,7 +103,7 @@ func (hd *{{{ toUpperCamelCase .ModuleName }}}Handler) Detail(c *gin.Context) {
// @Param Token header string true "token" // @Param Token header string true "token"
{{{- range .Columns }}} {{{- range .Columns }}}
{{{- if .IsInsert }}} {{{- if .IsInsert }}}
// @Param {{{toUpperCamelCase .GoField }}} body {{{ .GoType }}} false "{{{ .ColumnComment }}}" // @Param {{{toUpperCamelCase .GoField }}} body {{{goToTsType .GoType }}} false "{{{ .ColumnComment }}}"
{{{- end }}} {{{- end }}}
{{{- end }}} {{{- end }}}
// @Success 200 {object} response.Response "成功" // @Success 200 {object} response.Response "成功"
@@ -122,7 +122,7 @@ func (hd *{{{ toUpperCamelCase .ModuleName }}}Handler) Add(c *gin.Context) {
// @Param Token header string true "token" // @Param Token header string true "token"
{{{- range .Columns }}} {{{- range .Columns }}}
{{{- if .IsEdit }}} {{{- if .IsEdit }}}
// @Param {{{toUpperCamelCase .GoField }}} body {{{ .GoType }}} false "{{{ .ColumnComment }}}" // @Param {{{toUpperCamelCase .GoField }}} body {{{goToTsType .GoType }}} false "{{{ .ColumnComment }}}"
{{{- end }}} {{{- end }}}
{{{- end }}} {{{- end }}}
// @Success 200 {object} response.Response "成功" // @Success 200 {object} response.Response "成功"
@@ -140,7 +140,7 @@ func (hd *{{{ toUpperCamelCase .ModuleName }}}Handler) Edit(c *gin.Context) {
// @Param Token header string true "token" // @Param Token header string true "token"
{{{- range .Columns }}} {{{- range .Columns }}}
{{{- if .IsPk }}} {{{- if .IsPk }}}
// @Param {{{toUpperCamelCase .GoField }}} body {{{ .GoType }}} false "{{{ .ColumnComment }}}" // @Param {{{toUpperCamelCase .GoField }}} body {{{goToTsType .GoType }}} false "{{{ .ColumnComment }}}"
{{{- end }}} {{{- end }}}
{{{- end }}} {{{- end }}}
// @Success 200 {object} response.Response "成功" // @Success 200 {object} response.Response "成功"
@@ -164,10 +164,10 @@ func (hd *{{{ toUpperCamelCase .ModuleName }}}Handler) Del(c *gin.Context) {
{{{- range .Columns }}} {{{- range .Columns }}}
{{{- if .IsQuery }}} {{{- if .IsQuery }}}
{{{- if eq .HtmlType "datetime" }}} {{{- if eq .HtmlType "datetime" }}}
// @Param {{{toUpperCamelCase .GoField }}}Start query {{{ .GoType }}} false "{{{ .ColumnComment }}}" // @Param {{{toUpperCamelCase .GoField }}}Start query {{{goToTsType .GoType }}} false "{{{ .ColumnComment }}}"
// @Param {{{toUpperCamelCase .GoField }}}End query {{{ .GoType }}} false "{{{ .ColumnComment }}}" // @Param {{{toUpperCamelCase .GoField }}}End query {{{goToTsType .GoType }}} false "{{{ .ColumnComment }}}"
{{{- else }}} {{{- else }}}
// @Param {{{toUpperCamelCase .GoField }}} query {{{ .GoType }}} false "{{{ .ColumnComment }}}" // @Param {{{toUpperCamelCase .GoField }}} query {{{goToTsType .GoType }}} false "{{{ .ColumnComment }}}"
{{{- end }}} {{{- end }}}
{{{- end }}} {{{- end }}}
{{{- end }}} {{{- end }}}

View File

@@ -9,10 +9,10 @@ type {{{ toUpperCamelCase .EntityName }}}ListReq struct {
{{{- range .Columns }}} {{{- range .Columns }}}
{{{- if .IsQuery }}} {{{- if .IsQuery }}}
{{{- if eq .HtmlType "datetime" }}} {{{- if eq .HtmlType "datetime" }}}
{{{ toUpperCamelCase .GoField }}}Start *string `form:"{{{ .GoField }}}Start"` // {{{ .ColumnComment }}} {{{ toUpperCamelCase .GoField }}}Start *string `` // {{{ .ColumnComment }}}
{{{ toUpperCamelCase .GoField }}}End *string `GoField }}}End" form:"{{{ .GoField }}}End"` // 结{{{ .ColumnComment }}} {{{ toUpperCamelCase .GoField }}}End *string `` // {{{ .ColumnComment }}}
{{{- else }}} {{{- else }}}
{{{ toUpperCamelCase .GoField }}} *{{{.GoType }}} ` form:"{{{ .GoField }}}"` // {{{ .ColumnComment }}} {{{ toUpperCamelCase .GoField }}} *{{{.GoType }}} `` // {{{ .ColumnComment }}}
{{{- end }}} {{{- end }}}
{{{- end }}} {{{- end }}}
{{{- end }}} {{{- end }}}
@@ -24,7 +24,7 @@ type {{{ toUpperCamelCase .EntityName }}}ListReq struct {
type {{{ toUpperCamelCase .EntityName }}}AddReq struct { type {{{ toUpperCamelCase .EntityName }}}AddReq struct {
{{{- range .Columns }}} {{{- range .Columns }}}
{{{- if .IsInsert }}} {{{- if .IsInsert }}}
{{{ toUpperCamelCase .GoField }}} interface{} `mapstructure:"{{{ .GoField }}}" json:"{{{ .GoField }}}" form:"{{{ .GoField }}}"` // {{{ .ColumnComment }}} {{{ toUpperCamelCase .GoField }}} {{{goWithAddEditType .GoType }}} `` // {{{ .ColumnComment }}}
{{{- end }}} {{{- end }}}
{{{- end }}} {{{- end }}}
} }
@@ -34,9 +34,9 @@ type {{{ toUpperCamelCase .EntityName }}}EditReq struct {
{{{- range .Columns }}} {{{- range .Columns }}}
{{{- if .IsEdit }}} {{{- if .IsEdit }}}
{{{- if .IsPk }}} {{{- if .IsPk }}}
{{{ toUpperCamelCase .GoField }}} {{{ .GoType }}} `mapstructure:"{{{ .GoField }}}" json:"{{{ .GoField }}}" form:"{{{ .GoField }}}"` // {{{ .ColumnComment }}} {{{ toUpperCamelCase .GoField }}} {{{ .GoType }}} `` // {{{ .ColumnComment }}}
{{{- else }}} {{{- else }}}
{{{ toUpperCamelCase .GoField }}} interface{} `mapstructure:"{{{ .GoField }}}" json:"{{{ .GoField }}}" form:"{{{ .GoField }}}"` // {{{ .ColumnComment }}} {{{ toUpperCamelCase .GoField }}} {{{goWithAddEditType .GoType }}} `` // {{{ .ColumnComment }}}
{{{- end }}} {{{- end }}}
{{{- end }}} {{{- end }}}
{{{- end }}} {{{- end }}}
@@ -46,7 +46,7 @@ type {{{ toUpperCamelCase .EntityName }}}EditReq struct {
type {{{ toUpperCamelCase .EntityName }}}DetailReq struct { type {{{ toUpperCamelCase .EntityName }}}DetailReq struct {
{{{- range .Columns }}} {{{- range .Columns }}}
{{{- if .IsPk }}} {{{- if .IsPk }}}
{{{ toUpperCamelCase .GoField }}} {{{.GoType }}} `form:"{{{ .GoField }}}"` // {{{ .ColumnComment }}} {{{ toUpperCamelCase .GoField }}} {{{.GoType }}} `` // {{{ .ColumnComment }}}
{{{- end }}} {{{- end }}}
{{{- end }}} {{{- end }}}
} }
@@ -55,7 +55,7 @@ type {{{ toUpperCamelCase .EntityName }}}DetailReq struct {
type {{{ toUpperCamelCase .EntityName }}}DelReq struct { type {{{ toUpperCamelCase .EntityName }}}DelReq struct {
{{{- range .Columns }}} {{{- range .Columns }}}
{{{- if .IsPk }}} {{{- if .IsPk }}}
{{{ toUpperCamelCase .GoField }}} {{{ .GoType }}} `form:"{{{ .GoField }}}"` // {{{ .ColumnComment }}} {{{ toUpperCamelCase .GoField }}} {{{ .GoType }}} `` // {{{ .ColumnComment }}}
{{{- end }}} {{{- end }}}
{{{- end }}} {{{- end }}}
} }
@@ -65,9 +65,9 @@ type {{{ toUpperCamelCase .EntityName }}}Resp struct {
{{{- range .Columns }}} {{{- range .Columns }}}
{{{- if or .IsList .IsPk }}} {{{- if or .IsList .IsPk }}}
{{{- if .IsPk }}} {{{- if .IsPk }}}
{{{ toUpperCamelCase .GoField }}} {{{ .GoType }}} `mapstructure:"{{{ .GoField }}}" json:"{{{ .GoField }}}"` // {{{ .ColumnComment }}} {{{ toUpperCamelCase .GoField }}} {{{.GoType }}} `` // {{{ .ColumnComment }}}
{{{- else }}} {{{- else }}}
{{{ toUpperCamelCase .GoField }}} {{{ .GoType }}} `mapstructure:"{{{ .GoField }}}" json:"{{{ .GoField }}}"` // {{{ .ColumnComment }}} {{{ toUpperCamelCase .GoField }}} {{{goWithRespType .GoType }}} `` // {{{ .ColumnComment }}}
{{{- end }}} {{{- end }}}
{{{- end }}} {{{- end }}}
{{{- end }}} {{{- end }}}

View File

@@ -133,7 +133,7 @@ func (service {{{ toCamelCase .EntityName }}}Service) Detail({{{ toUpperCamelCas
// Add {{{ .FunctionName }}}新增 // Add {{{ .FunctionName }}}新增
func (service {{{ toCamelCase .EntityName }}}Service) Add(addReq {{{ toUpperCamelCase .EntityName }}}AddReq) (createId int,e error) { func (service {{{ toCamelCase .EntityName }}}Service) Add(addReq {{{ toUpperCamelCase .EntityName }}}AddReq) (createId int,e error) {
var obj model.{{{ toUpperCamelCase .EntityName }}} var obj model.{{{ toUpperCamelCase .EntityName }}}
util.ConvertUtil.Copy(&obj, addReq) util.ConvertUtil.StructToStruct(addReq,&obj)
err := service.db.Create(&obj).Error err := service.db.Create(&obj).Error
e = response.CheckMysqlErr(err) e = response.CheckMysqlErr(err)
if e != nil { if e != nil {
@@ -141,7 +141,6 @@ func (service {{{ toCamelCase .EntityName }}}Service) Add(addReq {{{ toUpperCame
} }
cacheUtil.SetCache(obj.{{{ toUpperCamelCase .PrimaryKey }}}, obj) cacheUtil.SetCache(obj.{{{ toUpperCamelCase .PrimaryKey }}}, obj)
createId = obj.{{{ toUpperCamelCase .PrimaryKey }}} createId = obj.{{{ toUpperCamelCase .PrimaryKey }}}
e = response.CheckErr(err, "添加失败")
return return
} }
@@ -156,15 +155,9 @@ func (service {{{ toCamelCase .EntityName }}}Service) Edit(editReq {{{ toUpperCa
if e = response.CheckErr(err, "查询失败"); e != nil { if e = response.CheckErr(err, "查询失败"); e != nil {
return return
} }
// util.ConvertUtil.Copy(&obj, editReq)
// util.ConvertUtil.Copy(&obj, editReq)
//
editInfo, err := convertor.StructToMap(editReq)
if err != nil {
return err
}
err = service.db.Model(&obj).Updates(editInfo).Error err = service.db.Model(&obj).Select("*").Updates(obj).Error
if e = response.CheckErr(err, "编辑失败"); e != nil { if e = response.CheckErr(err, "编辑失败"); e != nil {
return return
} }
@@ -205,9 +198,12 @@ func (service {{{ toCamelCase .EntityName }}}Service) GetExcelCol() []excel2.Col
var cols = []excel2.Col{ var cols = []excel2.Col{
{{{- range .Columns }}} {{{- range .Columns }}}
{{{- if eq .HtmlType "datetime" }}} {{{- if eq .HtmlType "datetime" }}}
{Name: "{{{.ColumnComment}}}", Key: "{{{ toUpperCamelCase .GoField }}}", Width: 15, Decode: excel2.DecodeTime }, {Name: "{{{.ColumnComment}}}", Key: "{{{ toUpperCamelCase .GoField }}}", Width: 15,Encode: util.NullTimeUtil.EncodeTime, Decode: util.NullTimeUtil.DecodeTime },
{{{- else if eq .GoType "int" }}}
{Name: "{{{.ColumnComment}}}", Key: "{{{ toUpperCamelCase .GoField }}}", Width: 15,Encode: core.EncodeInt, Decode: core.DecodeInt},
{{{- else }}} {{{- else }}}
{Name: "{{{.ColumnComment}}}", Key: "{{{ toUpperCamelCase .GoField }}}", Width: 15}, {Name: "{{{.ColumnComment}}}", Key: "{{{ toUpperCamelCase .GoField }}}", Width: 15},
{{{- end }}} {{{- end }}}
{{{- end }}} {{{- end }}}
} }

View File

@@ -4,7 +4,7 @@ import { clearObjEmpty } from "@/utils/utils";
export type type_{{{.ModuleName}}} = { export type type_{{{.ModuleName}}} = {
{{{- range .Columns }}} {{{- range .Columns }}}
{{{ .GoField }}}?: {{{goToTsType .GoType}}}; {{{toUpperCamelCase .GoField }}}?: {{{goToTsType .GoType}}};
{{{- end }}} {{{- end }}}
} }
// 查询 // 查询
@@ -12,10 +12,10 @@ export type type_{{{.ModuleName}}}_query = {
{{{- range .Columns }}} {{{- range .Columns }}}
{{{- if .IsQuery }}} {{{- if .IsQuery }}}
{{{- if eq .HtmlType "datetime" }}} {{{- if eq .HtmlType "datetime" }}}
{{{ .GoField }}}Start?: string; {{{toUpperCamelCase .GoField }}}Start?: string;
{{{ .GoField }}}End?: string; {{{toUpperCamelCase .GoField }}}End?: string;
{{{- else }}} {{{- else }}}
{{{ .GoField }}}?: {{{goToTsType .GoType}}}; {{{toUpperCamelCase .GoField }}}?: {{{goToTsType .GoType}}};
{{{- end }}} {{{- end }}}
{{{- end }}} {{{- end }}}
{{{- end }}} {{{- end }}}
@@ -24,7 +24,7 @@ export type type_{{{.ModuleName}}}_query = {
export type type_{{{.ModuleName}}}_edit = { export type type_{{{.ModuleName}}}_edit = {
{{{- range .Columns }}} {{{- range .Columns }}}
{{{- if or .IsEdit .IsInsert }}} {{{- if or .IsEdit .IsInsert }}}
{{{ .GoField }}}?: {{{goToTsType .GoType}}}; {{{toUpperCamelCase .GoField }}}?: {{{goToTsType .GoType}}};
{{{- end }}} {{{- end }}}
{{{- end }}} {{{- end }}}
} }
@@ -48,11 +48,11 @@ export function {{{.ModuleName}}}_list_all(params?: type_{{{.ModuleName}}}_query
} }
// {{{.FunctionName}}}详情 // {{{.FunctionName}}}详情
export function {{{.ModuleName}}}_detail({{{ .PrimaryKey }}}: number | string) { export function {{{.ModuleName}}}_detail({{{toUpperCamelCase .PrimaryKey }}}: number | string) {
return request<type_{{{.ModuleName}}}>({ return request<type_{{{.ModuleName}}}>({
url: '/{{{.ModuleName}}}/detail', url: '/{{{.ModuleName}}}/detail',
method: 'GET', method: 'GET',
data: { {{{ .PrimaryKey }}} } data: { {{{toUpperCamelCase .PrimaryKey }}} }
}) })
} }
@@ -75,12 +75,12 @@ export function {{{.ModuleName}}}_edit(data: type_{{{.ModuleName}}}_edit) {
} }
// {{{.FunctionName}}}删除 // {{{.FunctionName}}}删除
export function {{{.ModuleName}}}_delete({{{ .PrimaryKey }}}: number | string) { export function {{{.ModuleName}}}_delete({{{toUpperCamelCase .PrimaryKey }}}: number | string) {
return request<null>({ return request<null>({
url: '/{{{.ModuleName}}}/del', url: '/{{{.ModuleName}}}/del',
method: "POST", method: "POST",
data:{ data:{
{{{ .PrimaryKey }}} {{{toUpperCamelCase .PrimaryKey }}}
}, },
}); });
} }

View File

@@ -3,15 +3,15 @@
<uv-form labelPosition="left" :model="form"> <uv-form labelPosition="left" :model="form">
{{{- range .Columns }}} {{{- range .Columns }}}
{{{- if .IsList }}} {{{- if .IsList }}}
<uv-form-item label="{{{.ColumnComment}}}" prop="{{{(.GoField)}}}" borderBottom> <uv-form-item label="{{{.ColumnComment}}}" prop="{{{(toUpperCamelCase .GoField)}}}" borderBottom>
{{{- if and (ne .DictType "") (or (eq .HtmlType "select") (eq .HtmlType "radio") (eq .HtmlType "checkbox")) }}} {{{- if and (ne .DictType "") (or (eq .HtmlType "select") (eq .HtmlType "radio") (eq .HtmlType "checkbox")) }}}
<dict-value :options="dictData.{{{ .DictType }}}" :value="row.{{{ (.GoField) }}}" /> <dict-value :options="dictData.{{{ .DictType }}}" :value="row.{{{ (toUpperCamelCase .GoField) }}}" />
{{{- else if and (ne .ListAllApi "") (or (eq .HtmlType "select") (eq .HtmlType "radio") (eq .HtmlType "checkbox")) }}} {{{- else if and (ne .ListAllApi "") (or (eq .HtmlType "select") (eq .HtmlType "radio") (eq .HtmlType "checkbox")) }}}
<dict-value :options="listAllData.{{{pathToName .ListAllApi }}}" :value="row.{{{ (.GoField) }}}" labelKey='id' valueKey='id' /> <dict-value :options="listAllData.{{{pathToName .ListAllApi }}}" :value="row.{{{ (toUpperCamelCase .GoField) }}}" labelKey='id' valueKey='id' />
{{{- else if eq .HtmlType "imageUpload" }}} {{{- else if eq .HtmlType "imageUpload" }}}
<uv-image :src="$filePath(form.{{{(.GoField)}}})" width="100%"></uv-image> <uv-image :src="$filePath(form.{{{(toUpperCamelCase .GoField)}}})" width="100%"></uv-image>
{{{- else }}} {{{- else }}}
{{form.{{{(.GoField)}}}}} {{form.{{{(toUpperCamelCase .GoField)}}}}}
{{{- end }}} {{{- end }}}
</uv-form-item> </uv-form-item>
{{{- end }}} {{{- end }}}
@@ -44,7 +44,7 @@
let form = ref({ let form = ref({
{{{- range .Columns }}} {{{- range .Columns }}}
{{{- if or .IsList .IsPk }}} {{{- if or .IsList .IsPk }}}
{{{ .GoField }}}: "", {{{ toUpperCamelCase .GoField }}}: "",
{{{- end }}} {{{- end }}}
{{{- end }}} {{{- end }}}
}); });

View File

@@ -3,20 +3,20 @@
<uv-form labelPosition="left" :model="form" :rules="formRules" ref="formRef"> <uv-form labelPosition="left" :model="form" :rules="formRules" ref="formRef">
{{{- range .Columns }}} {{{- range .Columns }}}
{{{- if .IsEdit }}} {{{- if .IsEdit }}}
<uv-form-item label="{{{ .ColumnComment }}}" prop="{{{ (.GoField) }}}" borderBottom> <uv-form-item label="{{{ .ColumnComment }}}" prop="{{{ (toUpperCamelCase .GoField) }}}" borderBottom>
{{{- if eq .HtmlType "input" }}} {{{- if eq .HtmlType "input" }}}
<uv-input v-model="form.{{{ (.GoField) }}}" border="surround"></uv-input> <uv-input v-model="form.{{{ (toUpperCamelCase .GoField) }}}" border="surround"></uv-input>
{{{- else if eq .HtmlType "number" }}} {{{- else if eq .HtmlType "number" }}}
<uv-number-box v-model="form.{{{ (.GoField) }}}" :min="-99999999" :max="99999999" :integer="true"></uv-number-box> <uv-number-box v-model="form.{{{ (toUpperCamelCase .GoField) }}}" :min="-99999999" :max="99999999" :integer="true"></uv-number-box>
{{{- else if eq .HtmlType "textarea" }}} {{{- else if eq .HtmlType "textarea" }}}
<uv-textarea v-model="form.{{{ (.GoField) }}}" border="surround"></uv-textarea> <uv-textarea v-model="form.{{{ (toUpperCamelCase .GoField) }}}" border="surround"></uv-textarea>
{{{- else if eq .HtmlType "datetime" }}} {{{- else if eq .HtmlType "datetime" }}}
<x-date v-model:time="form.{{{ (.GoField) }}}"></x-date> <x-date v-model:time="form.{{{ (toUpperCamelCase .GoField) }}}"></x-date>
{{{- else if or (eq .HtmlType "checkbox") (eq .HtmlType "radio") (eq .HtmlType "select")}}} {{{- else if or (eq .HtmlType "checkbox") (eq .HtmlType "radio") (eq .HtmlType "select")}}}
{{{- if ne .DictType "" }}} {{{- if ne .DictType "" }}}
<x-picker v-model="form.{{{ (.GoField) }}}" valueKey="value" labelKey="name" :columns="dictData.{{{ .DictType }}}"></x-picker> <x-picker v-model="form.{{{ (toUpperCamelCase .GoField) }}}" valueKey="value" labelKey="name" :columns="dictData.{{{ .DictType }}}"></x-picker>
{{{- else if ne .ListAllApi "" }}} {{{- else if ne .ListAllApi "" }}}
<x-picker v-model="form.{{{ (.GoField) }}}" valueKey="id" labelKey="id" :columns="listAllData.{{{pathToName .ListAllApi}}}"></x-picker> <x-picker v-model="form.{{{ (toUpperCamelCase .GoField) }}}" valueKey="{{{toUpperCamelCase .PrimaryKey}}}" labelKey="{{{toUpperCamelCase .PrimaryKey}}}" :columns="listAllData.{{{pathToName .ListAllApi}}}"></x-picker>
{{{- else }}} {{{- else }}}
请选择字典生成代码 请选择字典生成代码
{{{- end }}} {{{- end }}}
@@ -55,15 +55,15 @@
let formRef = ref(); let formRef = ref();
let form = ref<type_{{{ .ModuleName }}}_edit>({ let form = ref<type_{{{ .ModuleName }}}_edit>({
{{{- range .Columns }}} {{{- range .Columns }}}
{{{- if eq (.GoField) $.PrimaryKey }}} {{{- if eq (toUpperCamelCase .GoField) $.PrimaryKey }}}
{{{ $.PrimaryKey }}}: '', {{{ $.PrimaryKey }}}: '',
{{{- else if .IsEdit }}} {{{- else if .IsEdit }}}
{{{- if eq .HtmlType "checkbox" }}} {{{- if eq .HtmlType "checkbox" }}}
{{{ (.GoField) }}}: [], {{{ (toUpperCamelCase .GoField) }}}: [],
{{{- else if eq .HtmlType "number" }}} {{{- else if eq .HtmlType "number" }}}
{{{ (.GoField) }}}: 0, {{{ (toUpperCamelCase .GoField) }}}: 0,
{{{- else }}} {{{- else }}}
{{{ (.GoField) }}}: '', {{{ (toUpperCamelCase .GoField) }}}: '',
{{{- end }}} {{{- end }}}
{{{- end }}} {{{- end }}}
{{{- end }}} {{{- end }}}
@@ -71,7 +71,7 @@
const formRules = { const formRules = {
{{{- range .Columns }}} {{{- range .Columns }}}
{{{- if and .IsEdit .IsRequired }}} {{{- if and .IsEdit .IsRequired }}}
{{{ (.GoField) }}}: [ {{{ (toUpperCamelCase .GoField) }}}: [
{ {
required: true, required: true,
{{{- if or (eq .HtmlType "checkbox") (eq .HtmlType "datetime") (eq .HtmlType "radio") (eq .HtmlType "select") (eq .HtmlType "imageUpload") }}} {{{- if or (eq .HtmlType "checkbox") (eq .HtmlType "datetime") (eq .HtmlType "radio") (eq .HtmlType "select") (eq .HtmlType "imageUpload") }}}
@@ -87,8 +87,8 @@
} }
onLoad((e) => { onLoad((e) => {
console.log("onLoad", e); console.log("onLoad", e);
if (e.id) { if (e.{{{toUpperCamelCase .PrimaryKey}}}) {
getDetails(e.id); getDetails(e.{{{toUpperCamelCase .PrimaryKey}}});
} }
}); });
@@ -115,8 +115,8 @@ const { listAllData } = useListAllData<{
}) })
{{{- end }}} {{{- end }}}
function getDetails(id) { function getDetails({{{toUpperCamelCase .PrimaryKey}}}) {
{{{ .ModuleName }}}_detail(id).then((res) => { {{{ .ModuleName }}}_detail({{{toUpperCamelCase .PrimaryKey}}}).then((res) => {
if (res.code == 200) { if (res.code == 200) {
if (res?.data) { if (res?.data) {
form.value = res?.data form.value = res?.data
@@ -133,11 +133,11 @@ const { listAllData } = useListAllData<{
function submit() { function submit() {
console.log("submit", form.value); console.log("submit", form.value);
formRef.value.validate().then(() => { formRef.value.validate().then(() => {
if (form.value.id) { if (form.value.{{{toUpperCamelCase .PrimaryKey}}}) {
{{{ .ModuleName }}}_edit(form.value).then((res) => { {{{ .ModuleName }}}_edit(form.value).then((res) => {
if (res.code == 200) { if (res.code == 200) {
toast("编辑成功"); toast("编辑成功");
getDetails(form.value?.id); getDetails(form.value?.{{{toUpperCamelCase .PrimaryKey}}});
} else { } else {
toast(res.message); toast(res.message);
} }

View File

@@ -20,11 +20,11 @@
<uv-list> <uv-list>
<uv-list-item <uv-list-item
v-for="item of pager.lists" v-for="item of pager.lists"
:key="item.id" :key="item.{{{toUpperCamelCase .PrimaryKey}}}"
clickable clickable
show-arrow show-arrow
:title="item.id" :title="item.{{{toUpperCamelCase .PrimaryKey}}}"
:right-text="item.id" :right-text="item.{{{toUpperCamelCase .PrimaryKey}}}"
@click="toDetails(item)" @click="toDetails(item)"
></uv-list-item> ></uv-list-item>
</uv-list> </uv-list>
@@ -72,10 +72,10 @@ const queryParams = reactive<type_{{{.ModuleName}}}_query>({
{{{- range .Columns }}} {{{- range .Columns }}}
{{{- if .IsQuery }}} {{{- if .IsQuery }}}
{{{- if eq .HtmlType "datetime" }}} {{{- if eq .HtmlType "datetime" }}}
{{{ (.GoField) }}}Start: '', {{{ (toUpperCamelCase .GoField) }}}Start: '',
{{{ (.GoField) }}}End: '', {{{ (toUpperCamelCase .GoField) }}}End: '',
{{{- else }}} {{{- else }}}
{{{ (.GoField) }}}: '', {{{ (toUpperCamelCase .GoField) }}}: '',
{{{- end }}} {{{- end }}}
{{{- end }}} {{{- end }}}
{{{- end }}} {{{- end }}}
@@ -110,7 +110,7 @@ onReachBottom(() => {
}); });
function toDetails(item) { function toDetails(item) {
toPath("/pages/{{{nameToPath .ModuleName }}}/details", { id: item.id }); toPath("/pages/{{{nameToPath .ModuleName }}}/details", { {{{toUpperCamelCase .PrimaryKey}}}: item.{{{toUpperCamelCase .PrimaryKey}}} });
} }
function moreSearch() { function moreSearch() {
toPath("/pages/{{{nameToPath .ModuleName }}}/search"); toPath("/pages/{{{nameToPath .ModuleName }}}/search");

View File

@@ -3,18 +3,18 @@
<uv-form labelPosition="left" labelWidth="80" :model="form"> <uv-form labelPosition="left" labelWidth="80" :model="form">
{{{- range .Columns }}} {{{- range .Columns }}}
{{{- if eq .IsQuery 1 }}} {{{- if eq .IsQuery 1 }}}
<uv-form-item label="{{{ .ColumnComment }}}" prop="{{{ (.GoField) }}}" borderBottom> <uv-form-item label="{{{ .ColumnComment }}}" prop="{{{ (toUpperCamelCase .GoField) }}}" borderBottom>
{{{- if eq .HtmlType "datetime" }}} {{{- if eq .HtmlType "datetime" }}}
<x-date-range v-model:startTime="form.{{{ (.GoField) }}}Start" <x-date-range v-model:startTime="form.{{{ (toUpperCamelCase .GoField) }}}Start"
v-model:endTime="form.{{{ (.GoField) }}}End"></x-date-range> v-model:endTime="form.{{{ (toUpperCamelCase .GoField) }}}End"></x-date-range>
{{{- else if or (eq .HtmlType "checkbox") (eq .HtmlType "radio") (eq .HtmlType "select") }}} {{{- else if or (eq .HtmlType "checkbox") (eq .HtmlType "radio") (eq .HtmlType "select") }}}
{{{- if ne .DictType "" }}} {{{- if ne .DictType "" }}}
<x-picker v-model="form.{{{ (.GoField) }}}" valueKey="value" labelKey="name" :columns="dictData.{{{ .DictType }}}"></x-picker> <x-picker v-model="form.{{{ (toUpperCamelCase .GoField) }}}" valueKey="value" labelKey="name" :columns="dictData.{{{ .DictType }}}"></x-picker>
{{{- else if ne .ListAllApi "" }}} {{{- else if ne .ListAllApi "" }}}
<x-picker v-model="form.{{{ (.GoField) }}}" valueKey="id" labelKey="id" :columns="listAllData.{{{pathToName .ListAllApi}}}"></x-picker> <x-picker v-model="form.{{{ (toUpperCamelCase .GoField) }}}" valueKey="{{{toUpperCamelCase .PrimaryKey}}}" labelKey="{{{toUpperCamelCase .PrimaryKey}}}" :columns="listAllData.{{{pathToName .ListAllApi}}}"></x-picker>
{{{- end }}} {{{- end }}}
{{{- else if eq .HtmlType "input" }}} {{{- else if eq .HtmlType "input" }}}
<uv-input v-model="form.{{{ (.GoField) }}}"> </uv-input> <uv-input v-model="form.{{{ (toUpperCamelCase .GoField) }}}"> </uv-input>
{{{- end }}} {{{- end }}}
</uv-form-item> </uv-form-item>
{{{- end }}} {{{- end }}}
@@ -71,10 +71,10 @@ const { listAllData } = useListAllData<{
{{{- range .Columns }}} {{{- range .Columns }}}
{{{- if .IsQuery }}} {{{- if .IsQuery }}}
{{{- if eq .HtmlType "datetime" }}} {{{- if eq .HtmlType "datetime" }}}
{{{ (.GoField) }}}Start: null, {{{ (toUpperCamelCase .GoField) }}}Start: null,
{{{ (.GoField) }}}End: null, {{{ (toUpperCamelCase .GoField) }}}End: null,
{{{- else }}} {{{- else }}}
{{{ (.GoField) }}}: null, {{{ (toUpperCamelCase .GoField) }}}: null,
{{{- end }}} {{{- end }}}
{{{- end }}} {{{- end }}}
{{{- end }}} {{{- end }}}

View File

@@ -43,8 +43,8 @@ export function {{{.ModuleName}}}_list_all(params?: type_{{{.ModuleName}}}_query
} }
// {{{.FunctionName}}}详情 // {{{.FunctionName}}}详情
export function {{{.ModuleName}}}_detail({{{ .PrimaryKey }}}: number | string) { export function {{{.ModuleName}}}_detail({{{toUpperCamelCase .PrimaryKey }}}: number | string) {
return request.get<type_{{{.ModuleName}}}>({ url: '/{{{.ModuleName}}}/detail', params: { {{{ .PrimaryKey }}} } }) return request.get<type_{{{.ModuleName}}}>({ url: '/{{{.ModuleName}}}/detail', params: { {{{toUpperCamelCase .PrimaryKey }}} } })
} }
// {{{.FunctionName}}}新增 // {{{.FunctionName}}}新增
@@ -58,8 +58,8 @@ export function {{{.ModuleName}}}_edit(data: type_{{{.ModuleName}}}_edit) {
} }
// {{{.FunctionName}}}删除 // {{{.FunctionName}}}删除
export function {{{.ModuleName}}}_delete({{{ .PrimaryKey }}}: number | string) { export function {{{.ModuleName}}}_delete({{{toUpperCamelCase .PrimaryKey }}}: number | string) {
return request.post<null>({ url: '/{{{.ModuleName}}}/del', data: { {{{ .PrimaryKey }}} } }) return request.post<null>({ url: '/{{{.ModuleName}}}/del', data: { {{{toUpperCamelCase .PrimaryKey }}} } })
} }
// {{{.FunctionName}}}导入 // {{{.FunctionName}}}导入

View File

@@ -12,7 +12,7 @@
<el-form ref="formRef" :model="formData" label-width="84px" :rules="formRules"> <el-form ref="formRef" :model="formData" label-width="84px" :rules="formRules">
{{{- range .Columns }}} {{{- range .Columns }}}
{{{- if .IsEdit }}} {{{- if .IsEdit }}}
{{{- if ne (toUpperCamelCase .GoField) "id" }}} {{{- if ne (toUpperCamelCase .GoField) "Id" }}}
{{{- if and (ne $.Table.TreeParent "") (eq (toUpperCamelCase .GoField) $.Table.TreeParent) }}} {{{- if and (ne $.Table.TreeParent "") (eq (toUpperCamelCase .GoField) $.Table.TreeParent) }}}
<el-form-item label="{{{ .ColumnComment }}}" prop="{{{ (toUpperCamelCase .GoField) }}}"> <el-form-item label="{{{ .ColumnComment }}}" prop="{{{ (toUpperCamelCase .GoField) }}}">
<el-tree-select <el-tree-select
@@ -59,8 +59,8 @@
<el-checkbox <el-checkbox
v-for="(item, index) in listAllData.{{{pathToName .ListAllApi }}}" v-for="(item, index) in listAllData.{{{pathToName .ListAllApi }}}"
:key="index" :key="index"
:label="item.id" :label="item.{{{toUpperCamelCase .PrimaryKey }}}"
:value="item.id" :value="item.{{{toUpperCamelCase .PrimaryKey }}}"
></el-checkbox> ></el-checkbox>
{{{- else }}} {{{- else }}}
<el-checkbox>请选择字典生成</el-checkbox> <el-checkbox>请选择字典生成</el-checkbox>
@@ -87,11 +87,11 @@
<el-option <el-option
v-for="(item, index) in listAllData.{{{pathToName .ListAllApi }}}" v-for="(item, index) in listAllData.{{{pathToName .ListAllApi }}}"
:key="index" :key="index"
:label="item.id" :label="item.{{{toUpperCamelCase .PrimaryKey }}}"
{{{- if eq .GoType "int" }}} {{{- if eq .GoType "int" }}}
:value="parseInt(item.id)" :value="parseInt(item.{{{toUpperCamelCase .PrimaryKey }}})"
{{{- else }}} {{{- else }}}
:value="String(item.id)" :value="String(item.{{{toUpperCamelCase .PrimaryKey }}})"
{{{- end }}} {{{- end }}}
clearable clearable
/> />
@@ -121,12 +121,12 @@
:key="index" :key="index"
:label="item.name" :label="item.name"
{{{- if eq .GoType "int" }}} {{{- if eq .GoType "int" }}}
:value="parseInt(item.id)" :value="parseInt(item.{{{toUpperCamelCase .PrimaryKey }}})"
{{{- else }}} {{{- else }}}
:value="item.id" :value="item.{{{toUpperCamelCase .PrimaryKey }}}"
{{{- end }}} {{{- end }}}
> >
{{ item.id }} {{ item.{{{toUpperCamelCase .PrimaryKey }}} }}
</el-radio> </el-radio>
{{{- else }}} {{{- else }}}
<el-radio label="0">请选择字典生成</el-radio> <el-radio label="0">请选择字典生成</el-radio>
@@ -260,7 +260,7 @@ const setFormData = async (data: Record<string, any>) => {
const getDetail = async (row: Record<string, any>) => { const getDetail = async (row: Record<string, any>) => {
try { try {
const data = await {{{ .ModuleName }}}_detail(row.{{{ .PrimaryKey }}}) const data = await {{{ .ModuleName }}}_detail(row.{{{toUpperCamelCase .PrimaryKey }}})
setFormData(data) setFormData(data)
} catch (error) {} } catch (error) {}
} }

View File

@@ -31,8 +31,8 @@
<el-option <el-option
v-for="(item, index) in listAllData.{{{pathToName .ListAllApi}}}" v-for="(item, index) in listAllData.{{{pathToName .ListAllApi}}}"
:key="index" :key="index"
:label="item.id" :label="item.{{{toUpperCamelCase .PrimaryKey }}}"
:value="item.id" :value="item.{{{toUpperCamelCase .PrimaryKey }}}"
/> />
{{{- else }}} {{{- else }}}
<el-option label="请选择字典生成" value="" /> <el-option label="请选择字典生成" value="" />
@@ -82,7 +82,7 @@
{{{- else if and (ne .ListAllApi "") (or (eq .HtmlType "select") (eq .HtmlType "radio") (eq .HtmlType "checkbox")) }}} {{{- else if and (ne .ListAllApi "") (or (eq .HtmlType "select") (eq .HtmlType "radio") (eq .HtmlType "checkbox")) }}}
<el-table-column label="{{{ .ColumnComment }}}" prop="{{{ (.GoField) }}}" min-width="100"> <el-table-column label="{{{ .ColumnComment }}}" prop="{{{ (.GoField) }}}" min-width="100">
<template #default="{ row }"> <template #default="{ row }">
<dict-value :options="listAllData.{{{pathToName .ListAllApi }}}" :value="row.{{{ (.GoField) }}}" labelKey='id' valueKey='id' /> <dict-value :options="listAllData.{{{pathToName .ListAllApi }}}" :value="row.{{{ (.GoField) }}}" labelKey='{{{toUpperCamelCase .PrimaryKey }}}' valueKey='{{{toUpperCamelCase .PrimaryKey }}}' />
</template> </template>
</el-table-column> </el-table-column>
{{{- else if eq .HtmlType "imageUpload" }}} {{{- else if eq .HtmlType "imageUpload" }}}
@@ -232,9 +232,9 @@ const handleEdit = async (data: any) => {
editRef.value?.getDetail(data) editRef.value?.getDetail(data)
} }
const handleDelete = async ({{{ .PrimaryKey }}}: number) => { const handleDelete = async ({{{toUpperCamelCase .PrimaryKey }}}: number) => {
await feedback.confirm('确定要删除?') await feedback.confirm('确定要删除?')
await {{{ .ModuleName }}}_delete({ {{{ .PrimaryKey }}} }) await {{{ .ModuleName }}}_delete({ {{{toUpperCamelCase .PrimaryKey }}} })
feedback.msgSuccess('删除成功') feedback.msgSuccess('删除成功')
getLists() getLists()
} }

View File

@@ -32,8 +32,8 @@
<el-option <el-option
v-for="(item, index) in listAllData.{{{pathToName .ListAllApi}}}" v-for="(item, index) in listAllData.{{{pathToName .ListAllApi}}}"
:key="index" :key="index"
:label="item.id" :label="item.{{{toUpperCamelCase .PrimaryKey }}}"
:value="item.id" :value="item.{{{toUpperCamelCase .PrimaryKey }}}"
/> />
{{{- else }}} {{{- else }}}
<el-option label="请选择字典生成" value="" /> <el-option label="请选择字典生成" value="" />
@@ -100,7 +100,7 @@
{{{- else if and (ne .ListAllApi "") (or (eq .HtmlType "select") (eq .HtmlType "radio") (eq .HtmlType "checkbox")) }}} {{{- else if and (ne .ListAllApi "") (or (eq .HtmlType "select") (eq .HtmlType "radio") (eq .HtmlType "checkbox")) }}}
<el-table-column label="{{{ .ColumnComment }}}" prop="{{{ (toUpperCamelCase .GoField) }}}" min-width="100"> <el-table-column label="{{{ .ColumnComment }}}" prop="{{{ (toUpperCamelCase .GoField) }}}" min-width="100">
<template #default="{ row }"> <template #default="{ row }">
<dict-value :options="listAllData.{{{pathToName .ListAllApi }}}" :value="row.{{{ (toUpperCamelCase .GoField) }}}" labelKey='id' valueKey='id' /> <dict-value :options="listAllData.{{{pathToName .ListAllApi }}}" :value="row.{{{ (toUpperCamelCase .GoField) }}}" labelKey='{{{toUpperCamelCase .PrimaryKey }}}' valueKey='{{{toUpperCamelCase .PrimaryKey }}}' />
</template> </template>
</el-table-column> </el-table-column>
@@ -136,7 +136,7 @@
v-perms="['admin:{{{ .ModuleName }}}:del']" v-perms="['admin:{{{ .ModuleName }}}:del']"
type="danger" type="danger"
link link
@click="handleDelete(row.{{{ .PrimaryKey }}})" @click="handleDelete(row.{{{toUpperCamelCase .PrimaryKey }}})"
> >
删除 删除
</el-button> </el-button>
@@ -231,10 +231,10 @@ const handleEdit = async (data: any) => {
editRef.value?.getDetail(data) editRef.value?.getDetail(data)
} }
const handleDelete = async ({{{ .PrimaryKey }}}: number) => { const handleDelete = async ({{{toUpperCamelCase .PrimaryKey }}}: number) => {
try { try {
await feedback.confirm('确定要删除?') await feedback.confirm('确定要删除?')
await {{{ .ModuleName }}}_delete( {{{ .PrimaryKey }}} ) await {{{ .ModuleName }}}_delete( {{{toUpperCamelCase .PrimaryKey }}} )
feedback.msgSuccess('删除成功') feedback.msgSuccess('删除成功')
getLists() getLists()
} catch (error) {} } catch (error) {}

View File

@@ -24,7 +24,9 @@ var TemplateUtil = templateUtil{
"toUpperCamelCase": util.StringUtil.ToUpperCamelCase, "toUpperCamelCase": util.StringUtil.ToUpperCamelCase,
"contains": util.ToolsUtil.Contains, "contains": util.ToolsUtil.Contains,
"goToTsType": GenUtil.GoToTsType, "goToTsType": GenUtil.GoToTsType,
"goToNullType": GenUtil.GoToNullType, "goToParamType": GenUtil.GoToParamType,
"goWithAddEditType": GenUtil.GoWithAddEditType,
"goWithRespType": GenUtil.GoWithRespType,
"getPageResp": GenUtil.GetPageResp, "getPageResp": GenUtil.GetPageResp,
"nameToPath": GenUtil.NameToPath, "nameToPath": GenUtil.NameToPath,
"pathToName": GenUtil.PathToName, "pathToName": GenUtil.PathToName,

View File

@@ -240,25 +240,61 @@ func (gu genUtil) GoToTsType(s string) string {
} }
/** /**
* @description: Go类型转可为null类型转换后还能解决前端对int传了string类型错误问题 * @description: Go类型转 添加编辑 类型
*/ */
func (gu genUtil) GoToNullType(s string) string { func (gu genUtil) GoWithAddEditType(s string) string {
// if s == "int64" { if s == "int" || s == "int8" || s == "int16" || s == "int32" || s == "int64" {
// return "null.Int" return "core.NullInt"
// } else if s == "int32" || s == "int" { } else if s == "float" || s == "float32" || s == "float64" {
// return "null.Int32" return "core.NullFloat"
// } else if s == "int8" || s == "int16" { } else if s == "string" {
// return "null.Int16" return "*string"
// } else if s == "float" || s == "float32" || s == "float64" { } else if s == "bool" {
// return "null.Float" return "*int"
// } else if s == "string" { } else if s == "time.Time" {
// return "null.String" return "core.NullTime"
// } else if s == "bool" { } else if s == "core.NullTime" {
// return "null.Bool" return "core.NullTime"
// } else if s == "time.Time" { }
// return "null.Time" return "string"
// } }
return "*" + s
/**
* @description: Go类型转 添加编辑 类型
*/
func (gu genUtil) GoWithRespType(s string) string {
if s == "int" || s == "int8" || s == "int16" || s == "int32" || s == "int64" {
return "core.NullInt"
} else if s == "float" || s == "float32" || s == "float64" {
return "core.NullFloat"
} else if s == "string" {
return "string"
} else if s == "bool" {
return "int"
} else if s == "time.Time" {
return "core.NullTime"
} else if s == "core.NullTime" {
return "core.NullTime"
}
return "string"
}
/**
* @description: Go类型转可为Param类型
*/
func (gu genUtil) GoToParamType(s string) string {
if s == "int" || s == "int8" || s == "int16" || s == "int32" || s == "int64" {
return "int"
} else if s == "float" || s == "float32" || s == "float64" {
return "float"
} else if s == "string" {
return "string"
} else if s == "bool" {
return "bool"
} else if s == "core.NullTime" {
return "string"
}
return "string"
} }
// 拼接字符串 // 拼接字符串

View File

@@ -10,8 +10,8 @@ import (
"gorm.io/gorm/schema" "gorm.io/gorm/schema"
) )
// const DateFormat = "2006-01-02" const DateFormat = "2006-01-02"
// const TimeFormat = "2006-01-02 15:04:05" const TimeFormat = "2006-01-02 15:04:05"
// NullTime 自定义时间格式 // NullTime 自定义时间格式
type NullTime struct { type NullTime struct {

View File

@@ -1,86 +0,0 @@
package core
import (
"database/sql/driver"
"encoding/json"
"fmt"
"time"
"gorm.io/gorm"
"gorm.io/gorm/schema"
)
const DateFormat = "2006-01-02"
const TimeFormat = "2006-01-02 15:04:05"
// TsTime 自定义时间格式
type TsTime time.Time
// 通过时间字符串生成时间戳
func ToUnix(date string) int64 {
if date == "" {
return 0
}
tt, _ := time.ParseInLocation(TimeFormat, date, time.Local)
return tt.Unix()
}
func NowTime() TsTime {
return TsTime(time.Now())
}
func (tst *TsTime) UnmarshalJSON(bs []byte) error {
var date string
err := json.Unmarshal(bs, &date)
if err != nil {
return err
}
tt, _ := time.ParseInLocation(TimeFormat, date, time.Local)
*tst = TsTime(tt)
return nil
}
// MarshalJSON 将TsTime类型的时间转化为JSON字符串格式
// 返回转化后的JSON字符串和错误信息
func (tst TsTime) MarshalJSON() ([]byte, error) {
tt := time.Time(tst).Format(TimeFormat)
return json.Marshal(tt)
}
// 写入数据库gorm调用
func (t TsTime) Value() (driver.Value, error) {
timeStr := t.String()
if timeStr == "0001-01-01 00:00:00" {
return nil, nil
}
return timeStr, nil
}
// 读取数据gorm调用
func (t *TsTime) Scan(v any) error {
// pt, err := time.ParseInLocation("2006-01-02 15:04:05", v.(time.Time).String(), time.Local)
if pt, ok := v.(time.Time); ok {
*t = TsTime(pt)
return nil
}
return fmt.Errorf("cant convert %s to time", v)
}
func (t TsTime) String() string {
return time.Time(t).Format(TimeFormat)
}
func (TsTime) GormDBDataType(db *gorm.DB, field *schema.Field) string {
// 使用 field.Tag、field.TagSettings 获取字段的 tag
// 查看 https://github.com/go-gorm/gorm/blob/master/schema/field.go 获取全部的选项
// 根据不同的数据库驱动返回不同的数据类型
// switch db.Dialector.Name() {
// case "mysql", "sqlite":
// return "JSON"
// case "postgres":
// return "JSONB"
// }
// return ""
return "DATETIME"
}

View File

@@ -1,102 +0,0 @@
package core
import (
"database/sql/driver"
"encoding/json"
"time"
"gorm.io/gorm"
"gorm.io/gorm/schema"
)
const DateFormat = "2006-01-02"
const TimeFormat = "2006-01-02 15:04:05"
// 注解:时间类型从time.Time改为string的原因是struct转interface{}时丢弃了部分信息导致导出excel时时间格式不对
// TsTime 自定义时间格式
type TsTime string
// 通过时间字符串生成时间戳
//
// func ToUnix(date string) int64 {
// if date == "" {
// return 0
// }
// tt, _ := time.ParseInLocation(TimeFormat, date, time.Local)
// return tt.Unix()
// }
func ParseTimeToTsTime(date time.Time) TsTime {
return TsTime(date.Format(TimeFormat))
}
func NowTime() TsTime {
return TsTime(time.Now().Format(TimeFormat))
}
func (tst *TsTime) UnmarshalJSON(bs []byte) error {
var date string
err := json.Unmarshal(bs, &date)
if err != nil {
return err
}
tt, _ := time.ParseInLocation(TimeFormat, date, time.Local)
*tst = TsTime(
tt.Format(TimeFormat),
)
return nil
}
// MarshalJSON 将TsTime类型的时间转化为JSON字符串格式
// 返回转化后的JSON字符串和错误信息
func (tst TsTime) MarshalJSON() ([]byte, error) {
// tt := time.Time(tst.Str).Format(TimeFormat)
tt, _ := time.Parse(TimeFormat, tst.String())
str := tt.Format(TimeFormat)
return json.Marshal(str)
}
// 写入数据库gorm调用
func (t TsTime) Value() (driver.Value, error) {
timeStr := t.String()
if timeStr == "" {
return nil, nil
}
return timeStr, nil
}
// 读取数据gorm调用
func (t *TsTime) Scan(v any) error {
// pt, err := time.ParseInLocation("2006-01-02 15:04:05", v.(time.Time).String(), time.Local)
if _, ok := v.(time.Time); ok {
*t = TsTime(v.(time.Time).Format(TimeFormat))
return nil
} else {
*t = ""
return nil
}
}
func (t TsTime) String() string {
return string(t)
}
func (TsTime) GormDBDataType(db *gorm.DB, field *schema.Field) string {
// 使用 field.Tag、field.TagSettings 获取字段的 tag
// 查看 https://github.com/go-gorm/gorm/blob/master/schema/field.go 获取全部的选项
// 根据不同的数据库驱动返回不同的数据类型
// switch db.Dialector.Name() {
// case "mysql", "sqlite":
// return "JSON"
// case "postgres":
// return "JSONB"
// }
// return ""
return "DATETIME"
}
func (TsTime) GormDataType() string {
return "time"
}

View File

@@ -1,84 +0,0 @@
package core
import (
"database/sql/driver"
"encoding/json"
"fmt"
"time"
"gorm.io/gorm"
"gorm.io/gorm/schema"
)
const DateFormat = "2006-01-02"
const TimeFormat = "2006-01-02 15:04:05"
// TsTime 自定义时间格式
type TsTime time.Time
func (tst *TsTime) IsZero() bool {
return tst.String() == "0001-01-01 00:00:00"
}
func (tst *TsTime) UnmarshalJSON(bs []byte) error {
var date string
err := json.Unmarshal(bs, &date)
if err != nil {
return err
}
if date == "" {
*tst = TsTime(time.Time{})
return nil
}
tt, _ := time.ParseInLocation(TimeFormat, date, time.Local)
*tst = TsTime(tt)
return nil
}
// MarshalJSON 将TsTime类型的时间转化为JSON字符串格式
// 返回转化后的JSON字符串和错误信息
func (tst TsTime) MarshalJSON() ([]byte, error) {
tt := time.Time(tst).Format(TimeFormat)
if tt == "0001-01-01 00:00:00" {
return json.Marshal(nil)
} else {
return json.Marshal(tt)
}
}
// 写入数据库gorm调用
func (t TsTime) Value() (driver.Value, error) {
timeStr := t.String()
if timeStr == "0001-01-01 00:00:00" {
return nil, nil
}
return timeStr, nil
}
// 读取数据gorm调用
func (t *TsTime) Scan(v any) error {
// pt, err := time.ParseInLocation("2006-01-02 15:04:05", v.(time.Time).String(), time.Local)
if pt, ok := v.(time.Time); ok {
*t = TsTime(pt)
return nil
}
return fmt.Errorf("cant convert %s to time", v)
}
func (t TsTime) String() string {
return time.Time(t).Format(TimeFormat)
}
func (TsTime) GormDBDataType(db *gorm.DB, field *schema.Field) string {
// 使用 field.Tag、field.TagSettings 获取字段的 tag
// 查看 https://github.com/go-gorm/gorm/blob/master/schema/field.go 获取全部的选项
// 根据不同的数据库驱动返回不同的数据类型
// switch db.Dialector.Name() {
// case "mysql", "sqlite":
// return "JSON"
// case "postgres":
// return "JSONB"
// }
// return ""
return "DATETIME"
}

View File

@@ -1,76 +0,0 @@
package util
import (
"errors"
"time"
"x_admin/core"
)
var TimeUtil = timeUtil{
DateFormat: "2006-01-02",
TimeFormat: "2006-01-02 15:04:05",
}
// arrayUtil 数组工具类
type timeUtil struct {
DateFormat string
TimeFormat string
}
// ToUnix 时间戳转时间戳
func (t timeUtil) ToUnix(date any) int64 {
switch v := date.(type) {
case string:
if v == "" {
return 0
}
tt, e := time.Parse(t.TimeFormat, v)
if e != nil {
return 0
}
return time.Time(tt).Unix()
case time.Time:
return v.Unix()
default:
return 0
}
}
// ParseTsTime 时间戳转时间
func (t timeUtil) ParseTsTime(value any) (core.TsTime, error) {
switch v := value.(type) {
case string:
tt, e := t.ParseString(v)
return tt, e
case time.Time:
tt := t.ParseTime(v)
return tt, nil
default:
return t.NowTime(), errors.New("not support type")
}
}
// ParseTime 时间转时间戳
func (t timeUtil) ParseTime(date time.Time) core.TsTime {
return core.TsTime(date)
}
// ParseString 时间字符串转时间戳
func (t timeUtil) ParseString(date string) (core.TsTime, error) {
tt, e := time.Parse(t.TimeFormat, date)
if e != nil {
return t.NowTime(), e
}
return core.TsTime(tt), nil
}
// NowTime 当前时间
func (t timeUtil) NowTime() core.TsTime {
return core.TsTime(time.Now())
}
// DecodeTime 时间解码
func (t timeUtil) DecodeTime(value any) (any, error) {
tt, e := t.ParseTsTime(value)
return tt, e
}