getDataOrigin.tsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. import { ref, createApp } from "vue";
  2. import { ElButton, ElDialog, ElInput, ElTree } from "element-plus";
  3. import {
  4. GetAllTablesAndViews,
  5. GetAllBasicData,
  6. } from "@shalu/service";
  7. import type Node from "element-plus/es/components/tree/src/model/node";
  8. type ViewItem = {
  9. name: string;
  10. schemaName: string;
  11. type: number;
  12. id: string;
  13. };
  14. type BisicData = {
  15. disabled?: boolean;
  16. displayOrder?: number;
  17. id?: string;
  18. isDisabled?: boolean;
  19. langName?: string;
  20. name: string;
  21. path?: string;
  22. updateTime?: string;
  23. value?: string;
  24. };
  25. /**
  26. * 获取系统内部的数据源
  27. * @param type 数据源类型
  28. * @returns 数据源
  29. */
  30. export const getDataOrigin = (
  31. type: "table" | "view",
  32. ): Promise<{ value: string; result?: any }> => {
  33. const data = ref();
  34. const loading = ref(false);
  35. const val = ref("");
  36. const filterText = ref("");
  37. const treeRef = ref();
  38. const valId = ref("");
  39. // 获取视图
  40. if (type === "view") {
  41. data.value = [
  42. { label: "系统视图", children: [] },
  43. { label: "数据源视图", children: [] },
  44. ];
  45. loading.value = true;
  46. GetAllTablesAndViews({ types: type })
  47. .then((res: any) => {
  48. const { bpmViewTables = [] } = res || {};
  49. bpmViewTables.forEach((item: ViewItem) => {
  50. const { name, schemaName, type, id } = item;
  51. if (data.value[type - 1]) {
  52. // type 1: 系统视图 2: 数据源视图
  53. data.value[type - 1].children.push({
  54. label: `${schemaName}(${name})`,
  55. value: schemaName,
  56. id,
  57. });
  58. }
  59. });
  60. })
  61. .finally(() => {
  62. loading.value = false;
  63. });
  64. }
  65. // 动态加载数据
  66. const onload = (node: Node, resove: (data: BisicData[]) => void) => {
  67. if (node.level === 0) {
  68. return resove([{ name: "基础数据" }]);
  69. }
  70. const data = node.data;
  71. GetAllBasicData({
  72. currentPage: 1,
  73. pageSize: 999,
  74. orderByProperty: "id",
  75. Ascending: true,
  76. totalPage: 1,
  77. totalCount: 1,
  78. filters: data?.id ? [{ name: "parentId", value: data.id }] : null,
  79. }).then((res: any) => {
  80. resove(res || []);
  81. });
  82. };
  83. const getTreeVNode = () => {
  84. return type === "table" ? (
  85. <ElTree
  86. ref={treeRef}
  87. lazy={true}
  88. load={onload}
  89. props={{
  90. label: (data: any) =>
  91. data.path ? `${data.path}(${data.name})` : data.name,
  92. children: "children",
  93. }}
  94. onNode-click={(node: BisicData) => {
  95. if (node?.path) {
  96. val.value = node.path;
  97. }
  98. }}
  99. filterNodeMethod={(value: string, data: any) => {
  100. // treeRef.value?.onload();
  101. return (
  102. data.path?.toUpperCase().includes(value.toUpperCase()) ||
  103. data.name.toUpperCase().includes(value.toUpperCase())
  104. );
  105. }}
  106. empty-text={"暂无数据"}
  107. />
  108. ) : (
  109. <ElTree
  110. ref={treeRef}
  111. data={data.value}
  112. defaultExpandAll={true}
  113. onNode-click={(node) => {
  114. if (node?.value && node.id) {
  115. val.value = node.value;
  116. valId.value = node.id;
  117. }
  118. }}
  119. filterNodeMethod={(value: string, data) => {
  120. return data.label?.includes(value);
  121. }}
  122. empty-text={"暂无数据"}
  123. />
  124. );
  125. };
  126. return new Promise((resolve, reject) => {
  127. const mountNode = document.createElement("div");
  128. const app = createApp({
  129. render() {
  130. return (
  131. <ElDialog
  132. modelValue={true}
  133. title={type === "table" ? "选择数据集" : "选择视图表"}
  134. width="800px"
  135. style={{ height: "600px" }}
  136. v-slots={{
  137. footer: () => (
  138. <ElButton
  139. type="primary"
  140. disabled={!val.value}
  141. onClick={() => {
  142. resolve({ value: val.value });
  143. document.body.removeChild(mountNode);
  144. }}
  145. >
  146. 确定
  147. </ElButton>
  148. ),
  149. }}
  150. onClose={() => {
  151. reject("close");
  152. document.body.removeChild(mountNode);
  153. }}
  154. >
  155. <ElInput
  156. size="small"
  157. placeholder="请输入关键字进行搜索"
  158. v-model={filterText.value}
  159. onInput={() => {
  160. treeRef.value?.filter(filterText.value);
  161. }}
  162. style={{
  163. marginBottom: "10px",
  164. height: "35px",
  165. fontSize: "14px",
  166. }}
  167. />
  168. <div style={{ height: "379px", overflow: "auto" }}>
  169. {getTreeVNode()}
  170. </div>
  171. </ElDialog>
  172. );
  173. },
  174. });
  175. document.body.appendChild(mountNode);
  176. app.mount(mountNode);
  177. });
  178. };