You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

370 lines
12 KiB

3 years ago
3 years ago
3 years ago
3 years ago
  1. <template>
  2. <div>
  3. <div class="gva-search-box">
  4. <el-form :inline="true" :model="searchInfo" class="demo-form-inline">
  5. {{- range .Fields}} {{- if .FieldSearchType}} {{- if eq .FieldType "bool" }}
  6. <el-form-item label="{{.FieldDesc}}" prop="{{.FieldJson}}">
  7. <el-select v-model="searchInfo.{{.FieldJson}}" clearable placeholder="请选择">
  8. <el-option
  9. key="true"
  10. label="是"
  11. value="true">
  12. </el-option>
  13. <el-option
  14. key="false"
  15. label="否"
  16. value="false">
  17. </el-option>
  18. </el-select>
  19. </el-form-item>
  20. {{- else }}
  21. <el-form-item label="{{.FieldDesc}}">
  22. <el-input v-model="searchInfo.{{.FieldJson}}" placeholder="搜索条件" />
  23. </el-form-item>{{ end }}{{ end }}{{ end }}
  24. <el-form-item>
  25. <el-button size="mini" type="primary" icon="search" @click="onSubmit">查询</el-button>
  26. <el-button size="mini" icon="refresh" @click="onReset">重置</el-button>
  27. </el-form-item>
  28. </el-form>
  29. </div>
  30. <div class="gva-table-box">
  31. <div class="gva-btn-list">
  32. <el-button size="mini" type="primary" icon="plus" @click="openDialog">新增</el-button>
  33. <el-popover v-model:visible="deleteVisible" placement="top" width="160">
  34. <p>确定要删除吗?</p>
  35. <div style="text-align: right; margin-top: 8px;">
  36. <el-button size="mini" type="text" @click="deleteVisible = false">取消</el-button>
  37. <el-button size="mini" type="primary" @click="onDelete">确定</el-button>
  38. </div>
  39. <template #reference>
  40. <el-button icon="delete" size="mini" style="margin-left: 10px;" :disabled="!multipleSelection.length">删除</el-button>
  41. </template>
  42. </el-popover>
  43. </div>
  44. <el-table
  45. ref="multipleTable"
  46. style="width: 100%"
  47. tooltip-effect="dark"
  48. :data="tableData"
  49. row-key="ID"
  50. @selection-change="handleSelectionChange"
  51. >
  52. <el-table-column type="selection" width="55" />
  53. <el-table-column align="left" label="日期" width="180">
  54. <template #default="scope">{{ "{{ formatDate(scope.row.CreatedAt) }}" }}</template>
  55. </el-table-column>
  56. {{- range .Fields}}
  57. {{- if .DictType}}
  58. <el-table-column align="left" label="{{.FieldDesc}}" prop="{{.FieldJson}}" width="120">
  59. <template #default="scope">
  60. {{"{{"}} filterDict(scope.row.{{.FieldJson}},{{.DictType}}Options) {{"}}"}}
  61. </template>
  62. </el-table-column>
  63. {{- else if eq .FieldType "bool" }}
  64. <el-table-column align="left" label="{{.FieldDesc}}" prop="{{.FieldJson}}" width="120">
  65. <template #default="scope">{{"{{"}} formatBoolean(scope.row.{{.FieldJson}}) {{"}}"}}</template>
  66. </el-table-column> {{- else }}
  67. <el-table-column align="left" label="{{.FieldDesc}}" prop="{{.FieldJson}}" width="120" />
  68. {{- end }}
  69. {{- end }}
  70. <el-table-column align="left" label="按钮组">
  71. <template #default="scope">
  72. <el-button type="text" icon="edit" size="small" class="table-button" @click="update{{.StructName}}Func(scope.row)">变更</el-button>
  73. <el-button type="text" icon="delete" size="mini" @click="deleteRow(scope.row)">删除</el-button>
  74. </template>
  75. </el-table-column>
  76. </el-table>
  77. <div class="gva-pagination">
  78. <el-pagination
  79. layout="total, sizes, prev, pager, next, jumper"
  80. :current-page="page"
  81. :page-size="pageSize"
  82. :page-sizes="[10, 30, 50, 100]"
  83. :total="total"
  84. @current-change="handleCurrentChange"
  85. @size-change="handleSizeChange"
  86. />
  87. </div>
  88. </div>
  89. <el-dialog v-model="dialogFormVisible" :before-close="closeDialog" title="弹窗操作">
  90. <el-form :model="formData" label-position="right" label-width="80px">
  91. {{- range .Fields}}
  92. <el-form-item label="{{.FieldDesc}}:">
  93. {{- if eq .FieldType "bool" }}
  94. <el-switch v-model="formData.{{.FieldJson}}" active-color="#13ce66" inactive-color="#ff4949" active-text="是" inactive-text="否" clearable ></el-switch>
  95. {{- end }}
  96. {{- if eq .FieldType "string" }}
  97. <el-input v-model="formData.{{.FieldJson}}" clearable placeholder="请输入" />
  98. {{- end }}
  99. {{- if eq .FieldType "int" }}
  100. {{- if .DictType}}
  101. <el-select v-model="formData.{{ .FieldJson }}" placeholder="请选择" style="width:100%" clearable>
  102. <el-option v-for="(item,key) in {{ .DictType }}Options" :key="key" :label="item.label" :value="item.value" />
  103. </el-select>
  104. {{- else }}
  105. <el-input v-model.number="formData.{{ .FieldJson }}" clearable placeholder="请输入" />
  106. {{- end }}
  107. {{- end }}
  108. {{- if eq .FieldType "time.Time" }}
  109. <el-date-picker v-model="formData.{{ .FieldJson }}" type="date" style="width:100%" placeholder="选择日期" clearable />
  110. {{- end }}
  111. {{- if eq .FieldType "float64" }}
  112. <el-input-number v-model="formData.{{ .FieldJson }}" style="width:100%" :precision="2" clearable />
  113. {{- end }}
  114. </el-form-item>
  115. {{- end }}
  116. </el-form>
  117. <template #footer>
  118. <div class="dialog-footer">
  119. <el-button size="small" @click="closeDialog">取 消</el-button>
  120. <el-button size="small" type="primary" @click="enterDialog">确 定</el-button>
  121. </div>
  122. </template>
  123. </el-dialog>
  124. </div>
  125. </template>
  126. <script>
  127. export default {
  128. name: '{{.StructName}}'
  129. }
  130. </script>
  131. <script setup>
  132. import {
  133. create{{.StructName}},
  134. delete{{.StructName}},
  135. delete{{.StructName}}ByIds,
  136. update{{.StructName}},
  137. find{{.StructName}},
  138. get{{.StructName}}List
  139. } from '@/api/{{.PackageName}}'
  140. // 全量引入格式化工具 请按需保留
  141. import { getDictFunc, formatDate, formatBoolean, filterDict } from '@/utils/format'
  142. import { ElMessage, ElMessageBox } from 'element-plus'
  143. import { ref } from 'vue'
  144. // 自动化生成的字典(可能为空)以及字段
  145. {{- range $index, $element := .DictTypes}}
  146. const {{ $element }}Options = ref([])
  147. {{- end }}
  148. const formData = ref({
  149. {{- range .Fields}}
  150. {{- if eq .FieldType "bool" }}
  151. {{.FieldJson}}: false,
  152. {{- end }}
  153. {{- if eq .FieldType "string" }}
  154. {{.FieldJson}}: '',
  155. {{- end }}
  156. {{- if eq .FieldType "int" }}
  157. {{.FieldJson}}: {{- if .DictType }} undefined{{ else }} 0{{- end }},
  158. {{- end }}
  159. {{- if eq .FieldType "time.Time" }}
  160. {{.FieldJson}}: new Date(),
  161. {{- end }}
  162. {{- if eq .FieldType "float64" }}
  163. {{.FieldJson}}: 0,
  164. {{- end }}
  165. {{- end }}
  166. })
  167. // =========== 表格控制部分 ===========
  168. const page = ref(1)
  169. const total = ref(0)
  170. const pageSize = ref(10)
  171. const tableData = ref([])
  172. const searchInfo = ref({})
  173. // 重置
  174. const onReset = () => {
  175. searchInfo.value = {}
  176. }
  177. // 搜索
  178. const onSubmit = () => {
  179. page.value = 1
  180. pageSize.value = 10
  181. {{- range .Fields}}{{- if eq .FieldType "bool" }}
  182. if (searchInfo.value.{{.FieldJson}} === ""){
  183. searchInfo.value.{{.FieldJson}}=null
  184. }{{ end }}{{ end }}
  185. getTableData()
  186. }
  187. // 分页
  188. const handleSizeChange = (val) => {
  189. pageSize.value = val
  190. getTableData()
  191. }
  192. // 修改页面容量
  193. const handleCurrentChange = (val) => {
  194. page.value = val
  195. getTableData()
  196. }
  197. // 查询
  198. const getTableData = async() => {
  199. const table = await get{{.StructName}}List({ page: page.value, pageSize: pageSize.value, ...searchInfo.value })
  200. if (table.code === 0) {
  201. tableData.value = table.data.list
  202. total.value = table.data.total
  203. page.value = table.data.page
  204. pageSize.value = table.data.pageSize
  205. }
  206. }
  207. getTableData()
  208. // ============== 表格控制部分结束 ===============
  209. // 获取需要的字典 可能为空 按需保留
  210. const setOptions = async () =>{
  211. {{- range $index, $element := .DictTypes }}
  212. {{ $element }}Options.value = await getDictFunc('{{$element}}')
  213. {{- end }}
  214. }
  215. // 获取需要的字典 可能为空 按需保留
  216. setOptions()
  217. // 多选数据
  218. const multipleSelection = ref([])
  219. // 多选
  220. const handleSelectionChange = (val) => {
  221. multipleSelection.value = val
  222. }
  223. // 删除行
  224. const deleteRow = (row) => {
  225. ElMessageBox.confirm('确定要删除吗?', '提示', {
  226. confirmButtonText: '确定',
  227. cancelButtonText: '取消',
  228. type: 'warning'
  229. }).then(() => {
  230. delete{{.StructName}}Func(row)
  231. })
  232. }
  233. // 批量删除控制标记
  234. const deleteVisible = ref(false)
  235. // 多选删除
  236. const onDelete = async() => {
  237. const ids = []
  238. if (multipleSelection.value.length === 0) {
  239. ElMessage({
  240. type: 'warning',
  241. message: '请选择要删除的数据'
  242. })
  243. return
  244. }
  245. multipleSelection.value &&
  246. multipleSelection.value.map(item => {
  247. ids.push(item.ID)
  248. })
  249. const res = await delete{{.StructName}}ByIds({ ids })
  250. if (res.code === 0) {
  251. ElMessage({
  252. type: 'success',
  253. message: '删除成功'
  254. })
  255. if (tableData.value.length === ids.length && page.value > 1) {
  256. page.value--
  257. }
  258. deleteVisible.value = false
  259. getTableData()
  260. }
  261. }
  262. // 行为控制标记(弹窗内部需要增还是改)
  263. const type = ref('')
  264. // 更新行
  265. const update{{.StructName}}Func = async(row) => {
  266. const res = await find{{.StructName}}({ ID: row.ID })
  267. type.value = 'update'
  268. if (res.code === 0) {
  269. formData.value = res.data.re{{.Abbreviation}}
  270. dialogFormVisible.value = true
  271. }
  272. }
  273. // 删除行
  274. const delete{{.StructName}}Func = async (row) => {
  275. const res = await delete{{.StructName}}({ ID: row.ID })
  276. if (res.code === 0) {
  277. ElMessage({
  278. type: 'success',
  279. message: '删除成功'
  280. })
  281. if (tableData.value.length === 1 && page.value > 1) {
  282. page.value--
  283. }
  284. getTableData()
  285. }
  286. }
  287. // 弹窗控制标记
  288. const dialogFormVisible = ref(false)
  289. // 打开弹窗
  290. const openDialog = () => {
  291. type.value = 'create'
  292. dialogFormVisible.value = true
  293. }
  294. // 关闭弹窗
  295. const closeDialog = () => {
  296. dialogFormVisible.value = false
  297. formData.value = {
  298. {{- range .Fields}}
  299. {{- if eq .FieldType "bool" }}
  300. {{.FieldJson}}: false,
  301. {{- end }}
  302. {{- if eq .FieldType "string" }}
  303. {{.FieldJson}}: '',
  304. {{- end }}
  305. {{- if eq .FieldType "int" }}
  306. {{.FieldJson}}: {{- if .DictType }} undefined{{ else }} 0{{- end }},
  307. {{- end }}
  308. {{- if eq .FieldType "time.Time" }}
  309. {{.FieldJson}}: new Date(),
  310. {{- end }}
  311. {{- if eq .FieldType "float64" }}
  312. {{.FieldJson}}: 0,
  313. {{- end }}
  314. {{- end }}
  315. }
  316. }
  317. // 弹窗确定
  318. const enterDialog = async () => {
  319. let res
  320. switch (type.value) {
  321. case 'create':
  322. res = await create{{.StructName}}(formData.value)
  323. break
  324. case 'update':
  325. res = await update{{.StructName}}(formData.value)
  326. break
  327. default:
  328. res = await create{{.StructName}}(formData.value)
  329. break
  330. }
  331. if (res.code === 0) {
  332. ElMessage({
  333. type: 'success',
  334. message: '创建/更改成功'
  335. })
  336. closeDialog()
  337. getTableData()
  338. }
  339. }
  340. </script>
  341. <style>
  342. </style>