Authored by yyq

login

... ... @@ -26,6 +26,11 @@ export default {
bounces: false,
clearCacheWhenDestroy: false
});
// queryString中携带bind_code,则弹出绑定弹框
if (this.$route.query.bind_code) {
this.$createThirdBind().show();
}
},
watch: {
'yoho.context.needLogin': function(newVal) {
... ...
... ... @@ -18,7 +18,7 @@ const errHandle = (error) => {
});
};
const request = (options, store) => {
sign(options);
// sign(options);
return axios(options).then((res) => {
if (res.data.code === 401) {
... ...
... ... @@ -4,7 +4,7 @@
<div class="under-row">
<i class="iconfont iconphone2"></i>
<div class="select-block">
<CubeSelect class="area-code-select" v-model="code" :options="options"></CubeSelect>
<CubeSelect class="area-code-select" v-model="code" :options="options" :title="selectTitle"></CubeSelect>
</div>
<CubeInput class="bind-input" v-model="phone" placeholder="请输入手机号"></CubeInput>
</div>
... ... @@ -14,7 +14,7 @@
<CubeButton class="send-sms-btn" :disabled="!!sendBtnText" @click="sendSMS">{{sendBtnText || '获取验证码'}}</CubeButton>
</div>
<div class="submit-row">
<CubeButton class="bind-btn">登录</CubeButton>
<CubeButton class="bind-btn" @click="bindSubmit">登录</CubeButton>
</div>
</div>
</div>
... ... @@ -23,6 +23,45 @@
<script>
import { Button, Input, Select } from 'cube-ui';
import { mapActions, mapState } from 'vuex';
const areaList = [{
value: '+61',
name: '澳大利亚'
}, {
value: '+82',
text: '韩国'
}, {
value: '+1',
text: '加拿大'
}, {
value: '+60',
text: '马来西亚'
}, {
value: '+1',
text: '美国'
}, {
value: '+81',
text: '日本'
}, {
value: '+65',
text: '新加坡'
}, {
value: '+44',
text: '英国'
}, {
value: '+86',
text: '中国'
}, {
value: '+853',
text: '中国澳门'
}, {
value: '+886',
text: '中国台湾'
}, {
value: '+852',
text: '中国香港'
}];
export default {
name: 'ThirdBind',
... ... @@ -30,16 +69,29 @@ export default {
return {
showBind: false,
code: '+86',
options: ['+86', '+8700', '+88'],
options: areaList,
selectTitle: '选择国家和地区',
phone: '',
smsCode: '',
sendBtnText: ''
}
},
methods: {
...mapActions(['sendBindSms', 'submitThirdBind']),
show() {
this.bindCode = this.$route.query.bind_code;
this.showBind = true;
},
close() {
this.showBind = false;
},
toast(msg, time = 2000) {
this.$createToast && this.$createToast({
txt: msg,
type: 'txt',
time
}).show();
},
sendSMS() {
let total = 60;
let timer = setInterval(() => {
... ... @@ -47,10 +99,32 @@ export default {
this.sendBtnText = '重新获取 ' + total;
} else {
this.sendBtnText = '';
clearInterval(timer)
clearInterval(timer);
}
}, 1000);
this.sendBtnText = '重新获取 ' + total;
this.sendBindSms({
mobile: this.phone,
bindCode: this.bindCode
});
},
bindSubmit() {
if (this.phone && this.smsCode) {
this.submitThirdBind({
mobile: this.phone,
code: this.smsCode,
bindCode: this.bindCode
}).then(res => {
if (res.code === 200) {
this.close();
} else {
this.toast(res.message);
}
});
} else {
this.toast(this.phone ? '请输入短信验证码' : '请输入手机号');
}
}
},
components: {
... ... @@ -101,7 +175,7 @@ export default {
}
.select-block {
width: 130px;
min-width: 130px;
position: relative;
margin-right: 10px;
... ...
... ... @@ -14,7 +14,6 @@ export default {
beforeRouteUpdate(to, from, next) {
},
mounted() {
this.$createThirdBind().show();
// this.$yoho.authRealName();
},
methods: {
... ...
... ... @@ -149,6 +149,19 @@ export default function(mergeState = {}) {
backImageUrl
});
},
sendBindSms({ commit }, { mobile, bindCode }) {
return this.$api.post('/passport/bind/sms/send', {
mobile,
bindCode
});
},
submitThirdBind({ commit }, { mobile, code, bindCode }) {
return this.$api.post('/passport/bind/third', {
mobile,
code,
bindCode
});
},
reportError(params, {error}) {
this.$reportError(error);
},
... ...
... ... @@ -12,6 +12,8 @@ router.post('/xianyu/upload/idcard', multipartMiddleware, upload.uploadIdCard);
router.get('/xianyu/passport/login/taobao', passport.login.taobaoLogin);
router.get('/xianyu/passport/callback/taobao', passport.login.taobaoCallback);
router.post('/xianyu/passport/bind/sms/send', passport.bind.sendSms);
router.post('/xianyu/passport/bind/third', passport.bind.bindByCode);
app.use(router);
... ...
const _ = require('lodash');
const moment = require('moment');
const crypto = global.yoho.crypto;
const log = global.yoho.logger;
... ... @@ -39,7 +40,7 @@ const dynamicDecrypt = (str) => {
let decryptInfo = {};
try {
let decryptStr = crypto.decrypt(getDailyKey(), `${str}`);
let decryptStr = _.trim(crypto.decrypt(getDailyKey(), `${str}`));
let decryptArr = decryptStr.split(connectSymbol);
decryptInfo.val = decryptArr[0];
... ...
... ... @@ -35,7 +35,7 @@ function signMD5(params, clientSecret) {
*/
function Strategy(options, verify) {
options = options || {};
options.authorizationURL = options.authorizationURL || 'https://oauth.taobao.com/authorize';
options.authorizationURL = options.authorizationURL || 'https://oauth.taobao.com/authorize?view=wap';
options.tokenURL = options.tokenURL || 'https://oauth.taobao.com/token';
options.scopeSeparator = options.scopeSeparator || ',';
options.customHeaders = options.customHeaders || {};
... ...
... ... @@ -13,7 +13,7 @@ const crypto = global.yoho.crypto;
const config = global.yoho.config;
const loginPage = `${config.siteUrl}/signin.html`;
const bindPage = `/xianyu/passport/bind`;
const homePage = `${config.siteUrl}/xianyu/channel`;
// taobao 登录
passport.use('taobao', new TaobaoStrategy({
... ... @@ -25,7 +25,7 @@ passport.use('taobao', new TaobaoStrategy({
done(null, profile);
}));
class signModel extends global.yoho.BaseModel {
class passportModel extends global.yoho.BaseModel {
constructor(ctx) {
super(ctx);
}
... ... @@ -41,10 +41,7 @@ class signModel extends global.yoho.BaseModel {
param.business_line = businessLine;
}
return this.get({ data: param }).then(res => {
console.log(res);
return res;
});
return this.get({ data: param });
}
syncUserSession({uid, sessionKey, req, res}) {
let userId = {
... ... @@ -106,10 +103,30 @@ class signModel extends global.yoho.BaseModel {
});
});
}
sendTaobaoBindCode(mobile) {
return this.post({
data: {
method: 'app.bind.sendCodeByTB',
source_type: 'taobao',
mobile
}
});
}
bindTaobaoAccountByCode({ mobile, code, openId }) {
return this.post({
data: {
method: 'app.bind.bindTBByCode',
source_type: 'taobao',
mobile,
code,
open_id: openId
}
});
}
};
const login = {
taobaoLogin: (req, res, next) => {
taobaoLogin(req, res, next) {
req.session.authState = uuid.v4();
return passport.authenticate('taobao', {
... ... @@ -117,14 +134,14 @@ const login = {
failWithError: true
})(req, res, next);
},
taobaoCallback: (req, res, next) => {
taobaoCallback(req, res, next) {
passport.authenticate('taobao', (err, user) => {
if (err || !user) {
log.error(`taobao authenticate error : ${JSON.stringify(err)}`);
return res.redirect(loginPage);
}
const model = req.ctx(signModel);
console.log(user);
const model = req.ctx(passportModel);
return model.signinByOpenID({
openId: user.open_uid,
... ... @@ -134,11 +151,9 @@ const login = {
if (result.code === 200) {
if (_.get(result, 'data.is_bind') === 'N') {
redirectUrl = req.cookies.third_backurl ? url.parse(req.cookies.third_backurl) : '/xianyu/passport/bind';
if (redirectUrl.indexOf('?')) {
redirectUrl += '&bind_code' + crypto.dynamicEncryption(user.open_uid);
}
redirectUrl = req.cookies.third_backurl ? url.parse(req.cookies.third_backurl) : homePage;
redirectUrl += redirectUrl.indexOf('?') > 0 ? '&' : '?';
redirectUrl += 'bind_code=' + encodeURIComponent(aes.dynamicEncryption(`taobao::${user.open_uid}`));
} else if (+_.get(result, 'data.uid') > 0) {
return model.syncUserSession({
uid: result.data.uid,
... ... @@ -146,7 +161,7 @@ const login = {
req,
res
}).finally(() => {
let refer = req.cookies.third_backurl ? url.parse(req.cookies.third_backurl) : '';
let refer = req.cookies.third_backurl ? url.parse(req.cookies.third_backurl) : homePage;
return res.redirect(refer);
});
... ... @@ -157,8 +172,74 @@ const login = {
})
})(req, res, next);
}
};
const bind = {
getBindThirdInfo(bindCode) {
let info = aes.dynamicDecrypt(bindCode);
let bindInfo = {
timestamp: info.timestamp
};
if (info.val) {
let splitArr = info.val.split('::');
bindInfo.type = splitArr[0];
bindInfo.openId = splitArr[1];
}
return bindInfo;
},
sendSms(req, res, next) {
let { mobile, bindCode } = req.body || {};
console.log(this)
let info = bind.getBindThirdInfo(bindCode);
if (info.type === 'taobao') {
req.ctx(passportModel).sendTaobaoBindCode(mobile).then(res.json).catch(next);
} else {
res.json({
code: 200,
message: `${info.type || ''} not found`
});
}
},
bindByCode(req, res, next) {
let { mobile, code, bindCode } = req.body || {};
let info = bind.getBindThirdInfo(bindCode);
if (info.type === 'taobao') {
const model = req.ctx(passportModel);
model.bindTaobaoAccountByCode({
mobile,
code,
openId: info.openId
}).then(result => {
if (_.get(result, 'data.is_bind') === 'Y') {
model.syncUserSession({
uid: result.data.uid,
sessionKey: result.data.session_key,
req,
res
}).finally(() => {
delete result.data;
res.json(result);
});
} else {
res.json(result);
}
}).catch(next);
} else {
res.json({
code: 400,
message: '登录失败请稍后重试'
});
}
}
}
module.exports = {
login
login,
bind
};
... ...