增加了公众号发跳转小程序认领授权的逻辑

This commit is contained in:
Xu Chang 2023-02-20 08:51:58 +08:00
parent 94210abc56
commit 0b6a0b42cb
4 changed files with 125 additions and 71 deletions

View File

@ -9,6 +9,7 @@ var x2js_1 = tslib_1.__importDefault(require("x2js"));
var oak_external_sdk_1 = require("oak-external-sdk");
var uuid_1 = require("oak-domain/lib/utils/uuid");
var domain_1 = require("../utils/domain");
var url_2 = require("oak-domain/lib/utils/url");
var X2Js = new x2js_1.default();
function assertFromWeChat(query, config) {
var _a;
@ -103,10 +104,10 @@ function setUserUnsubscribed(openId, context) {
}
function setUserSubscribed(openId, eventKey, context) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
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 _a, applicationId, applicationType, list, now, data, doUpdate, sceneStr, wcqId, _b, wechatQrCode, application, _c, type, config, systemId, _d, appId, appSecret, wechatInstance, expired, entity, entityId, _e, _f, userEntityGrant, _g, id, granter, expired_1, entity2, qrCodeType, name_1, _h, domain, url, _j, appMp, config_1, appId_1, content;
var _this = this;
return tslib_1.__generator(this, function (_j) {
switch (_j.label) {
return tslib_1.__generator(this, function (_k) {
switch (_k.label) {
case 0:
_a = context.getApplication(), applicationId = _a.id, applicationType = _a.type;
return [4 /*yield*/, context.select('wechatUser', {
@ -123,7 +124,7 @@ function setUserSubscribed(openId, eventKey, context) {
count: 1,
}, { dontCollect: true })];
case 1:
list = _j.sent();
list = _k.sent();
now = Date.now();
data = {
// activeAt: now,
@ -195,7 +196,7 @@ function setUserSubscribed(openId, eventKey, context) {
}
});
}); };
if (!eventKey) return [3 /*break*/, 9];
if (!eventKey) return [3 /*break*/, 11];
sceneStr = void 0;
if (eventKey.startsWith('qrscene_')) {
sceneStr = eventKey.slice(eventKey.indexOf('qrscene_') + 8);
@ -218,10 +219,10 @@ function setUserSubscribed(openId, eventKey, context) {
count: 10,
}, { dontCollect: true })];
case 2:
_b = tslib_1.__read.apply(void 0, [_j.sent(), 1]), wechatQrCode = _b[0];
if (!wechatQrCode) return [3 /*break*/, 8];
_b = tslib_1.__read.apply(void 0, [_k.sent(), 1]), wechatQrCode = _b[0];
if (!wechatQrCode) return [3 /*break*/, 10];
application = context.getApplication();
_c = application, type = _c.type, config = _c.config;
_c = application, type = _c.type, config = _c.config, systemId = _c.systemId;
(0, assert_1.default)(type === 'wechatPublic');
_d = config, appId = _d.appId, appSecret = _d.appSecret;
wechatInstance = oak_external_sdk_1.WechatSDK.getInstance(appId, 'wechatPublic', appSecret);
@ -241,19 +242,20 @@ function setUserSubscribed(openId, eventKey, context) {
case 'user': return [3 /*break*/, 3];
case 'userEntityGrant': return [3 /*break*/, 4];
}
return [3 /*break*/, 7];
return [3 /*break*/, 9];
case 3:
{
// 裂变获得的用户
if (list[0] && !list[0].userId) {
Object.assign(data, { userId: entityId });
}
return [3 /*break*/, 7];
return [3 /*break*/, 9];
}
_j.label = 4;
_k.label = 4;
case 4: return [4 /*yield*/, context.select('userEntityGrant', {
data: {
id: 1,
qrCodeType: 1,
granter: {
id: 1,
name: 1,
@ -267,9 +269,10 @@ function setUserSubscribed(openId, eventKey, context) {
}
}, { dontCollect: true })];
case 5:
_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;
_f = tslib_1.__read.apply(void 0, [_k.sent(), 1]), userEntityGrant = _f[0];
_g = userEntityGrant, id = _g.id, granter = _g.granter, expired_1 = _g.expired, entity2 = _g.entity, qrCodeType = _g.qrCodeType;
name_1 = (granter === null || granter === void 0 ? void 0 : granter.name) || (granter === null || granter === void 0 ? void 0 : granter.nickname) || '某用户';
if (!(qrCodeType === 'wechatPublic')) return [3 /*break*/, 7];
return [4 /*yield*/, context.select('domain', {
data: {
id: 1,
@ -293,7 +296,7 @@ function setUserSubscribed(openId, eventKey, context) {
}
}, { dontCollect: true })];
case 6:
_h = tslib_1.__read.apply(void 0, [_j.sent(), 1]), domain = _h[0];
_h = tslib_1.__read.apply(void 0, [_k.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,
@ -304,18 +307,46 @@ function setUserSubscribed(openId, eventKey, context) {
openId: openId,
type: 'news',
url: url,
title: "".concat(name_1, "\u7ED9\u60A8\u521B\u5EFA\u4E86\u4E00\u4E2A\u6388\u6743"),
title: "".concat(name_1, "\u4E3A\u60A8\u521B\u5EFA\u4E86\u4E00\u4E2A\u6388\u6743"),
description: '请接受',
picurl: 'http://img95.699pic.com/element/40018/2473.png_860.png',
});
_j.label = 7;
case 7: return [3 /*break*/, 9];
return [3 /*break*/, 9];
case 7:
(0, assert_1.default)(qrCodeType === 'wechatPublicForMp');
return [4 /*yield*/, context.select('application', {
data: {
id: 1,
config: 1,
},
filter: {
systemId: systemId,
type: 'wechatMp',
}
}, { dontCollect: true })];
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));
_j.label = 9;
case 9: return [4 /*yield*/, doUpdate()];
_j = tslib_1.__read.apply(void 0, [_k.sent(), 1]), appMp = _j[0];
(0, assert_1.default)(appMp, '公众号推送小程序码时找不到关联的小程序');
config_1 = appMp.config;
appId_1 = config_1.appId;
content = "".concat(name_1, "\u4E3A\u60A8\u521B\u5EFA\u4E86\u4E00\u4E2A\u6388\u6743\uFF0C<a href=\"#\" data-miniprogram-appid=\"").concat(appId_1, "\" data-miniprogram-path=\"").concat((0, url_2.composeUrl)('/pages/wecharQrCode/scan', {
oakId: wcqId,
time: "".concat(Date.now()),
}), "\">\u8BF7\u70B9\u51FB\u9886\u53D6</a>");
(0, assert_1.default)(!expired_1); // 如果生成的wechatQrCode没过期userEntityGrant就不可能过期。
wechatInstance.sendServeMessage({
openId: openId,
type: 'text',
content: content,
});
_k.label = 9;
case 9: return [3 /*break*/, 11];
case 10:
_j.sent();
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));
_k.label = 11;
case 11: return [4 /*yield*/, doUpdate()];
case 12:
_k.sent();
return [2 /*return*/];
}
});

