Skip to content

Commit

Permalink
feat and fix
Browse files Browse the repository at this point in the history
  • Loading branch information
kanyxmo committed Mar 6, 2024
1 parent b844dc1 commit 388623f
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 90 deletions.
11 changes: 3 additions & 8 deletions src/components/ma-codeEditor/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,7 @@ const initEditorValue = () => {
}
}

watch(
() => props.modelValue,
(newModelValue) => {
initEditorValue()
}
)
watch( () => props.modelValue, () => initEditorValue() )

onMounted(() => {
instance = monaco.editor.create(dom.value, options)
Expand All @@ -110,7 +105,7 @@ onMounted(() => {

const getInstance = () => instance

defineExpose({ getInstance })
defineExpose({ getInstance, initEditorValue })
</script>

<style scoped lang="less">
Expand All @@ -119,4 +114,4 @@ defineExpose({ getInstance })
border-radius: 3px;
background: var(--color-bg-2);
}
</style>
</style>
4 changes: 2 additions & 2 deletions src/components/ma-form/formItem/form-dialog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
}
Expand All @@ -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,
),
Expand Down
12 changes: 10 additions & 2 deletions src/components/ma-form/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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 }
)
}

// 联动
Expand Down Expand Up @@ -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')
Expand Down
5 changes: 5 additions & 0 deletions src/components/ma-form/js/defaultOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,9 @@ export default {

// 数据源列表,配合表单设计器使用,单独无法使用
sourceList: [],

// 全局CSS class
globalCss: '',
// 全局function
globalFunction: '',
}
101 changes: 54 additions & 47 deletions src/components/ma-form/js/networkRequest.js
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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,
Expand All @@ -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
}
Expand Down Expand Up @@ -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 ?? {}
Expand All @@ -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)
}
}
}

Expand Down
29 changes: 29 additions & 0 deletions src/components/ma-form/js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
31 changes: 0 additions & 31 deletions src/utils/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 388623f

Please sign in to comment.