1、 getTokenValue增加参数allowUnloggedIn 2、FrontendRuntimeContext setTokenValue不强制

This commit is contained in:
wkj 2024-03-25 13:46:15 +08:00
parent 9136ba3c02
commit 65c022c0f9
10 changed files with 160 additions and 218 deletions

View File

@ -23,7 +23,7 @@ export declare abstract class FrontendRuntimeContext<ED extends EntityDict & Bas
getApplicationId(): string;
getSystemId(): string | undefined;
getApplication(): Partial<import("../oak-app-domain/Application/Schema").Schema> | undefined;
getTokenValue(): string | undefined;
getTokenValue(allowUnloggedIn?: boolean): string | undefined;
getToken(allowUnloggedIn?: boolean): Partial<ED["token"]["Schema"]> | undefined;
getCurrentUserId(allowUnloggedIn?: boolean): string | undefined;
isRoot(): boolean;

View File

@ -23,15 +23,13 @@ export class FrontendRuntimeContext extends Frc {
a: appId,
});
resolve(undefined);
}
catch (err) {
} catch (err) {
if (err instanceof OakApplicationLoadingException) {
const fn = this.application.subscribe(() => {
fn();
setInner(resolve, reject);
});
}
else {
} else {
reject(err);
}
}
@ -42,22 +40,20 @@ export class FrontendRuntimeContext extends Frc {
const setTokenValue = async () => {
const setInner = (resolve, reject) => {
try {
const tokenValue = this.token.getTokenValue();
const tokenValue = this.token.getTokenValue(true);
if (tokenValue) {
Object.assign(data, {
t: tokenValue,
});
}
resolve(undefined);
}
catch (err) {
} catch (err) {
if (err instanceof OakUserInfoLoadingException) {
const fn = this.token.subscribe(() => {
fn();
setInner(resolve, reject);
});
}
else {
} else {
reject(err);
}
}
@ -77,8 +73,8 @@ export class FrontendRuntimeContext extends Frc {
getApplication() {
return this.application?.getApplication();
}
getTokenValue() {
return this.token?.getTokenValue();
getTokenValue(allowUnloggedIn) {
return this.token?.getTokenValue(allowUnloggedIn);
}
getToken(allowUnloggedIn) {
return this.token?.getToken(allowUnloggedIn);
@ -97,12 +93,14 @@ export class FrontendRuntimeContext extends Frc {
if (userInfo) {
const { userState } = userInfo;
if (userState === 'disabled') {
throw new OakUserDisabledException('您的帐号已经被禁用,请联系客服');
}
else if (['merged'].includes(userState)) {
throw new OakTokenExpiredException('您的登录状态有异常,请重新登录 ');
}
else {
throw new OakUserDisabledException(
'您的帐号已经被禁用,请联系客服'
);
} else if (['merged'].includes(userState)) {
throw new OakTokenExpiredException(
'您的登录状态有异常,请重新登录 '
);
} else {
assert(userState === 'normal' || userState === 'shadow');
}
return true;

View File

@ -22,9 +22,9 @@ export declare class Token<ED extends EntityDict, Cxt extends BackendRuntimeCont
}): Promise<void>;
loginWechatMp(): Promise<void>;
syncUserInfoWechatMp(): Promise<void>;
logout(): Promise<void>;
removeToken(disablePublish?: boolean): void;
getTokenValue(): string | undefined;
logout(dontPublish?: boolean): Promise<void>;
removeToken(dontPublish?: boolean): void;
getTokenValue(allowUnloggedIn?: boolean): string | undefined;
getToken(allowUnloggedIn?: boolean): Partial<ED["token"]["Schema"]> | undefined;
getUserId(allowUnloggedIn?: boolean): NonNullable<ED["token"]["Schema"]["userId"]> | undefined;
getUserInfo(): NonNullable<ED["token"]["Schema"]["user"]> | undefined;

View File

@ -11,41 +11,6 @@ export class Token extends Feature {
storage;
async loadSavedToken() {
let tokenValue = await this.storage.load(LOCAL_STORAGE_KEYS.token);
/* if (!tokenValue) {
// 历史数据原来用的key太随意
tokenValue = await this.storage.load('token:token');
if (tokenValue) {
await this.storage.save(LOCAL_STORAGE_KEYS.token, tokenValue);
await this.storage.remove('token:token');
}
} */
// if (tokenValue) {
// const env = await this.environment.getEnv();
// try {
// const { result } = await this.cache.exec(
// 'refreshToken',
// {
// tokenValue,
// env,
// },
// undefined,
// true,
// true
// );
// if (this.tokenValue !== result) {
// this.tokenValue = result;
// await this.storage.save(LOCAL_STORAGE_KEYS.token, result);
// }
// }
// catch (err) {
// // refresh出了任何错都无视直接放弃此token
// console.warn(err);
// this.tokenValue = undefined;
// this.removeToken(true);
// }
// } else {
// this.tokenValue = undefined;
// }
await this.refreshTokenData(tokenValue);
this.publish();
}
@ -56,12 +21,18 @@ export class Token extends Feature {
this.environment = environment;
this.tokenValue = ''; // 置个空字符串代表还在load storage缓存的数据
this.loadSavedToken();
if (process.env.OAK_PLATFORM === 'web' && (process.env.NODE_ENV !== 'development' || process.env.PROD === 'true')) {
if (
process.env.OAK_PLATFORM === 'web' &&
(process.env.NODE_ENV !== 'development' ||
process.env.PROD === 'true')
) {
// 纯前台模式 多窗口时不监听storage
// 在web下可能多窗口一个窗口更新了token其它窗口应跟着变
window.addEventListener('storage', async (e) => {
if (e.key === LOCAL_STORAGE_KEYS.token) {
this.tokenValue = e.newValue ? JSON.parse(e.newValue) : undefined;
this.tokenValue = e.newValue
? JSON.parse(e.newValue)
: undefined;
await this.refreshTokenData(this.tokenValue);
this.publish();
}
@ -72,35 +43,44 @@ export class Token extends Feature {
if (tokenValue) {
const env = await this.environment.getEnv();
try {
const { result } = await this.cache.exec('refreshToken', {
tokenValue,
env,
}, undefined, true, true);
const { result } = await this.cache.exec(
'refreshToken',
{
tokenValue,
env,
},
undefined,
true,
true
);
if (this.tokenValue !== result) {
this.tokenValue = result;
await this.storage.save(LOCAL_STORAGE_KEYS.token, result);
}
}
catch (err) {
} catch (err) {
// refresh出了任何错都无视直接放弃此token
console.warn(err);
this.tokenValue = undefined;
this.removeToken(true);
}
}
else {
} else {
this.tokenValue = undefined;
}
}
async loginByMobile(mobile, password, captcha, disableRegister) {
const env = await this.environment.getEnv();
const { result } = await this.cache.exec('loginByMobile', {
password,
mobile,
captcha,
disableRegister,
env,
}, undefined, true);
const { result } = await this.cache.exec(
'loginByMobile',
{
password,
mobile,
captcha,
disableRegister,
env,
},
undefined,
true
);
this.tokenValue = result;
await this.storage.save(LOCAL_STORAGE_KEYS.token, result);
this.publish();
@ -141,7 +121,12 @@ export class Token extends Feature {
const info = await wx.getUserProfile({
desc: '同步微信昵称和头像信息',
});
const { userInfo: { nickName: nickname, avatarUrl }, encryptedData, signature, iv, } = info;
const {
userInfo: { nickName: nickname, avatarUrl },
encryptedData,
signature,
iv,
} = info;
await this.cache.exec('syncUserInfoWechatMp', {
nickname,
avatarUrl,
@ -151,21 +136,27 @@ export class Token extends Feature {
});
this.publish();
}
async logout() {
await this.cache.exec('logout', {
tokenValue: this.tokenValue,
}, undefined, undefined, true);
this.removeToken();
async logout(dontPublish) {
await this.cache.exec(
'logout',
{
tokenValue: this.tokenValue,
},
undefined,
undefined,
true
);
this.removeToken(dontPublish);
}
removeToken(disablePublish) {
removeToken(dontPublish) {
this.tokenValue = undefined;
this.storage.remove(LOCAL_STORAGE_KEYS.token);
if (!disablePublish) {
if (!dontPublish) {
this.publish();
}
}
getTokenValue() {
if (this.tokenValue === '') {
getTokenValue(allowUnloggedIn) {
if (!allowUnloggedIn && this.tokenValue === '') {
throw new OakUserInfoLoadingException();
}
return this.tokenValue;
@ -231,7 +222,10 @@ export class Token extends Feature {
async switchTo(userId) {
const currentUserId = this.getUserId();
if (currentUserId === userId) {
throw new OakRowInconsistencyException(undefined, '您已经是当前用户');
throw new OakRowInconsistencyException(
undefined,
'您已经是当前用户'
);
}
await this.cache.exec('switchTo', {
userId,

View File

@ -23,7 +23,7 @@ export declare abstract class FrontendRuntimeContext<ED extends EntityDict & Bas
getApplicationId(): string;
getSystemId(): string | undefined;
getApplication(): Partial<import("../oak-app-domain/Application/Schema").Schema> | undefined;
getTokenValue(): string | undefined;
getTokenValue(allowUnloggedIn?: boolean): string | undefined;
getToken(allowUnloggedIn?: boolean): Partial<ED["token"]["Schema"]> | undefined;
getCurrentUserId(allowUnloggedIn?: boolean): string | undefined;
isRoot(): boolean;

View File

@ -26,15 +26,16 @@ class FrontendRuntimeContext extends oak_frontend_base_1.FrontendRuntimeContext
a: appId,
});
resolve(undefined);
}
catch (err) {
if (err instanceof Exception_1.OakApplicationLoadingException) {
} catch (err) {
if (
err instanceof
Exception_1.OakApplicationLoadingException
) {
const fn = this.application.subscribe(() => {
fn();
setInner(resolve, reject);
});
}
else {
} else {
reject(err);
}
}
@ -45,22 +46,22 @@ class FrontendRuntimeContext extends oak_frontend_base_1.FrontendRuntimeContext
const setTokenValue = async () => {
const setInner = (resolve, reject) => {
try {
const tokenValue = this.token.getTokenValue();
const tokenValue = this.token.getTokenValue(true);
if (tokenValue) {
Object.assign(data, {
t: tokenValue,
});
}
resolve(undefined);
}
catch (err) {
if (err instanceof Exception_1.OakUserInfoLoadingException) {
} catch (err) {
if (
err instanceof Exception_1.OakUserInfoLoadingException
) {
const fn = this.token.subscribe(() => {
fn();
setInner(resolve, reject);
});
}
else {
} else {
reject(err);
}
}
@ -80,8 +81,8 @@ class FrontendRuntimeContext extends oak_frontend_base_1.FrontendRuntimeContext
getApplication() {
return this.application?.getApplication();
}
getTokenValue() {
return this.token?.getTokenValue();
getTokenValue(allowUnloggedIn) {
return this.token?.getTokenValue(allowUnloggedIn);
}
getToken(allowUnloggedIn) {
return this.token?.getToken(allowUnloggedIn);
@ -100,13 +101,17 @@ class FrontendRuntimeContext extends oak_frontend_base_1.FrontendRuntimeContext
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');
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;
}

View File

@ -22,9 +22,9 @@ export declare class Token<ED extends EntityDict, Cxt extends BackendRuntimeCont
}): Promise<void>;
loginWechatMp(): Promise<void>;
syncUserInfoWechatMp(): Promise<void>;
logout(): Promise<void>;
removeToken(disablePublish?: boolean): void;
getTokenValue(): string | undefined;
logout(dontPublish?: boolean): Promise<void>;
removeToken(dontPublish?: boolean): void;
getTokenValue(allowUnloggedIn?: boolean): string | undefined;
getToken(allowUnloggedIn?: boolean): Partial<ED["token"]["Schema"]> | undefined;
getUserId(allowUnloggedIn?: boolean): NonNullable<ED["token"]["Schema"]["userId"]> | undefined;
getUserInfo(): NonNullable<ED["token"]["Schema"]["user"]> | undefined;

View File

@ -14,41 +14,6 @@ class Token extends Feature_1.Feature {
storage;
async loadSavedToken() {
let tokenValue = await this.storage.load(constants_1.LOCAL_STORAGE_KEYS.token);
/* if (!tokenValue) {
// 历史数据原来用的key太随意
tokenValue = await this.storage.load('token:token');
if (tokenValue) {
await this.storage.save(LOCAL_STORAGE_KEYS.token, tokenValue);
await this.storage.remove('token:token');
}
} */
// if (tokenValue) {
// const env = await this.environment.getEnv();
// try {
// const { result } = await this.cache.exec(
// 'refreshToken',
// {
// tokenValue,
// env,
// },
// undefined,
// true,
// true
// );
// if (this.tokenValue !== result) {
// this.tokenValue = result;
// await this.storage.save(LOCAL_STORAGE_KEYS.token, result);
// }
// }
// catch (err) {
// // refresh出了任何错都无视直接放弃此token
// console.warn(err);
// this.tokenValue = undefined;
// this.removeToken(true);
// }
// } else {
// this.tokenValue = undefined;
// }
await this.refreshTokenData(tokenValue);
this.publish();
}
@ -154,21 +119,21 @@ class Token extends Feature_1.Feature {
});
this.publish();
}
async logout() {
async logout(dontPublish) {
await this.cache.exec('logout', {
tokenValue: this.tokenValue,
}, undefined, undefined, true);
this.removeToken();
this.removeToken(dontPublish);
}
removeToken(disablePublish) {
removeToken(dontPublish) {
this.tokenValue = undefined;
this.storage.remove(constants_1.LOCAL_STORAGE_KEYS.token);
if (!disablePublish) {
if (!dontPublish) {
this.publish();
}
}
getTokenValue() {
if (this.tokenValue === '') {
getTokenValue(allowUnloggedIn) {
if (!allowUnloggedIn && this.tokenValue === '') {
throw new Exception_2.OakUserInfoLoadingException();
}
return this.tokenValue;

View File

@ -85,7 +85,7 @@ export abstract class FrontendRuntimeContext<
const setTokenValue = async() => {
const setInner = (resolve: (value: unknown) => void, reject: (reason?: any) => void) => {
try {
const tokenValue = this.token.getTokenValue();
const tokenValue = this.token.getTokenValue(true);
if (tokenValue) {
Object.assign(data, {
t: tokenValue,
@ -126,8 +126,8 @@ export abstract class FrontendRuntimeContext<
return this.application?.getApplication();
}
getTokenValue() {
return this.token?.getTokenValue();
getTokenValue(allowUnloggedIn?: boolean) {
return this.token?.getTokenValue(allowUnloggedIn);
}
getToken(allowUnloggedIn?: boolean) {

View File

@ -31,43 +31,7 @@ export class Token<
private async loadSavedToken() {
let tokenValue = await this.storage.load(LOCAL_STORAGE_KEYS.token);
/* if (!tokenValue) {
// 历史数据原来用的key太随意
tokenValue = await this.storage.load('token:token');
if (tokenValue) {
await this.storage.save(LOCAL_STORAGE_KEYS.token, tokenValue);
await this.storage.remove('token:token');
}
} */
// if (tokenValue) {
// const env = await this.environment.getEnv();
// try {
// const { result } = await this.cache.exec(
// 'refreshToken',
// {
// tokenValue,
// env,
// },
// undefined,
// true,
// true
// );
// if (this.tokenValue !== result) {
// this.tokenValue = result;
// await this.storage.save(LOCAL_STORAGE_KEYS.token, result);
// }
// }
// catch (err) {
// // refresh出了任何错都无视直接放弃此token
// console.warn(err);
// this.tokenValue = undefined;
// this.removeToken(true);
// }
// } else {
// this.tokenValue = undefined;
// }
await this.refreshTokenData(tokenValue)
await this.refreshTokenData(tokenValue);
this.publish();
}
@ -82,13 +46,19 @@ export class Token<
this.environment = environment;
this.tokenValue = ''; // 置个空字符串代表还在load storage缓存的数据
this.loadSavedToken();
if (process.env.OAK_PLATFORM === 'web' && (process.env.NODE_ENV !== 'development' || process.env.PROD === 'true')) {
if (
process.env.OAK_PLATFORM === 'web' &&
(process.env.NODE_ENV !== 'development' ||
process.env.PROD === 'true')
) {
// 纯前台模式 多窗口时不监听storage
// 在web下可能多窗口一个窗口更新了token其它窗口应跟着变
window.addEventListener('storage', async (e) => {
if (e.key === LOCAL_STORAGE_KEYS.token) {
this.tokenValue = e.newValue ? JSON.parse(e.newValue) : undefined;
await this.refreshTokenData(this.tokenValue)
this.tokenValue = e.newValue
? JSON.parse(e.newValue)
: undefined;
await this.refreshTokenData(this.tokenValue);
this.publish();
}
});
@ -96,27 +66,31 @@ export class Token<
}
async refreshTokenData(tokenValue?: string) {
if (tokenValue) {
const env = await this.environment.getEnv();
try {
const { result } = await this.cache.exec('refreshToken', {
if (!tokenValue) {
this.tokenValue = undefined;
return;
}
const env = await this.environment.getEnv();
try {
const { result } = await this.cache.exec(
'refreshToken',
{
tokenValue,
env,
}, undefined, true, true);
if (this.tokenValue !== result) {
this.tokenValue = result;
await this.storage.save(LOCAL_STORAGE_KEYS.token, result);
}
},
undefined,
true,
true
);
if (this.tokenValue !== result) {
this.tokenValue = result;
await this.storage.save(LOCAL_STORAGE_KEYS.token, result);
}
catch (err) {
// refresh出了任何错都无视直接放弃此token
console.warn(err);
this.tokenValue = undefined;
this.removeToken(true);
}
}
else {
} catch (err) {
// refresh出了任何错都无视直接放弃此token
console.warn(err);
this.tokenValue = undefined;
this.removeToken(true);
}
}
@ -202,23 +176,29 @@ export class Token<
this.publish();
}
async logout() {
await this.cache.exec('logout', {
tokenValue: this.tokenValue!,
}, undefined, undefined, true);
this.removeToken();
async logout(dontPublish?: boolean) {
await this.cache.exec(
'logout',
{
tokenValue: this.tokenValue!,
},
undefined,
undefined,
true
);
this.removeToken(dontPublish);
}
removeToken(disablePublish?: boolean) {
removeToken(dontPublish?: boolean) {
this.tokenValue = undefined;
this.storage.remove(LOCAL_STORAGE_KEYS.token);
if (!disablePublish) {
if (!dontPublish) {
this.publish();
}
}
getTokenValue() {
if (this.tokenValue === '') {
getTokenValue(allowUnloggedIn?: boolean) {
if (!allowUnloggedIn && this.tokenValue === '') {
throw new OakUserInfoLoadingException();
}
return this.tokenValue;