1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- /* eslint-disable no-console */
- export default {
- get user() {
- const val = sessionStorage.getItem('user');
- if (!val || val == '') return null;
- try {
- if (val) return JSON.parse(val);
- } catch (err) {
- console.error(err);
- }
- return null;
- },
- set user(userinfo) {
- if (!userinfo) {
- sessionStorage.removeItem('user');
- } else {
- sessionStorage.setItem('user', JSON.stringify(userinfo));
- }
- if (this.unit) {
- this.lastUnit = this.unit;
- }
- },
- get token() {
- return sessionStorage.getItem('token') || '';
- },
- get scope() {
- return sessionStorage.getItem('x-domain') || '';
- },
- set token(token) {
- if (!token) {
- sessionStorage.removeItem('token');
- } else {
- sessionStorage.setItem('token', token);
- }
- },
- get isGuest() {
- return !this.user || this.user.role === 'guest';
- },
- get unit() {
- if (!this.user || this.user.iss !== 'platform') return undefined;
- const unit = this.user.sub.split('@', 2)[1] || 'master';
- return unit;
- },
- get platform() {
- const unit = this.unit || this.lastUnit;
- return unit === 'master' ? 'master' : 'school';
- },
- set lastUnit(value) {
- localStorage.setItem('unit', value);
- },
- get lastUnit() {
- return localStorage.getItem('unit');
- },
- get roles() {
- const val = sessionStorage.getItem('roles');
- if (!val) return [];
- try {
- if (val) return JSON.parse(val);
- } catch (err) {
- console.error(err);
- }
- return [];
- },
- set roles(roles) {
- if (!roles) {
- sessionStorage.removeItem('roles');
- } else {
- sessionStorage.setItem('roles', roles && JSON.stringify(roles));
- }
- },
- save({ userinfo, token, roles }) {
- this.user = userinfo;
- this.token = token;
- this.roles = roles || [];
- },
- };
|