backendContext增加了later模式

This commit is contained in:
Xu Chang 2024-03-06 23:01:15 +08:00
parent 0992b961a5
commit 3e76b9f9e9
2 changed files with 63 additions and 47 deletions

View File

@ -23,8 +23,7 @@ import { cloneDeep } from 'oak-domain/lib/utils/lodash';
*/
export abstract class BackendRuntimeContext<ED extends EntityDict & BaseEntityDict>
extends BRC<ED>
implements RuntimeContext
{
implements RuntimeContext {
protected application?: Partial<ED['application']['Schema']>;
protected token?: Partial<ED['token']['Schema']>;
protected amIRoot?: boolean;
@ -70,7 +69,8 @@ export abstract class BackendRuntimeContext<ED extends EntityDict & BaseEntityDi
}
}
async setTokenValue(tokenValue: string, later?: boolean) {
async setTokenValue(tokenValue: string, later?: boolean, userId?: string) {
if (!later || !userId) {
const result = await this.select(
'token',
{
@ -101,7 +101,9 @@ export abstract class BackendRuntimeContext<ED extends EntityDict & BaseEntityDi
console.log(
`构建BackendRuntimeContext对应tokenValue「${tokenValue}找不到相关的user`
);
if (!later) {
throw new OakTokenExpiredException();
}
// this.tokenException = new OakTokenExpiredException();
return;
}
@ -119,6 +121,18 @@ export abstract class BackendRuntimeContext<ED extends EntityDict & BaseEntityDi
this.amIReallyRoot = player?.isRoot!;
this.token = token;
}
const [user] = await this.select('user', {
data: {
id: 1,
isRoot: 1,
},
filter: { id: userId },
}, { dontCollect: true });
assert(user, '初始化context时有userId但查询不到user');
this.amIRoot = user.isRoot!;
this.amIReallyRoot = user.isRoot!;
this.userId = userId;
}
async setApplication(appId: string) {
const result = await this.select(
@ -146,13 +160,13 @@ export abstract class BackendRuntimeContext<ED extends EntityDict & BaseEntityDi
if (data) {
const closeRootMode = this.openRootMode();
try {
const { a: appId, t: tokenValue, rm } = data;
const { a: appId, t: tokenValue, rm, userId } = data;
const promises: Promise<void>[] = [];
if (appId) {
promises.push(this.setApplication(appId));
}
if (tokenValue) {
promises.push(this.setTokenValue(tokenValue, later));
promises.push(this.setTokenValue(tokenValue, later, userId));
}
if (promises.length > 0) {
await Promise.all(promises);
@ -236,6 +250,7 @@ export abstract class BackendRuntimeContext<ED extends EntityDict & BaseEntityDi
a: this.application?.id,
t: this.token?.value,
rm: this.rootMode,
userId: this.getCurrentUserId(),
};
}

View File

@ -28,6 +28,7 @@ export type AspectDict<
export interface SerializedData extends Fsd {
a?: string;
t?: string;
userId?: string; // 后台专用
rm?: boolean;
};