runtimeContext的声明修正

This commit is contained in:
Xu Chang 2022-07-13 14:12:42 +08:00
parent d6a67f7c57
commit d7e0d255a6
8 changed files with 58 additions and 51 deletions

View File

@ -15,12 +15,12 @@ declare type AppType = SelectRowShape<EntityDict['application']['Schema'], {
}; };
}>; }>;
export declare abstract class GeneralRuntimeContext<ED extends EntityDict> extends UniversalContext<ED> { export declare abstract class GeneralRuntimeContext<ED extends EntityDict> extends UniversalContext<ED> {
private applicationId?;
private application?; private application?;
private token?; private token?;
private rwLockApplication; private rwLockApplication;
constructor(store: RowStore<ED, GeneralRuntimeContext<ED>>, applicationId?: string); constructor(store: RowStore<ED, GeneralRuntimeContext<ED>>, applicationId?: string);
private loadApplication; getApplicationId(): string | undefined;
getApplicationId(): string;
getSystemId(): string; getSystemId(): string;
setToken(token?: string): void; setToken(token?: string): void;
getApplication(): Promise<SelectRowShape<import("general-app-domain/Application/Schema").Schema, { getApplication(): Promise<SelectRowShape<import("general-app-domain/Application/Schema").Schema, {
@ -34,7 +34,7 @@ export declare abstract class GeneralRuntimeContext<ED extends EntityDict> exten
name: 1; name: 1;
config: 1; config: 1;
}; };
}>>; }> | undefined>;
setApplication(app: AppType): void; setApplication(app: AppType): void;
getToken(): Promise<SelectRowShape<ED["token"]["Schema"], { getToken(): Promise<SelectRowShape<ED["token"]["Schema"], {
id: 1; id: 1;

View File

@ -9,18 +9,34 @@ const lodash_1 = require("lodash");
const concurrent_1 = require("oak-domain/lib/utils/concurrent"); const concurrent_1 = require("oak-domain/lib/utils/concurrent");
const assert_1 = __importDefault(require("assert")); const assert_1 = __importDefault(require("assert"));
class GeneralRuntimeContext extends UniversalContext_1.UniversalContext { class GeneralRuntimeContext extends UniversalContext_1.UniversalContext {
applicationId;
application; application;
token; token;
rwLockApplication; rwLockApplication;
constructor(store, applicationId) { constructor(store, applicationId) {
super(store); super(store);
this.rwLockApplication = new concurrent_1.RWLock(); this.rwLockApplication = new concurrent_1.RWLock();
this.applicationId = applicationId;
if (!applicationId) {
this.rwLockApplication.acquire('X'); this.rwLockApplication.acquire('X');
if (applicationId) {
this.loadApplication(applicationId);
} }
} }
async loadApplication(id) { getApplicationId() {
return this.applicationId;
}
getSystemId() {
return this.application.systemId;
}
setToken(token) {
this.token = token;
}
async getApplication() {
let result;
await this.rwLockApplication.acquire('X');
if (this.application) {
result = this.application;
}
else if (this.applicationId) {
const { result: [application] } = await this.rowStore.select('application', { const { result: [application] } = await this.rowStore.select('application', {
data: { data: {
id: 1, id: 1,
@ -35,31 +51,19 @@ class GeneralRuntimeContext extends UniversalContext_1.UniversalContext {
}, },
}, },
filter: { filter: {
id, id: this.applicationId,
}, },
}, this); }, this);
result = application;
this.application = application; this.application = application;
this.rwLockApplication.release();
} }
getApplicationId() {
const result = this.application?.id;
return result;
}
getSystemId() {
return this.application.systemId;
}
setToken(token) {
this.token = token;
}
async getApplication() {
await this.rwLockApplication.acquire('S');
const result = this.application;
this.rwLockApplication.release(); this.rwLockApplication.release();
return result; return result;
} }
setApplication(app) { setApplication(app) {
(0, assert_1.default)(!this.application); (0, assert_1.default)(!this.application);
this.application = app; this.application = app;
this.applicationId = app.id;
this.rwLockApplication.release(); this.rwLockApplication.release();
} }
async getToken() { async getToken() {

View File

@ -7,7 +7,7 @@ exports.createWechatQrCode = void 0;
const assert_1 = __importDefault(require("assert")); const assert_1 = __importDefault(require("assert"));
async function createWechatQrCode(options, context) { async function createWechatQrCode(options, context) {
const { entity, entityId, applicationId, tag, lifetimeLength, permanent, props } = options; const { entity, entityId, applicationId, tag, lifetimeLength, permanent, props } = options;
const { type: appType, config } = await context.getApplication(); const { type: appType, config } = (await context.getApplication());
if (appType === 'wechatMp') { if (appType === 'wechatMp') {
const { qrCodePrefix } = config; const { qrCodePrefix } = config;
const id = await generateNewId(); const id = await generateNewId();

View File

@ -96,6 +96,7 @@ const triggers = [
when: 'before', when: 'before',
fn: async ({ operation }, context) => { fn: async ({ operation }, context) => {
const app = await context.getApplication(); const app = await context.getApplication();
if (app) {
const { filter } = operation; const { filter } = operation;
if (!filter) { if (!filter) {
(0, lodash_1.assign)(operation, { (0, lodash_1.assign)(operation, {
@ -113,6 +114,8 @@ const triggers = [
} }
return 1; return 1;
} }
return 0;
}
} }
]; ];
exports.default = triggers; exports.default = triggers;

View File

@ -19,7 +19,7 @@ const triggers = [
const { data, filter } = operation; const { data, filter } = operation;
const fn = async (userEntityGrantData) => { const fn = async (userEntityGrantData) => {
const { userId } = (await context.getToken()); const { userId } = (await context.getToken());
const { id: applicationId, config: appConfig, system: { config: SystemConfig } } = await context.getApplication(); const { id: applicationId, config: appConfig, system: { config: SystemConfig } } = (await context.getApplication());
(0, assert_1.default)(userId); (0, assert_1.default)(userId);
const { type, entity, entityId, relation, id } = userEntityGrantData; const { type, entity, entityId, relation, id } = userEntityGrantData;
const { result } = await context.rowStore.select('userEntityGrant', { const { result } = await context.rowStore.select('userEntityGrant', {

View File

@ -71,7 +71,7 @@ export abstract class GeneralRuntimeContext<ED extends EntityDict> extends Unive
id: this.applicationId, id: this.applicationId,
}, },
}, this); }, this);
result = application; result = application as AppType;
this.application = application as AppType; this.application = application as AppType;
} }
this.rwLockApplication.release(); this.rwLockApplication.release();

View File

@ -447,7 +447,7 @@ export async function loginWechatMp<ED extends EntityDict, Cxt extends GeneralRu
id, id,
userId: wechatUser2.userId as string, userId: wechatUser2.userId as string,
playerId: wechatUser2.userId as string, playerId: wechatUser2.userId as string,
applicationId: application.id, applicationId: application!.id,
entity: 'wechatUser', entity: 'wechatUser',
entityId: wechatUser2.id as string, entityId: wechatUser2.id as string,
wechatUser: { wechatUser: {

View File

@ -59,7 +59,7 @@ const triggers: Trigger<EntityDict, 'userEntityGrant', GeneralRuntimeContext<Ent
expired: false, expired: false,
}); });
// 如果是微信体系的应用为之创建一个默认的weChatQrCode // 如果是微信体系的应用为之创建一个默认的weChatQrCode
if (['wechatPublic', 'wechatMp'].includes(appConfig.type)) { if (['wechatPublic', 'wechatMp'].includes(appConfig!.type)) {
await createWechatQrCode( await createWechatQrCode(
{ {
entity: 'userEntityGrant', entity: 'userEntityGrant',