From 388623f8ccb60383d10b38bcea16cdb9e482eaa9 Mon Sep 17 00:00:00 2001 From: "X.Mo" Date: Wed, 6 Mar 2024 16:37:58 +0800 Subject: [PATCH] feat and fix --- src/components/ma-codeEditor/index.vue | 11 +- .../ma-form/formItem/form-dialog.vue | 4 +- src/components/ma-form/index.vue | 12 ++- src/components/ma-form/js/defaultOptions.js | 5 + src/components/ma-form/js/networkRequest.js | 101 ++++++++++-------- src/components/ma-form/js/utils.js | 29 +++++ src/utils/common.js | 31 ------ 7 files changed, 103 insertions(+), 90 deletions(-) diff --git a/src/components/ma-codeEditor/index.vue b/src/components/ma-codeEditor/index.vue index 4af74563..42d98f41 100644 --- a/src/components/ma-codeEditor/index.vue +++ b/src/components/ma-codeEditor/index.vue @@ -92,12 +92,7 @@ const initEditorValue = () => { } } -watch( - () => props.modelValue, - (newModelValue) => { - initEditorValue() - } -) +watch( () => props.modelValue, () => initEditorValue() ) onMounted(() => { instance = monaco.editor.create(dom.value, options) @@ -110,7 +105,7 @@ onMounted(() => { const getInstance = () => instance -defineExpose({ getInstance }) +defineExpose({ getInstance, initEditorValue }) + \ No newline at end of file diff --git a/src/components/ma-form/formItem/form-dialog.vue b/src/components/ma-form/formItem/form-dialog.vue index b0f97b10..4f3dadf8 100644 --- a/src/components/ma-form/formItem/form-dialog.vue +++ b/src/components/ma-form/formItem/form-dialog.vue @@ -52,7 +52,7 @@ const maDialog = (p, ctx) => { props.component[key](...argsList, { formModel, getColumnService, columns }) } } else { - evs[key] = evs[key] = function() { + evs[key] = function() { const argsList = Array.prototype.slice.call(arguments) rv(key, { ...argsList }) } @@ -72,7 +72,7 @@ const maDialog = (p, ctx) => { columns: componentProps.formList, options: Object.assign(componentProps.options), modelValue: form.value, - onSubmit: async (data, done) => evs.onSubmit && await evs?.onSubmit(data, done), + onSubmit: async (data, done) => await rv('onSubmit', { data, done }) }, componentProps?.formSlot, ), diff --git a/src/components/ma-form/index.vue b/src/components/ma-form/index.vue index 9066d818..4225dcd7 100644 --- a/src/components/ma-form/index.vue +++ b/src/components/ma-form/index.vue @@ -75,7 +75,8 @@ import { isNil, get, cloneDeep } from 'lodash' import defaultOptions from './js/defaultOptions.js' import { getComponentName, toHump, - interactiveControl, handleFlatteningColumns + interactiveControl, handleFlatteningColumns, + insertGlobalCssToHead, insertGlobalFunctionsToHtml } from './js/utils.js' import { loadDict, handlerCascader } from './js/networkRequest.js' import arrayComponentDefault from './js/defaultArrayComponent.js' @@ -163,7 +164,12 @@ const init = async () => { // 字典 if (! cascaderList.value.includes(item.dataIndex) && item.dict) { - await loadDict(dictList.value, item) + await loadDict( + dictList.value, + item, + options.value.sourceList, + { formModel: form.value, getColumnService, columns: flatteningColumns.value } + ) } // 联动 @@ -198,6 +204,8 @@ const setDialogRef = async (ref) => { onMounted(async () => { updateOptions() + insertGlobalCssToHead(options.value.globalCss) + insertGlobalFunctionsToHtml(options.value.globalFunction) // maEvent.handleCommonEvent(options.value, 'onMounted') options.value.init && await init() // maEvent.handleCommonEvent(options.value, 'onInit') diff --git a/src/components/ma-form/js/defaultOptions.js b/src/components/ma-form/js/defaultOptions.js index 116c4859..441ca5fa 100644 --- a/src/components/ma-form/js/defaultOptions.js +++ b/src/components/ma-form/js/defaultOptions.js @@ -51,4 +51,9 @@ export default { // 数据源列表,配合表单设计器使用,单独无法使用 sourceList: [], + + // 全局CSS class + globalCss: '', + // 全局function + globalFunction: '', } \ No newline at end of file diff --git a/src/components/ma-form/js/networkRequest.js b/src/components/ma-form/js/networkRequest.js index 22d6a5f2..f0c3878b 100644 --- a/src/components/ma-form/js/networkRequest.js +++ b/src/components/ma-form/js/networkRequest.js @@ -1,4 +1,5 @@ -import { isArray, isFunction, set } from 'lodash' +import { toRaw } from 'vue' +import { isArray, isFunction, set, isUndefined } from 'lodash' import { request } from '@/utils/request' import commonApi from '@/api/common' import { Message } from '@arco-design/web-vue' @@ -32,7 +33,7 @@ export const requestDict = (url, method, params, data, header = {}, timeout = 10 return request({ url, method, params, data, header, timeout }) } -export const requestDataSource = async (config, formConfig, runCode, errorCode) => { +export const requestDataSource = async (config, maFormObject = {}) => { try { const response = await requestDict( config.url, @@ -42,11 +43,10 @@ export const requestDataSource = async (config, formConfig, runCode, errorCode) coverSourceArrayToObj(config.header), config.timeout * 1000, ) - - const func = new Function('response', 'config', 'formConfig', runCode) - return await func.call( null, response, config, formConfig ) + const func = new Function('response', 'config', 'maFormObject', toRaw(config.response).getValue()) + return await func.call( null, response, config, maFormObject ) } catch (err) { - const func = new Function('message', 'response', 'config', errorCode) + const func = new Function('message', 'response', 'config', toRaw(config.errorHandle).getValue()) await func.call( null, Message, err, config ) return err } @@ -85,15 +85,17 @@ export const handlerDictProps = (item, tmpArr) => { return data } -export const loadDict = async (dictList, item) => { - if (allowUseDictComponent.includes(item.formType) && item.dict) { - const dataIndex = item.parentDataIndex ? `${item.parentDataIndex}.${item.dataIndex}` : item.dataIndex - if (item.dict.name) { - const response = await commonApi.getDict(item.dict.name) - if (response.data) { - dictList[dataIndex] = handlerDictProps(item, response.data) - } - } else if (item.dict.remote) { +export const loadDict = async (dictList, item, sourceList = [], maFormObject = {}) => { + if (! allowUseDictComponent.includes(item.formType)) { + return + } + const dataIndex = item.parentDataIndex ? `${item.parentDataIndex}.${item.dataIndex}` : item.dataIndex + if (item.dict.name) { + const response = await commonApi.getDict(item.dict.name) + if (response.data) { + dictList[dataIndex] = handlerDictProps(item, response.data) + } + } else if (item.dict.remote) { let requestData = { openPage: item.dict?.openPage ?? false, remoteOption: item.dict.remoteOption ?? {} @@ -117,43 +119,48 @@ export const loadDict = async (dictList, item) => { } } } - } else if (item.dict.url) { - let requestData = { - openPage: item.dict?.openPage ?? false, - remoteOption: item.dict.remoteOption ?? {} - } - requestData = Object.assign(requestData, item.dict.pageOption) + } else if (item.dict.url) { + let requestData = { + openPage: item.dict?.openPage ?? false, + remoteOption: item.dict.remoteOption ?? {} + } + requestData = Object.assign(requestData, item.dict.pageOption) - if (requestData.openPage) { - if (item.dict?.method === 'GET' || item.dict?.method === 'get') { - item.dict.params = Object.assign(item.dict.params ?? {}, requestData) - } else { - item.dict.body = Object.assign(item.dict.body ?? {}, requestData) - } - const { data } = await requestDict(item.dict.url, item.dict.method || 'GET', item.dict.params || {}, item.dict.body || {}) - dictList[dataIndex] = handlerDictProps(item, data.items) - dictList[dataIndex].pageInfo = data.pageInfo + if (requestData.openPage) { + if (item.dict?.method === 'GET' || item.dict?.method === 'get') { + item.dict.params = Object.assign(item.dict.params ?? {}, requestData) } else { - const dictData = tool.local.get('dictData') - if (item.dict.cache && dictData[dataIndex]) { - dictList[dataIndex] = dictData[dataIndex] - } else { - const { data } = await requestDict(item.dict.url, item.dict.method || 'GET', item.dict.params || {}, item.dict.body || {}) - dictList[dataIndex] = handlerDictProps(item, data) - if (item.dict.cache) { - dictData[dataIndex] = dictList[dataIndex] - tool.local.set('dictData', dictData) - } - } + item.dict.body = Object.assign(item.dict.body ?? {}, requestData) } - } else if (item.dict.data) { - if (isArray(item.dict.data)) { - dictList[dataIndex] = handlerDictProps(item, item.dict.data) - } else if (isFunction(item.dict.data)) { - const response = await item.dict.data() - dictList[dataIndex] = handlerDictProps(item, response) + const { data } = await requestDict(item.dict.url, item.dict.method || 'GET', item.dict.params || {}, item.dict.body || {}) + dictList[dataIndex] = handlerDictProps(item, data.items) + dictList[dataIndex].pageInfo = data.pageInfo + } else { + const dictData = tool.local.get('dictData') + if (item.dict.cache && dictData[dataIndex]) { + dictList[dataIndex] = dictData[dataIndex] + } else { + const { data } = await requestDict(item.dict.url, item.dict.method || 'GET', item.dict.params || {}, item.dict.body || {}) + dictList[dataIndex] = handlerDictProps(item, data) + if (item.dict.cache) { + dictData[dataIndex] = dictList[dataIndex] + tool.local.set('dictData', dictData) + } } } + } else if ((item?.sourceIndex || item?.sourceIndex === 0) + && !isUndefined(sourceList[item?.sourceIndex]) + ) { + const config = sourceList[item.sourceIndex] + const data = await requestDataSource(config, maFormObject) + dictList[dataIndex] = handlerDictProps(item, data) + } else if (item.dict.data) { + if (isArray(item.dict.data)) { + dictList[dataIndex] = handlerDictProps(item, item.dict.data) + } else if (isFunction(item.dict.data)) { + const response = await item.dict.data() + dictList[dataIndex] = handlerDictProps(item, response) + } } } diff --git a/src/components/ma-form/js/utils.js b/src/components/ma-form/js/utils.js index ed6dd9e5..ecb79d3b 100644 --- a/src/components/ma-form/js/utils.js +++ b/src/components/ma-form/js/utils.js @@ -99,4 +99,33 @@ export const handleFlatteningColumns = (data, columns) => { columns.push(item) } } +} + +export const insertGlobalCssToHead = (cssCode) => { + const head = document.getElementsByTagName('head')[0] + const oldStyle = document.getElementById('mineadmin-global-css') + oldStyle && head.removeChild(oldStyle) + + const newStyle = document.createElement('style') + newStyle.rel = 'stylesheet' + newStyle.id = 'mineadmin-global-css' + try { + newStyle.appendChild(document.createTextNode(cssCode)) + } catch(ex) { + newStyle.styleSheet.cssText = cssCode + } + + head.appendChild(newStyle) +} + +export const insertGlobalFunctionsToHtml = (functionsCode) => { + const bodyEle = document.getElementsByTagName('body')[0] + const oldScriptEle = document.getElementById('mineadmin-global-functions') + oldScriptEle && bodyEle.removeChild(oldScriptEle) + + const newScriptEle = document.createElement('script') + newScriptEle.id = 'mineadmin-global-functions' + newScriptEle.type = 'text/javascript' + newScriptEle.innerHTML = functionsCode + bodyEle.appendChild(newScriptEle) } \ No newline at end of file diff --git a/src/utils/common.js b/src/utils/common.js index 415cf7fe..b3658864 100644 --- a/src/utils/common.js +++ b/src/utils/common.js @@ -229,35 +229,4 @@ export const loadCss = (href, callback) => { } } document.body.appendChild(s) -} - -// 插入css -export const insertGlobalCssToHead = (cssCode) => { - const head = document.getElementsByTagName('head')[0] - const oldStyle = document.getElementById('mineadmin-global-css') - oldStyle && head.removeChild(oldStyle) - - const newStyle = document.createElement('style') - newStyle.rel = 'stylesheet' - newStyle.id = 'mineadmin-global-css' - try { - newStyle.appendChild(document.createTextNode(cssCode)) - } catch(ex) { - newStyle.styleSheet.cssText = cssCode - } - - head.appendChild(newStyle) -} - -// 插入js函数 -export const insertGlobalFunctionsToHtml = function (functionsCode) { - const bodyEle = document.getElementsByTagName('body')[0] - const oldScriptEle = document.getElementById('mineadmin-global-functions') - oldScriptEle && bodyEle.removeChild(oldScriptEle) - - const newScriptEle = document.createElement('script') - newScriptEle.id = 'mineadmin-global-functions' - newScriptEle.type = 'text/javascript' - newScriptEle.innerHTML = functionsCode - bodyEle.appendChild(newScriptEle) } \ No newline at end of file