node.js - koa2的render问题

查看:87
本文介绍了node.js - koa2的render问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

请求:'GET /my/userinfo': getUserInfo
getUserInfo:

var getUserInfo = async (ctx, next) => {
    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没效果。求大神指教一下。看了好多资料还是不会。= =、

解决方案

这个是基于你的写法修改的

var getUserInfo = async (ctx, next) => {
    if(!ctx.query.code){
        ctx.redirect('/my/order');
    }else{
        let code = ctx.query.code;
        var data = tools.getToken(code);
        return data.then(function(data) {
            data = JSON.parse(data);
            tools.getUserInfo(data.access_token, data.openid).then(function(data) {
                data = JSON.parse(data);
                return ctx.render('user', {
                    data: data
                });
            });
        });             
    }};

比较好一点的写法

const getUserInfo = async (ctx, next) => {
  if (!ctx.query.code) {
    ctx.redirect('/my/order');
    return;
  }
  try {
    const code = ctx.query.code;
    const tokenInfo = await tools.getToken(code);
    const user = await tools.getUserInfo(tokenInfo.access_token, tokenInfo.openid);
    await ctx.render('user', {data: user});
  } catch (e) {
    await ctx.render('xxxx'); 
  }
};

getTokengetUserInfo应该在内部对返回结果进行prase,而且你没有进行异常处理

这篇关于node.js - koa2的render问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