View File

@ -40,9 +40,7 @@ function render(props) {
!isOwner &&
number > confirmed && ((0, jsx_runtime_1.jsx)(antd_1.Button, tslib_1.__assign({ size: "large", block: true, type: "primary", onClick: function () {
handleConfirm();
}, disabled: oakExecuting }, { children: "\u9886\u53D6" }))), !oakLoading && !expired && (isOwner || hasConfirmed) && ((0, jsx_runtime_1.jsxs)(antd_1.Button, tslib_1.__assign({ size: "large", block: true, type: "primary", onClick: function () {
redirectPage();
}, disabled: oakExecuting }, { children: ["\u9886\u53D6\u6210\u529F", redirectTo ? '(' + redirectCounter + 's)' : ''] }))), utils_1.isWeiXin && ((0, jsx_runtime_1.jsx)(antd_1.Button, tslib_1.__assign({ size: "large", block: true, onClick: function () {
}, disabled: oakExecuting }, { children: "\u9886\u53D6" }))), utils_1.isWeiXin && ((0, jsx_runtime_1.jsx)(antd_1.Button, tslib_1.__assign({ size: "large", block: true, onClick: function () {
WeixinJSBridge.call('closeWindow');
} }, { children: "\u5173\u95ED" })))] }))] })));
}

View File

