This commit is contained in:
wenjiarui 2022-12-22 14:24:08 +08:00
parent 8413c65888
commit ada14e6e62
2 changed files with 109 additions and 0 deletions

View File

@ -22,4 +22,20 @@ export declare class WechatPublicInstance {
url: any;
expireSeconds: any;
}>;
sendTemplateMessage(options: {
openId: string;
templateId: string;
url?: string;
data: Object;
miniProgram?: {
appid: string;
pagepath: string;
};
clientMsgId?: string;
}): Promise<any>;
batchGetArticle(options: {
offset?: number;
count: number;
noContent?: 0 | 1;
}): Promise<any>;
}

View File

@ -167,6 +167,99 @@ var WechatPublicInstance = /** @class */ (function () {
});
});
};
WechatPublicInstance.prototype.sendTemplateMessage = function (options) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var openId, templateId, url, data, miniProgram, clientMsgId, myInit, result, errcode;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
openId = options.openId, templateId = options.templateId, url = options.url, data = options.data, miniProgram = options.miniProgram, clientMsgId = options.clientMsgId;
myInit = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
touser: openId,
template_id: templateId,
url: url,
miniProgram: miniProgram,
client_msg_id: clientMsgId,
data: data,
}),
};
return [4 /*yield*/, this.access("https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".concat(this.accessToken), {
errcode: 0,
errmsg: 'ok',
msgid: Date.now(),
}, myInit)];
case 1:
result = _a.sent();
errcode = result.errcode;
if (errcode === 0) {
return [2 /*return*/, Object.assign({ success: true }, result)];
}
return [2 /*return*/, Object.assign({ success: false }, result)];
}
});
});
};
WechatPublicInstance.prototype.batchGetArticle = function (options) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var offset, count, noContent, myInit, result, errcode;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
offset = options.offset, count = options.count, noContent = options.noContent;
myInit = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
offset: offset,
count: count,
no_content: noContent,
}),
};
return [4 /*yield*/, this.access("https://api.weixin.qq.com/cgi-bin/freepublish/batchget?access_token=".concat(this.accessToken), {
"total_count": 1,
"item_count": 1,
"item": [
{
"article_id": 'test',
"content": {
"news_item": [
{
"title": '测试文章',
"author": '测试作者',
"digest": '测试摘要',
"content": '测试内容',
"content_source_url": '',
"thumb_media_id": 'TEST_MEDIA_ID',
"show_cover_pic": 1,
"need_open_comment": 0,
"only_fans_can_comment": 0,
"url": 'TEST_ARTICLE_URL',
"is_deleted": false
}
]
},
"update_time": Date.now(),
},
]
}, myInit)];
case 1:
result = _a.sent();
errcode = result.errcode;
if (!errcode) {
return [2 /*return*/, result];
}
throw new Error(JSON.stringify(result));
}
});
});
};
return WechatPublicInstance;
}());
exports.WechatPublicInstance = WechatPublicInstance;