BitTigerio 2018-03-17
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;