import _isObject from 'lodash/isObject' ; import _get from 'lodash/get' ; import _isNull from 'lodash/isNull' ; import _isNil from 'lodash/isNil' ; import jwtDecode from 'jwt-decode' ; import { getStorage } from '@shared/manager/storage' ; param jwt export function getJWTPayload < T > ( jwt ) { try { const decoded = jwtDecode < T > ( jwt ) ; return decoded ; } catch ( e ) { console . error ( `getJWTInfo Error: [jwt: ${ jwt }]` , e ) ; } return { } ; } let _userJWT = null ; /**
 * 设置用户登录标识
 */ export async function setUserJWT ( jwt ) { _userJWT = jwt ; await getStorage ( ) . set ( 'jsonwebtoken' , jwt ) ; } /**
 * 获取用户登录标识
 */ export async function getUserJWT ( ) { if ( _isNull ( _userJWT ) ) { const jwt = await getStorage ( ) . get ( 'jsonwebtoken' ) ; _userJWT = jwt ; return jwt ; } return _userJWT ; } export interface JWTUserInfoData { name ? : string ; uuid ? : string ; avatar ? : string ; } /**
 * 获取token中的明文信息
 * 明确需要返回一个对象
 */ export async function getJWTUserInfo ( ) { try { const token = await getUserJWT ( ) ; const info = getJWTPayload ( token ) ; if ( _isObject ( info ) ) { return info ; } } catch ( e ) { console . error ( 'getJWTInfo Error:' , e ) ; } return { } ; }