index.vue 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339
  1. <template>
  2. <div class="app-container">
  3. <el-row :gutter="20">
  4. <el-col :span="18">
  5. <el-card shadow="always" :body-style="{ padding: '20px' }">
  6. <template #header>
  7. <div>
  8. <span>字段编辑</span>
  9. </div>
  10. </template>
  11. <!-- <Queryfrom
  12. :form-vals="checkedWhere"
  13. @getList="getList"
  14. ref="mychild"
  15. /> -->
  16. <el-select
  17. v-model="tableName"
  18. placeholder="请选择数据表"
  19. filterable
  20. @change="getList"
  21. class="mb10"
  22. >
  23. <el-option
  24. v-for="item in tableList"
  25. :key="item.tableName"
  26. :label="item.tableComment"
  27. :value="item.tableName"
  28. >
  29. <span style="float: left">{{ item.tableComment }}</span>
  30. <span style="float: right; color: #8492a6; font-size: 13px">{{
  31. item.tableName
  32. }}</span>
  33. </el-option>
  34. </el-select>
  35. <!-- <el-form
  36. :model="tableFieldList"
  37. ref="tableForm"
  38. :rules="rules"
  39. :inline="false"
  40. size="normal"
  41. > -->
  42. <el-table
  43. :data="tableFieldList"
  44. border
  45. ref="dragTable"
  46. row-key="id"
  47. :max-height="tableHeight"
  48. >
  49. <!--
  50. :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
  51. -->
  52. >
  53. <el-table-column
  54. type="index"
  55. label="序号"
  56. width="50"
  57. class-name="allowDrag"
  58. >
  59. </el-table-column>
  60. <el-table-column prop="fieldName" label="数据字段">
  61. </el-table-column>
  62. <el-table-column prop="fieldDescription" label="字段描述">
  63. <template slot-scope="scope">
  64. <!-- <el-form-item size="normal" prop="fieldDescription"> -->
  65. <el-input v-model="scope.row.fieldDescription"></el-input>
  66. <!-- </el-form-item> -->
  67. </template>
  68. </el-table-column>
  69. <el-table-column prop="relationTable" label="关联表">
  70. <template slot-scope="scope">
  71. <el-select
  72. v-model="scope.row.relationTable"
  73. :disabled="
  74. !scope.row.relationTableList ||
  75. !scope.row.relationTableList.length
  76. "
  77. placeholder="请选择关联的表"
  78. clearable
  79. filterable
  80. @change="ralationTableChange(scope.row)"
  81. >
  82. <el-option
  83. v-for="item in scope.row.relationTableList"
  84. :key="item.tableName"
  85. :label="item.tableComment"
  86. :value="item.tableName"
  87. >
  88. <span style="float: left">{{ item.tableComment }}</span>
  89. <span
  90. style="float: right; color: #8492a6; font-size: 13px"
  91. >{{ item.tableName }}</span
  92. >
  93. </el-option>
  94. </el-select>
  95. </template>
  96. </el-table-column>
  97. <el-table-column prop="relationFieldName" label="关联条件字段">
  98. <template slot-scope="scope">
  99. <el-select
  100. v-model="scope.row.relationFieldName"
  101. @change="relationFieldChange(scope.row)"
  102. :disabled="!scope.row.disableRelaFieldName"
  103. placeholder="关联字段"
  104. filterable
  105. >
  106. <el-option
  107. v-for="item in scope.row.relaFieldNameList"
  108. :key="item.fieldName"
  109. :label="item.fieldDescription"
  110. :value="item.fieldName"
  111. >
  112. </el-option>
  113. </el-select>
  114. </template>
  115. </el-table-column>
  116. <el-table-column prop="relationType" label="关联方式">
  117. <template slot-scope="scope">
  118. <el-select
  119. v-model="scope.row.relationType"
  120. placeholder="请选择关联方式"
  121. :disabled="!scope.row.disableRelaType"
  122. @change="relationTypeChangeHandler(scope.row)"
  123. filterable
  124. >
  125. <el-option
  126. v-for="item in relaTypeList"
  127. :key="item.value"
  128. :label="item.label"
  129. :value="item.value"
  130. >
  131. </el-option>
  132. </el-select>
  133. </template>
  134. </el-table-column>
  135. <!-- <el-table-column prop="relationShowField" label="关联显示字段">
  136. <template slot-scope="scope">
  137. <el-select
  138. v-model="scope.row.relationShowField"
  139. :disabled="!scope.row.disableRelaFieldName"
  140. placeholder="请选择"
  141. clearable
  142. filterable
  143. multiple
  144. >
  145. <el-option
  146. v-for="item in scope.row.relaFieldNameList"
  147. :key="item.fieldName"
  148. :label="item.fieldDescription"
  149. :value="item.fieldName + ' ' + item.fieldDescription"
  150. >
  151. </el-option>
  152. </el-select>
  153. </template>
  154. </el-table-column> -->
  155. <el-table-column prop="isShow" label="是否显示">
  156. <template slot-scope="scope">
  157. <el-switch v-model="scope.row.isShow"> </el-switch>
  158. </template>
  159. </el-table-column>
  160. <el-table-column prop="isSearch" label="是否包含查询">
  161. <template slot-scope="scope">
  162. <el-switch v-model="scope.row.isSearch"> </el-switch>
  163. </template>
  164. </el-table-column>
  165. <el-table-column prop="isExport" label="是否导出">
  166. <template slot-scope="scope">
  167. <el-switch v-model="scope.row.isExport"> </el-switch>
  168. </template>
  169. </el-table-column>
  170. <!-- <el-table-column
  171. label="操作"
  172. align="center"
  173. class-name="small-padding fixed-width"
  174. >
  175. <template slot-scope="scope">
  176. <el-button
  177. size="mini"
  178. type="text"
  179. icon="el-icon-edit"
  180. @click="handleUpdate(scope.row)"
  181. >修改
  182. </el-button>
  183. <el-button
  184. size="mini"
  185. type="text"
  186. icon="el-icon-delete"
  187. @click="handleDelete(scope.row)"
  188. >删除
  189. </el-button>
  190. </template>
  191. </el-table-column> -->
  192. </el-table>
  193. <!-- </el-form> -->
  194. </el-card>
  195. </el-col>
  196. <el-col :span="6">
  197. <el-card shadow="always" :body-style="{ padding: '5px' }">
  198. <template #header>
  199. <div>
  200. <span>菜单编辑</span>
  201. </div>
  202. </template>
  203. <el-form
  204. ref="formData"
  205. :model="formData"
  206. label-width="100px"
  207. :rules="rules"
  208. >
  209. <el-form-item label="菜单名称" prop="menuName">
  210. <el-input v-model="formData.menuName"></el-input>
  211. </el-form-item>
  212. <el-form-item label="菜单路由" prop="routePath">
  213. <!-- <el-select v-model="formData.routePath" placeholder="请选择路由">
  214. <el-option label="区域一" value="shanghai"></el-option>
  215. <el-option label="区域二" value="beijing"></el-option>
  216. </el-select> -->
  217. <treeselect
  218. :append-to-body="true"
  219. v-model="formData.routePath"
  220. :options="menus"
  221. :normalizer="normalizer"
  222. :show-count="true"
  223. placeholder="请选择父级路由"
  224. />
  225. </el-form-item>
  226. <el-form-item label="是否显示列表" v-if="false">
  227. <el-switch v-model="formData.isShowList"></el-switch>
  228. </el-form-item>
  229. <el-form-item label="时间格式" prop="timeFormate">
  230. <el-select
  231. v-model="formData.timeFormate"
  232. placeholder="请选择时间格式"
  233. >
  234. <el-option
  235. v-for="val in dict.type.sys_time_format"
  236. :key="val.value"
  237. :label="val.label"
  238. :value="val.value"
  239. ></el-option>
  240. </el-select>
  241. </el-form-item>
  242. <el-form-item label="表格主键" prop="primaryKey">
  243. <el-select
  244. v-model="formData.primaryKey"
  245. placeholder="请选择表格主键"
  246. >
  247. <el-option
  248. v-for="val in primaryKeyList"
  249. :key="val.fieldName"
  250. :label="val.fieldDescription"
  251. :value="val.fieldName"
  252. ></el-option>
  253. </el-select>
  254. </el-form-item>
  255. <el-form-item label="排序依赖字段">
  256. <el-select
  257. v-model="formData.orderByColumn"
  258. placeholder="请选择排序依赖"
  259. >
  260. <el-option
  261. v-for="val in orderByFieldList"
  262. :key="val.fieldName"
  263. :label="val.fieldDescription"
  264. :value="val.tableName + '.' + val.fieldName"
  265. ></el-option>
  266. </el-select>
  267. </el-form-item>
  268. <el-form-item label="排序方式" v-show="formData.orderByColumn">
  269. <el-radio v-model="formData.isAsc" label="ASC">升序</el-radio>
  270. <el-radio v-model="formData.isAsc" label="DESC">降序</el-radio>
  271. </el-form-item>
  272. <el-form-item>
  273. <el-button size="mini" @click="previewHandle">预览</el-button>
  274. <el-button size="mini" type="primary" @click="createHandle">{{
  275. tId ? "确认修改" : "确认创建"
  276. }}</el-button>
  277. </el-form-item>
  278. </el-form>
  279. </el-card>
  280. </el-col>
  281. </el-row>
  282. <el-dialog title="效果预览" :visible.sync="isShowPreview" width="50%">
  283. <el-row :gutter="10" class="mb8">
  284. <el-col :span="18">
  285. <el-input placeholder="请输入" clearable />
  286. </el-col>
  287. <el-col :span="6">
  288. <el-button
  289. type="primary"
  290. icon="el-icon-search"
  291. size="small"
  292. @click="() => {}"
  293. >搜索</el-button
  294. >
  295. <el-button icon="el-icon-refresh" size="small" @click="() => {}"
  296. >重置</el-button
  297. >
  298. </el-col>
  299. </el-row>
  300. <el-row :gutter="10" class="mb8">
  301. <el-col :span="1.5">
  302. <el-button
  303. type="primary"
  304. plain
  305. icon="el-icon-plus"
  306. size="mini"
  307. @click="() => {}"
  308. >新增
  309. </el-button>
  310. </el-col>
  311. <el-col :span="1.5">
  312. <el-button
  313. type="success"
  314. plain
  315. icon="el-icon-edit"
  316. size="mini"
  317. :disabled="false"
  318. @click="() => {}"
  319. >修改
  320. </el-button>
  321. </el-col>
  322. <el-col :span="1.5">
  323. <el-button
  324. type="danger"
  325. plain
  326. icon="el-icon-delete"
  327. size="mini"
  328. :disabled="false"
  329. @click="() => {}"
  330. >删除
  331. </el-button>
  332. </el-col>
  333. <el-col :span="1.5">
  334. <el-button plain icon="el-icon-upload2" size="mini" @click="() => {}"
  335. >导入
  336. </el-button>
  337. </el-col>
  338. <el-col :span="1.5">
  339. <el-button
  340. type="warning"
  341. plain
  342. icon="el-icon-download"
  343. size="mini"
  344. @click="() => {}"
  345. >导出
  346. </el-button>
  347. </el-col>
  348. <!-- <right-toolbar
  349. showSearch.sync="true"
  350. @queryTable="() => {}"
  351. ></right-toolbar> -->
  352. </el-row>
  353. <el-table :data="tableDataList" @selection-change="() => {}">
  354. <el-table-column
  355. type="selection"
  356. width="55"
  357. align="center"
  358. ></el-table-column>
  359. <!-- <span v-for="(key, val) in columns" :key="key">
  360. <el-table-column :label="key" align="center" :prop="val" />
  361. </span> -->
  362. <el-table-column
  363. v-for="item in columns"
  364. :key="item.key"
  365. :label="item.value"
  366. align="center"
  367. :prop="toUpperCase(item.key)"
  368. />
  369. <el-table-column
  370. label="操作"
  371. align="center"
  372. class-name="small-padding fixed-width"
  373. >
  374. <!-- <template slot-scope="scope"> -->
  375. <template>
  376. <!-- <el-button
  377. size="mini"
  378. type="text"
  379. icon="el-icon-edit"
  380. @click="() => {}"
  381. >修改
  382. </el-button>
  383. <el-button
  384. size="mini"
  385. type="text"
  386. icon="el-icon-delete"
  387. @click="() => {}"
  388. >删除
  389. </el-button> -->
  390. <el-dropdown>
  391. <el-button type="warning" plain size="small">
  392. 处理<i class="el-icon-arrow-down el-icon--right"></i>
  393. </el-button>
  394. <el-dropdown-menu slot="dropdown">
  395. <el-dropdown-item
  396. ><el-button
  397. size="mini"
  398. type="text"
  399. icon="el-icon-edit"
  400. @click="() => {}"
  401. >修改</el-button
  402. ></el-dropdown-item
  403. >
  404. <el-dropdown-item
  405. ><el-button
  406. size="mini"
  407. type="text"
  408. icon="el-icon-delete"
  409. @click="() => {}"
  410. >删除</el-button
  411. >
  412. </el-dropdown-item>
  413. </el-dropdown-menu>
  414. </el-dropdown>
  415. </template>
  416. </el-table-column>
  417. </el-table>
  418. <pagination
  419. v-show="total > 0"
  420. :total="total"
  421. :page.sync="queryParams.pageNum"
  422. :limit.sync="queryParams.pageSize"
  423. @pagination="pageList"
  424. />
  425. <template #footer>
  426. <span>
  427. <el-button @click="isShowPreview = false">关闭</el-button>
  428. <el-button type="primary" @click="createHandle">{{
  429. tId ? "确认修改" : "确认创建"
  430. }}</el-button>
  431. </span>
  432. </template>
  433. </el-dialog>
  434. </div>
  435. </template>
  436. <script>
  437. import {
  438. delTableData,
  439. dragTableInfo,
  440. listTable,
  441. unionListTableData,
  442. getInfoBySqlKey,
  443. addTableData,
  444. } from "@/api/tablelist/commonTable";
  445. import { getDicts } from "@/api/system/dict/data";
  446. import {
  447. getFormName,
  448. getListName,
  449. dragTablePreview,
  450. } from "@/api/dragform/form.js";
  451. import { getTableInfo, editTable } from "@/api/system/table.js";
  452. import { addDragTable } from "@/api/tablelist/commonTable.js";
  453. import { getParticMenu, updateMenu } from "@/api/system/menu.js";
  454. import { getMenuList, addMenu } from "@/api/menu.js";
  455. import Queryfrom from "@/views/tablelist/commonTable/queryfrom.vue";
  456. import { mapState } from "vuex";
  457. import Sortable from "sortablejs";
  458. import Treeselect from "@riophae/vue-treeselect";
  459. import "@riophae/vue-treeselect/dist/vue-treeselect.css";
  460. import { v4 as uuidv4 } from "uuid";
  461. export default {
  462. name: "tableMange",
  463. dicts: ["sys_time_format"],
  464. props: [],
  465. components: { Queryfrom, Treeselect },
  466. data() {
  467. return {
  468. // 修改表格时的menuId
  469. menuId: "",
  470. // 修改表格的id
  471. tId: "",
  472. // 当前模板信息
  473. templateInfo: {},
  474. // 排序方式 默认降序
  475. sortState: true,
  476. // 列信息
  477. columns: {},
  478. // 查询条件
  479. queryFromWhere: {},
  480. // 当前table唯一标识
  481. sqlkey: "",
  482. // 表格的高度
  483. tableHeight: document.documentElement.scrollHeight - 245 + "px",
  484. tableList: [], //所有表格列表
  485. tableFieldList: [], // 当前表格字段数据
  486. tableName: "", // 当前表名称
  487. relationTableList: [], //关联表格列表
  488. relationFieldList: [], //关联字段列表
  489. relationTypeList: [], //关联方式列表
  490. total: 0, //表格数据总数
  491. relaTypeList: [
  492. {
  493. label: "等值连接",
  494. value: "INNER JOIN",
  495. },
  496. {
  497. label: "左连接",
  498. value: "LEFT JOIN",
  499. },
  500. {
  501. label: "右连接",
  502. value: "RIGHT JOIN",
  503. },
  504. ],
  505. headerList: [
  506. {
  507. prop: "fieldName",
  508. label: "数据字段",
  509. },
  510. {
  511. prop: "fieldDescription",
  512. label: "字段描述",
  513. },
  514. {
  515. prop: "relationTable",
  516. label: "关联表",
  517. },
  518. {
  519. prop: "relationFieldName",
  520. label: "关联字段",
  521. },
  522. {
  523. prop: "relationType",
  524. label: "关联方式",
  525. },
  526. {
  527. prop: "isShow",
  528. label: "是否显示",
  529. },
  530. {
  531. prop: "isSearch",
  532. label: "是否包含查询",
  533. },
  534. {
  535. prop: "isExport",
  536. label: "是否导出",
  537. },
  538. ], //表头列表
  539. // 右侧筛选条件勾选数据
  540. // checkedList: [], //勾选的查询框
  541. // checkedWhere:[],//需要渲染的查询表单
  542. // 右侧表单编辑
  543. formData: {
  544. menuName: "",
  545. routePath: undefined,
  546. isShowList: true,
  547. timeFormate: "",
  548. orderByColumn: "",
  549. isAsc: "ASC",
  550. primaryKey: "",
  551. },
  552. rules: {
  553. menuName: [
  554. { required: true, message: "请输入菜单名称", trigger: "blur" },
  555. ],
  556. routePath: [
  557. { required: true, message: "请选择路由", trigger: "change" },
  558. ],
  559. timeFormate: [
  560. { required: true, message: "请选择时间格式", trigger: "change" },
  561. ],
  562. primaryKey: [
  563. { required: true, message: "请选择表格主键", trigger: "blur" },
  564. ],
  565. fieldDescription: [
  566. { required: true, message: "请输入字段描述", trigger: "blur" },
  567. ],
  568. },
  569. menus: [], //路由列表数据
  570. // 预览弹窗相关
  571. isShowPreview: false, //弹窗显示与隐藏
  572. queryParams: {
  573. pageNum: 1,
  574. pageSize: 10,
  575. orderByColumn: "",
  576. isAsc: "DESC",
  577. basicMap: {
  578. sql: "",
  579. },
  580. },
  581. loading: false, //表格加载
  582. tableDataList: [], //数据
  583. editData: {},
  584. };
  585. },
  586. computed: {
  587. ...mapState({
  588. databaseName: (state) => state.user.dataSource.databaseName,
  589. databaseType: (state) => state.user.dataSource.databaseType,
  590. username: (state) => state.user.dataSource.username,
  591. }),
  592. searchArr() {
  593. if (!this.templateInfo?.where?.length) return [];
  594. return this.templateInfo.where.map((item) => item.conditionName);
  595. },
  596. checkedWhere() {
  597. if (!this.templateInfo?.where?.length) return [];
  598. return this.templateInfo?.where.filter((item) => {
  599. return this.checkedList.includes(item.conditionName);
  600. });
  601. },
  602. orderByFieldList() {
  603. // return this.tableFieldList.filter((item) => item.isShow);
  604. return this.tableFieldList;
  605. },
  606. primaryKeyList() {
  607. return this.tableFieldList.filter((item) => !item.isChildren);
  608. },
  609. menuOrderNum() {
  610. if (!this.formData.routePath) return 0;
  611. let targetMenu = this.getTargetMenu(this.menus);
  612. if (targetMenu.children?.length) {
  613. return targetMenu.children.length;
  614. } else {
  615. return 0;
  616. }
  617. },
  618. },
  619. watch: {
  620. tableName: function (val) {
  621. if (val) {
  622. this.relationTableList = this.tableList
  623. .filter((item) => item.tableName != val)
  624. .map((item) => {
  625. return {
  626. tableName: item.tableName,
  627. tableComment: item.tableComment,
  628. };
  629. });
  630. } else {
  631. this.relationTableList = [];
  632. }
  633. },
  634. },
  635. methods: {
  636. // 获取目标menu
  637. getTargetMenu(menus) {
  638. for (let i = 0; i < menus.length; i++) {
  639. if (menus[i].menuId == this.formData.routePath) {
  640. return menus[i];
  641. } else if (menus.children?.length) {
  642. return this.getTargetMenu(menus.children);
  643. }
  644. }
  645. return false;
  646. },
  647. /** 查询列表 */
  648. getList(queryParams) {
  649. if (!this.tableName) return;
  650. let data = {
  651. databaseName: this.databaseName,
  652. databaseType: this.databaseType,
  653. tableName: this.tableName,
  654. };
  655. // 获取当前表单结构信息
  656. getListName(data).then((res) => {
  657. this.tableFieldList = res.map((item, index) => {
  658. return {
  659. id: item.fieldName,
  660. fieldName: item.fieldName,
  661. fieldDescription: item.fieldDescription,
  662. relationTable: "",
  663. relationFieldName: "",
  664. relaFieldNameList: [],
  665. disableRelaFieldName: false,
  666. relationType: "",
  667. relationShowField: [],
  668. relationShowFiledList: [],
  669. disableRelaType: false,
  670. isShow: true,
  671. isSearch: false,
  672. isExport: true,
  673. relationTableList: this.relationTableList,
  674. tableName: this.tableName,
  675. relationFieldList: [],
  676. };
  677. });
  678. });
  679. },
  680. // 关联表变化回调
  681. async ralationTableChange(row) {
  682. this.tableFieldList = this.tableFieldList.filter((item) => {
  683. return !row.relationFieldList.some((val) => {
  684. return val.id == item.id;
  685. });
  686. });
  687. row.relationFieldName = "";
  688. row.relationType = "";
  689. row.disableRelaFieldName = false;
  690. row.disableRelaType = false;
  691. row.relationFieldList = [];
  692. if (!row.relationTable) {
  693. return;
  694. }
  695. // 获取关联表的字段
  696. let data = {
  697. databaseName: this.databaseName,
  698. databaseType: this.databaseType,
  699. tableName: row.relationTable,
  700. };
  701. let res = await getListName(data);
  702. // 关联字段下拉列表数据
  703. row.relaFieldNameList = res.map((item) => {
  704. return {
  705. fieldName: item.fieldName,
  706. fieldDescription: item.fieldDescription,
  707. };
  708. });
  709. let relationTableList = row.relationTableList.filter(
  710. (item) => row.relationTable != item.tableName
  711. );
  712. row.relationFieldList = row.relaFieldNameList.map((item, index) => {
  713. return {
  714. id: row.relationTable + " " + item.fieldName,
  715. fieldName: item.fieldName,
  716. fieldDescription: item.fieldDescription,
  717. relationTable: "",
  718. relationFieldName: "",
  719. relaFieldNameList: [],
  720. disableRelaFieldName: false,
  721. relationType: "",
  722. relationShowField: [],
  723. relationShowFiledList: [],
  724. disableRelaType: false,
  725. isShow: true,
  726. isSearch: false,
  727. isExport: true,
  728. relationTableList,
  729. tableName: row.relationTable,
  730. relationFieldList: [],
  731. isChildren: true,
  732. };
  733. });
  734. // this.tableFieldList = [...this.tableFieldList, ...row.relationFieldList];
  735. row.disableRelaFieldName = true;
  736. },
  737. // 关联类型变化回调
  738. relationTypeChangeHandler(row) {
  739. let tempRelationFieldList = row.relationFieldList.filter((item) => {
  740. return !this.tableFieldList.find((val) => val.id === item.id);
  741. });
  742. this.tableFieldList = [...this.tableFieldList, ...tempRelationFieldList];
  743. },
  744. // 关联字段回调
  745. relationFieldChange(row) {
  746. if (!row.relationFieldName) {
  747. row.relationType = "";
  748. row.disableRelaType = false;
  749. row.relaFieldNameList = [];
  750. return;
  751. }
  752. row.disableRelaType = true;
  753. },
  754. // 勾选框回调
  755. handleSelectionChange(selection) {
  756. this.ids = selection.map((item) => item.id);
  757. // this.single = selection.length != 1;
  758. this.multiple = !selection.length;
  759. },
  760. // 分页查询
  761. pageList(row) {
  762. this.$refs.mychild.pageList(
  763. row == undefined
  764. ? {
  765. limit: this.queryParams.pageSize,
  766. page: this.queryParams.pageNum,
  767. }
  768. : row
  769. );
  770. },
  771. // 多选框改变回调
  772. checkedChangeHandle(val) {},
  773. // 获取所有表格
  774. async getAllTable() {
  775. let data = {
  776. databaseName: this.databaseName,
  777. databaseType: this.databaseType,
  778. };
  779. let res = await getFormName(data);
  780. const baseTable = await this.getDicts("base_table");
  781. this.tableList = res.data.filter((item) => {
  782. return !baseTable.data.some(
  783. (value) =>
  784. value.dictValue.toLowerCase() == item.tableName.toLowerCase()
  785. );
  786. });
  787. },
  788. //处理表格行拖拽
  789. initDragTable() {
  790. const el = this.$refs.dragTable.$el.querySelectorAll(
  791. ".el-table__body-wrapper > table > tbody"
  792. )[0];
  793. const sortable = Sortable.create(el, {
  794. handle: ".allowDrag",
  795. onEnd: (evt) => {
  796. const targetRow = this.tableFieldList.splice(evt.oldIndex, 1)[0];
  797. this.tableFieldList.splice(evt.newIndex, 0, targetRow);
  798. for (let index in this.tableFieldList) {
  799. this.tableFieldList[index].sort = parseInt(index) + 1;
  800. }
  801. },
  802. });
  803. },
  804. /** 转换菜单数据结构 */
  805. normalizer(node) {
  806. if (node.children && !node.children.length) {
  807. delete node.children;
  808. }
  809. return {
  810. id: node.menuId,
  811. label: node.menuName,
  812. children: node.children,
  813. };
  814. },
  815. // 获取路由表单数据
  816. async getMenuList() {
  817. let res = await getMenuList();
  818. this.menus = this.handleTree(res.data, "menuId");
  819. },
  820. // 校验字段合法性(递归版)
  821. validateField(tableFieldList, validateParams) {
  822. if (!tableFieldList.length) {
  823. return;
  824. }
  825. for (let i = 0; i < tableFieldList.length; i++) {
  826. let temp = tableFieldList[i];
  827. if (!temp.fieldDescription?.trim() && temp.isShow) {
  828. //描述字段不能为空
  829. validateParams.isFieldDescrib = true;
  830. }
  831. if (
  832. temp.relationTable &&
  833. (!temp.relationFieldName || !temp.relationType)
  834. ) {
  835. // 关联条件不足
  836. validateParams.isRelationFieldAll = true;
  837. }
  838. if (temp.relationFieldList.length) {
  839. this.validateField(temp.relationFieldList, validateParams);
  840. }
  841. }
  842. },
  843. // 校验字段合法性(非递归版)
  844. validateTableData(tableFieldList) {
  845. if (!tableFieldList.length) {
  846. return {
  847. val: false,
  848. meg: "字段个数不能为空",
  849. };
  850. }
  851. for (let i = 0; i < tableFieldList.length; i++) {
  852. let temp = tableFieldList[i];
  853. if (!temp.fieldDescription?.trim() && temp.isShow) {
  854. return {
  855. val: false,
  856. msg: "显示的字段,字段描述不能为空",
  857. };
  858. }
  859. if (
  860. temp.relationTable &&
  861. (!temp.relationFieldName || !temp.relationType)
  862. ) {
  863. return {
  864. val: false,
  865. msg: "关联条件不足,请完善关联条件",
  866. };
  867. }
  868. }
  869. if (tableFieldList.filter((item) => item.isShow).length == 0) {
  870. return {
  871. val: false,
  872. msg: "显示的字段数不能为空",
  873. };
  874. }
  875. if (
  876. tableFieldList.filter((item) => item.isExport && !item.isChildren)
  877. .length == 0
  878. ) {
  879. return {
  880. val: false,
  881. msg: "导出字段中至少应有一个主表字段",
  882. };
  883. }
  884. // 导出的字段的描述不能重复
  885. let describeArr = tableFieldList
  886. .filter((item) => item.isExport)
  887. .map((item) => item.fieldDescription);
  888. let isRepeat = false;
  889. describeArr.map((item) => {
  890. if (describeArr.indexOf(item) != describeArr.lastIndexOf(item)) {
  891. isRepeat = true;
  892. }
  893. });
  894. if (isRepeat) {
  895. return {
  896. val: false,
  897. msg: "导出的字段描述不能重复",
  898. };
  899. }
  900. return {
  901. val: true,
  902. msg: "",
  903. };
  904. },
  905. // 递归拼接查询语句
  906. getSQLString(tableFieldList, fieldArr, tableArr, sqlType = "mysql") {
  907. let asOrSpace = sqlType == "oracle" ? " " : " AS ";
  908. for (let i = 0; i < tableFieldList.length; i++) {
  909. let temp = tableFieldList[i];
  910. if (temp.isShow) {
  911. let tempArr = temp.tableName + "." + temp.fieldName;
  912. if (temp.isChildren) {
  913. tempArr += asOrSpace + temp.tableName + "_" + temp.fieldName;
  914. }
  915. fieldArr.push(tempArr);
  916. }
  917. if (temp.relationTable && temp.relationFieldName && temp.relationType) {
  918. // fieldArr.push(temp.relationTable + "." + temp.relationFieldName);
  919. let isNeedUsername = sqlType == "oracle" ? this.username + "." : "";
  920. tableArr.push(
  921. temp.relationType +
  922. " " +
  923. isNeedUsername +
  924. temp.relationTable +
  925. asOrSpace +
  926. temp.relationTable +
  927. " ON " +
  928. temp.relationTable +
  929. "." +
  930. temp.relationFieldName +
  931. " = " +
  932. temp.tableName +
  933. "." +
  934. temp.fieldName
  935. );
  936. }
  937. // if (temp.relationFieldList.length) {
  938. // this.getSQLString(temp.relationFieldList, fieldArr, tableArr);
  939. // }
  940. }
  941. // 如果主键不包含显示,则添加至sql语句中
  942. let isNotInclude = this.primaryKeyList.find(
  943. (val) => !val.isShow && val.fieldName == this.formData.primaryKey
  944. );
  945. if (isNotInclude) {
  946. fieldArr.push(isNotInclude.tableName + "." + isNotInclude.fieldName);
  947. }
  948. },
  949. // 拼接查询sql语句
  950. getSQLStr() {
  951. let sqlType = this.databaseType; //数据库类型
  952. // let sqlType = "oracle";
  953. let sql = "";
  954. // mysql
  955. sql += "SELECT ";
  956. let fieldNameArr = [],
  957. relaTypeArr = [];
  958. this.getSQLString(
  959. this.tableFieldList,
  960. fieldNameArr,
  961. relaTypeArr,
  962. sqlType
  963. );
  964. // console.log("discriISEmpty:", discriISEmpty);
  965. // if (discriISEmpty) {
  966. // return false;
  967. // }
  968. let isNeedUsername = sqlType == "oracle" ? this.username + "." : "";
  969. let asOrSpace = sqlType == "oracle" ? " " : " AS ";
  970. sql +=
  971. fieldNameArr.join(",") +
  972. " FROM " +
  973. isNeedUsername +
  974. this.tableName +
  975. asOrSpace +
  976. this.tableName;
  977. if (relaTypeArr.length) {
  978. sql += " " + relaTypeArr.join(" ");
  979. }
  980. return sql;
  981. },
  982. // 处理列表信息
  983. columnsHandler(columns) {
  984. let resArr = [];
  985. columns.forEach((item) => {
  986. for (const key in item) {
  987. let tempObj = {};
  988. tempObj.key = key;
  989. tempObj.value = item[key];
  990. resArr.push(tempObj);
  991. }
  992. });
  993. return resArr;
  994. },
  995. // 下划线命名转驼峰命名
  996. toUpperCase(str) {
  997. let nstr = str.replace(/(?:_)+([^_])/g, function ($0, $1) {
  998. return $1.toUpperCase();
  999. });
  1000. nstr = nstr.replace(nstr[0], nstr[0].toLowerCase());
  1001. return nstr;
  1002. },
  1003. // 递归获取列表信息
  1004. getCol(
  1005. tableFieldList,
  1006. columns,
  1007. searchFieldList = [],
  1008. tableExportField = {}
  1009. ) {
  1010. if (!tableFieldList.length) return;
  1011. for (let i = 0; i < tableFieldList.length; i++) {
  1012. let temp = tableFieldList[i];
  1013. let tempFieldName = "",
  1014. exportFieldName = "";
  1015. if (temp.isChildren) {
  1016. tempFieldName = temp.tableName + "_" + temp.fieldName;
  1017. exportFieldName = temp.tableName + "@" + temp.fieldName;
  1018. } else {
  1019. tempFieldName = temp.fieldName;
  1020. exportFieldName = temp.fieldName;
  1021. }
  1022. if (temp.isShow) {
  1023. let tempObj = {};
  1024. // if (temp.isChildren) {
  1025. // tempObj[temp.tableName + "_" + temp.fieldName] =
  1026. // temp.fieldDescription;
  1027. // } else {
  1028. // tempObj[temp.fieldName] = temp.fieldDescription;
  1029. // }
  1030. tempObj[tempFieldName] = temp.fieldDescription;
  1031. columns.push(tempObj);
  1032. }
  1033. if (temp.isSearch) {
  1034. searchFieldList.push(temp.tableName + "." + temp.fieldName);
  1035. }
  1036. if (temp.isExport) {
  1037. tableExportField[exportFieldName] = temp.fieldDescription;
  1038. }
  1039. // 递归
  1040. // if (temp.relationFieldList) {
  1041. // this.getCol(
  1042. // temp.relationFieldList,
  1043. // columns,
  1044. // searchFieldList,
  1045. // tableExportField
  1046. // );
  1047. // }
  1048. }
  1049. },
  1050. // 更新路由
  1051. reloadRouter() {
  1052. this.$store.dispatch("GenerateRoutes").then((accessRoutes) => {
  1053. this.$router.addRoutes(accessRoutes); // 动态添加可访问路由表
  1054. });
  1055. },
  1056. // 预览结果回调
  1057. async previewHandle() {
  1058. this.$refs.formData.validate(async (valid) => {
  1059. // 至少包含一个查询项
  1060. if (valid) {
  1061. if (!this.tableName) {
  1062. this.$message.error("请选择数据表");
  1063. return;
  1064. }
  1065. // 检验表单合法性
  1066. let validRes = this.validateTableData(this.tableFieldList);
  1067. if (!validRes.val) {
  1068. this.$message.error(validRes.msg);
  1069. return;
  1070. }
  1071. // let validateParams = {
  1072. // isFieldDescrib: false,
  1073. // isRelationFieldAll: false,
  1074. // };
  1075. // this.validateField(this.tableFieldList, validateParams);
  1076. // console.log(validateParams);
  1077. // if (validateParams.isFieldDescrib) {
  1078. // this.$message.error("需要显示的字段描述不能为空");
  1079. // return;
  1080. // }
  1081. // if (validateParams.isRelationFieldAll) {
  1082. // this.$message.error("请补全关联条件");
  1083. // return;
  1084. // }
  1085. // 拼接预览的sql查询语句
  1086. this.queryParams.basicMap.sql = this.getSQLStr();
  1087. // 获取表头信息
  1088. let tempColumns = [],
  1089. searchFieldList = [];
  1090. this.getCol(this.tableFieldList, tempColumns, searchFieldList);
  1091. if (!searchFieldList.length) {
  1092. this.$message.warning("请至少选择一个包含查询字段");
  1093. return false;
  1094. }
  1095. this.columns = this.columnsHandler(tempColumns);
  1096. this.queryParams.isAsc = this.formData.isAsc;
  1097. // 发送请求获取预览数据
  1098. let res = await dragTablePreview(this.queryParams);
  1099. this.tableDataList = [];
  1100. res.rows.forEach((item) => {
  1101. this.tableDataList.push(item.resultMap);
  1102. });
  1103. this.total = res.total;
  1104. this.isShowPreview = true;
  1105. } else {
  1106. this.$message.warning("请完善表单");
  1107. return false;
  1108. }
  1109. });
  1110. // this.$refs.tableForm.validate((val) => {
  1111. // if (val) {
  1112. // } else {
  1113. // this.$message.warning("请完善表单");
  1114. // return false;
  1115. // }
  1116. // });
  1117. },
  1118. // 创建回调
  1119. async createHandle() {
  1120. this.$refs.formData.validate(async (valid) => {
  1121. if (valid) {
  1122. // 检验表单合法性
  1123. let validRes = this.validateTableData(this.tableFieldList);
  1124. if (!validRes.val) {
  1125. this.$message.error(validRes.msg);
  1126. return;
  1127. }
  1128. let columns = [],
  1129. searchFieldList = [],
  1130. tableExportField = {};
  1131. this.getCol(
  1132. this.tableFieldList,
  1133. columns,
  1134. searchFieldList,
  1135. tableExportField
  1136. );
  1137. let echoData = {
  1138. tableName: this.tableName,
  1139. tableFieldData: this.tableFieldList,
  1140. formData: this.formData,
  1141. };
  1142. if (!searchFieldList.length) {
  1143. this.$message.warning("请至少选择一个包含查询字段");
  1144. return false;
  1145. }
  1146. // let validateParams = {
  1147. // isFieldDescrib: false,
  1148. // isRelationFieldAll: false,
  1149. // };
  1150. // this.validateField(this.tableFieldList, validateParams);
  1151. // console.log(validateParams);
  1152. // if (validateParams.isFieldDescrib) {
  1153. // this.$message.error("需要显示的字段描述不能为空");
  1154. // return;
  1155. // }
  1156. // if (validateParams.isRelationFieldAll) {
  1157. // this.$message.error("请补全关联条件");
  1158. // return;
  1159. // }
  1160. let uuid = uuidv4();
  1161. // 表单
  1162. let result;
  1163. if (this.tId) {
  1164. let payLoad = {
  1165. menuId: this.menuId,
  1166. menuName: this.formData.menuName,
  1167. parentId: this.formData.routePath,
  1168. component: "tablelist/commonTable/listInfo",
  1169. icon: "",
  1170. isCache: "0",
  1171. isFrame: "1",
  1172. menuType: "C",
  1173. orderNum: this.menuOrderNum,
  1174. status: "0",
  1175. visible: "0",
  1176. };
  1177. result = await updateMenu(payLoad);
  1178. } else {
  1179. let sqlKeyObj = {
  1180. sqlkey: uuid,
  1181. };
  1182. let payLoad = {
  1183. component: "tablelist/commonTable/listInfo",
  1184. icon: "",
  1185. isCache: "0",
  1186. isFrame: "1",
  1187. menuName: this.formData.menuName,
  1188. menuType: "C",
  1189. orderNum: this.menuOrderNum,
  1190. parentId: this.formData.routePath,
  1191. path: uuid,
  1192. query: JSON.stringify(sqlKeyObj),
  1193. status: "0",
  1194. visible: "0",
  1195. };
  1196. result = await addMenu(payLoad);
  1197. }
  1198. if (result.code == 200) {
  1199. // 更新路由
  1200. this.reloadRouter();
  1201. // let isAsc = this.formData.isAsc == "ASC" ? 0 : 1;
  1202. let data = {
  1203. tId: this.tId,
  1204. dtName: this.formData.menuName,
  1205. // menuId: this.formData.routePath,
  1206. // tableKey: this.formData.routePath, // 暂定
  1207. dtTableName: this.tableName,
  1208. primaryKey: this.formData.primaryKey,
  1209. orderByColumn: this.formData.orderByColumn,
  1210. sortOrder: this.formData.isAsc,
  1211. sqlKey: uuid,
  1212. dtColumnName: columns, //列字段标题名称(存储显示字段信息
  1213. // dtColumnName: JSON.stringify(columns).replace(/"/g, "'"), //列字段标题名称(存储显示字段信息
  1214. timeFormat: this.formData.timeFormate,
  1215. searchFieldList: searchFieldList, //搜索字段数组
  1216. tableSql: this.getSQLStr(), // 暂定
  1217. tableExportField: tableExportField, //导出字段名及列名
  1218. echoData: JSON.stringify(echoData),
  1219. };
  1220. // this.tableFieldList.forEach((item) => {
  1221. // if (item.isShow) {
  1222. // let tempObj = {};
  1223. // tempObj[item.fieldName] = item.fieldDescription;
  1224. // data.dtColumnName.push(tempObj);
  1225. // if (item.children.length) {
  1226. // item.children.map((child) => {
  1227. // let tempObj = {};
  1228. // tempObj[child.fieldName] = child.fieldDescription;
  1229. // data.dtColumnName.push(tempObj);
  1230. // });
  1231. // }
  1232. // }
  1233. // if (item.isSearch) {
  1234. // data.searchFieldList.push(item.fieldName);
  1235. // }
  1236. // if (item.isExport) {
  1237. // data.tableExportField[item.fieldName] = item.fieldDescription;
  1238. // }
  1239. // });
  1240. let res;
  1241. if (this.tId) {
  1242. data.menuId = this.menuId;
  1243. data.sqlKey = this.editData.sqlKey;
  1244. res = await editTable(data);
  1245. } else {
  1246. data.menuId = result.data;
  1247. res = await addDragTable(data);
  1248. }
  1249. // 关闭当前页面
  1250. if (this.tId) {
  1251. if (res.code == 200) {
  1252. this.$message.success("修改成功");
  1253. } else {
  1254. this.$message.warning("修改失败");
  1255. }
  1256. this.$tab.closePage();
  1257. this.$router.push({
  1258. path: "/system/fromModel/index/tablelist",
  1259. });
  1260. return;
  1261. }
  1262. if (res.code == 200) {
  1263. this.$message.success("创建成功");
  1264. } else {
  1265. this.$message.warning("创建失败");
  1266. }
  1267. this.isShowPreview = false;
  1268. }
  1269. } else {
  1270. this.$message.warning("请完善表单");
  1271. return false;
  1272. }
  1273. });
  1274. },
  1275. // 修改表格回显数据
  1276. async initTableData(tId) {
  1277. let res = await getTableInfo(tId);
  1278. if (res.code == 200) {
  1279. let echoData = JSON.parse(res.data.echoData);
  1280. this.tableName = echoData.tableName;
  1281. this.tableFieldList = echoData.tableFieldData;
  1282. this.formData = echoData.formData;
  1283. this.editData = res.data;
  1284. let val = await getParticMenu(res.data.sqlKey);
  1285. if (val.code == 200) {
  1286. this.menuId = val.data.menuId;
  1287. }
  1288. } else {
  1289. this.$message.error("数据回显失败");
  1290. }
  1291. },
  1292. },
  1293. created() {},
  1294. mounted() {
  1295. this.getAllTable();
  1296. this.initDragTable();
  1297. this.getMenuList();
  1298. if (this.$route.query.tId) {
  1299. this.tId = this.$route.query.tId;
  1300. this.initTableData(this.tId);
  1301. }
  1302. },
  1303. };
  1304. </script>
  1305. <style scoped lang="scss">
  1306. ::v-deep .right_card {
  1307. min-height: 500px !important;
  1308. }
  1309. ::v-deep .vue-treeselect--has-value .vue-treeselect__input {
  1310. vertical-align: middle !important;
  1311. }
  1312. </style>