QLScriptPublic/wxapp/wcs.js
2026-05-14 15:10:35 +08:00

58 lines
1.8 KiB
JavaScript

const axios = require("axios");
class WeChatCodeServer {
constructor(options) {
this.serverUrl = options.url;
this.appid = options.appid;
this.auth = options.auth;
}
getCode(openid) {
console.log('等待获取code:');
return new Promise((resolve, reject) => {
axios.post(this.serverUrl + '/wx/code', { appid: this.appid, openid }, {
headers: {
'auth': this.auth
},
timeout: 30 * 1000
}).then(res => {
console.log('获取code成功:');
resolve(res);
}).catch(err => {
reject(err);
});
});
}
cloudInit(openid) {
console.log('等待云函数初始化:');
return new Promise((resolve, reject) => {
axios.post(this.serverUrl + '/wx/call/init', { appid: this.appid, openid }, {
headers: {
'auth': this.auth
},
timeout: 30 * 1000
}).then(res => {
console.log('云函数初始化成功:');
resolve(res);
}).catch(err => {
reject(err);
});
});
}
cloudCall(openid) {
console.log('等待云函数调用:');
return new Promise((resolve, reject) => {
axios.post(this.serverUrl + '/wx/cloud/call', { appid: this.appid, openid }, {
headers: {
'auth': this.auth
},
timeout: 30 * 1000
}).then(res => {
console.log('云函数调用成功:');
resolve(res);
}).catch(err => {
reject(err);
});
});
}
}
module.exports = WeChatCodeServer;