From 0f4b1e3db1963579e87c78a60eec23bce12b4bf1 Mon Sep 17 00:00:00 2001 From: wangyaqi Date: Thu, 15 Apr 2021 21:02:46 +0800 Subject: [PATCH] feat: uni-id mixin --- src/core/service/plugins/index.js | 6 +++ .../service/framework/plugins/index.js | 8 +++- .../runtime/wrapper/app-base-parser.js | 5 +++ src/shared/uni-id-mixin.js | 39 +++++++++++++++++++ src/shared/util.js | 2 + 5 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 src/shared/uni-id-mixin.js diff --git a/src/core/service/plugins/index.js b/src/core/service/plugins/index.js index 01aa6e1d8..78d9521fd 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 1cbe96dd6..d87bc8096 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 aa346c27e..ed8b57f18 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 000000000..170357bf4 --- /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 b1d99934b..e62e68952 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' -- GitLab