Skip to content

Instantly share code, notes, and snippets.

@justan
Created July 31, 2018 02:48
Show Gist options
  • Save justan/dbe7b45fafd61fe42bed949a3a3034aa to your computer and use it in GitHub Desktop.
Save justan/dbe7b45fafd61fe42bed949a3a3034aa to your computer and use it in GitHub Desktop.
获取小程序码实例 wxacode
// 云函数入口函数
const request = require('request')
const appId = 'xxxxxx'
const appSecret = 'xxxxxxxx'
exports.main = ({userInfo}, context) => {
return new Promise((resolve, reject) => {
request({
url: `https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=${appId}&secret=${appSecret}`
}, (e, res, data) => {
if (e) {
reject(e)
} else {
let { access_token } = JSON.parse(data)
request({
url: `https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=${access_token}`,
json: true,
method: 'post',
encoding: null,
body: {
path: 'pages/index'
}
}, (e, res, data) => {
if(e) {
reject(e)
}else {
resolve(Buffer.from(data).toString('base64'))
}
})
}
})
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment