提交 18ca489f 编写于 作者: 雪洛's avatar 雪洛

feat: uniIDHasRole uniIDHasPermission

上级 8944889e
......@@ -5,12 +5,15 @@ import { isFunction } from '@vue/shared'
import { applyOptions } from './componentOptions'
import { set, hasHook, callHook } from './componentInstance'
import { errorHandler } from './appConfig'
import { uniIdMixin } from './uni-id-mixin'
export function initApp(app: App) {
const appConfig = app._context.config
if (isFunction((app._component as any).onError)) {
appConfig.errorHandler = errorHandler
}
const globalProperties = appConfig.globalProperties
uniIdMixin(globalProperties)
if (__PLATFORM__ !== 'h5' && __PLATFORM__ !== 'app') {
// 小程序,待重构,不再挂靠全局
globalProperties.$hasHook = hasHook
......
function b64DecodeUnicode(str: string): string {
return decodeURIComponent(
atob(str)
.split('')
.map(function (c) {
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2)
})
.join('')
)
}
interface UniIdUserInfo {
uid: null | string
role: Array<string>
permission: Array<string>
tokenExpired: number
}
function getCurrentUserInfo(): UniIdUserInfo {
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
}
export function uniIdMixin(globalProperties: Record<string, any>): void {
globalProperties.uniIDHasRole = function (roleId: string): boolean {
const { role } = getCurrentUserInfo()
return role.indexOf(roleId) > -1
}
globalProperties.uniIDHasPermission = function (
permissionId: string
): boolean {
const { permission } = getCurrentUserInfo()
return this.uniIDHasRole('admin') || permission.indexOf(permissionId) > -1
}
globalProperties.uniIDTokenValid = function (): boolean {
const { tokenExpired } = getCurrentUserInfo()
return tokenExpired > Date.now()
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册