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

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;
filetype: string;
}): Promise<any>;
getTemporaryMaterial(options: {
mediaId: string;
}): Promise<any>;
sendServeMessage(options: ServeMessageOption): Promise<any>;
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);
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) {
const { openId, type } = options;
const myInit = {

View File

@ -663,7 +663,11 @@ export class WechatPublicInstance {
};
const token = await this.getAccessToken();
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() {
const myInit = {

View File

@ -86,6 +86,9 @@ export declare class WechatMpInstance {
filename: string;
filetype: string;
}): Promise<any>;
getTemporaryMaterial(options: {
mediaId: string;
}): Promise<any>;
sendServeMessage(options: ServeMessageOption): Promise<any>;
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);
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) {
const { openId, type } = options;
const myInit = {

View File

@ -667,7 +667,11 @@ class WechatPublicInstance {
};
const token = await this.getAccessToken();
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() {
const myInit = {

View File

@ -135,11 +135,11 @@ export class WechatMpInstance {
const json = JSON.parse(data);
if (typeof json.errcode === 'number' && json.errcode !== 0) {
if ([40001, 42001].includes(json.errcode)) {
if (fresh) {
throw new OakServerProxyException(
'刚刷新的token不可能马上过期请检查是否有并发刷新token的逻辑'
);
}
if (fresh) {
throw new OakServerProxyException(
'刚刷新的token不可能马上过期请检查是否有并发刷新token的逻辑'
);
}
return this.refreshAccessToken(url, init);
}
throw new OakExternalException(
@ -375,6 +375,30 @@ export class WechatMpInstance {
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) {
const { openId, type } = options;
const myInit = {

View File

@ -546,7 +546,7 @@ export class WechatPublicInstance {
expireSeconds: result.expire_seconds,
};
}
async sendTemplateMessage(options: {
openId: string;
templateId: string;
@ -916,7 +916,11 @@ export class WechatPublicInstance {
`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() {