@ -56,17 +56,6 @@ exports.default = OakComponent({
var url = pathname.substring(0, 1) === '/'
? pathname
: "/".concat(pathname);
// if (props) {
// for (const param in props) {
// const param2 = param as unknown as keyof typeof props;
// url += url.includes('?') ? '&' : '?';
// url += `${param}=${
// typeof props[param2] === 'string'
// ? props[param2]
// : JSON.stringify(props[param2])
// }`;
// }
// }
this.redirectTo(tslib_1.__assign({ url: url }, props_1), state);
return {
loading: false,

View File

@ -10,10 +10,11 @@ import {
} from 'oak-external-sdk';
import { EntityDict } from '../general-app-domain';
import { BRC } from '../types/RuntimeCxt';
import { WechatPublicConfig } from '../entities/Application';
import { WechatMpConfig, WechatPublicConfig } from '../entities/Application';
import { WechatPublicEventData } from 'oak-external-sdk';
import { expandUuidTo36Bytes, generateNewIdAsync } from 'oak-domain/lib/utils/uuid';
import { composeDomainUrl } from '../utils/domain';
import { composeUrl } from 'oak-domain/lib/utils/url';
type VerifyQuery = {
signature: string,
@ -218,7 +219,7 @@ async function setUserSubscribed(openId: string, eventKey: string, context: BRC)
);
if (wechatQrCode) {
const application = context.getApplication();
const { type, config } = application!;
const { type, config, systemId } = application!;
assert(type === 'wechatPublic');
const { appId, appSecret } = config as WechatPublicConfig;
@ -254,6 +255,7 @@ async function setUserSubscribed(openId: string, eventKey: string, context: BRC)
{
data: {
id: 1,
qrCodeType: 1,
granter: {
id: 1,
name: 1,
@ -268,46 +270,80 @@ async function setUserSubscribed(openId: string, eventKey: string, context: BRC)
},
{ dontCollect: true },
);
const { id, granter, expired, entity: entity2 } = userEntityGrant!;
const { id, granter, expired, entity: entity2, qrCodeType } = userEntityGrant!;
const name = granter?.name || granter?.nickname || '某用户';
const [domain] = await context.select('domain', {
data: {
id: 1,
url: 1,
apiPath: 1,
protocol: 1,
port: 1,
},
filter: {
systemId: {
$in: {
entity: 'application',
data: {
systemId: 1,
},
filter: {
id: applicationId,
if (qrCodeType === 'wechatPublic') {
// 推domain上的scan/code链接
const [domain] = await context.select('domain', {
data: {
id: 1,
url: 1,
apiPath: 1,
protocol: 1,
port: 1,
},
filter: {
systemId: {
$in: {
entity: 'application',
data: {
systemId: 1,
},
filter: {
id: applicationId,
}
}
}
}
}
}, { dontCollect: true });
assert(domain, `处理userEntityGrant时找不到对应的domainapplicationId是「${applicationId}`);
const url = composeDomainUrl(domain as EntityDict['domain']['Schema'], 'wechatQrCode/scan', {
scene: sceneStr,
time: `${Date.now()}`,
});
}, { dontCollect: true });
assert(domain, `处理userEntityGrant时找不到对应的domainapplicationId是「${applicationId}`);
const url = composeDomainUrl(domain as EntityDict['domain']['Schema'], 'wechatQrCode/scan', {
scene: sceneStr,
time: `${Date.now()}`,
});
assert(!expired); // 如果生成的wechatQrCode没过期userEntityGrant就不可能过期。
wechatInstance.sendServeMessage({
openId,
type: 'news',
url,
title: `${name}给您创建了一个授权`,
description: '请接受',
picurl: 'http://img95.699pic.com/element/40018/2473.png_860.png',
});
assert(!expired); // 如果生成的wechatQrCode没过期userEntityGrant就不可能过期。
wechatInstance.sendServeMessage({
openId,
type: 'news',
url,
title: `${name}为您创建了一个授权`,
description: '请接受',
picurl: 'http://img95.699pic.com/element/40018/2473.png_860.png',
});
}
else {
assert(qrCodeType === 'wechatPublicForMp');
// 找到相关的小程序
const [ appMp ] = await context.select('application', {
data: {
id: 1,
config: 1,
},
filter: {
systemId,
type: 'wechatMp',
}
}, { dontCollect: true });
assert(appMp, '公众号推送小程序码时找不到关联的小程序');
const { config } = appMp;
const { appId } = config as WechatMpConfig;
// 先试着发文字链接
const content = `${name}为您创建了一个授权,<a href="#" data-miniprogram-appid="${appId}" data-miniprogram-path="${composeUrl('/pages/wecharQrCode/scan', {
oakId: wcqId,
time: `${Date.now()}`,
})}"></a>`;
assert(!expired); // 如果生成的wechatQrCode没过期userEntityGrant就不可能过期。
wechatInstance.sendServeMessage({
openId,
type: 'text',
content,
});
}
}
}
}