index.vue 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <template>
  2. <LjDialog
  3. ref="ljDialogRef"
  4. class="is-selector lj-selector"
  5. :draggable="drawerProps.draggable"
  6. :overflow="drawerProps.overflow"
  7. :width="drawerProps.width"
  8. :modal-class="drawerProps.modalClass"
  9. :modal="false"
  10. :style="{ height: '60%' }"
  11. @close="onDialogClose"
  12. >
  13. <template #header>
  14. <div class="flx-1">
  15. <span class="text-h5-b">{{ props.title }}</span>
  16. </div>
  17. </template>
  18. <div class="flx-col h-full">
  19. <LjHeaderMenu :update="isDialogVisible" :data="initParams" :action="btnGroup" />
  20. <LjFoldLayoutDouble v-bind="layoutConfig" class="flx-1 overflow-hidden">
  21. <template #secondMain>
  22. <div class="flx h-full">
  23. <div class="flx-col w-full">
  24. <FormulaEditor ref="formulaEditorRef" v-model="localFormula" :tree-data="treeData" />
  25. </div>
  26. </div>
  27. </template>
  28. </LjFoldLayoutDouble>
  29. </div>
  30. </LjDialog>
  31. </template>
  32. <script lang="ts" setup>
  33. import { ref, reactive, nextTick, onMounted } from "vue";
  34. import { useI18n } from "vue-i18n";
  35. import LjDialog from "@/components/LjDialog/index.vue";
  36. import LjFoldLayoutDouble from "@/components/LjFoldLayoutDouble/index.vue";
  37. import FormulaEditor from "./editor.vue";
  38. import { GetFormulaVarList } from "@/api/modules/basicinfo";
  39. import { useAuthButtons } from "@/hooks/useAuthButtons";
  40. const { t } = useI18n();
  41. const { buttonDefault } = useAuthButtons(t);
  42. const props = defineProps({
  43. title: { type: String, default: "公式编辑器" }
  44. });
  45. const emit = defineEmits<{
  46. (e: "confirm", payload: { formula: string; valid: boolean; parameters: any[] }): void;
  47. }>();
  48. interface TreeNode {
  49. text: string;
  50. value?: number | string | null;
  51. children?: TreeNode[];
  52. }
  53. // ---------- 状态 ----------
  54. const ljDialogRef = ref();
  55. const formulaEditorRef = ref();
  56. const localFormula = ref<string>("");
  57. const requestType = ref<number>(0);
  58. const treeData = ref<TreeNode[]>([]);
  59. const isDialogVisible = ref(false);
  60. const initParams = ref<any>();
  61. // 抽屉配置(用 reactive 明确类型)
  62. const drawerProps = reactive({
  63. draggable: true,
  64. overflow: true,
  65. width: "60%",
  66. modalClass: "lj-file-dialog"
  67. });
  68. // 布局配置(命名语义化)
  69. const layoutConfig = ref<Record<string, any>>({
  70. // 这里放置需要传给 LjFoldLayoutDouble 的配置项(若无可保持空对象)
  71. });
  72. // 按钮组
  73. const btnGroup = ref([
  74. buttonDefault({
  75. label: "取消",
  76. clickFunc: () => {
  77. hide();
  78. }
  79. }),
  80. buttonDefault({
  81. label: "清空",
  82. clickFunc: () => {
  83. formulaEditorRef.value?.reset?.();
  84. localFormula.value = "";
  85. }
  86. }),
  87. buttonDefault({
  88. label: "检查公式",
  89. clickFunc: async () => {
  90. // 子组件的 check 方法返回 boolean
  91. const ok = await formulaEditorRef.value?.check?.();
  92. // 提示逻辑由子组件处理(这里仅作为动作触发)
  93. return ok;
  94. }
  95. }),
  96. buttonDefault({
  97. label: "确认",
  98. clickFunc: async () => {
  99. await handleConfirm();
  100. }
  101. })
  102. ]);
  103. // ---------- 方法 ----------
  104. const getFormulaVarList = async () => {
  105. try {
  106. const res: any = await GetFormulaVarList({ list: [], type: 1 });
  107. treeData.value = res?.recursionList ?? [];
  108. } catch (err) {
  109. treeData.value = [];
  110. }
  111. };
  112. const show = (type = 0, refresh = false, formula = "") => {
  113. localFormula.value = formula ?? "";
  114. requestType.value = type ?? 0;
  115. ljDialogRef.value?.show?.(refresh);
  116. nextTick(() => {
  117. isDialogVisible.value = true;
  118. });
  119. };
  120. const hide = () => {
  121. ljDialogRef.value?.hide?.();
  122. isDialogVisible.value = false;
  123. };
  124. const onDialogClose = () => {
  125. // 当对话框关闭时需要同步状态
  126. isDialogVisible.value = false;
  127. };
  128. const handleConfirm = async () => {
  129. // 调用子组件的校验方法
  130. let valid = true;
  131. if (formulaEditorRef.value?.check) {
  132. try {
  133. valid = await formulaEditorRef.value.check();
  134. } catch (err) {
  135. console.warn("公式校验出错:", err);
  136. valid = false;
  137. }
  138. }
  139. if (!valid) return;
  140. // 返回更丰富的上下文
  141. emit("confirm", {
  142. formula: localFormula.value,
  143. valid,
  144. parameters: treeData.value
  145. });
  146. hide();
  147. };
  148. // ---------- 生命周期 ----------
  149. onMounted(() => {
  150. getFormulaVarList();
  151. });
  152. // 暴露给父组件使用
  153. defineExpose({
  154. show,
  155. hide
  156. });
  157. </script>
  158. <style lang="scss">
  159. .lj-selector {
  160. padding: 0;
  161. .el-tree {
  162. background: unset;
  163. }
  164. }
  165. .ljselector-aside-collapse {
  166. height: 100%;
  167. display: flex;
  168. flex-direction: column;
  169. .el-collapse-item {
  170. display: flex;
  171. flex-direction: column;
  172. overflow: auto;
  173. height: auto;
  174. &.is-active {
  175. flex: 1;
  176. }
  177. &:not(.is-active) {
  178. flex-shrink: 0;
  179. }
  180. .el-collapse-item__header {
  181. flex-shrink: 0;
  182. }
  183. .el-collapse-item__wrap {
  184. flex: 1;
  185. overflow: auto;
  186. background-color: $color-gray-2;
  187. padding: 8px 4px;
  188. .lj-infor-item {
  189. background-color: $color-gray-1;
  190. }
  191. }
  192. }
  193. }
  194. </style>