搞不懂啊。求指教。
发布于 7 个月前 作者 autumnQH 713 次浏览 来自 问答

请求:'GET /my/userinfo': getUserInfo getUserInfo: var getUserInfo = async (ctx, next) => { console.log('userinfo'); //var code = ctx.params.code; if(!ctx.query.code){ ctx.redirect('/my/order'); }else{ let code = ctx.query.code; var data = tools.getToken(code); data.then(function(data) { data = JSON.parse(data); tools.getUserInfo(data.access_token, data.openid).then(function(data) { data = JSON.parse(data); ctx.render('user', { data: data }); }); }); }}; tools.getToken: exports.getToken = async function (code) { let options = { method: 'get', url: 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='+ config.weixin.appid +'&secret='+ config.weixin.appSecret +'&code='+ code +'&grant_type=authorization_code', headers: [ { name: 'content-type', value: 'application/x-www-form-urlencoded' } ], } return new Promise(function (resolve, reject){ request(options, function(err, res, body) { if(body){ return resolve(body); }else{ return reject(err); } }); }); } tools.getUserInfo: exports.getUserInfo = function (AccessToken, openid) { let options ={ method: 'get', url: 'https://api.weixin.qq.com/sns/userinfo?access_token='+ AccessToken+'&openid='+ openid+'&lang=zh_CN', headers: [ { name: 'content-type', value: 'application/x-www-form-urlencoded' } ], }**** return new Promise((resolve, reject)=>{ request(options, function(err, res, body) { if(res){ return resolve(body); }else{ return reject(err); } }); }); } 现在ctx.render没效果。求大神指教一下。看了好多资料还是不会。= =、

1 回复

就不能弄弄格式

回到顶部