index.vue 40 KB

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