...
|
...
|
@@ -5,19 +5,65 @@ |
|
|
*/
|
|
|
'use strict';
|
|
|
|
|
|
// const library = '../../../library';
|
|
|
// const _ = require('lodash');
|
|
|
// const channelModel = require('../models/channel');
|
|
|
// const helpers = require(`${library}/helpers`);
|
|
|
// const log = require(`${library}/logger`);
|
|
|
const library = '../../../library';
|
|
|
const passport = require('passport');
|
|
|
const cookie = require(`${library}/cookie`);
|
|
|
const helpers = require(`${library}/helpers`);
|
|
|
const log = require(`${library}/logger`);
|
|
|
const config = require('../../../config/common');
|
|
|
const AuthHelper = require('../models/auth-helper');
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 频道页底部 bannel
|
|
|
* @param {[object]} req
|
|
|
* @param {[object]} res
|
|
|
* @return {[type]}
|
|
|
*/
|
|
|
exports.index = (req, res) => {
|
|
|
res.send('login');
|
|
|
function doPassportCallback(openId, nickname, req, res) {
|
|
|
let shoppingKey = cookie.getShoppingKey(req);
|
|
|
let refer = req.cookies.refer;
|
|
|
|
|
|
if (refer) {
|
|
|
refer = decodeURI(req.cookies.refer);
|
|
|
} else {
|
|
|
refer = `${config.siteUrl}/?go=1`;
|
|
|
}
|
|
|
|
|
|
if (openId && nickname) {
|
|
|
AuthHelper.signinByOpenID(nickname, openId, 'wechat', shoppingKey).then((result) => {
|
|
|
if (result.data['is_bind'] && result.data['is_bind'] === 'N') { //eslint-disable-line
|
|
|
res.redirect(helpers.urlFormat('/passport/bind/index', {
|
|
|
openId: openId,
|
|
|
sourceType: 'wechat'
|
|
|
}));
|
|
|
} else if (result.code === 200 && result.data.uid) {
|
|
|
return AuthHelper.syncUserSession(result.data.uid, res);
|
|
|
}
|
|
|
}).then(() => {
|
|
|
res.redirect(refer);
|
|
|
}).catch((e) => {
|
|
|
log.error('频道页面渲染错误:' + JSON.stringify(e));
|
|
|
res.send('error');
|
|
|
});
|
|
|
}
|
|
|
}
|
|
|
|
|
|
const wechat = {
|
|
|
beforeLogin: (req, res, next) => {
|
|
|
let refer = req.query.refer;
|
|
|
|
|
|
res.cookie('refer', encodeURI(refer));
|
|
|
next();
|
|
|
},
|
|
|
login: (req, res, next) => {
|
|
|
return passport.authenticate('weixin')(req, res, next);
|
|
|
},
|
|
|
callback: (req, res, next) => {
|
|
|
passport.authenticate('weixin', (err, user) => {
|
|
|
if (err) {
|
|
|
return next(err);
|
|
|
}
|
|
|
let nickname = user.nickname;
|
|
|
let openId = user.unionid || user.openid;
|
|
|
|
|
|
doPassportCallback(openId, nickname, req, res);
|
|
|
})(req, res, next);
|
|
|
}
|
|
|
};
|
|
|
|
|
|
exports.wechat = wechat; |
|
|
\ No newline at end of file |
...
|
...
|
|