UNPKG

enc-framework

Version:

enc-framework 核心组件.

122 lines (94 loc) 2.94 kB
import Cookies from 'js-cookie'; // sessionContex对象 function SessionContext(params) { // 参数 this.params = params || {}; // token信息 this.tokenInfoKey = "_token_info_"; // systemInfoKey this.systemInfoKey = "_system_info_"; // platformInfoKey this.platformInfoKey = "_platform_info_"; return this; } //设置token信息 SessionContext.prototype.setTokenInfo = function (tokenInfo) { tokenInfo.getTokenDateStr = new Date().getTime() Cookies.set(this.tokenInfoKey, JSON.stringify(tokenInfo),{path:tokenInfo.cookiepath}); } //因为cookie 缓存用户token信息时候,数据过多导致保存失败 所以分开存储 //设置用户信息 SessionContext.prototype.setTokenUser = function (userInfo) { Cookies.set('_user_', JSON.stringify(userInfo)); } //获取用户信息 SessionContext.prototype.getTokenUser = function (userInfo) { var tokenUser = Cookies.get('_user_'); if (tokenUser&&tokenUser.toString()!='undefined') { return JSON.parse(tokenUser); } return null; } //获取用户Token信息 SessionContext.prototype.getTokenInfo = function () { // var tokenInfo = localStorage.getItem(this.tokenInfoKey); var tokenInfo = Cookies.get(this.tokenInfoKey); if (tokenInfo) { return JSON.parse(tokenInfo); } return null; } //获取token信息 SessionContext.prototype.getToken = function () { var tokenInfo = this.getTokenInfo(); if (tokenInfo) { return tokenInfo.access_token; } return null; } //清理token信息 SessionContext.prototype.clearTokenInfo = function (cookiepath) { //localStorage.removeItem(this.tokenInfoKey); Cookies.remove(this.tokenInfoKey,{path:cookiepath}); Cookies.remove('_user_',{path:cookiepath}); } //设置系统信息 SessionContext.prototype.setSystemInfo = function (systemInfo) { localStorage.setItem(this.systemInfoKey, JSON.stringify(systemInfo)); } //获取系统信息 SessionContext.prototype.getSystemInfo = function () { var systemInfo = localStorage.getItem(this.systemInfoKey); if (systemInfo) { return JSON.parse(systemInfo); } return null; } //设置平台信息 SessionContext.prototype.setPlatformInfo = function (platformInfo) { localStorage.setItem(this.platformInfoKey, JSON.stringify(platformInfo)); } //获取平台信息 SessionContext.prototype.getPlatformInfo = function () { var platformInfo = localStorage.getItem(this.platformInfoKey); if (platformInfo) { return JSON.parse(platformInfo); } return null; } //清理系统信息 SessionContext.prototype.clearSystemInfo = function () { localStorage.removeItem(this.systemInfoKey); } // 新建context对象 const context = new SessionContext(); // session上下文持有对象 const sessionContextHodler = { // context对象 context : context, // 获取上下文对象 getContext(){ return this.context; } } export default sessionContextHodler