aspect中wechatLogin的一些小问题

This commit is contained in:
Xu Chang 2023-01-31 19:53:55 +08:00
parent 87f4955d64
commit 41d73db8fd
5 changed files with 169 additions and 164 deletions

View File

@ -460,10 +460,10 @@ function loginWechat(_a, context) {
switch (_b.label) {
case 0:
loginLogic = function () { return tslib_1.__awaiter(_this, void 0, void 0, function () {
var application, _a, type, config, systemId, appId, appSecret, config2, config2, wechatInstance, _b, sessionKey, openId, unionId, _c, wechatUser, id, wechatUser2, wechatUserUpdateData, _d, token, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, wechatUser3, wechatUser2, _t, _u, _v, wechatUserCreateData_1, _w, _x, _y, userData, wechatUserCreateData, _z, _0, _1;
var _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24;
return tslib_1.__generator(this, function (_25) {
switch (_25.label) {
var application, _a, type, config, systemId, appId, appSecret, config2, config2, wechatInstance, _b, sessionKey, openId, unionId, _c, wechatUser, id, wechatUser2, wechatUserUpdateData, userId, _d, token, _e, _f, _g, _h, _j, _k, userData2, _l, _m, _o, _p, wechatUser3, wechatUser2, _q, _r, _s, wechatUserCreateData_1, _t, _u, _v, userData, wechatUserCreateData, _w, _x, _y;
var _z, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20;
return tslib_1.__generator(this, function (_21) {
switch (_21.label) {
case 0:
application = context.getApplication();
_a = application, type = _a.type, config = _a.config, systemId = _a.systemId;
@ -483,7 +483,7 @@ function loginWechat(_a, context) {
wechatInstance = oak_external_sdk_1.WechatSDK.getInstance(appId, appSecret, type);
return [4 /*yield*/, wechatInstance.code2Session(code)];
case 1:
_b = _25.sent(), sessionKey = _b.sessionKey, openId = _b.openId, unionId = _b.unionId;
_b = _21.sent(), sessionKey = _b.sessionKey, openId = _b.openId, unionId = _b.unionId;
return [4 /*yield*/, context.select('wechatUser', {
data: {
id: 1,
@ -511,10 +511,10 @@ function loginWechat(_a, context) {
dontCollect: true,
})];
case 2:
_c = tslib_1.__read.apply(void 0, [_25.sent(), 1]), wechatUser = _c[0];
_c = tslib_1.__read.apply(void 0, [_21.sent(), 1]), wechatUser = _c[0];
return [4 /*yield*/, (0, uuid_1.generateNewIdAsync)()];
case 3:
id = _25.sent();
id = _21.sent();
if (!wechatUser) return [3 /*break*/, 18];
wechatUser2 = wechatUser;
wechatUserUpdateData = {
@ -525,6 +525,7 @@ function loginWechat(_a, context) {
unionId: unionId,
});
}
userId = wechatUser2.userId;
if (!wechatUser2.userId) return [3 /*break*/, 11];
return [4 /*yield*/, context.select('token', {
data: {
@ -544,99 +545,97 @@ function loginWechat(_a, context) {
blockTrigger: true,
})];
case 4:
_d = tslib_1.__read.apply(void 0, [_25.sent(), 1]), token = _d[0];
_d = tslib_1.__read.apply(void 0, [_21.sent(), 1]), token = _d[0];
if (!(token && (0, lodash_1.isEqual)(token.env, env))) return [3 /*break*/, 8];
_f = (_e = context).operate;
_g = ['token'];
_2 = {};
_z = {};
return [4 /*yield*/, (0, uuid_1.generateNewIdAsync)()];
case 5:
_2.id = _25.sent(),
_2.action = 'update';
_3 = {};
_4 = {};
_z.id = _21.sent(),
_z.action = 'update';
_0 = {};
_1 = {};
return [4 /*yield*/, (0, uuid_1.generateNewIdAsync)()];
case 6: return [4 /*yield*/, _f.apply(_e, _g.concat([(_2.data = (_3.wechatUser = (_4.id = _25.sent(),
_4.action = 'update',
_4.data = wechatUserUpdateData,
_4),
_3),
_2.filter = {
case 6: return [4 /*yield*/, _f.apply(_e, _g.concat([(_z.data = (_0.wechatUser = (_1.id = _21.sent(),
_1.action = 'update',
_1.data = wechatUserUpdateData,
_1),
_0),
_z.filter = {
id: token.id,
},
_2), {
_z), {
dontCollect: true,
}]))];
case 7:
_25.sent();
_21.sent();
return [2 /*return*/, token.id];
case 8:
_j = (_h = context).operate;
_k = ['token'];
_5 = {};
_2 = {};
return [4 /*yield*/, (0, uuid_1.generateNewIdAsync)()];
case 9: return [4 /*yield*/, _j.apply(_h, _k.concat([(_5.id = _25.sent(),
_5.action = 'disable',
_5.data = {},
_5.filter = {
case 9: return [4 /*yield*/, _j.apply(_h, _k.concat([(_2.id = _21.sent(),
_2.action = 'disable',
_2.data = {},
_2.filter = {
applicationId: application.id,
ableState: 'enabled',
userId: wechatUser2.userId,
playerId: wechatUser2.userId,
},
_5), {
_2), {
dontCollect: true,
}]))];
case 10:
_25.sent();
_21.sent();
return [3 /*break*/, 14];
case 11:
// 创建user
_m = (_l = Object).assign;
_o = [wechatUserUpdateData];
_6 = {};
_7 = {};
_3 = {};
return [4 /*yield*/, (0, uuid_1.generateNewIdAsync)()];
case 12:
_7.id = _25.sent(),
_7.action = 'create';
_8 = {};
_3.id = _21.sent(),
_3.action = 'create';
_4 = {};
return [4 /*yield*/, (0, uuid_1.generateNewIdAsync)()];
case 13:
// 创建user
_m.apply(_l, _o.concat([(_6.user = (_7.data = (_8.id = _25.sent(),
_8.userState = 'normal',
_8),
_7),
_6)]));
_25.label = 14;
userData2 = (_3.data = (_4.id = _21.sent(),
_4.userState = 'normal',
_4),
_3);
Object.assign(wechatUserUpdateData, {
user: userData2,
});
userId = userData2.data.id;
_21.label = 14;
case 14:
_q = (_p = context).operate;
_r = ['token'];
_9 = {};
_m = (_l = context).operate;
_o = ['token'];
_5 = {};
return [4 /*yield*/, (0, uuid_1.generateNewIdAsync)()];
case 15:
_9.id = _25.sent(),
_9.action = 'create';
_10 = {
_5.id = _21.sent(),
_5.action = 'create';
_6 = {
id: id,
userId: wechatUser2.userId,
playerId: wechatUser2.userId,
userId: userId,
playerId: userId,
applicationId: application.id,
entity: 'wechatUser',
entityId: wechatUser2.id
};
_11 = {};
_7 = {};
return [4 /*yield*/, (0, uuid_1.generateNewIdAsync)()];
case 16: return [4 /*yield*/, _q.apply(_p, _r.concat([(_9.data = (_10.wechatUser = (_11.id = _25.sent(),
_11.action = 'update',
_11.data = wechatUserUpdateData,
_11),
_10.env = env,
_10),
_9), {}]))];
case 16: return [4 /*yield*/, _m.apply(_l, _o.concat([(_5.data = (_6.wechatUser = (_7.id = _21.sent(),
_7.action = 'update',
_7.data = wechatUserUpdateData,
_7),
_6.env = env,
_6),
_5), {}]))];
case 17:
_25.sent();
_21.sent();
return [2 /*return*/, id];
case 18:
if (!unionId) return [3 /*break*/, 26];
@ -656,129 +655,129 @@ function loginWechat(_a, context) {
dontCollect: true,
})];
case 19:
_s = tslib_1.__read.apply(void 0, [_25.sent(), 1]), wechatUser3 = _s[0];
_p = tslib_1.__read.apply(void 0, [_21.sent(), 1]), wechatUser3 = _p[0];
wechatUser2 = wechatUser3;
if (!(wechatUser2 && wechatUser2.userId)) return [3 /*break*/, 26];
_u = (_t = context).operate;
_v = ['token'];
_12 = {};
_r = (_q = context).operate;
_s = ['token'];
_8 = {};
return [4 /*yield*/, (0, uuid_1.generateNewIdAsync)()];
case 20: return [4 /*yield*/, _u.apply(_t, _v.concat([(_12.id = _25.sent(),
_12.action = 'disable',
_12.data = {},
_12.filter = {
case 20: return [4 /*yield*/, _r.apply(_q, _s.concat([(_8.id = _21.sent(),
_8.action = 'disable',
_8.data = {},
_8.filter = {
applicationId: application.id,
ableState: 'enabled',
userId: wechatUser2.userId,
playerId: wechatUser2.userId,
},
_12), {
_8), {
dontCollect: true,
}]))];
case 21:
_25.sent();
_13 = {};
_21.sent();
_9 = {};
return [4 /*yield*/, (0, uuid_1.generateNewIdAsync)()];
case 22:
wechatUserCreateData_1 = (_13.id = _25.sent(),
_13.sessionKey = sessionKey,
_13.unionId = unionId,
_13.origin = 'mp',
_13.openId = openId,
_13.applicationId = application.id,
_13.userId = wechatUser2.userId,
_13);
_x = (_w = context).operate;
_y = ['token'];
_14 = {};
wechatUserCreateData_1 = (_9.id = _21.sent(),
_9.sessionKey = sessionKey,
_9.unionId = unionId,
_9.origin = 'mp',
_9.openId = openId,
_9.applicationId = application.id,
_9.userId = wechatUser2.userId,
_9);
_u = (_t = context).operate;
_v = ['token'];
_10 = {};
return [4 /*yield*/, (0, uuid_1.generateNewIdAsync)()];
case 23:
_14.id = _25.sent(),
_14.action = 'create';
_15 = {
_10.id = _21.sent(),
_10.action = 'create';
_11 = {
id: id,
userId: wechatUser2.userId,
playerId: wechatUser2.userId,
applicationId: application.id
};
_16 = {};
_12 = {};
return [4 /*yield*/, (0, uuid_1.generateNewIdAsync)()];
case 24: return [4 /*yield*/, _x.apply(_w, _y.concat([(_14.data = (_15.wechatUser = (_16.id = _25.sent(),
_16.action = 'create',
_16.data = wechatUserCreateData_1,
_16),
_15.env = env,
_15),
_14), {
case 24: return [4 /*yield*/, _u.apply(_t, _v.concat([(_10.data = (_11.wechatUser = (_12.id = _21.sent(),
_12.action = 'create',
_12.data = wechatUserCreateData_1,
_12),
_11.env = env,
_11),
_10), {
dontCollect: true,
}]))];
case 25:
_25.sent();
_21.sent();
return [2 /*return*/, id];
case 26:
_17 = {};
_13 = {};
return [4 /*yield*/, (0, uuid_1.generateNewIdAsync)()];
case 27:
_17.id = _25.sent(),
_17.userState = 'normal';
_18 = {};
_13.id = _21.sent(),
_13.userState = 'normal';
_14 = {};
return [4 /*yield*/, (0, uuid_1.generateNewIdAsync)()];
case 28:
_18.id = _25.sent(),
_18.action = 'create';
_19 = {};
_14.id = _21.sent(),
_14.action = 'create';
_15 = {};
return [4 /*yield*/, (0, uuid_1.generateNewIdAsync)()];
case 29:
userData = (_17.userSystem$user = [
(_18.data = (_19.id = _25.sent(),
_19.systemId = systemId,
_19),
_18)
userData = (_13.userSystem$user = [
(_14.data = (_15.id = _21.sent(),
_15.systemId = systemId,
_15),
_14)
],
_17);
_20 = {};
_13);
_16 = {};
return [4 /*yield*/, (0, uuid_1.generateNewIdAsync)()];
case 30:
_20.id = _25.sent(),
_20.sessionKey = sessionKey,
_20.unionId = unionId,
_20.origin = type === 'wechatPublic' ? 'public' : 'web',
_20.openId = openId,
_20.applicationId = application.id;
_21 = {};
_16.id = _21.sent(),
_16.sessionKey = sessionKey,
_16.unionId = unionId,
_16.origin = type === 'wechatPublic' ? 'public' : 'web',
_16.openId = openId,
_16.applicationId = application.id;
_17 = {};
return [4 /*yield*/, (0, uuid_1.generateNewIdAsync)()];
case 31:
wechatUserCreateData = (_20.user = (_21.id = _25.sent(),
_21.action = 'create',
_21.data = userData,
_21),
_20);
_0 = (_z = context).operate;
_1 = ['token'];
_22 = {};
wechatUserCreateData = (_16.user = (_17.id = _21.sent(),
_17.action = 'create',
_17.data = userData,
_17),
_16);
_x = (_w = context).operate;
_y = ['token'];
_18 = {};
return [4 /*yield*/, (0, uuid_1.generateNewIdAsync)()];
case 32:
_22.id = _25.sent(),
_22.action = 'create';
_23 = {
_18.id = _21.sent(),
_18.action = 'create';
_19 = {
id: id,
userId: userData.id,
playerId: userData.id,
applicationId: application.id
};
_24 = {};
_20 = {};
return [4 /*yield*/, (0, uuid_1.generateNewIdAsync)()];
case 33: return [4 /*yield*/, _0.apply(_z, _1.concat([(_22.data = (_23.wechatUser = (_24.id = _25.sent(),
_24.action = 'create',
_24.data = wechatUserCreateData,
_24),
_23.env = env,
_23),
_22), {
case 33: return [4 /*yield*/, _x.apply(_w, _y.concat([(_18.data = (_19.wechatUser = (_20.id = _21.sent(),
_20.action = 'create',
_20.data = wechatUserCreateData,
_20),
_19.env = env,
_19),
_18), {
dontCollect: true,
}]))];
case 34:
_25.sent();
_21.sent();
return [2 /*return*/, id];
}
});

View File

@ -103,12 +103,12 @@ function setUserUnsubscribed(openId, context) {
}
function setUserSubscribed(openId, eventKey, context) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var applicationId, list, now, data, doUpdate, sceneStr, wcqId, _a, wechatQrCode, application, _b, type, config, _c, appId, appSecret, wechatInstance, expired, entity, entityId, _d, _e, userEntityGrant, _f, id, granter, expired_1, entity2, name_1, _g, domain, url;
var _a, applicationId, applicationType, list, now, data, doUpdate, sceneStr, wcqId, _b, wechatQrCode, application, _c, type, config, _d, appId, appSecret, wechatInstance, expired, entity, entityId, _e, _f, userEntityGrant, _g, id, granter, expired_1, entity2, name_1, _h, domain, url;
var _this = this;
return tslib_1.__generator(this, function (_h) {
switch (_h.label) {
return tslib_1.__generator(this, function (_j) {
switch (_j.label) {
case 0:
applicationId = context.getApplicationId();
_a = context.getApplication(), applicationId = _a.id, applicationType = _a.type;
return [4 /*yield*/, context.select('wechatUser', {
data: {
id: 1,
@ -123,7 +123,7 @@ function setUserSubscribed(openId, eventKey, context) {
count: 1,
}, { dontCollect: true })];
case 1:
list = _h.sent();
list = _j.sent();
now = Date.now();
data = {
// activeAt: now,
@ -164,6 +164,7 @@ function setUserSubscribed(openId, eventKey, context) {
_e.apply(_d, _f.concat([(_l.id = _o.sent(),
_l.subscribed = true,
_l.subscribedAt = now,
_l.origin = applicationType === 'wechatPublic' ? 'public' : 'web',
_l.applicationId = applicationId,
_l.openId = openId,
_l)]));
@ -217,12 +218,12 @@ function setUserSubscribed(openId, eventKey, context) {
count: 10,
}, { dontCollect: true })];
case 2:
_a = tslib_1.__read.apply(void 0, [_h.sent(), 1]), wechatQrCode = _a[0];
_b = tslib_1.__read.apply(void 0, [_j.sent(), 1]), wechatQrCode = _b[0];
if (!wechatQrCode) return [3 /*break*/, 8];
application = context.getApplication();
_b = application, type = _b.type, config = _b.config;
_c = application, type = _c.type, config = _c.config;
(0, assert_1.default)(type === 'wechatPublic');
_c = config, appId = _c.appId, appSecret = _c.appSecret;
_d = config, appId = _d.appId, appSecret = _d.appSecret;
wechatInstance = oak_external_sdk_1.WechatSDK.getInstance(appId, appSecret, 'wechatPublic');
expired = wechatQrCode.expired;
if (expired) {
@ -235,8 +236,8 @@ function setUserSubscribed(openId, eventKey, context) {
return [2 /*return*/];
}
entity = wechatQrCode.entity, entityId = wechatQrCode.entityId;
_d = entity;
switch (_d) {
_e = entity;
switch (_e) {
case 'user': return [3 /*break*/, 3];
case 'userEntityGrant': return [3 /*break*/, 4];
}
@ -249,7 +250,7 @@ function setUserSubscribed(openId, eventKey, context) {
}
return [3 /*break*/, 7];
}
_h.label = 4;
_j.label = 4;
case 4: return [4 /*yield*/, context.select('userEntityGrant', {
data: {
id: 1,
@ -266,8 +267,8 @@ function setUserSubscribed(openId, eventKey, context) {
}
}, { dontCollect: true })];
case 5:
_e = tslib_1.__read.apply(void 0, [_h.sent(), 1]), userEntityGrant = _e[0];
_f = userEntityGrant, id = _f.id, granter = _f.granter, expired_1 = _f.expired, entity2 = _f.entity;
_f = tslib_1.__read.apply(void 0, [_j.sent(), 1]), userEntityGrant = _f[0];
_g = userEntityGrant, id = _g.id, granter = _g.granter, expired_1 = _g.expired, entity2 = _g.entity;
name_1 = (granter === null || granter === void 0 ? void 0 : granter.name) || (granter === null || granter === void 0 ? void 0 : granter.nickname) || '某用户';
return [4 /*yield*/, context.select('domain', {
data: {
@ -292,7 +293,7 @@ function setUserSubscribed(openId, eventKey, context) {
}
}, { dontCollect: true })];
case 6:
_g = tslib_1.__read.apply(void 0, [_h.sent(), 1]), domain = _g[0];
_h = tslib_1.__read.apply(void 0, [_j.sent(), 1]), domain = _h[0];
(0, assert_1.default)(domain, "\u5904\u7406userEntityGrant\u65F6\uFF0C\u627E\u4E0D\u5230\u5BF9\u5E94\u7684domain\uFF0CapplicationId\u662F\u300C".concat(applicationId, "\u300D"));
url = (0, domain_1.composeDomainUrl)(domain, 'wechatQrCode/scan', {
scene: sceneStr,
@ -307,14 +308,14 @@ function setUserSubscribed(openId, eventKey, context) {
description: '请接受',
picurl: 'http://img95.699pic.com/element/40018/2473.png_860.png',
});
_h.label = 7;
_j.label = 7;
case 7: return [3 /*break*/, 9];
case 8:
console.warn("\u7EBF\u4E0A\u6709\u626B\u63CF\u4E8C\u7EF4\u7801\u573A\u666F\u503C\uFF0C\u4F46\u627E\u4E0D\u5230\u5BF9\u5E94\u7684qrCode\uFF0CeventKey\u662F".concat(eventKey));
_h.label = 9;
_j.label = 9;
case 9: return [4 /*yield*/, doUpdate()];
case 10:
_h.sent();
_j.sent();
return [2 /*return*/];
}
});

View File

@ -35,4 +35,4 @@ export declare function makeException(data: {
name: string;
message?: string;
[A: string]: any;
}): import("oak-domain/lib/types").OakException | import("oak-domain/lib/types").OakExternalException | undefined;
}): any;

View File

@ -423,6 +423,8 @@ export async function loginWechat<ED extends EntityDict, Cxt extends BackendRunt
unionId,
});
}
let userId = wechatUser2.userId;
if (wechatUser2.userId) {
// 若用户没有更换任何环境则重用原来的token避免token表增长过快
const [token] = await context.select('token', {
@ -479,16 +481,18 @@ export async function loginWechat<ED extends EntityDict, Cxt extends BackendRunt
}
else {
// 创建user
Object.assign(wechatUserUpdateData, {
user: {
const userData2: EntityDict['user']['CreateSingle'] = {
id: await generateNewIdAsync(),
action: 'create',
data: {
id: await generateNewIdAsync(),
action: 'create',
data: {
id: await generateNewIdAsync(),
userState: 'normal',
} as CreateUser,
},
userState: 'normal',
} as CreateUser,
};
Object.assign(wechatUserUpdateData, {
user: userData2,
});
userId = userData2.data.id;
}
await context.operate('token', {
@ -496,8 +500,8 @@ export async function loginWechat<ED extends EntityDict, Cxt extends BackendRunt
action: 'create',
data: {
id,
userId: wechatUser2.userId as string,
playerId: wechatUser2.userId as string,
userId,
playerId: userId,
applicationId: application!.id,
entity: 'wechatUser',
entityId: wechatUser2.id as string,

View File

@ -110,7 +110,7 @@ async function setUserUnsubscribed(openId: string, context: BRC) {
async function setUserSubscribed(openId: string, eventKey: string, context: BRC) {
const applicationId = context.getApplicationId();
const { id: applicationId, type: applicationType } = context.getApplication()!;
const list = await context.select(
'wechatUser',
{
@ -160,6 +160,7 @@ async function setUserSubscribed(openId: string, eventKey: string, context: BRC)
id: await generateNewIdAsync(),
subscribed: true,
subscribedAt: now,
origin: applicationType === 'wechatPublic' ? 'public' : 'web',
applicationId,
openId,
});