From 4b39499624b387c963d413376ed36af3240c568a Mon Sep 17 00:00:00 2001 From: aringlai Date: Wed, 27 Jan 2021 14:46:09 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=E6=89=A9=E5=B1=95?= =?UTF-8?q?=E5=B1=9E=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../fes-plugin-enums/src/runtime/core.tpl | 59 ++++++++++++++++--- packages/fes-template/src/pages/index.vue | 43 ++++++++++++-- 2 files changed, 87 insertions(+), 15 deletions(-) diff --git a/packages/fes-plugin-enums/src/runtime/core.tpl b/packages/fes-plugin-enums/src/runtime/core.tpl index 6977d31b..e5789ce1 100644 --- a/packages/fes-plugin-enums/src/runtime/core.tpl +++ b/packages/fes-plugin-enums/src/runtime/core.tpl @@ -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} _enum 枚举数组,数组元素可以是数组或者对象 - * @param {object} opt {keyName: 'key', valueName: ''} keyName: 指定枚举键名称取值属性 valueName 指定枚举键值取值属性 before: 是否添加在现有的之前 + * @param {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} _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) { diff --git a/packages/fes-template/src/pages/index.vue b/packages/fes-template/src/pages/index.vue index a08cbeda..bf40156a 100644 --- a/packages/fes-template/src/pages/index.vue +++ b/packages/fes-template/src/pages/index.vue @@ -7,7 +7,8 @@

数据字典

{{item.value}}:{{item.key}}
-
{{item.value}}:{{item.key}}
+
{{item.name}}:{{item.disabled}}
+
{{enumsGet('roles', '2', { dir: 'eName' })}}
@@ -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 }; } };