oak-general-business/lib/context/FrontendRuntimeContext.js

132 lines
4.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.FrontendRuntimeContext = void 0;
const assert_1 = require("oak-domain/lib/utils/assert");
const FrontendRuntimeContext_1 = require("oak-frontend-base/es/context/FrontendRuntimeContext");
const Exception_1 = require("../types/Exception");
const types_1 = require("oak-domain/lib/types");
;
class FrontendRuntimeContext extends FrontendRuntimeContext_1.FrontendRuntimeContext {
application;
token;
constructor(store, features) {
super(store, features);
this.application = features.application;
this.token = features.token;
}
async getSerializedData() {
const data = await super.getSerializedData();
const setAppId = async () => {
// appId必须要取到不能失败
const setInner = (resolve, reject) => {
try {
if (!this.application) {
// 有可能在系统初始化的时候调用this.application还没建立
resolve(undefined);
return;
}
const appId = this.application.getApplicationId();
(0, assert_1.assert)(appId);
Object.assign(data, {
a: appId,
});
resolve(undefined);
}
catch (err) {
if (err instanceof Exception_1.OakApplicationLoadingException) {
const fn = this.application.subscribe(() => {
fn();
setInner(resolve, reject);
});
}
else {
reject(err);
}
}
};
return new Promise((resolve, reject) => setInner(resolve, reject));
};
await setAppId();
const setTokenValue = async () => {
const setInner = (resolve, reject) => {
try {
if (!this.token) {
// 有可能在系统初始化的时候调用this.token还没建立
resolve(undefined);
return;
}
const tokenValue = this.token.getTokenValue();
if (tokenValue) {
Object.assign(data, {
t: tokenValue,
});
}
resolve(undefined);
}
catch (err) {
if (err instanceof Exception_1.OakUserInfoLoadingException) {
const fn = this.token.subscribe(() => {
fn();
setInner(resolve, reject);
});
}
else {
reject(err);
}
}
};
return new Promise((resolve, reject) => setInner(resolve, reject));
};
await setTokenValue();
Object.assign(data, {
v: this.application.getVersion(),
});
return data;
}
getApplicationId() {
return this.application?.getApplicationId();
}
getSystemId() {
const app = this.application?.getApplication();
return app?.systemId;
}
getApplication() {
return this.application?.getApplication();
}
getTokenValue() {
return this.token?.getTokenValue();
}
getToken(allowUnloggedIn) {
return this.token?.getToken(allowUnloggedIn);
}
getCurrentUserId(allowUnloggedIn) {
return this.token?.getUserId(allowUnloggedIn);
}
isRoot() {
return this.token?.isRoot() || false;
}
isReallyRoot() {
return this.token?.isReallyRoot() || false;
}
allowUserUpdate() {
const userInfo = this.token?.getUserInfo();
if (userInfo) {
const { userState } = userInfo;
if (userState === 'disabled') {
throw new Exception_1.OakUserDisabledException('您的帐号已经被禁用,请联系客服');
}
else if (['merged'].includes(userState)) {
throw new Exception_1.OakTokenExpiredException('您的登录状态有异常,请重新登录 ');
}
else {
(0, assert_1.assert)(userState === 'normal' || userState === 'shadow');
}
return true;
}
throw new types_1.OakUnloggedInException('您尚未登录');
}
}
exports.FrontendRuntimeContext = FrontendRuntimeContext;
;
exports.default = FrontendRuntimeContext;