redis.on('connect', function () {
     howdo
         .task(function (next) {
             redis.get(KEY, next);
         })
         .task(function (next, val) {
             expect(val).to.equal(null);
             redis.set(KEY, 1, next);
         })
         .task(function (next) {
             redis.get(KEY, next);
         })
         .task(function (next, val) {
             expect(val).to.equal(1);
             redis.set(KEY, 2, 1000, next);
         })
         .task(function (next) {
             setTimeout(function () {
                 redis.get(KEY, next);
             }, 1000);
         })
         .task(function (next, val) {
             expect(val).to.equal(null);
             redis.set(KEY, 3, next);
         })
         .task(function (next) {
             redis.get(KEY, next);
         })
         .task(function (next, val) {
             expect(val).to.equal(3);
             redis.remove(KEY, 3);
             next();
         })
         .task(function (next) {
             redis.get(KEY, next);
         })
         .task(function (next, val) {
             expect(val).to.equal(null);
             next();
         })
         .follow(done);
 });
Example #2
0
    return function (req, res, next) {
        if (req.session.$weixinUser) {
            res.locals.$weixinUser = req.session.$weixinUser;
            return next();
        }

        var redirectURL = configs.weixin.authCallbackURL + qs.stringify({
                url: req.$fullURL
            });
        var q = object.assign({}, weixinAuthOptions, {
            redirect_uri: redirectURL,
            state: random.string()
        });
        var url = configs.weixin.authURL + qs.stringify(q);
        var code = req.query.code;

        if (typeis.Array(code)) {
            code = code.pop();
        }

        if (!code) {
            return res.redirect(url);
        }

        howdo
        // 获取 accessToken、openId
            .task(function (next) {
                weixin.getAccessToken(req.query.code, next);
            })
            // 获取用户基本信息
            .task(function (next, ao) {
                weixin.getUserInfo(ao.openId, ao.accessToken, next);
            })
            .follow(function (err, weixinUser) {
                if (err) {
                    return res.redirect(url);
                }

                res.locals.$weixinUser = req.session.$weixinUser = weixinUser;
                next();
            });
    };