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,41 +9,20 @@ 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.rwLockApplication.acquire('X'); this.applicationId = applicationId;
if (applicationId) { if (!applicationId) {
this.loadApplication(applicationId); this.rwLockApplication.acquire('X');
} }
} }
async loadApplication(id) {
const { result: [application] } = await this.rowStore.select('application', {
data: {
id: 1,
name: 1,
config: 1,
type: 1,
systemId: 1,
system: {
id: 1,
name: 1,
config: 1,
},
},
filter: {
id,
},
}, this);
this.application = application;
this.rwLockApplication.release();
}
getApplicationId() { getApplicationId() {
const result = this.application?.id; return this.applicationId;
return result;
} }
getSystemId() { getSystemId() {
return this.application.systemId; return this.application.systemId;
@ -52,14 +31,39 @@ class GeneralRuntimeContext extends UniversalContext_1.UniversalContext {
this.token = token; this.token = token;
} }
async getApplication() { async getApplication() {
await this.rwLockApplication.acquire('S'); let result;
const result = this.application; await this.rwLockApplication.acquire('X');
if (this.application) {
result = this.application;
}
else if (this.applicationId) {
const { result: [application] } = await this.rowStore.select('application', {
data: {
id: 1,
name: 1,
config: 1,
type: 1,
systemId: 1,
system: {
id: 1,
name: 1,
config: 1,
},
},
filter: {
id: this.applicationId,
},
}, this);
result = application;
this.application = 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,22 +96,25 @@ const triggers = [
when: 'before', when: 'before',
fn: async ({ operation }, context) => { fn: async ({ operation }, context) => {
const app = await context.getApplication(); const app = await context.getApplication();
const { filter } = operation; if (app) {
if (!filter) { const { filter } = operation;
(0, lodash_1.assign)(operation, { if (!filter) {
filter: { (0, lodash_1.assign)(operation, {
systemId: app.systemId, filter: {
}, systemId: app.systemId,
}); },
});
}
else {
(0, lodash_1.assign)(operation, {
filter: (0, filter_1.addFilterSegment)({
systemId: app.systemId,
}, filter),
});
}
return 1;
} }
else { return 0;
(0, lodash_1.assign)(operation, {
filter: (0, filter_1.addFilterSegment)({
systemId: app.systemId,
}, filter),
});
}
return 1;
} }
} }
]; ];

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',