Created
July 31, 2018 02:48
-
-
Save justan/dbe7b45fafd61fe42bed949a3a3034aa to your computer and use it in GitHub Desktop.
获取小程序码实例 wxacode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 云函数入口函数 | |
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