增加获取小程序临时素材接口

This commit is contained in:
Wang Kejun 2023-10-13 11:10:02 +08:00
parent 579c9803c1
commit 55fad9e54b
8 changed files with 91 additions and 9 deletions

View File

@ -86,6 +86,9 @@ export declare class WechatMpInstance {
filename: string; filename: string;
filetype: string; filetype: string;
}): Promise<any>; }): Promise<any>;
getTemporaryMaterial(options: {
mediaId: string;
}): Promise<any>;
sendServeMessage(options: ServeMessageOption): Promise<any>; sendServeMessage(options: ServeMessageOption): Promise<any>;
private isJson; private isJson;
} }

View File

@ -213,6 +213,26 @@ export class WechatMpInstance {
const result = await this.access(`https://api.weixin.qq.com/cgi-bin/media/upload?access_token=${token}&type=${type}`, myInit); const result = await this.access(`https://api.weixin.qq.com/cgi-bin/media/upload?access_token=${token}&type=${type}`, myInit);
return result; return result;
} }
// 获取临时素材
async getTemporaryMaterial(options) {
const { mediaId } = options;
const myInit = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
media_id: mediaId,
}),
};
const token = await this.getAccessToken();
const result = await this.access(`https://api.weixin.qq.com/cgi-bin/media/get?access_token=${token}`, myInit);
if (this.isJson(result)) {
return result;
}
const arrayBuffer = await result.arrayBuffer();
return arrayBuffer;
}
async sendServeMessage(options) { async sendServeMessage(options) {
const { openId, type } = options; const { openId, type } = options;
const myInit = { const myInit = {

View File

@ -663,7 +663,11 @@ export class WechatPublicInstance {
}; };
const token = await this.getAccessToken(); const token = await this.getAccessToken();
const result = await this.access(`https://api.weixin.qq.com/cgi-bin/media/get?access_token=${token}`, myInit); const result = await this.access(`https://api.weixin.qq.com/cgi-bin/media/get?access_token=${token}`, myInit);
return result; if (this.isJson(result)) {
return result;
}
const arrayBuffer = await result.arrayBuffer();
return arrayBuffer;
} }
async getTicket() { async getTicket() {
const myInit = { const myInit = {

View File

@ -86,6 +86,9 @@ export declare class WechatMpInstance {
filename: string; filename: string;
filetype: string; filetype: string;
}): Promise<any>; }): Promise<any>;
getTemporaryMaterial(options: {
mediaId: string;
}): Promise<any>;
sendServeMessage(options: ServeMessageOption): Promise<any>; sendServeMessage(options: ServeMessageOption): Promise<any>;
private isJson; private isJson;
} }

View File

@ -217,6 +217,26 @@ class WechatMpInstance {
const result = await this.access(`https://api.weixin.qq.com/cgi-bin/media/upload?access_token=${token}&type=${type}`, myInit); const result = await this.access(`https://api.weixin.qq.com/cgi-bin/media/upload?access_token=${token}&type=${type}`, myInit);
return result; return result;
} }
// 获取临时素材
async getTemporaryMaterial(options) {
const { mediaId } = options;
const myInit = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
media_id: mediaId,
}),
};
const token = await this.getAccessToken();
const result = await this.access(`https://api.weixin.qq.com/cgi-bin/media/get?access_token=${token}`, myInit);
if (this.isJson(result)) {
return result;
}
const arrayBuffer = await result.arrayBuffer();
return arrayBuffer;
}
async sendServeMessage(options) { async sendServeMessage(options) {
const { openId, type } = options; const { openId, type } = options;
const myInit = { const myInit = {

View File

@ -667,7 +667,11 @@ class WechatPublicInstance {
}; };
const token = await this.getAccessToken(); const token = await this.getAccessToken();
const result = await this.access(`https://api.weixin.qq.com/cgi-bin/media/get?access_token=${token}`, myInit); const result = await this.access(`https://api.weixin.qq.com/cgi-bin/media/get?access_token=${token}`, myInit);
return result; if (this.isJson(result)) {
return result;
}
const arrayBuffer = await result.arrayBuffer();
return arrayBuffer;
} }
async getTicket() { async getTicket() {
const myInit = { const myInit = {

View File

@ -135,11 +135,11 @@ export class WechatMpInstance {
const json = JSON.parse(data); const json = JSON.parse(data);
if (typeof json.errcode === 'number' && json.errcode !== 0) { if (typeof json.errcode === 'number' && json.errcode !== 0) {
if ([40001, 42001].includes(json.errcode)) { if ([40001, 42001].includes(json.errcode)) {
if (fresh) { if (fresh) {
throw new OakServerProxyException( throw new OakServerProxyException(
'刚刷新的token不可能马上过期请检查是否有并发刷新token的逻辑' '刚刷新的token不可能马上过期请检查是否有并发刷新token的逻辑'
); );
} }
return this.refreshAccessToken(url, init); return this.refreshAccessToken(url, init);
} }
throw new OakExternalException( throw new OakExternalException(
@ -375,6 +375,30 @@ export class WechatMpInstance {
return result; return result;
} }
// 获取临时素材
async getTemporaryMaterial(options: { mediaId: string }) {
const { mediaId } = options;
const myInit = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
media_id: mediaId,
}),
};
const token = await this.getAccessToken();
const result = await this.access(
`https://api.weixin.qq.com/cgi-bin/media/get?access_token=${token}`,
myInit
);
if (this.isJson(result)) {
return result;
}
const arrayBuffer = await result.arrayBuffer();
return arrayBuffer;
}
async sendServeMessage(options: ServeMessageOption) { async sendServeMessage(options: ServeMessageOption) {
const { openId, type } = options; const { openId, type } = options;
const myInit = { const myInit = {

View File

@ -546,7 +546,7 @@ export class WechatPublicInstance {
expireSeconds: result.expire_seconds, expireSeconds: result.expire_seconds,
}; };
} }
async sendTemplateMessage(options: { async sendTemplateMessage(options: {
openId: string; openId: string;
templateId: string; templateId: string;
@ -916,7 +916,11 @@ export class WechatPublicInstance {
`https://api.weixin.qq.com/cgi-bin/media/get?access_token=${token}`, `https://api.weixin.qq.com/cgi-bin/media/get?access_token=${token}`,
myInit myInit
); );
return result; if (this.isJson(result)) {
return result;
}
const arrayBuffer = await result.arrayBuffer();
return arrayBuffer;
} }
async getTicket() { async getTicket() {