koa2通过https调用生成小程序码

网友投稿 448 2022-05-30

记录一下nodejs下使用HTTPS调用生成小程序码的方法

# 主要思路

调用getwxacodeunlimit之后将图片buffer写入本地并调用云存储http api上传至云存储空间

# 主程序代码

server.js

app.use(async (ctx, next) => {

await next()

ctx.set('content-type', ctx.mimeType)

ctx.body = ctx.data

})

// 获取小程序码

router.get('/getWxaCode', async (ctx, next) => {

let page = ctx.request.query.page || '',

token = ctx.request.header.token || ''

// 获取小程序码配置

const codeOptions = {

method: 'POST',

url:

'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=' + token,

body: {

page: page,

width: 230,

scene: 'pc=1',

koa2通过https调用生成小程序码

},

json: true,

encoding: null,

}

let imgBuffer = await new Promise(function (resolve, reject) {

request.post(codeOptions, function (error, response, body) {

if (!error && response.statusCode == 200) {

resolve(body)

}

reject(error)

})

})

fs.writeFile(page.replace(/\//g, '-') + '.jpg', imgBuffer, function (err) {

//生成图片(把buffer写入到图片文件)

if (err) {

console.log(err)

}

})

// 上传至云存储

const upOptions = {

method: 'POST',

url: 'https://api.weixin.qq.com/tcb/uploadfile?access_token=' + token,

body: {

env: env,//小程序云环境的ID

path: page.replace(/\//g, '-') + '.jpg',

},

json: true,

}

let res = await new Promise(function (resolve, reject) {

request(upOptions, function (error, response, body) {

if (!error && response.statusCode == 200) {

resolve(body)

}

reject(error)

})

})

ctx.data = res

await next()

})

# 参考文档

获取小程序码-wxacode.getUnlimited

获取文件上传链接-uploadFile

https Koa 小程序

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:《数据科学与分析:Python语言实现》 —2.1.2 使用iPython/Jupyter Notebook
下一篇:基础命令考试
相关文章