feat: 支持扩展属性

This commit is contained in:
aringlai 2021-01-27 14:46:09 +08:00
parent 119a50f370
commit 4b39499624
2 changed files with 87 additions and 15 deletions

View File

@ -11,16 +11,26 @@ Object.keys(_ENUMS).forEach(key => {
* 获取枚举键值如不传key则返回name的枚举数组
* @param {string} name 枚举名称
* @param {string} key 枚举键名称
* @param {string} dir 枚举键值路径默认value格式如value.propName、value.propName[0]
* @param {{
* dir: string
* extend: Array<{
* key:string
* dir:string
* transfer: Function
* }>}} opt 配置项
*/
function get(name, key, opt = { dir: 'value' }) {
function get(name, key, opt = { dir: 'value', extend: []}) {
if (Object.prototype.toString.call(key) === '[object Object]') {
opt = key
key = ''
}
let list = ENUMS[name] || []
if (key) {
let res = list.filter(item => item.key === key)[0]
if (!res) return key
return readonly(parseValueDir(res.value, opt.dir) || key)
} else {
return readonly(list)
return readonly(format(list, opt.extend))
}
}
@ -49,17 +59,48 @@ function push(name, _enum, opt = { keyName: '', valueName: '' }) {
/**
* 基于现有的枚举,连接上新的枚举后返回新的枚举
* @param {string} name 枚举名称
* @param {Array<Object|Array>} _enum 枚举数组,数组元素可以是数组或者对象
* @param {object} opt {keyName: 'key', valueName: ''} keyName: 指定枚举键名称取值属性 valueName 指定枚举键值取值属性 before: 是否添加在现有的之前
* @param {Array<any|Array>} _enum 枚举数组,数组元素可以是数组或者对象
* @param {{
* keyName: string,
* valueName: string,
* before: boolean,
* extend: Array<{
* key: string
* dir: string
* transfer: Function
* }>
* }} opt 配置项keyName: 指定枚举键名称取值属性 valueName 指定枚举键值取值属性 before: 是否添加在现有的之前
*/
function concat(name, _enum, opt = { keyName: '', valueName: '', before: false }) {
function concat(name, _enum, opt = { keyName: '', valueName: '', before: false, extend: [] }) {
let list = ENUMS[name] || []
let partList = convert(name, _enum, opt) || []
if (opt.before) {
return readonly(partList.concat(list))
list = partList.concat(list)
} else {
return readonly(list.concat(partList))
list = list.concat(partList)
}
return readonly(format(list, extend))
}
/**
* 格式化枚举
* @param {Array} _enum 枚举数组
* @param {Array<{key:string, dir:string, transfer: Function}>} extend 格式化规则
*/
function format(_enum = [], extend = []) {
if (!extend || extend.length <= 0) return _enum;
return _enum.map(item => {
let _item = {...item}
extend.forEach(fItem => {
if (!fItem.key) return
if (typeof fItem.transfer === 'function') {
_item[fItem.key] = fItem.transfer(item)
} else {
_item[fItem.key] = parseValueDir(item.value, fItem.dir)
}
})
return _item
})
}
/**
@ -90,7 +131,7 @@ function parseValueDir(value, dir='value') {
* 转换传入的枚举数组
* @param {string} name 枚举名称
* @param {Array<Object|Array>} _enum 枚举数组,数组元素可以是数组或者对象
* @param {object} opt {keyName: 'key', valueName: ''} keyName: 指定枚举键名称取值属性 valueName 指定枚举键值取值属性
* @param {{keyName: 'key', valueName: string}} opt keyName: 指定枚举键名称取值属性 valueName 指定枚举键值取值属性
*/
function convert(name, _enum, opt = { keyName: '', valueName: '' }) {
if (!name) {

View File

@ -7,7 +7,8 @@
<input />
<h4>数据字典</h4>
<div v-for="item in enumsGet('status')" :key="item.key">{{item.value}}{{item.key}}</div>
<div v-for="item in enumsGet('constomStatus')" :key="item.key">{{item.value}}{{item.key}}</div>
<div v-for="item in roles" :key="item.key">{{item.name}}{{item.disabled}}</div>
<div>{{enumsGet('roles', '2', { dir: 'eName' })}}</div>
</div>
</template>
<config>
@ -29,10 +30,39 @@ export default {
const localI18n = useI18n();
const router = useRouter();
const accessId = ref('/onepiece1');
console.log(enums.get('status'));
const constomStatus = enums.push('constomStatus', [{ id: 1, desc: '是' }, { id: 0, desc: '否' }], { keyName: 'id', valueName: 'desc' });
console.log(constomStatus);
console.log(enums.concat('status', [['2', '未定义']]));
enums.push('roles', [
{
id: '1',
cName: '系统管理员',
eName: 'System',
perm: ['1', '2', '3']
},
{
id: '2',
cName: '业务管理员',
eName: 'Business',
perm: ['1', '2']
},
{
id: '3',
cName: '普通用户',
eName: 'User',
perm: ['1']
}
], { keyName: 'id' });
const roles = enums.get('roles', {
extend: [
{
key: 'name',
dir: 'cName'
},
{
key: 'disabled',
transfer: item => item.value.perm.some(i => i >= 2)
}
]
});
console.log(roles);
onMounted(() => {
console.log(router);
setTimeout(() => {
@ -51,7 +81,8 @@ export default {
fes,
accessOnepicess,
t: localI18n.t,
enumsGet: enums.get
enumsGet: enums.get,
roles
};
}
};