page-help-v2.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. import Mitt from "../../../utils/mitt";
  2. import {PageHeader, PageJsonObject, ReportForms} from "@/api/reports/report-query-center";
  3. import {Ref, ref} from 'vue'
  4. import XEUtils from "xe-utils";
  5. import {reportQueryCenterApi, saveTheFile} from "@/api/base-data/report-center";
  6. import * as ElementPlus from "element-plus";
  7. // @ts-ignore
  8. import XcComboGridV2 from "../../../components/xiao-chan/combo-grid/XcComboGridV2.vue";
  9. // @ts-ignore
  10. import CyComboGrid from "@/components/cy/combo-grid/src/CyComboGrid.vue";
  11. import {copyStrFunc} from "@/utils/public";
  12. // @ts-ignore
  13. import SystemDeptSelect from "@/components/system/dept-select/SystemDeptSelect.vue";
  14. // @ts-ignore
  15. import SystemStaffSelect from '@/components/system/staff-select/SystemStaffSelect.vue'
  16. import {xcMessage} from "@/utils/xiaochan-element-plus";
  17. import {CyJsonEditorDialog} from "@/components/cy/cy-monaco-editor/CyMonacoEditor";
  18. interface TableData {
  19. data: any[],
  20. columns: any[]
  21. }
  22. export const ElAndXc = {
  23. ...ElementPlus,
  24. XcComboGridV2: XcComboGridV2,
  25. CyComboGrid: CyComboGrid,
  26. SystemDeptSelect: SystemDeptSelect,
  27. SystemStaffSelect: SystemStaffSelect
  28. }
  29. export const createFile = () => {
  30. const data: PageJsonObject = {
  31. header: [],
  32. submitUrl: '',
  33. params: {},
  34. fromConfig: {
  35. labelWidth: '80px',
  36. inline: true
  37. },
  38. paramsDefaultValue: {}
  39. }
  40. return JSON.stringify(data)
  41. }
  42. export interface PageHelpV2Mitt {
  43. changePageJson: (data: ReportForms) => void;
  44. componentReady: (data: any) => void
  45. doTest: (id: string, queryParam: any) => void;
  46. openMagic: () => void
  47. doTestResult: (data: any, id: string, url: string) => void;
  48. getCurrentPageData: () => any
  49. queryTree: () => void
  50. [key: string]: (...args: any[]) => any;
  51. }
  52. export interface ComponentBind {
  53. renderName: 'select' | 'boolean' | 'input' | 'number',
  54. label: string,
  55. selectData?: string[],
  56. defaultValue: string | number | boolean,
  57. on?: {
  58. [key: string]: string
  59. },
  60. jsonType?: string
  61. }
  62. export const pageHelpV2Mitt = new Mitt<PageHelpV2Mitt>();
  63. export function capitalizeFirstLetter(str: string, prefix: string = '') {
  64. return prefix + str.charAt(0).toUpperCase() + str.slice(1);
  65. }
  66. export const REPORT_FOLDER = "8283a0956dd9455cbf77c6246306ec98"
  67. export function usePageStore(props: any) {
  68. const currentClickIndex = ref(-1)
  69. const pageData = ref<PageJsonObject>({
  70. header: [],
  71. params: {},
  72. submitUrl: '',
  73. fromConfig: {
  74. inline: true,
  75. labelWidth: '120px'
  76. },
  77. paramsDefaultValue: {}
  78. })
  79. const tableBind = ref({
  80. data: [],
  81. columns: []
  82. })
  83. function clearOnAndFunc() {
  84. XEUtils.arrayEach(pageData.value.header, (item: PageHeader) => {
  85. if (item.on) {
  86. for (let onKey in item.on) {
  87. const key = capitalizeFirstLetter(onKey, 'on')
  88. delete item.bind[key]
  89. }
  90. }
  91. if (item.func) {
  92. for (let funcKey in item.func) {
  93. delete item.bind[funcKey]
  94. }
  95. }
  96. })
  97. }
  98. function handleSavaData() {
  99. const clone: PageJsonObject = XEUtils.clone(pageData.value, true)
  100. if (XEUtils.isEmpty(clone.paramsDefaultValue)) {
  101. defaultValue()
  102. xcMessage.error('没有设置默认值。')
  103. return
  104. }
  105. console.log(clone.paramsDefaultValue, clone.params)
  106. if (Object.keys(clone.paramsDefaultValue).length !== Object.keys(clone.params).length) {
  107. defaultValue()
  108. xcMessage.error('有新增的组件没有创建默认值。')
  109. return
  110. }
  111. clone.params = {}
  112. clone.header.forEach(item => {
  113. if (item.name === 'ElSelectV2') {
  114. item.bind.options = []
  115. }
  116. if (item.name === 'CyComboGrid') {
  117. item.bind.data = []
  118. }
  119. })
  120. const data = pageHelpV2Mitt.emit('getCurrentPageData')
  121. data.pageJson = JSON.stringify(clone)
  122. saveTheFile(data).then(r => {
  123. pageHelpV2Mitt.emit('queryTree')
  124. })
  125. }
  126. const bindData = ref()
  127. let changeCurrentBind: (item: any) => Promise<any> | null = () => null
  128. function getColumns(data: Ref<TableData>, tableName: 'dialogTableRef' | 'mainTableRef' = 'mainTableRef', copy = true) {
  129. if (data.value.columns.length === 0 && data.value.data.length > 0) {
  130. for (let key in data.value.data[0]) {
  131. if (key === '_X_ROW_KEY') {
  132. continue;
  133. }
  134. data.value.columns.push({
  135. title: key,
  136. field: key,
  137. width: 120,
  138. })
  139. }
  140. } else if (data.value.columns.length > 0) {
  141. const table = {
  142. mainTableRef: mainTableRef,
  143. dialogTableRef: dialogTableRef
  144. }
  145. const temp = XEUtils.eachAndReturnList(data.value.columns, (item) => {
  146. return {
  147. ...item,
  148. width: table[tableName].value.getColumnWidth(item.field)
  149. }
  150. })
  151. copy && copyStrFunc(temp)
  152. }
  153. }
  154. const mainTableRef = ref()
  155. const dialogTableRef = ref()
  156. const compData = ref([])
  157. async function addComponentRefresh() {
  158. compData.value = await reportQueryCenterApi('/reportCenterOption/components/addComponents')
  159. }
  160. const mutation = {
  161. setPageData: (data: PageJsonObject, currentIndex = -1) => {
  162. }
  163. }
  164. function defaultValue() {
  165. let temp: {};
  166. const headerParams = {}
  167. pageData.value.header.forEach(item => {
  168. // @ts-ignore
  169. headerParams[item.key] = ""
  170. })
  171. if (XEUtils.isEmpty(pageData.value.paramsDefaultValue)) {
  172. temp = headerParams
  173. } else {
  174. temp = {...headerParams, ...pageData.value.paramsDefaultValue};
  175. }
  176. // @ts-ignore
  177. CyJsonEditorDialog(temp, {bodyWidth: '60%'}).then(res => {
  178. pageData.value.paramsDefaultValue = res.json
  179. })
  180. }
  181. return {
  182. currentClickIndex,
  183. pageData,
  184. clearOnAndFunc,
  185. handleSavaData,
  186. tableBind,
  187. bindData,
  188. changeCurrentBind,
  189. mainTableRef,
  190. props,
  191. getColumns,
  192. dialogTableRef,
  193. compData,
  194. addComponentRefresh,
  195. defaultValue,
  196. mutation
  197. }
  198. }
  199. class HelperStore {
  200. Return = usePageStore({})
  201. }
  202. type PageStore = HelperStore['Return']
  203. export type {PageStore}