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没效果。求大神指教一下。看了好多资料还是不会。= =、

阅读 6.6k
3 个回答

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

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,而且你没有进行异常处理

看代码应该是用模板来写的页面对吧,看看中间件中是否正确设置了静态资源目录,和模板

谢谢各位大神。我自己研究出来了。确实用的是答案的方法,只不过没用try-cahth。用了if-else。原因也有模版问题。谢谢

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题