diff --git a/src/core/service/plugins/index.js b/src/core/service/plugins/index.js index 01aa6e1d88615fcd65b7754b130eb9d7b7761347..78d9521fdde4968f1d67e57fb622ad4a6712f40d 100644 --- a/src/core/service/plugins/index.js +++ b/src/core/service/plugins/index.js @@ -24,6 +24,10 @@ import { getTabBarScrollPosition } from './app/router-guard' +import { + uniIdMixin +} from 'uni-shared' + function getMinId (routes) { let minId = 0 routes.forEach(route => { @@ -74,6 +78,8 @@ export default { lifecycleMixin(Vue) + uniIdMixin(Vue) + /* eslint-disable no-undef */ if (typeof __UNI_ROUTER_BASE__ !== 'undefined') { __uniConfig.router.base = __UNI_ROUTER_BASE__ diff --git a/src/platforms/app-plus/service/framework/plugins/index.js b/src/platforms/app-plus/service/framework/plugins/index.js index 1cbe96dd616d92d5bc6826ad412e3605ea350ca8..d87bc80968fab15e57f1e1afca3911a5793976db 100644 --- a/src/platforms/app-plus/service/framework/plugins/index.js +++ b/src/platforms/app-plus/service/framework/plugins/index.js @@ -22,6 +22,10 @@ import { vdSyncCallbacks } from '../subscribe-handlers/on-vd-sync-callback' +import { + uniIdMixin +} from 'uni-shared' + export default { install (Vue, options) { initVue(Vue) @@ -31,6 +35,8 @@ export default { initPolyfill(Vue) + uniIdMixin(Vue) + Vue.prototype.getOpenerEventChannel = function () { if (!this.$root.$scope.eventChannel) { this.$root.$scope.eventChannel = new EventChannel() @@ -94,4 +100,4 @@ export default { return callback ? undefined : result } } -} +} diff --git a/src/platforms/mp-weixin/runtime/wrapper/app-base-parser.js b/src/platforms/mp-weixin/runtime/wrapper/app-base-parser.js index aa346c27edc92e6c7e9b059dbb6ee2f0b4693da8..ed8b57f1806fa64463b49fde04b497cc699c7340 100644 --- a/src/platforms/mp-weixin/runtime/wrapper/app-base-parser.js +++ b/src/platforms/mp-weixin/runtime/wrapper/app-base-parser.js @@ -11,6 +11,10 @@ import { getEventChannel } from 'uni-helpers/navigate-to' +import { + uniIdMixin +} from 'uni-shared' + const hooks = [ 'onShow', 'onHide', @@ -101,6 +105,7 @@ export default function parseBaseApp (vm, { if (vm.$options.store) { Vue.prototype.$store = vm.$options.store } + uniIdMixin(Vue) Vue.prototype.mpHost = __PLATFORM__ diff --git a/src/shared/uni-id-mixin.js b/src/shared/uni-id-mixin.js new file mode 100644 index 0000000000000000000000000000000000000000..170357bf47623524954a5e4cebafae1eca1d045c --- /dev/null +++ b/src/shared/uni-id-mixin.js @@ -0,0 +1,39 @@ +function b64DecodeUnicode (str) { + return decodeURIComponent(atob(str).split('').map(function (c) { + return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2) + }).join('')) +} + +function getCurrentUserInfo () { + const token = (__PLATFORM__ === 'h5' || __PLATFORM__ === 'app-plus' ? uni : __GLOBAL__).getStorageSync('uni_id_token') || '' + const tokenArr = token.split('.') + if (!token || tokenArr.length !== 3) { + return { + uid: null, + role: [], + permission: [] + } + } + let userInfo + try { + userInfo = JSON.parse(b64DecodeUnicode(tokenArr[1])) + } catch (error) { + throw new Error('获取当前用户信息出错,详细错误信息为:' + error.message) + } + return userInfo +} + +export function uniIdMixin (Vue) { + Vue.prototype.uniIDHasRole = function (roleId) { + const { + role + } = getCurrentUserInfo() + return role.indexOf(roleId) > -1 + } + Vue.prototype.uniIDHasPermission = function (permissionId) { + const { + permission + } = getCurrentUserInfo() + return this.uniIDHasRole('admin') || permission.indexOf(permissionId) > -1 + } +} diff --git a/src/shared/util.js b/src/shared/util.js index b1d99934bcb53970229fb72e7334a0c6326f6e49..e62e68952c8a05eb156cd7dd8bdfb6601228d567 100644 --- a/src/shared/util.js +++ b/src/shared/util.js @@ -193,3 +193,5 @@ export function deepClone (vnodes, createElement) { return vnodes.map(cloneVNode) } + +export * from './uni-id-mixin'