mirror of
https://gitee.com/myxzgzs/boyue-ui-admin-vue3
synced 2025-08-09 00:42:42 +08:00
fix: 修复所有函数类型 - 解决设计器保存后函数变成字符串的问题
This commit is contained in:
parent
66e266b7c2
commit
8f88c4a09b
@ -49,14 +49,17 @@ export const setConfAndFields2 = (
|
||||
detailPreview = detailPreview.value
|
||||
}
|
||||
|
||||
// 解析配置
|
||||
// 修复所有函数类型(解决设计器保存后函数变成字符串的问题)。例如说:
|
||||
// https://t.zsxq.com/rADff
|
||||
// https://t.zsxq.com/ZfbGt
|
||||
// https://t.zsxq.com/mHOoj
|
||||
// https://t.zsxq.com/BSylB
|
||||
const option = JSON.parse(conf)
|
||||
const rule = decodeFields(fields)
|
||||
|
||||
// 🔧 修复所有函数类型 - 解决设计器保存后函数变成字符串的问题
|
||||
const fixFunctions = (obj: any) => {
|
||||
if (obj && typeof obj === 'object') {
|
||||
Object.keys(obj).forEach(key => {
|
||||
Object.keys(obj).forEach((key) => {
|
||||
// 检查是否是函数相关的属性
|
||||
if (isFunctionProperty(key)) {
|
||||
// 如果不是函数类型,重新构建为函数
|
||||
@ -70,7 +73,6 @@ export const setConfAndFields2 = (
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// 判断是否是函数属性
|
||||
const isFunctionProperty = (key: string): boolean => {
|
||||
const functionKeys = [
|
||||
@ -105,100 +107,73 @@ export const setConfAndFields2 = (
|
||||
'renderContent', // 渲染内容
|
||||
'render' // 渲染函数
|
||||
]
|
||||
|
||||
// 检查是否以函数相关前缀开头
|
||||
const functionPrefixes = ['on', 'before', 'after', 'handle']
|
||||
|
||||
return functionKeys.includes(key) ||
|
||||
functionPrefixes.some(prefix => key.startsWith(prefix))
|
||||
return functionKeys.includes(key) || functionPrefixes.some((prefix) => key.startsWith(prefix))
|
||||
}
|
||||
|
||||
// 根据函数名创建默认函数
|
||||
const createDefaultFunction = (key: string): Function => {
|
||||
switch (key) {
|
||||
case 'beforeFetch':
|
||||
return (config: any) => {
|
||||
console.log('beforeFetch被调用:', config)
|
||||
|
||||
// 添加认证头
|
||||
const token = localStorage.getItem('token')
|
||||
if (token) {
|
||||
config.headers = {
|
||||
...config.headers,
|
||||
'Authorization': 'Bearer ' + token
|
||||
Authorization: 'Bearer ' + token
|
||||
}
|
||||
}
|
||||
|
||||
// 添加通用请求头
|
||||
config.headers = {
|
||||
...config.headers,
|
||||
'Content-Type': 'application/json',
|
||||
'X-Requested-With': 'XMLHttpRequest'
|
||||
}
|
||||
|
||||
// 添加时间戳防止缓存
|
||||
config.params = {
|
||||
...config.params,
|
||||
_t: Date.now()
|
||||
}
|
||||
|
||||
return config
|
||||
}
|
||||
|
||||
case 'afterFetch':
|
||||
return (data: any) => {
|
||||
console.log('afterFetch被调用:', data)
|
||||
return data
|
||||
}
|
||||
|
||||
case 'onSubmit':
|
||||
return (formData: any) => {
|
||||
console.log('onSubmit被调用:', formData)
|
||||
return (_formData: any) => {
|
||||
return true
|
||||
}
|
||||
|
||||
case 'onReset':
|
||||
return () => {
|
||||
console.log('onReset被调用')
|
||||
return true
|
||||
}
|
||||
|
||||
case 'onChange':
|
||||
return (value: any, oldValue: any) => {
|
||||
console.log('onChange被调用:', { value, oldValue })
|
||||
}
|
||||
|
||||
return (_value: any, _oldValue: any) => {}
|
||||
case 'remoteMethod':
|
||||
return (query: string) => {
|
||||
console.log('remoteMethod被调用:', query)
|
||||
}
|
||||
|
||||
case 'parseFunc':
|
||||
return (data: any) => {
|
||||
console.log('parseFunc被调用:', data)
|
||||
// 默认解析逻辑:如果是数组直接返回,否则尝试获取list属性
|
||||
if (Array.isArray(data)) {
|
||||
return data
|
||||
}
|
||||
return data?.list || data?.data || []
|
||||
}
|
||||
|
||||
case 'validator':
|
||||
return (rule: any, value: any, callback: Function) => {
|
||||
console.log('validator被调用:', { rule, value })
|
||||
return (_rule: any, _value: any, callback: Function) => {
|
||||
callback()
|
||||
}
|
||||
|
||||
case 'beforeUpload':
|
||||
return (file: any) => {
|
||||
console.log('beforeUpload被调用:', file)
|
||||
return (_file: any) => {
|
||||
return true
|
||||
}
|
||||
|
||||
default:
|
||||
// 通用默认函数
|
||||
return (...args: any[]) => {
|
||||
console.log(`${key}被调用:`, args)
|
||||
// 对于事件处理函数,返回true表示继续执行
|
||||
if (key.startsWith('on') || key.startsWith('handle')) {
|
||||
return true
|
||||
@ -208,10 +183,8 @@ export const setConfAndFields2 = (
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 修复 option 中的所有函数
|
||||
fixFunctions(option)
|
||||
|
||||
// 修复 rule 中的所有函数(包括组件的 props)
|
||||
if (Array.isArray(rule)) {
|
||||
rule.forEach((item: any) => {
|
||||
|
Loading…
x
Reference in New Issue
Block a user