form-create: 字典选择器分离,重新封装 api 选择器
This commit is contained in:
parent
c77526e8c5
commit
7c158af3cc
@ -1,4 +1,4 @@
|
|||||||
import { useFormCreateDesigner } from './src/useFormCreateDesigner'
|
import { useFormCreateDesigner } from './src/useFormCreateDesigner'
|
||||||
import { useCurrencySelect } from './src/components/useCurrencySelect'
|
import { useApiSelect } from './src/components/useApiSelect'
|
||||||
|
|
||||||
export { useFormCreateDesigner, useCurrencySelect }
|
export { useFormCreateDesigner, useApiSelect }
|
||||||
|
59
src/components/FormCreate/src/components/DictSelect.vue
Normal file
59
src/components/FormCreate/src/components/DictSelect.vue
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
<!-- 数据字典 Select 选择器 -->
|
||||||
|
<template>
|
||||||
|
<el-select v-if="selectType === 'select'" class="w-1/1" v-bind="attrs">
|
||||||
|
<el-option
|
||||||
|
v-for="(dict, index) in getDictOptions"
|
||||||
|
:key="index"
|
||||||
|
:label="dict.label"
|
||||||
|
:value="dict.value"
|
||||||
|
/>
|
||||||
|
</el-select>
|
||||||
|
<el-radio-group v-if="selectType === 'radio'" class="w-1/1" v-bind="attrs">
|
||||||
|
<el-radio v-for="(dict, index) in getDictOptions" :key="index" :value="dict.value">
|
||||||
|
{{ dict.label }}
|
||||||
|
</el-radio>
|
||||||
|
</el-radio-group>
|
||||||
|
<el-checkbox-group v-if="selectType === 'checkbox'" class="w-1/1" v-bind="attrs">
|
||||||
|
<el-checkbox
|
||||||
|
v-for="(dict, index) in getDictOptions"
|
||||||
|
:key="index"
|
||||||
|
:label="dict.label"
|
||||||
|
:value="dict.value"
|
||||||
|
/>
|
||||||
|
</el-checkbox-group>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<script lang="ts" setup>
|
||||||
|
import { getBoolDictOptions, getIntDictOptions, getStrDictOptions } from '@/utils/dict'
|
||||||
|
|
||||||
|
defineOptions({ name: 'DictSelect' })
|
||||||
|
|
||||||
|
const attrs = useAttrs()
|
||||||
|
|
||||||
|
// 接受父组件参数
|
||||||
|
interface Props {
|
||||||
|
dictType: string // 字典类型
|
||||||
|
valueType?: 'str' | 'int' | 'bool' // 字典值类型
|
||||||
|
selectType?: 'select' | 'radio' | 'checkbox' // 选择器类型,下拉框 select、多选框 checkbox、单选框 radio
|
||||||
|
formCreateInject?: any
|
||||||
|
}
|
||||||
|
|
||||||
|
const props = withDefaults(defineProps<Props>(), {
|
||||||
|
valueType: 'str',
|
||||||
|
selectType: 'select'
|
||||||
|
})
|
||||||
|
|
||||||
|
// 获得字典配置
|
||||||
|
const getDictOptions = computed(() => {
|
||||||
|
switch (props.valueType) {
|
||||||
|
case 'str':
|
||||||
|
return getStrDictOptions(props.dictType)
|
||||||
|
case 'int':
|
||||||
|
return getIntDictOptions(props.dictType)
|
||||||
|
case 'bool':
|
||||||
|
return getBoolDictOptions(props.dictType)
|
||||||
|
default:
|
||||||
|
return []
|
||||||
|
}
|
||||||
|
})
|
||||||
|
</script>
|
@ -1,9 +1,9 @@
|
|||||||
import request from '@/config/axios'
|
import request from '@/config/axios'
|
||||||
import { isEmpty } from '@/utils/is'
|
import { isEmpty } from '@/utils/is'
|
||||||
import { CurrencySelectProps } from '@/components/FormCreate/src/type'
|
import { ApiSelectProps } from '@/components/FormCreate/src/type'
|
||||||
import { getBoolDictOptions, getIntDictOptions, getStrDictOptions } from '@/utils/dict'
|
import { jsonParse } from '@/utils'
|
||||||
|
|
||||||
export const useCurrencySelect = (option: CurrencySelectProps) => {
|
export const useApiSelect = (option: ApiSelectProps) => {
|
||||||
return defineComponent({
|
return defineComponent({
|
||||||
name: option.name,
|
name: option.name,
|
||||||
props: {
|
props: {
|
||||||
@ -18,47 +18,50 @@ export const useCurrencySelect = (option: CurrencySelectProps) => {
|
|||||||
default: () => option.valueField ?? 'value'
|
default: () => option.valueField ?? 'value'
|
||||||
},
|
},
|
||||||
// api 接口
|
// api 接口
|
||||||
restful: {
|
url: {
|
||||||
type: String,
|
type: String,
|
||||||
default: () => option.restful ?? ''
|
default: () => option.url ?? ''
|
||||||
},
|
},
|
||||||
// 字典类型
|
// 请求类型
|
||||||
dictType: {
|
method: {
|
||||||
|
type: String,
|
||||||
|
default: 'GET'
|
||||||
|
},
|
||||||
|
// 请求参数
|
||||||
|
data: {
|
||||||
type: String,
|
type: String,
|
||||||
default: ''
|
default: ''
|
||||||
},
|
},
|
||||||
// 字典值类型 'str' | 'int' | 'bool'
|
|
||||||
dictValueType: {
|
|
||||||
type: String,
|
|
||||||
default: 'str'
|
|
||||||
},
|
|
||||||
// 选择器类型,下拉框 select、多选框 checkbox、单选框 radio
|
// 选择器类型,下拉框 select、多选框 checkbox、单选框 radio
|
||||||
selectType: {
|
selectType: {
|
||||||
type: String,
|
type: String,
|
||||||
default: 'select'
|
default: 'select'
|
||||||
|
},
|
||||||
|
// 是否多选
|
||||||
|
multiple: {
|
||||||
|
type: Boolean,
|
||||||
|
default: false
|
||||||
}
|
}
|
||||||
// // 是否多选
|
|
||||||
// multiple: {
|
|
||||||
// type: Boolean,
|
|
||||||
// default: false
|
|
||||||
// }
|
|
||||||
},
|
},
|
||||||
setup(props) {
|
setup(props) {
|
||||||
const attrs = useAttrs()
|
const attrs = useAttrs()
|
||||||
const options = ref<any[]>([]) // 下拉数据
|
const options = ref<any[]>([]) // 下拉数据
|
||||||
const getOptions = async () => {
|
const getOptions = async () => {
|
||||||
options.value = []
|
options.value = []
|
||||||
// 字典选择器
|
|
||||||
if (option.isDict) {
|
|
||||||
options.value = getDictOptions()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
// 接口选择器
|
// 接口选择器
|
||||||
if (isEmpty(props.restful)) {
|
if (isEmpty(props.url)) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// TODO 只支持 GET 查询,复杂下拉构建条件请使用业务表单
|
let data = []
|
||||||
const data = await request.get({ url: props.restful })
|
switch (props.method) {
|
||||||
|
case 'GET':
|
||||||
|
data = await request.get({ url: props.url })
|
||||||
|
break
|
||||||
|
case 'POST':
|
||||||
|
data = await request.post({ url: props.url, data: jsonParse(props.data) })
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
if (Array.isArray(data)) {
|
if (Array.isArray(data)) {
|
||||||
options.value = data.map((item: any) => ({
|
options.value = data.map((item: any) => ({
|
||||||
label: item[props.labelField],
|
label: item[props.labelField],
|
||||||
@ -66,26 +69,24 @@ export const useCurrencySelect = (option: CurrencySelectProps) => {
|
|||||||
}))
|
}))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
console.log(`接口[${props.restful}] 返回结果不是一个数组`)
|
console.log(`接口[${props.url}] 返回结果不是一个数组`)
|
||||||
}
|
|
||||||
// 获得字典配置
|
|
||||||
const getDictOptions = () => {
|
|
||||||
switch (props.dictValueType) {
|
|
||||||
case 'str':
|
|
||||||
return getStrDictOptions(props.dictType)
|
|
||||||
case 'int':
|
|
||||||
return getIntDictOptions(props.dictType)
|
|
||||||
case 'bool':
|
|
||||||
return getBoolDictOptions(props.dictType)
|
|
||||||
default:
|
|
||||||
return []
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
onMounted(async () => {
|
onMounted(async () => {
|
||||||
await getOptions()
|
await getOptions()
|
||||||
})
|
})
|
||||||
// TODO puhui999: 单下拉多选的时候页面会卡住报错,下次解决
|
|
||||||
const buildSelect = () => {
|
const buildSelect = () => {
|
||||||
|
if (props.multiple) {
|
||||||
|
// fix:多写此步是为了解决 multiple 属性问题
|
||||||
|
return (
|
||||||
|
<el-select class="w-1/1" {...attrs} multiple>
|
||||||
|
{options.value.map((item, index) => (
|
||||||
|
<el-option key={index} label={item.label} value={item.value} />
|
||||||
|
))}
|
||||||
|
</el-select>
|
||||||
|
)
|
||||||
|
}
|
||||||
return (
|
return (
|
||||||
<el-select class="w-1/1" {...attrs}>
|
<el-select class="w-1/1" {...attrs}>
|
||||||
{options.value.map((item, index) => (
|
{options.value.map((item, index) => (
|
@ -8,6 +8,15 @@ const selectRule = [
|
|||||||
{ label: '下拉框', value: 'select' },
|
{ label: '下拉框', value: 'select' },
|
||||||
{ label: '单选框', value: 'radio' },
|
{ label: '单选框', value: 'radio' },
|
||||||
{ label: '多选框', value: 'checkbox' }
|
{ label: '多选框', value: 'checkbox' }
|
||||||
|
],
|
||||||
|
// 参考 https://www.form-create.com/v3/guide/control 组件联动,单选框和多选框不需要多选属性
|
||||||
|
control: [
|
||||||
|
{
|
||||||
|
value: 'select',
|
||||||
|
condition: '=',
|
||||||
|
method: 'hidden',
|
||||||
|
rule: ['multiple']
|
||||||
|
}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{ type: 'switch', field: 'multiple', title: '是否多选' },
|
{ type: 'switch', field: 'multiple', title: '是否多选' },
|
||||||
@ -79,4 +88,60 @@ const selectRule = [
|
|||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|
||||||
export default selectRule
|
const apiSelectRule = [
|
||||||
|
{
|
||||||
|
type: 'input',
|
||||||
|
field: 'url',
|
||||||
|
title: 'url 地址',
|
||||||
|
props: {
|
||||||
|
placeholder: '/system/user/simple-list'
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: 'select',
|
||||||
|
field: 'method',
|
||||||
|
title: '请求类型',
|
||||||
|
value: 'GET',
|
||||||
|
options: [
|
||||||
|
{ label: 'GET', value: 'GET' },
|
||||||
|
{ label: 'POST', value: 'POST' }
|
||||||
|
],
|
||||||
|
control: [
|
||||||
|
{
|
||||||
|
value: 'GET',
|
||||||
|
condition: '!=',
|
||||||
|
method: 'hidden',
|
||||||
|
rule: [
|
||||||
|
{
|
||||||
|
type: 'input',
|
||||||
|
field: 'data',
|
||||||
|
title: '请求参数 JSON 格式',
|
||||||
|
props: {
|
||||||
|
autosize: true,
|
||||||
|
type: 'textarea',
|
||||||
|
placeholder: '{"type": 1}'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: 'input',
|
||||||
|
field: 'labelField',
|
||||||
|
title: 'label 属性',
|
||||||
|
props: {
|
||||||
|
placeholder: 'nickname'
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: 'input',
|
||||||
|
field: 'valueField',
|
||||||
|
title: 'value 属性',
|
||||||
|
props: {
|
||||||
|
placeholder: 'id'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
export { selectRule, apiSelectRule }
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import { generateUUID } from '@/utils'
|
import { generateUUID } from '@/utils'
|
||||||
import * as DictDataApi from '@/api/system/dict/dict.type'
|
import * as DictDataApi from '@/api/system/dict/dict.type'
|
||||||
import { localeProps, makeRequiredRule } from '@/components/FormCreate/src/utils'
|
import { localeProps, makeRequiredRule } from '@/components/FormCreate/src/utils'
|
||||||
import selectRule from '@/components/FormCreate/src/config/selectRule'
|
import { selectRule } from '@/components/FormCreate/src/config/selectRule'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 字典选择器规则,如果规则使用到动态数据则需要单独配置不能使用 useSelectRule
|
* 字典选择器规则,如果规则使用到动态数据则需要单独配置不能使用 useSelectRule
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import { generateUUID } from '@/utils'
|
import { generateUUID } from '@/utils'
|
||||||
import { localeProps, makeRequiredRule } from '@/components/FormCreate/src/utils'
|
import { localeProps, makeRequiredRule } from '@/components/FormCreate/src/utils'
|
||||||
import selectRule from '@/components/FormCreate/src/config/selectRule'
|
import { selectRule } from '@/components/FormCreate/src/config/selectRule'
|
||||||
import { SelectRuleOption } from '@/components/FormCreate/src/type'
|
import { SelectRuleOption } from '@/components/FormCreate/src/type'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,13 +1,13 @@
|
|||||||
import { Rule } from '@form-create/element-ui' //左侧拖拽按钮
|
import { Rule } from '@form-create/element-ui' //左侧拖拽按钮
|
||||||
|
|
||||||
//左侧拖拽按钮
|
// 左侧拖拽按钮
|
||||||
export interface MenuItem {
|
export interface MenuItem {
|
||||||
label: string
|
label: string
|
||||||
name: string
|
name: string
|
||||||
icon: string
|
icon: string
|
||||||
}
|
}
|
||||||
|
|
||||||
//左侧拖拽按钮分类
|
// 左侧拖拽按钮分类
|
||||||
export interface Menu {
|
export interface Menu {
|
||||||
title: string
|
title: string
|
||||||
name: string
|
name: string
|
||||||
@ -16,7 +16,7 @@ export interface Menu {
|
|||||||
|
|
||||||
export interface MenuList extends Array<Menu> {}
|
export interface MenuList extends Array<Menu> {}
|
||||||
|
|
||||||
//拖拽组件的规则
|
// 拖拽组件的规则
|
||||||
export interface DragRule {
|
export interface DragRule {
|
||||||
icon: string
|
icon: string
|
||||||
name: string
|
name: string
|
||||||
@ -33,11 +33,11 @@ export interface DragRule {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 通用下拉组件 Props 类型
|
// 通用下拉组件 Props 类型
|
||||||
export interface CurrencySelectProps {
|
export interface ApiSelectProps {
|
||||||
name: string // 组件名称
|
name: string // 组件名称
|
||||||
labelField?: string // 选项标签
|
labelField?: string // 选项标签
|
||||||
valueField?: string // 选项的值
|
valueField?: string // 选项的值
|
||||||
restful?: string // api 接口
|
url?: string // url 接口
|
||||||
isDict?: boolean // 是否字典选择器
|
isDict?: boolean // 是否字典选择器
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -45,5 +45,5 @@ export interface CurrencySelectProps {
|
|||||||
export interface SelectRuleOption {
|
export interface SelectRuleOption {
|
||||||
label: string // label 名称
|
label: string // label 名称
|
||||||
name: string // 组件名称
|
name: string // 组件名称
|
||||||
props?: Rule[] // 组件规则
|
props?: any[] // 组件规则
|
||||||
}
|
}
|
||||||
|
@ -8,6 +8,7 @@ import {
|
|||||||
} from './config'
|
} from './config'
|
||||||
import { Ref } from 'vue'
|
import { Ref } from 'vue'
|
||||||
import { Menu } from '@/components/FormCreate/src/type'
|
import { Menu } from '@/components/FormCreate/src/type'
|
||||||
|
import { apiSelectRule } from '@/components/FormCreate/src/config/selectRule'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 表单设计器增强 hook
|
* 表单设计器增强 hook
|
||||||
@ -50,36 +51,12 @@ export const useFormCreateDesigner = async (designer: Ref) => {
|
|||||||
const userSelectRule = useSelectRule({ name: 'UserSelect', label: '用户选择器' })
|
const userSelectRule = useSelectRule({ name: 'UserSelect', label: '用户选择器' })
|
||||||
const deptSelectRule = useSelectRule({ name: 'DeptSelect', label: '部门选择器' })
|
const deptSelectRule = useSelectRule({ name: 'DeptSelect', label: '部门选择器' })
|
||||||
const dictSelectRule = useDictSelectRule()
|
const dictSelectRule = useDictSelectRule()
|
||||||
const restfulSelectRule = useSelectRule({
|
const apiSelectRule0 = useSelectRule({
|
||||||
name: 'RestfulSelect',
|
name: 'ApiSelect',
|
||||||
label: '接口选择器',
|
label: '接口选择器',
|
||||||
props: [
|
props: [...apiSelectRule]
|
||||||
{
|
|
||||||
type: 'input',
|
|
||||||
field: 'restful',
|
|
||||||
title: 'restful api 接口',
|
|
||||||
props: {
|
|
||||||
placeholder: '/system/user/simple-list'
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
type: 'input',
|
|
||||||
field: 'labelField',
|
|
||||||
title: 'label 属性',
|
|
||||||
props: {
|
|
||||||
placeholder: 'nickname'
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
type: 'input',
|
|
||||||
field: 'valueField',
|
|
||||||
title: 'value 属性',
|
|
||||||
props: {
|
|
||||||
placeholder: 'id'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
]
|
|
||||||
})
|
})
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 构建系统字段菜单
|
* 构建系统字段菜单
|
||||||
*/
|
*/
|
||||||
@ -88,7 +65,7 @@ export const useFormCreateDesigner = async (designer: Ref) => {
|
|||||||
designer.value?.removeMenuItem('select')
|
designer.value?.removeMenuItem('select')
|
||||||
designer.value?.removeMenuItem('radio')
|
designer.value?.removeMenuItem('radio')
|
||||||
designer.value?.removeMenuItem('checkbox')
|
designer.value?.removeMenuItem('checkbox')
|
||||||
const components = [userSelectRule, deptSelectRule, dictSelectRule, restfulSelectRule]
|
const components = [userSelectRule, deptSelectRule, dictSelectRule, apiSelectRule0]
|
||||||
const menu: Menu = {
|
const menu: Menu = {
|
||||||
name: 'system',
|
name: 'system',
|
||||||
title: '系统字段',
|
title: '系统字段',
|
||||||
|
@ -19,28 +19,26 @@ import formCreate from '@form-create/element-ui'
|
|||||||
import install from '@form-create/element-ui/auto-import'
|
import install from '@form-create/element-ui/auto-import'
|
||||||
//======================= 自定义组件 =======================
|
//======================= 自定义组件 =======================
|
||||||
import { UploadFile, UploadImg, UploadImgs } from '@/components/UploadFile'
|
import { UploadFile, UploadImg, UploadImgs } from '@/components/UploadFile'
|
||||||
import { useCurrencySelect } from '@/components/FormCreate'
|
import { useApiSelect } from '@/components/FormCreate'
|
||||||
import { Editor } from '@/components/Editor'
|
import { Editor } from '@/components/Editor'
|
||||||
|
import DictSelect from '@/components/FormCreate/src/components/DictSelect.vue'
|
||||||
|
|
||||||
const UserSelect = useCurrencySelect({
|
const UserSelect = useApiSelect({
|
||||||
name: 'UserSelect',
|
name: 'UserSelect',
|
||||||
labelField: 'nickname',
|
labelField: 'nickname',
|
||||||
valueField: 'id',
|
valueField: 'id',
|
||||||
restful: '/system/user/simple-list'
|
url: '/system/user/simple-list'
|
||||||
})
|
})
|
||||||
const DeptSelect = useCurrencySelect({
|
const DeptSelect = useApiSelect({
|
||||||
name: 'DeptSelect',
|
name: 'DeptSelect',
|
||||||
labelField: 'name',
|
labelField: 'name',
|
||||||
valueField: 'id',
|
valueField: 'id',
|
||||||
restful: '/system/dept/simple-list'
|
url: '/system/dept/simple-list'
|
||||||
})
|
})
|
||||||
const RestfulSelect = useCurrencySelect({
|
const ApiSelect = useApiSelect({
|
||||||
name: 'RestfulSelect'
|
name: 'ApiSelect'
|
||||||
})
|
|
||||||
const DictSelect = useCurrencySelect({
|
|
||||||
name: 'DictSelect',
|
|
||||||
isDict: true
|
|
||||||
})
|
})
|
||||||
|
|
||||||
const components = [
|
const components = [
|
||||||
ElAside,
|
ElAside,
|
||||||
ElPopconfirm,
|
ElPopconfirm,
|
||||||
@ -60,7 +58,7 @@ const components = [
|
|||||||
DictSelect,
|
DictSelect,
|
||||||
UserSelect,
|
UserSelect,
|
||||||
DeptSelect,
|
DeptSelect,
|
||||||
RestfulSelect,
|
ApiSelect,
|
||||||
Editor
|
Editor
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@ -435,3 +435,17 @@ export const areaReplace = (areaName: string) => {
|
|||||||
.replace('自治区', '')
|
.replace('自治区', '')
|
||||||
.replace('省', '')
|
.replace('省', '')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 解析 JSON 字符串
|
||||||
|
*
|
||||||
|
* @param str
|
||||||
|
*/
|
||||||
|
export function jsonParse(str: string) {
|
||||||
|
try {
|
||||||
|
return JSON.parse(str)
|
||||||
|
} catch (e) {
|
||||||
|
console.log(`str[${str}] 不是一个 JSON 字符串`)
|
||||||
|
return ''
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user