微信小程序获取到Openid
前台代码片段
onLoad: function () { wx.login({ success(res) { console.log(res.code) if (res.code) { wx.request({ url: 'http://localhost:1996/pay', data:{ code: res.code }, success(res) { console.log(res.data) } }); } else { console.log('登录失败!' + res.errMsg) } } }); },
后台代码片段
const Router = require('koa-router'); const router = new Router(); const request = require('request'); function getOpenId(code) { return new Promise((resolve, reject) => { const id = 'wxbd53208a40c5d51c'; // AppID(小程序ID) const secret = '92fea41a425cc8ff9d9596c434ec67fb';// AppSecret(小程序密钥) let url = `https://api.weixin.qq.com/sns/jscode2session?appid=${id}&secret=${secret}&js_code=${code}&grant_type=authorization_code`; request(url, (error, response, body) => { if (error) reject(error); resolve(body); }); }) } let index = router.get('/pay', async (ctx, next) => { await next(); ctx.set("Access-Control-Allow-Origin", '*'); ctx.status = ; ctx.type = 'json'; if (ctx.query) { const { code } = ctx.query; const data = await getOpenId(code); ctx.body = data; } }).routes(); module.exports = index;
相关推荐
zuoliangzhu 2020-05-16
蓝色深海 2020-03-24
kgshuo 2020-09-25
Tomato 2020-09-10
taiyangyu 2020-09-10
CodeAndroid 2020-09-10
small 2020-07-29
sucheng 2020-07-26
zuoliangzhu 2020-07-20
CodeAndroid 2020-07-14
xiaoxubbs 2020-07-04
sucheng 2020-06-25
kgshuo 2020-06-14
意外金喜 2020-06-14
zuoliangzhu 2020-06-14
tianping 2020-06-14
hgzhang 2020-06-14
killgod 2020-06-14