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

View File

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