Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
phsl
/
admin
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
7c158af3
authored
May 06, 2024
by
puhui999
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
form-create: 字典选择器分离,重新封装 api 选择器
parent
c77526e8
Show whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
205 additions
and
91 deletions
+205
-91
src/components/FormCreate/index.ts
+2
-2
src/components/FormCreate/src/components/DictSelect.vue
+59
-0
src/components/FormCreate/src/components/useApiSelect.tsx
+40
-39
src/components/FormCreate/src/config/selectRule.ts
+66
-1
src/components/FormCreate/src/config/useDictSelectRule.ts
+1
-1
src/components/FormCreate/src/config/useSelectRule.ts
+1
-1
src/components/FormCreate/src/type/index.ts
+6
-6
src/components/FormCreate/src/useFormCreateDesigner.ts
+6
-29
src/plugins/formCreate/index.ts
+10
-12
src/utils/index.ts
+14
-0
No files found.
src/components/FormCreate/index.ts
View file @
7c158af3
import
{
useFormCreateDesigner
}
from
'./src/useFormCreateDesigner'
import
{
use
CurrencySelect
}
from
'./src/components/useCurrency
Select'
import
{
use
ApiSelect
}
from
'./src/components/useApi
Select'
export
{
useFormCreateDesigner
,
use
Currency
Select
}
export
{
useFormCreateDesigner
,
use
Api
Select
}
src/components/FormCreate/src/components/DictSelect.vue
0 → 100644
View file @
7c158af3
<!-- 数据字典 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
>
src/components/FormCreate/src/components/use
Currency
Select.tsx
→
src/components/FormCreate/src/components/use
Api
Select.tsx
View file @
7c158af3
import
request
from
'@/config/axios'
import
{
isEmpty
}
from
'@/utils/is'
import
{
Currency
SelectProps
}
from
'@/components/FormCreate/src/type'
import
{
getBoolDictOptions
,
getIntDictOptions
,
getStrDictOptions
}
from
'@/utils/dict
'
import
{
Api
SelectProps
}
from
'@/components/FormCreate/src/type'
import
{
jsonParse
}
from
'@/utils
'
export
const
use
CurrencySelect
=
(
option
:
Currency
SelectProps
)
=>
{
export
const
use
ApiSelect
=
(
option
:
Api
SelectProps
)
=>
{
return
defineComponent
({
name
:
option
.
name
,
props
:
{
...
...
@@ -18,47 +18,50 @@ export const useCurrencySelect = (option: CurrencySelectProps) => {
default
:
()
=>
option
.
valueField
??
'value'
},
// api 接口
restfu
l
:
{
ur
l
:
{
type
:
String
,
default
:
()
=>
option
.
restfu
l
??
''
default
:
()
=>
option
.
ur
l
??
''
},
//
字典
类型
dictType
:
{
//
请求
类型
method
:
{
type
:
String
,
default
:
''
default
:
'
GET
'
},
//
字典值类型 'str' | 'int' | 'bool'
d
ictValueType
:
{
//
请求参数
d
ata
:
{
type
:
String
,
default
:
'
str
'
default
:
''
},
// 选择器类型,下拉框 select、多选框 checkbox、单选框 radio
selectType
:
{
type
:
String
,
default
:
'select'
},
// 是否多选
multiple
:
{
type
:
Boolean
,
default
:
false
}
// // 是否多选
// multiple: {
// type: Boolean,
// default: false
// }
},
setup
(
props
)
{
const
attrs
=
useAttrs
()
const
options
=
ref
<
any
[]
>
([])
// 下拉数据
const
getOptions
=
async
()
=>
{
options
.
value
=
[]
// 字典选择器
if
(
option
.
isDict
)
{
options
.
value
=
getDictOptions
()
return
}
// 接口选择器
if
(
isEmpty
(
props
.
restfu
l
))
{
if
(
isEmpty
(
props
.
ur
l
))
{
return
}
// TODO 只支持 GET 查询,复杂下拉构建条件请使用业务表单
const
data
=
await
request
.
get
({
url
:
props
.
restful
})
let
data
=
[]
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
))
{
options
.
value
=
data
.
map
((
item
:
any
)
=>
({
label
:
item
[
props
.
labelField
],
...
...
@@ -66,26 +69,24 @@ export const useCurrencySelect = (option: CurrencySelectProps) => {
}))
return
}
console
.
log
(
`接口[
${
props
.
restful
}
] 返回结果不是一个数组`
)
}
// 获得字典配置
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
[]
}
console
.
log
(
`接口[
${
props
.
url
}
] 返回结果不是一个数组`
)
}
onMounted
(
async
()
=>
{
await
getOptions
()
})
// TODO puhui999: 单下拉多选的时候页面会卡住报错,下次解决
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
(
<
el
-
select
class=
"w-1/1"
{
...
attrs
}
>
{
options
.
value
.
map
((
item
,
index
)
=>
(
...
...
src/components/FormCreate/src/config/selectRule.ts
View file @
7c158af3
...
...
@@ -8,6 +8,15 @@ const selectRule = [
{
label
:
'下拉框'
,
value
:
'select'
},
{
label
:
'单选框'
,
value
:
'radio'
},
{
label
:
'多选框'
,
value
:
'checkbox'
}
],
// 参考 https://www.form-create.com/v3/guide/control 组件联动,单选框和多选框不需要多选属性
control
:
[
{
value
:
'select'
,
condition
:
'='
,
method
:
'hidden'
,
rule
:
[
'multiple'
]
}
]
},
{
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
}
src/components/FormCreate/src/config/useDictSelectRule.ts
View file @
7c158af3
import
{
generateUUID
}
from
'@/utils'
import
*
as
DictDataApi
from
'@/api/system/dict/dict.type'
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
...
...
src/components/FormCreate/src/config/useSelectRule.ts
View file @
7c158af3
import
{
generateUUID
}
from
'@/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'
/**
...
...
src/components/FormCreate/src/type/index.ts
View file @
7c158af3
import
{
Rule
}
from
'@form-create/element-ui'
//左侧拖拽按钮
//左侧拖拽按钮
//
左侧拖拽按钮
export
interface
MenuItem
{
label
:
string
name
:
string
icon
:
string
}
//左侧拖拽按钮分类
//
左侧拖拽按钮分类
export
interface
Menu
{
title
:
string
name
:
string
...
...
@@ -16,7 +16,7 @@ export interface Menu {
export
interface
MenuList
extends
Array
<
Menu
>
{}
//拖拽组件的规则
//
拖拽组件的规则
export
interface
DragRule
{
icon
:
string
name
:
string
...
...
@@ -33,11 +33,11 @@ export interface DragRule {
}
// 通用下拉组件 Props 类型
export
interface
Currency
SelectProps
{
export
interface
Api
SelectProps
{
name
:
string
// 组件名称
labelField
?:
string
// 选项标签
valueField
?:
string
// 选项的值
restful
?:
string
// api
接口
url
?:
string
// url
接口
isDict
?:
boolean
// 是否字典选择器
}
...
...
@@ -45,5 +45,5 @@ export interface CurrencySelectProps {
export
interface
SelectRuleOption
{
label
:
string
// label 名称
name
:
string
// 组件名称
props
?:
Rule
[]
// 组件规则
props
?:
any
[]
// 组件规则
}
src/components/FormCreate/src/useFormCreateDesigner.ts
View file @
7c158af3
...
...
@@ -8,6 +8,7 @@ import {
}
from
'./config'
import
{
Ref
}
from
'vue'
import
{
Menu
}
from
'@/components/FormCreate/src/type'
import
{
apiSelectRule
}
from
'@/components/FormCreate/src/config/selectRule'
/**
* 表单设计器增强 hook
...
...
@@ -50,36 +51,12 @@ export const useFormCreateDesigner = async (designer: Ref) => {
const
userSelectRule
=
useSelectRule
({
name
:
'UserSelect'
,
label
:
'用户选择器'
})
const
deptSelectRule
=
useSelectRule
({
name
:
'DeptSelect'
,
label
:
'部门选择器'
})
const
dictSelectRule
=
useDictSelectRule
()
const
restfulSelectRule
=
useSelectRule
({
name
:
'
Restful
Select'
,
const
apiSelectRule0
=
useSelectRule
({
name
:
'
Api
Select'
,
label
:
'接口选择器'
,
props
:
[
{
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'
}
}
]
props
:
[...
apiSelectRule
]
})
/**
* 构建系统字段菜单
*/
...
...
@@ -88,7 +65,7 @@ export const useFormCreateDesigner = async (designer: Ref) => {
designer
.
value
?.
removeMenuItem
(
'select'
)
designer
.
value
?.
removeMenuItem
(
'radio'
)
designer
.
value
?.
removeMenuItem
(
'checkbox'
)
const
components
=
[
userSelectRule
,
deptSelectRule
,
dictSelectRule
,
restfulSelectRule
]
const
components
=
[
userSelectRule
,
deptSelectRule
,
dictSelectRule
,
apiSelectRule0
]
const
menu
:
Menu
=
{
name
:
'system'
,
title
:
'系统字段'
,
...
...
src/plugins/formCreate/index.ts
View file @
7c158af3
...
...
@@ -19,28 +19,26 @@ import formCreate from '@form-create/element-ui'
import
install
from
'@form-create/element-ui/auto-import'
//======================= 自定义组件 =======================
import
{
UploadFile
,
UploadImg
,
UploadImgs
}
from
'@/components/UploadFile'
import
{
use
Currency
Select
}
from
'@/components/FormCreate'
import
{
use
Api
Select
}
from
'@/components/FormCreate'
import
{
Editor
}
from
'@/components/Editor'
import
DictSelect
from
'@/components/FormCreate/src/components/DictSelect.vue'
const
UserSelect
=
use
Currency
Select
({
const
UserSelect
=
use
Api
Select
({
name
:
'UserSelect'
,
labelField
:
'nickname'
,
valueField
:
'id'
,
restfu
l
:
'/system/user/simple-list'
ur
l
:
'/system/user/simple-list'
})
const
DeptSelect
=
use
Currency
Select
({
const
DeptSelect
=
use
Api
Select
({
name
:
'DeptSelect'
,
labelField
:
'name'
,
valueField
:
'id'
,
restfu
l
:
'/system/dept/simple-list'
ur
l
:
'/system/dept/simple-list'
})
const
RestfulSelect
=
useCurrencySelect
({
name
:
'RestfulSelect'
})
const
DictSelect
=
useCurrencySelect
({
name
:
'DictSelect'
,
isDict
:
true
const
ApiSelect
=
useApiSelect
({
name
:
'ApiSelect'
})
const
components
=
[
ElAside
,
ElPopconfirm
,
...
...
@@ -60,7 +58,7 @@ const components = [
DictSelect
,
UserSelect
,
DeptSelect
,
Restful
Select
,
Api
Select
,
Editor
]
...
...
src/utils/index.ts
View file @
7c158af3
...
...
@@ -435,3 +435,17 @@ export const areaReplace = (areaName: string) => {
.
replace
(
'自治区'
,
''
)
.
replace
(
'省'
,
''
)
}
/**
* 解析 JSON 字符串
*
* @param str
*/
export
function
jsonParse
(
str
:
string
)
{
try
{
return
JSON
.
parse
(
str
)
}
catch
(
e
)
{
console
.
log
(
`str[
${
str
}
] 不是一个 JSON 字符串`
)
return
''
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment