提交 434d5d8c 编写于 作者: Q qiang

chore: build

上级 6a24d2d9
......@@ -54,12 +54,59 @@ function errorHandler(err, instance, info) {
}
}
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 = uni.getStorageSync('uni_id_token') || '';
const tokenArr = token.split('.');
if (!token || tokenArr.length !== 3) {
return {
uid: null,
role: [],
permission: [],
tokenExpired: 0,
};
}
let userInfo;
try {
userInfo = JSON.parse(b64DecodeUnicode(tokenArr[1]));
}
catch (error) {
throw new Error('获取当前用户信息出错,详细错误信息为:' + error.message);
}
userInfo.tokenExpired = userInfo.exp * 1000;
delete userInfo.exp;
delete userInfo.iat;
return userInfo;
}
function uniIdMixin(globalProperties) {
globalProperties.uniIDHasRole = function (roleId) {
const { role } = getCurrentUserInfo();
return role.indexOf(roleId) > -1;
};
globalProperties.uniIDHasPermission = function (permissionId) {
const { permission } = getCurrentUserInfo();
return this.uniIDHasRole('admin') || permission.indexOf(permissionId) > -1;
};
globalProperties.uniIDTokenValid = function () {
const { tokenExpired } = getCurrentUserInfo();
return tokenExpired > Date.now();
};
}
function initApp(app) {
const appConfig = app._context.config;
if (isFunction(app._component.onError)) {
appConfig.errorHandler = errorHandler;
}
const globalProperties = appConfig.globalProperties;
uniIdMixin(globalProperties);
{
// 小程序,待重构,不再挂靠全局
globalProperties.$hasHook = hasHook;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册