index-sub.vue.vm 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. {% macro getColumnDictType(column) %}
  2. {% if column.dict_type != '' %}{{ column.dict_type }}
  3. {% else %}null
  4. {% endif %}
  5. {% endmacro %}
  6. <template>
  7. <div class="app-container">
  8. <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
  9. {%- for column in table.columns %}
  10. {%- if column.is_query %}
  11. <el-form-item label="{{ column.column_comment }}" prop="{{ underscore(column.java_field) }}">
  12. {%- if column.html_type == 'input' %}
  13. <el-input
  14. v-model="queryParams.{{ underscore(column.java_field) }}"
  15. placeholder="请输入{{ column.column_comment }}"
  16. clearable
  17. @keyup.enter.native="handleQuery"
  18. />
  19. {%- elif column.html_type == 'select' and column.dict_type != '' %}
  20. <el-select v-model="queryParams.{{ underscore(column.java_field) }}" placeholder="请选择{{ column.column_comment }}" clearable>
  21. <el-option
  22. v-for="dict in {{ getColumnDictType(column) }}Options"
  23. :key="dict.value"
  24. :label="dict.label"
  25. :value="dict.value"
  26. />
  27. </el-select>
  28. {%- elif column.html_type == 'datetime' %}
  29. <el-date-picker
  30. v-model="daterange{{ column.java_field }}"
  31. value-format="yyyy-MM-dd"
  32. type="daterange"
  33. range-separator="-"
  34. start-placeholder="开始日期"
  35. end-placeholder="结束日期"
  36. ></el-date-picker>
  37. {%- endif %}
  38. </el-form-item>
  39. {%- endif %}
  40. {%- endfor %}
  41. <el-form-item>
  42. <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
  43. <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
  44. </el-form-item>
  45. </el-form>
  46. <el-row :gutter="10" class="mb8">
  47. <el-col :span="1.5">
  48. <el-button
  49. type="primary"
  50. plain
  51. icon="el-icon-plus"
  52. size="mini"
  53. @click="handleAdd"
  54. v-hasPermi="['{{ table.module_name }}:{{ table.business_name }}:add']"
  55. >新增</el-button>
  56. </el-col>
  57. <el-col :span="1.5">
  58. <el-button
  59. type="success"
  60. plain
  61. icon="el-icon-edit"
  62. size="mini"
  63. :disabled="single"
  64. @click="handleUpdate"
  65. v-hasPermi="['{{ table.module_name }}:{{ table.business_name }}:edit']"
  66. >修改</el-button>
  67. </el-col>
  68. <el-col :span="1.5">
  69. <el-button
  70. type="danger"
  71. plain
  72. icon="el-icon-delete"
  73. size="mini"
  74. :disabled="multiple"
  75. @click="handleDelete"
  76. v-hasPermi="['{{ table.module_name }}:{{ table.business_name }}:remove']"
  77. >删除</el-button>
  78. </el-col>
  79. <el-col :span="1.5">
  80. <el-button
  81. type="warning"
  82. plain
  83. icon="el-icon-download"
  84. size="mini"
  85. @click="handleExport"
  86. v-hasPermi="['{{ table.module_name }}:{{ table.business_name }}:export']"
  87. >导出</el-button>
  88. </el-col>
  89. <el-col :span="1.5">
  90. <el-button
  91. type="info"
  92. plain
  93. icon="el-icon-upload2"
  94. size="mini"
  95. @click="handleImport"
  96. v-hasPermi="['{{ table.module_name }}:{{ table.business_name }}:import']"
  97. >导入</el-button>
  98. </el-col>
  99. <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
  100. </el-row>
  101. <el-table v-loading="loading" :data="{{ table.business_name }}List" @selection-change="handleSelectionChange">
  102. <el-table-column type="selection" width="55" align="center" />
  103. {%- for column in table.columns %}
  104. {%- if column.is_list %}
  105. <el-table-column label="{{ column.column_comment }}" align="center" v-if="columns[{{ column.list_index }}].visible" prop="{{ underscore(column.java_field) }}" />
  106. {%- endif %}
  107. {%- endfor %}
  108. <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
  109. <template slot-scope="scope">
  110. <el-button
  111. size="mini"
  112. type="text"
  113. icon="el-icon-edit"
  114. @click="handleUpdate(scope.row)"
  115. v-hasPermi="['{{ table.module_name }}:{{ table.business_name }}:edit']"
  116. >修改</el-button>
  117. <el-button
  118. size="mini"
  119. type="text"
  120. icon="el-icon-delete"
  121. @click="handleDelete(scope.row)"
  122. v-hasPermi="['{{ table.module_name }}:{{ table.business_name }}:remove']"
  123. >删除</el-button>
  124. </template>
  125. </el-table-column>
  126. </el-table>
  127. <pagination
  128. v-show="total>0"
  129. :total="total"
  130. :page.sync="queryParams.pageNum"
  131. :limit.sync="queryParams.pageSize"
  132. @pagination="getList"
  133. />
  134. <!-- {{ table.function_name }}导入对话框 -->
  135. <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
  136. <el-upload
  137. ref="upload"
  138. :limit="1"
  139. accept=".xlsx, .xls"
  140. :headers="upload.headers"
  141. :action="upload.url + '?updateSupport=' + upload.updateSupport"
  142. :disabled="upload.isUploading"
  143. :on-progress="handleFileUploadProgress"
  144. :on-success="handleFileSuccess"
  145. :auto-upload="false"
  146. drag
  147. >
  148. <i class="el-icon-upload"></i>
  149. <div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
  150. <div class="el-upload__tip text-center" slot="tip">
  151. <div class="el-upload__tip" slot="tip">
  152. <el-checkbox v-model="upload.updateSupport"/>
  153. 是否更新已经存在的{{ table.function_name }}数据
  154. </div>
  155. <span>仅允许导入xls、xlsx格式文件。</span>
  156. <el-link
  157. type="primary"
  158. :underline="false"
  159. style="font-size:12px;vertical-align: baseline;"
  160. @click="importTemplate"
  161. >下载模板
  162. </el-link>
  163. </div>
  164. </el-upload>
  165. <div slot="footer" class="dialog-footer">
  166. <el-button type="primary" @click="submitFileForm">确 定</el-button>
  167. <el-button @click="upload.open = false">取 消</el-button>
  168. </div>
  169. </el-dialog>
  170. <!-- 添加或修改{{ table.function_name }}(含子表) -->
  171. <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
  172. <el-form ref="form" :model="form" :rules="rules" label-width="100px">
  173. {%- for column in table.columns %}
  174. {%- if column.is_insert %}
  175. <el-form-item label="{{ column.column_comment }}" prop="{{ underscore(column.java_field) }}">
  176. {%- if column.html_type == 'input' %}
  177. <el-input v-model="form.{{ underscore(column.java_field) }}" placeholder="请输入{{ column.column_comment }}" />
  178. {%- elif column.html_type == 'textarea' %}
  179. <el-input v-model="form.{{ underscore(column.java_field) }}" type="textarea" placeholder="请输入内容" />
  180. {%- elif column.html_type == 'select' %}
  181. <el-select v-model="form.{{ underscore(column.java_field) }}" placeholder="请选择{{ column.column_comment }}">
  182. <el-option
  183. v-for="dict in {{ getColumnDictType(column) }}Options"
  184. :key="dict.value"
  185. :label="dict.label"
  186. :value="dict.value"
  187. ></el-option>
  188. </el-select>
  189. {%- elif column.html_type == 'radio' %}
  190. <el-radio-group v-model="form.{{ underscore(column.java_field) }}">
  191. <el-radio
  192. v-for="dict in {{ getColumnDictType(column) }}Options"
  193. :key="dict.value"
  194. :label="dict.value"
  195. >{{ dict.label }}</el-radio>
  196. </el-radio-group>
  197. {%- elif column.html_type == 'checkbox' %}
  198. <el-checkbox-group v-model="form.{{ underscore(column.java_field) }}">
  199. <el-checkbox
  200. v-for="dict in {{ getColumnDictType(column) }}Options"
  201. :key="dict.value"
  202. :label="dict.value"
  203. >{{ dict.label }}</el-checkbox>
  204. </el-checkbox-group>
  205. {%- elif column.html_type == 'datetime' %}
  206. <el-date-picker clearable
  207. v-model="form.{{ underscore(column.java_field) }}"
  208. type="date"
  209. value-format="yyyy-MM-dd"
  210. placeholder="选择{{ column.column_comment }}">
  211. </el-date-picker>
  212. {%- elif column.html_type == 'imageUpload' %}
  213. <image-upload v-model="form.{{ underscore(column.java_field) }}"/>
  214. {%- elif column.html_type == 'fileUpload' %}
  215. <file-upload v-model="form.{{ underscore(column.java_field) }}"/>
  216. {%- endif %}
  217. </el-form-item>
  218. {%- endif %}
  219. {%- endfor %}
  220. <!-- 子表区域 -->
  221. <div style="margin-top: 10px; font-weight: bold;">子表</div>
  222. <div style="margin-bottom: 10px; color: #909399;">
  223. 主表主键将通过 <code>{{ table.sub_table_fk_name if table.sub_table_fk_name else 'parent_id' }}</code> 作为外键写入子表。
  224. </div>
  225. <el-button type="primary" icon="el-icon-plus" size="mini" @click="addSubRow">新增子项</el-button>
  226. <el-table :data="subRows" border style="width: 100%; margin-top: 10px;">
  227. <el-table-column label="#" width="50">
  228. <template slot-scope="scope">{{ "{{ scope.$index + 1 }}" }}</template>
  229. </el-table-column>
  230. <el-table-column label="键" width="200">
  231. <template slot-scope="scope">
  232. <el-input v-model="scope.row.key" placeholder="键" />
  233. </template>
  234. </el-table-column>
  235. <el-table-column label="值">
  236. <template slot-scope="scope">
  237. <el-input v-model="scope.row.value" placeholder="值" />
  238. </template>
  239. </el-table-column>
  240. <el-table-column label="操作" width="100">
  241. <template slot-scope="scope">
  242. <el-button type="text" size="mini" @click="removeSubRow(scope.$index)">删除</el-button>
  243. </template>
  244. </el-table-column>
  245. </el-table>
  246. </el-form>
  247. <div slot="footer" class="dialog-footer">
  248. <el-button type="primary" @click="submitForm">确 定</el-button>
  249. <el-button @click="cancel">取 消</el-button>
  250. </div>
  251. </el-dialog>
  252. </div>
  253. </template>
  254. <script>
  255. {# 使用后端工具函数生成接口前缀:
  256. - apiName : scheduleInfo
  257. - ApiName : ScheduleInfo(listScheduleInfo / getScheduleInfo 等) #}
  258. {% set apiName = to_camel_case(table.business_name, False) %}
  259. {% set ApiName = capitalize_first(apiName) %}
  260. import {
  261. list{{ ApiName }},
  262. get{{ ApiName }},
  263. del{{ ApiName }},
  264. add{{ ApiName }},
  265. update{{ ApiName }}
  266. } from "@/api/{{ table.module_name }}/{{ table.business_name }}";
  267. import { getToken } from "@/utils/auth";
  268. export default {
  269. name: "{{ ApiName }}",
  270. data() {
  271. return {
  272. // 遮罩层
  273. loading: true,
  274. // 选中数组
  275. ids: [],
  276. // 非单个禁用
  277. single: true,
  278. // 非多个禁用
  279. multiple: true,
  280. // 显示搜索条件
  281. showSearch: true,
  282. // 总条数
  283. total: 0,
  284. // {{ table.function_name }}表格数据
  285. {{ table.business_name }}List: [],
  286. // 表格列信息
  287. columns: [
  288. {%- for column in table.columns %}
  289. {%- if column.is_list %}
  290. { key: {{ column.list_index }}, label: '{{ column.column_comment }}', visible: true }{% if not loop.last %},{% endif %}
  291. {%- endif %}
  292. {%- endfor %}
  293. ],
  294. {%- for column in table.columns %}
  295. {%- if column.dict_type != '' %}
  296. // {{ column.column_comment }}字典
  297. {{ getColumnDictType(column) }}Options: [],
  298. {%- endif %}
  299. {%- endfor %}
  300. // 弹出层标题
  301. title: "",
  302. // 是否显示弹出层
  303. open: false,
  304. // 导入相关参数
  305. upload: {
  306. // 是否显示弹出层
  307. open: false,
  308. // 弹出层标题
  309. title: "",
  310. // 是否禁用上传
  311. isUploading: false,
  312. // 是否更新已经存在的数据
  313. updateSupport: 0,
  314. // 设置上传的请求头部
  315. headers: { Authorization: "Bearer " + getToken() },
  316. // 上传的地址
  317. url: process.env.VUE_APP_BASE_API + "/{{ table.module_name }}/{{ table.business_name }}/importData"
  318. },
  319. {%- for column in table.columns %}
  320. {%- if column.html_type == 'datetime' %}
  321. // {{ column.column_comment }}时间范围
  322. daterange{{ column.java_field }}: [],
  323. {%- endif %}
  324. {%- endfor %}
  325. // 查询参数(使用后端字段名,下划线风格)
  326. queryParams: {
  327. pageNum: 1,
  328. pageSize: 10,
  329. {%- for column in table.columns %}
  330. {%- if column.is_query %}
  331. {{ underscore(column.java_field) }}: null{% if not loop.last %},{% endif %}
  332. {%- endif %}
  333. {%- endfor %}
  334. },
  335. // 表单参数(提交前再统一处理需要转换的字段)
  336. form: {},
  337. // 子表行(通用 key/value 示例,可按需在后端解析)
  338. subRows: [],
  339. // 表单校验(同样用后端字段名,下划线)
  340. rules: {
  341. {%- for column in table.columns %}
  342. {%- if column.is_required %}
  343. {{ underscore(column.java_field) }}: [
  344. { required: true, message: "{{ column.column_comment }}不能为空", trigger: "blur" }
  345. ]{% if not loop.last %},{% endif %}
  346. {%- endif %}
  347. {%- endfor %}
  348. }
  349. };
  350. },
  351. created() {
  352. {%- set dictExists = false %}
  353. {%- for column in table.columns %}
  354. {%- if column.dict_type != '' %}
  355. {%- if not dictExists %}
  356. this.getDicts("{{ getColumnDictType(column) }}").then(response => {
  357. this.{{ getColumnDictType(column) }}Options = response.data;
  358. });
  359. {%- set dictExists = true %}
  360. {%- endif %}
  361. {%- endif %}
  362. {%- endfor %}
  363. this.getList();
  364. },
  365. methods: {
  366. /** 查询{{ table.function_name }}列表 */
  367. getList() {
  368. this.loading = true;
  369. {%- set has_datetime = false %}
  370. {%- for column in table.columns %}
  371. {%- if column.html_type == 'datetime' %}
  372. {%- if not has_datetime %}
  373. this.queryParams.params = {};
  374. {%- set has_datetime = true %}
  375. {%- endif %}
  376. if (null != this.daterange{{ column.java_field }} && '' != this.daterange{{ column.java_field }}.toString()) {
  377. this.queryParams.params["begin{{ column.java_field }}"] = this.daterange{{ column.java_field }}[0];
  378. this.queryParams.params["end{{ column.java_field }}"] = this.daterange{{ column.java_field }}[1];
  379. }
  380. {%- endif %}
  381. {%- endfor %}
  382. list{{ ApiName }}(this.queryParams).then(response => {
  383. const rows = Array.isArray(response && response.rows) ? response.rows : (Array.isArray(response && response.data) ? response.data : []);
  384. this.{{ table.business_name }}List = rows;
  385. this.total = response.total || rows.length;
  386. this.loading = false;
  387. });
  388. },
  389. {%- for column in table.columns %}
  390. {%- if column.dict_type != '' %}
  391. // {{ column.column_comment }}字典翻译
  392. dict_{{ underscore(column.java_field) }}_format(row, column) {
  393. return this.selectDictLabel(this.{{ getColumnDictType(column) }}Options, row.{{ underscore(column.java_field) }});
  394. },
  395. {%- endif %}
  396. {%- endfor %}
  397. // 多选框选中数据
  398. handleSelectionChange(selection) {
  399. this.ids = selection.map(item => item.{{ underscore(table.pk_column.java_field) if table.pk_column }})
  400. this.single = selection.length!==1
  401. this.multiple = !selection.length
  402. },
  403. /** 新增按钮操作 */
  404. handleAdd() {
  405. this.reset();
  406. this.open = true;
  407. this.title = "添加{{ table.function_name }}";
  408. },
  409. /** 修改按钮操作 */
  410. handleUpdate(row) {
  411. this.reset();
  412. const {{ underscore(table.pk_column.java_field) if table.pk_column }} = row.{{ underscore(table.pk_column.java_field) if table.pk_column }} || this.ids
  413. get{{ ApiName }}({{ underscore(table.pk_column.java_field) if table.pk_column }}).then(response => {
  414. this.form = response.data || {};
  415. // 如果后端返回子表数据,尝试映射为本页面的 subRows(key/value),否则保持空
  416. const children = this.form.children || this.form.subRows || [];
  417. this.subRows = Array.isArray(children) ? children.map((it) => {
  418. if (it && typeof it === 'object' && ('key' in it || 'value' in it)) return { key: it.key || '', value: it.value || '' };
  419. // 兜底转字符串
  420. return { key: '', value: JSON.stringify(it) };
  421. }) : [];
  422. this.open = true;
  423. this.title = "修改{{ table.function_name }}";
  424. });
  425. },
  426. /** 子表:新增一行 */
  427. addSubRow() {
  428. this.subRows.push({ key: "", value: "" });
  429. },
  430. /** 子表:删除一行 */
  431. removeSubRow(index) {
  432. this.subRows.splice(index, 1);
  433. },
  434. // 表单重置
  435. reset() {
  436. this.form = {
  437. {%- for column in table.columns %}
  438. {{ underscore(column.java_field) }}: null{% if not loop.last %},{% endif %}
  439. {%- endfor %}
  440. };
  441. this.subRows = [];
  442. this.resetForm("form");
  443. },
  444. // 取消按钮
  445. cancel() {
  446. this.open = false;
  447. this.reset();
  448. },
  449. /** 搜索按钮操作 */
  450. handleQuery() {
  451. this.queryParams.pageNum = 1;
  452. this.getList();
  453. },
  454. /** 重置按钮操作 */
  455. resetQuery() {
  456. {%- for column in table.columns %}
  457. {%- if column.html_type == 'datetime' %}
  458. this.daterange{{ column.java_field }} = [];
  459. {%- endif %}
  460. {%- endfor %}
  461. this.resetForm("queryForm");
  462. this.handleQuery();
  463. },
  464. /** 导出按钮操作 */
  465. handleExport() {
  466. this.download(
  467. "{{ table.module_name }}/{{ table.business_name }}/export",
  468. { ...this.queryParams },
  469. "{{ table.business_name }}_" + new Date().getTime() + ".xlsx"
  470. );
  471. },
  472. /** 导入按钮操作 */
  473. handleImport() {
  474. this.upload.title = "{{ table.function_name }}导入";
  475. this.upload.open = true;
  476. },
  477. /** 下载模板操作 */
  478. importTemplate() {
  479. this.download(
  480. "{{ table.module_name }}/{{ table.business_name }}/importTemplate",
  481. {},
  482. "{{ table.business_name }}_template_" + new Date().getTime() + ".xlsx"
  483. );
  484. },
  485. // 文件上传中处理
  486. handleFileUploadProgress() {
  487. this.upload.isUploading = true;
  488. },
  489. // 文件上传成功处理
  490. handleFileSuccess(response) {
  491. this.upload.open = false;
  492. this.upload.isUploading = false;
  493. this.$refs.upload.clearFiles();
  494. this.$alert(
  495. "<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" +
  496. response.msg +
  497. "</div>",
  498. "导入结果",
  499. { dangerouslyUseHTMLString: true }
  500. );
  501. this.$modal.closeLoading()
  502. this.getList();
  503. },
  504. // 提交上传文件
  505. submitFileForm() {
  506. this.$modal.loading("导入中请稍后")
  507. this.$refs.upload.submit();
  508. },
  509. /** 提交按钮 */
  510. submitForm() {
  511. // 将子表行附加到主表提交数据中,示例:挂载到 children 字段
  512. const payload = Object.assign({}, this.form, {
  513. // 后端可按需解析:children 或 subRows
  514. children: this.subRows.map(r => ({ key: r.key, value: r.value })),
  515. subRows: this.subRows.map(r => ({ key: r.key, value: r.value })),
  516. // 写入子表外键字段(若后端按需使用)
  517. {{ table.sub_table_fk_name if table.sub_table_fk_name else 'parent_id' }}: this.form.{{ underscore(table.pk_column.java_field) if table.pk_column }}
  518. });
  519. this.$refs["form"].validate(valid => {
  520. if (valid) {
  521. if (this.form.{{ underscore(table.pk_column.java_field) if table.pk_column }} != null) {
  522. update{{ ApiName }}(payload).then(response => {
  523. this.$modal.msgSuccess("修改成功");
  524. this.open = false;
  525. this.getList();
  526. });
  527. } else {
  528. add{{ ApiName }}(payload).then(response => {
  529. this.$modal.msgSuccess("新增成功");
  530. this.open = false;
  531. this.getList();
  532. });
  533. }
  534. }
  535. });
  536. },
  537. /** 删除按钮操作 */
  538. handleDelete(row) {
  539. const {{ table.business_name }}Ids = row.{{ underscore(table.pk_column.java_field) if table.pk_column }} || this.ids;
  540. this.$modal.confirm('是否确认删除{{ table.function_name }}编号为"' + {{ table.business_name }}Ids + '"的数据项?').then(function() {
  541. return del{{ ApiName }}({{ table.business_name }}Ids);
  542. }).then(() => {
  543. this.getList();
  544. this.$modal.msgSuccess("删除成功");
  545. }).catch(() => {});
  546. }
  547. }
  548. };
  549. </script>