mirror of
https://gitee.com/myxzgzs/boyue-ui-admin-vue3
synced 2025-08-08 16:32:43 +08:00
fix: 修复所有函数类型 - 解决设计器保存后函数变成字符串的问题
This commit is contained in:
parent
66e266b7c2
commit
8f88c4a09b
@ -49,14 +49,17 @@ export const setConfAndFields2 = (
|
|||||||
detailPreview = detailPreview.value
|
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 option = JSON.parse(conf)
|
||||||
const rule = decodeFields(fields)
|
const rule = decodeFields(fields)
|
||||||
|
|
||||||
// 🔧 修复所有函数类型 - 解决设计器保存后函数变成字符串的问题
|
// 🔧 修复所有函数类型 - 解决设计器保存后函数变成字符串的问题
|
||||||
const fixFunctions = (obj: any) => {
|
const fixFunctions = (obj: any) => {
|
||||||
if (obj && typeof obj === 'object') {
|
if (obj && typeof obj === 'object') {
|
||||||
Object.keys(obj).forEach(key => {
|
Object.keys(obj).forEach((key) => {
|
||||||
// 检查是否是函数相关的属性
|
// 检查是否是函数相关的属性
|
||||||
if (isFunctionProperty(key)) {
|
if (isFunctionProperty(key)) {
|
||||||
// 如果不是函数类型,重新构建为函数
|
// 如果不是函数类型,重新构建为函数
|
||||||
@ -70,135 +73,107 @@ export const setConfAndFields2 = (
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 判断是否是函数属性
|
// 判断是否是函数属性
|
||||||
const isFunctionProperty = (key: string): boolean => {
|
const isFunctionProperty = (key: string): boolean => {
|
||||||
const functionKeys = [
|
const functionKeys = [
|
||||||
'beforeFetch', // 请求前处理
|
'beforeFetch', // 请求前处理
|
||||||
'afterFetch', // 请求后处理
|
'afterFetch', // 请求后处理
|
||||||
'onSubmit', // 表单提交
|
'onSubmit', // 表单提交
|
||||||
'onReset', // 表单重置
|
'onReset', // 表单重置
|
||||||
'onChange', // 值变化
|
'onChange', // 值变化
|
||||||
'onInput', // 输入事件
|
'onInput', // 输入事件
|
||||||
'onClick', // 点击事件
|
'onClick', // 点击事件
|
||||||
'onFocus', // 获取焦点
|
'onFocus', // 获取焦点
|
||||||
'onBlur', // 失去焦点
|
'onBlur', // 失去焦点
|
||||||
'onMounted', // 组件挂载
|
'onMounted', // 组件挂载
|
||||||
'onCreated', // 组件创建
|
'onCreated', // 组件创建
|
||||||
'onReload', // 重新加载
|
'onReload', // 重新加载
|
||||||
'remoteMethod', // 远程搜索方法
|
'remoteMethod', // 远程搜索方法
|
||||||
'parseFunc', // 解析函数
|
'parseFunc', // 解析函数
|
||||||
'validator', // 验证器
|
'validator', // 验证器
|
||||||
'asyncValidator', // 异步验证器
|
'asyncValidator', // 异步验证器
|
||||||
'formatter', // 格式化函数
|
'formatter', // 格式化函数
|
||||||
'parser', // 解析函数
|
'parser', // 解析函数
|
||||||
'beforeUpload', // 上传前处理
|
'beforeUpload', // 上传前处理
|
||||||
'onSuccess', // 成功回调
|
'onSuccess', // 成功回调
|
||||||
'onError', // 错误回调
|
'onError', // 错误回调
|
||||||
'onProgress', // 进度回调
|
'onProgress', // 进度回调
|
||||||
'onPreview', // 预览回调
|
'onPreview', // 预览回调
|
||||||
'onRemove', // 移除回调
|
'onRemove', // 移除回调
|
||||||
'onExceed', // 超出限制回调
|
'onExceed', // 超出限制回调
|
||||||
'filterMethod', // 过滤方法
|
'filterMethod', // 过滤方法
|
||||||
'sortMethod', // 排序方法
|
'sortMethod', // 排序方法
|
||||||
'loadData', // 加载数据
|
'loadData', // 加载数据
|
||||||
'renderContent', // 渲染内容
|
'renderContent', // 渲染内容
|
||||||
'render' // 渲染函数
|
'render' // 渲染函数
|
||||||
]
|
]
|
||||||
|
|
||||||
// 检查是否以函数相关前缀开头
|
// 检查是否以函数相关前缀开头
|
||||||
const functionPrefixes = ['on', 'before', 'after', 'handle']
|
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 => {
|
const createDefaultFunction = (key: string): Function => {
|
||||||
switch (key) {
|
switch (key) {
|
||||||
case 'beforeFetch':
|
case 'beforeFetch':
|
||||||
return (config: any) => {
|
return (config: any) => {
|
||||||
console.log('beforeFetch被调用:', config)
|
|
||||||
|
|
||||||
// 添加认证头
|
// 添加认证头
|
||||||
const token = localStorage.getItem('token')
|
const token = localStorage.getItem('token')
|
||||||
if (token) {
|
if (token) {
|
||||||
config.headers = {
|
config.headers = {
|
||||||
...config.headers,
|
...config.headers,
|
||||||
'Authorization': 'Bearer ' + token
|
Authorization: 'Bearer ' + token
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 添加通用请求头
|
// 添加通用请求头
|
||||||
config.headers = {
|
config.headers = {
|
||||||
...config.headers,
|
...config.headers,
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
'X-Requested-With': 'XMLHttpRequest'
|
'X-Requested-With': 'XMLHttpRequest'
|
||||||
}
|
}
|
||||||
|
|
||||||
// 添加时间戳防止缓存
|
// 添加时间戳防止缓存
|
||||||
config.params = {
|
config.params = {
|
||||||
...config.params,
|
...config.params,
|
||||||
_t: Date.now()
|
_t: Date.now()
|
||||||
}
|
}
|
||||||
|
|
||||||
return config
|
return config
|
||||||
}
|
}
|
||||||
|
|
||||||
case 'afterFetch':
|
case 'afterFetch':
|
||||||
return (data: any) => {
|
return (data: any) => {
|
||||||
console.log('afterFetch被调用:', data)
|
|
||||||
return data
|
return data
|
||||||
}
|
}
|
||||||
|
|
||||||
case 'onSubmit':
|
case 'onSubmit':
|
||||||
return (formData: any) => {
|
return (_formData: any) => {
|
||||||
console.log('onSubmit被调用:', formData)
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
case 'onReset':
|
case 'onReset':
|
||||||
return () => {
|
return () => {
|
||||||
console.log('onReset被调用')
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
case 'onChange':
|
case 'onChange':
|
||||||
return (value: any, oldValue: any) => {
|
return (_value: any, _oldValue: any) => {}
|
||||||
console.log('onChange被调用:', { value, oldValue })
|
|
||||||
}
|
|
||||||
|
|
||||||
case 'remoteMethod':
|
case 'remoteMethod':
|
||||||
return (query: string) => {
|
return (query: string) => {
|
||||||
console.log('remoteMethod被调用:', query)
|
console.log('remoteMethod被调用:', query)
|
||||||
}
|
}
|
||||||
|
|
||||||
case 'parseFunc':
|
case 'parseFunc':
|
||||||
return (data: any) => {
|
return (data: any) => {
|
||||||
console.log('parseFunc被调用:', data)
|
|
||||||
// 默认解析逻辑:如果是数组直接返回,否则尝试获取list属性
|
// 默认解析逻辑:如果是数组直接返回,否则尝试获取list属性
|
||||||
if (Array.isArray(data)) {
|
if (Array.isArray(data)) {
|
||||||
return data
|
return data
|
||||||
}
|
}
|
||||||
return data?.list || data?.data || []
|
return data?.list || data?.data || []
|
||||||
}
|
}
|
||||||
|
|
||||||
case 'validator':
|
case 'validator':
|
||||||
return (rule: any, value: any, callback: Function) => {
|
return (_rule: any, _value: any, callback: Function) => {
|
||||||
console.log('validator被调用:', { rule, value })
|
|
||||||
callback()
|
callback()
|
||||||
}
|
}
|
||||||
|
|
||||||
case 'beforeUpload':
|
case 'beforeUpload':
|
||||||
return (file: any) => {
|
return (_file: any) => {
|
||||||
console.log('beforeUpload被调用:', file)
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
default:
|
default:
|
||||||
// 通用默认函数
|
// 通用默认函数
|
||||||
return (...args: any[]) => {
|
return (...args: any[]) => {
|
||||||
console.log(`${key}被调用:`, args)
|
|
||||||
// 对于事件处理函数,返回true表示继续执行
|
// 对于事件处理函数,返回true表示继续执行
|
||||||
if (key.startsWith('on') || key.startsWith('handle')) {
|
if (key.startsWith('on') || key.startsWith('handle')) {
|
||||||
return true
|
return true
|
||||||
@ -208,11 +183,9 @@ export const setConfAndFields2 = (
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// 修复 option 中的所有函数
|
||||||
// 修复option中的所有函数
|
|
||||||
fixFunctions(option)
|
fixFunctions(option)
|
||||||
|
// 修复 rule 中的所有函数(包括组件的 props)
|
||||||
// 修复rule中的所有函数(包括组件的props)
|
|
||||||
if (Array.isArray(rule)) {
|
if (Array.isArray(rule)) {
|
||||||
rule.forEach((item: any) => {
|
rule.forEach((item: any) => {
|
||||||
fixFunctions(item)
|
fixFunctions(item)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user