From e69750a5345382e3817c7655681e05e90370b982 Mon Sep 17 00:00:00 2001 From: moxun Date: Sat, 28 Sep 2019 11:46:29 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AE=80=E5=8C=96=E6=95=B0=E6=8D=AE=E5=AD=97?= =?UTF-8?q?=E5=85=B8=EF=BC=8C=E6=94=AF=E6=8C=81=E5=8D=95=E7=BB=84=E4=BB=B6?= =?UTF-8?q?=E5=86=85=E5=A4=9A=E5=AD=97=E5=85=B8=E5=90=8C=E6=97=B6=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=EF=BC=8C=E5=85=A8=E5=B1=80=E6=B7=B7=E5=85=A5=EF=BC=8C?= =?UTF-8?q?=E7=BB=84=E4=BB=B6=E4=B8=8D=E9=85=8D=E7=BD=AE=E5=AD=97=E5=85=B8?= =?UTF-8?q?=E6=97=B6=EF=BC=8C=E4=B8=8D=E5=AF=B9data=E9=99=84=E5=8A=A0dict?= =?UTF-8?q?=E5=B1=9E=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/dict/Dict.js | 29 +++++++++++++++++++++++++++++ src/components/dict/index.js | 29 +++++++++++++++++++++++++++++ src/main.js | 2 ++ src/views/system/user/index.vue | 13 +++++-------- 4 files changed, 65 insertions(+), 8 deletions(-) create mode 100644 src/components/dict/Dict.js create mode 100644 src/components/dict/index.js diff --git a/src/components/dict/Dict.js b/src/components/dict/Dict.js new file mode 100644 index 0000000..80d046e --- /dev/null +++ b/src/components/dict/Dict.js @@ -0,0 +1,29 @@ +import Vue from 'vue' +import { get as getDictDetail } from '@/api/dictDetail' + +export default class Dict { + constructor (dict) { + this.dict = dict + } + + async init (names, completeCallback) { + if (names === undefined || name === null) { + throw new Error('need dict names') + } + const ps = [] + names.forEach(n => { + Vue.set(this.dict.dict, n, {}) + Vue.set(this.dict.label, n, {}) + Vue.set(this.dict, n, []) + ps.push(getDictDetail(n).then(data => { + this.dict[n].splice(0, 0, ...data.content) + data.content.forEach(d => { + Vue.set(this.dict.dict[n], d.value, d) + Vue.set(this.dict.label[n], d.value, d.label) + }) + })) + }) + await Promise.all(ps) + completeCallback() + } +} diff --git a/src/components/dict/index.js b/src/components/dict/index.js new file mode 100644 index 0000000..4d27a7b --- /dev/null +++ b/src/components/dict/index.js @@ -0,0 +1,29 @@ +import Dict from './Dict' + +const install = function (Vue) { + Vue.mixin({ + data () { + if (this.$options.dicts instanceof Array) { + const dict = { + dict: {}, + label: {}, + } + return { + dict, + } + } + return {} + }, + created () { + if (this.$options.dicts instanceof Array) { + new Dict(this.dict).init(this.$options.dicts, () => { + this.$nextTick(() => { + this.$emit('dictReady') + }) + }) + } + }, + }) +} + +export default { install } diff --git a/src/main.js b/src/main.js index 905dfa8..dca6efa 100644 --- a/src/main.js +++ b/src/main.js @@ -11,6 +11,7 @@ import '@/styles/index.scss' // global css import App from './App' import router from './router/routers' import permission from './components/permission' +import dict from './components/dict' import store from './store' import '@/icons' // icon @@ -19,6 +20,7 @@ import './router/index' // permission control Vue.use(mavonEditor) Vue.use(permission) +Vue.use(dict) Vue.use(ElementUI, { locale }) Vue.config.productionTip = false diff --git a/src/views/system/user/index.vue b/src/views/system/user/index.vue index 0859757..460486d 100644 --- a/src/views/system/user/index.vue +++ b/src/views/system/user/index.vue @@ -1,7 +1,7 @@