|
@@ -27,6 +27,11 @@ export const useApiSelect = (option: ApiSelectProps) => {
|
|
|
type: String,
|
|
|
default: 'GET'
|
|
|
},
|
|
|
+ // 选项解析函数
|
|
|
+ parseFunc: {
|
|
|
+ type: String,
|
|
|
+ default: ''
|
|
|
+ },
|
|
|
// 请求参数
|
|
|
data: {
|
|
|
type: String,
|
|
@@ -63,15 +68,43 @@ export const useApiSelect = (option: ApiSelectProps) => {
|
|
|
}
|
|
|
|
|
|
if (Array.isArray(data)) {
|
|
|
- options.value = data.map((item: any) => ({
|
|
|
- label: item[props.labelField],
|
|
|
- value: item[props.valueField]
|
|
|
- }))
|
|
|
+ let parse: any = null
|
|
|
+ if (!!props.parseFunc) {
|
|
|
+ // 解析字符串函数
|
|
|
+ parse = new Function(`return ${props.parseFunc}`)()
|
|
|
+ }
|
|
|
+ options.value = data.map(
|
|
|
+ (item: any) =>
|
|
|
+ parse?.(item) ?? {
|
|
|
+ label: parseExpression(item, props.labelField),
|
|
|
+ value: parseExpression(item, props.valueField)
|
|
|
+ }
|
|
|
+ )
|
|
|
return
|
|
|
}
|
|
|
console.log(`接口[${props.url}] 返回结果不是一个数组`)
|
|
|
}
|
|
|
|
|
|
+ function parseExpression(data: any, template: string) {
|
|
|
+ // 检测是否使用了表达式
|
|
|
+ if (template.indexOf('${') === -1) {
|
|
|
+ return data[template]
|
|
|
+ }
|
|
|
+ // 正则表达式匹配模板字符串中的 ${...}
|
|
|
+ const pattern = /\$\{([^}]*)}/g
|
|
|
+ // 使用replace函数配合正则表达式和回调函数来进行替换
|
|
|
+ return template.replace(pattern, (_, expr) => {
|
|
|
+ // expr 是匹配到的 ${} 内的表达式(这里是属性名),从 data 中获取对应的值
|
|
|
+ const result = data[expr.trim()] // 去除前后空白,以防用户输入带空格的属性名
|
|
|
+ if (!result) {
|
|
|
+ console.warn(
|
|
|
+ `接口选择器选项模版[${template}][${expr.trim()}] 解析值失败结果为[${result}], 请检查属性名称是否存在于接口返回值中,存在则忽略此条!!!`
|
|
|
+ )
|
|
|
+ }
|
|
|
+ return result
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
onMounted(async () => {
|
|
|
await getOptions()
|
|
|
})
|