...
|
...
|
@@ -2,6 +2,7 @@ import wx from '../utils/wx'; |
|
|
import event from '../common/event';
|
|
|
import Promise from '../vendors/es6-promise';
|
|
|
import accountModel from '../models/account/index';
|
|
|
import config from '../common/config';
|
|
|
|
|
|
/**
|
|
|
* 用户未授权-发送验证码
|
...
|
...
|
@@ -14,7 +15,7 @@ function sendVerifyCode(area, mobile, degrees) { |
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 用户已授权-发送验证码
|
|
|
* 用户已授权-发送验证码`
|
|
|
* @param mobile 手机号
|
|
|
* @param area 国家码
|
|
|
* @param degrees 验证码
|
...
|
...
|
@@ -40,7 +41,8 @@ function isNeedImgCheck() { |
|
|
function getVerifyCode(area, mobile, degrees) {
|
|
|
let app = getApp();
|
|
|
|
|
|
if (!app.globalData.unionID) {
|
|
|
// disableAutoLogin为用户退出,则直接走手机验证码登录接口
|
|
|
if (!app.globalData.unionID || app._getSync('disableAutoLogin')) {
|
|
|
return sendVerifyCode(area, mobile, degrees);
|
|
|
}
|
|
|
|
...
|
...
|
@@ -74,9 +76,13 @@ function checkVerifyCode(area, mobile, verifyCode) { |
|
|
* @param verifyCode
|
|
|
*/
|
|
|
function bindMobileAction(area, mobile, verifyCode) {
|
|
|
if (!getApp().globalData.unionID) {
|
|
|
let app = getApp();
|
|
|
|
|
|
// disableAutoLogin为用户退出,则直接走手机验证码登录接口
|
|
|
if (!app.globalData.unionID || app._getSync('disableAutoLogin')) {
|
|
|
return autoSignin(area, mobile, verifyCode);
|
|
|
}
|
|
|
|
|
|
return checkVerifyCode(area, mobile, verifyCode);
|
|
|
}
|
|
|
|
...
|
...
|
@@ -89,71 +95,79 @@ function bindMobileAction(area, mobile, verifyCode) { |
|
|
function wechatUserIsBind(unionID, nickName) {
|
|
|
let app = getApp();
|
|
|
|
|
|
return accountModel.wechatUserIsBind(unionID, nickName)
|
|
|
.then(data => {
|
|
|
wx.hideLoading();
|
|
|
if (data.data &&
|
|
|
data.data.is_bind &&
|
|
|
data.data.is_bind === 'Y') { // 已经绑定
|
|
|
let userInfo = {};
|
|
|
|
|
|
userInfo.is_bind = data.data.is_bind;
|
|
|
userInfo.mobile = data.data.mobile;
|
|
|
userInfo.ssouid = data.data.ssouid;
|
|
|
userInfo.uid = data.data.uid;
|
|
|
userInfo.sessionKey = data.data.session_key;
|
|
|
|
|
|
app.setUserInfo(userInfo);
|
|
|
app.setSessionKey(userInfo.sessionKey);
|
|
|
|
|
|
return Promise.resolve({
|
|
|
code: 10003,
|
|
|
data: userInfo,
|
|
|
message: '微信用户已绑定手机号'
|
|
|
});
|
|
|
} else {
|
|
|
return Promise.resolve({
|
|
|
code: 10004,
|
|
|
message: '微信用户未绑定手机号'
|
|
|
});
|
|
|
}
|
|
|
});
|
|
|
return accountModel.wechatUserIsBind(unionID, nickName).then(data => {
|
|
|
wx.hideLoading();
|
|
|
if (data.data &&
|
|
|
data.data.is_bind &&
|
|
|
data.data.is_bind === 'Y') { // 已经绑定
|
|
|
let userInfo = {};
|
|
|
|
|
|
userInfo.is_bind = data.data.is_bind;
|
|
|
userInfo.mobile = data.data.mobile;
|
|
|
userInfo.ssouid = data.data.ssouid;
|
|
|
userInfo.uid = data.data.uid;
|
|
|
userInfo.sessionKey = data.data.session_key;
|
|
|
|
|
|
app._removeSync('disableAutoLogin');
|
|
|
app.setUserInfo(userInfo);
|
|
|
app.setSessionKey(userInfo.sessionKey);
|
|
|
|
|
|
// 登录成功回调
|
|
|
event.emit('yas-login-type-report', { LOGIN_TYPE: 4 });
|
|
|
|
|
|
return Promise.resolve({
|
|
|
code: 10003,
|
|
|
data: userInfo,
|
|
|
message: '微信用户已绑定手机号'
|
|
|
});
|
|
|
} else {
|
|
|
return Promise.resolve({
|
|
|
code: 10004,
|
|
|
message: '微信用户未绑定手机号'
|
|
|
});
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取unionID、如果用户拒绝授权则无法获得unionID
|
|
|
* 获取unionID、如果用户拒绝授权则无法获得unionID<废弃>
|
|
|
* @param srd_session
|
|
|
* @param showMsg
|
|
|
* @returns {Promise.<T>}
|
|
|
*/
|
|
|
function getUnionID(srd_session, showMsg) {
|
|
|
let nickName;
|
|
|
let app = getApp();
|
|
|
let nickName;
|
|
|
|
|
|
return wx.getUserInfo()
|
|
|
.then(res => {
|
|
|
let userInfo = res.userInfo;
|
|
|
|
|
|
nickName = userInfo.nickName;
|
|
|
return accountModel.decodeUserInfo(srd_session, res.encryptedData, res.iv);
|
|
|
})
|
|
|
.then(data => {
|
|
|
if (data.data.union_id) {
|
|
|
app.setUnionID(data.data.union_id);
|
|
|
return wechatUserIsBind(data.data.union_id, nickName);
|
|
|
}
|
|
|
})
|
|
|
.catch(() => {
|
|
|
let msg = {
|
|
|
succeed: false,
|
|
|
message: '使用微信小程序登录需要微信授权,您已经拒绝了该请求,请删除小程序重新进入。'
|
|
|
};
|
|
|
return wx.getSetting().then(rset => {
|
|
|
if (rset.authSetting['scope.userInfo']) {
|
|
|
return wx.getUserInfo().then(res => {
|
|
|
let userInfo = res.userInfo;
|
|
|
|
|
|
if (showMsg) {
|
|
|
msg.code = 10001;
|
|
|
}
|
|
|
return Promise.reject(msg);
|
|
|
});
|
|
|
nickName = userInfo.nickName;
|
|
|
event.emit('enable-tap-login');
|
|
|
return accountModel.decodeUserInfo(srd_session, res.encryptedData, res.iv);
|
|
|
});
|
|
|
}
|
|
|
return rset;
|
|
|
}).then(data => {
|
|
|
if (data && data.data && data.data.union_id) {
|
|
|
app.setUnionID(data.data.union_id);
|
|
|
return wechatUserIsBind(data.data.union_id, nickName);
|
|
|
}
|
|
|
}).catch(() => {
|
|
|
let msg = {
|
|
|
succeed: false,
|
|
|
message: '使用微信小程序登录需要微信授权,您已经拒绝了该请求,请删除小程序重新进入。'
|
|
|
};
|
|
|
|
|
|
if (showMsg) {
|
|
|
msg.code = 10001;
|
|
|
}
|
|
|
|
|
|
event.emit('enable-tap-login');
|
|
|
return Promise.reject(msg);
|
|
|
});
|
|
|
}
|
|
|
|
|
|
/**
|
...
|
...
|
@@ -161,44 +175,56 @@ function getUnionID(srd_session, showMsg) { |
|
|
* @param showMsg
|
|
|
* @returns {Promise.<T>}
|
|
|
*/
|
|
|
function wechatAuthLogin(showMsg) { // showMsg: 是否显示拒绝授权提示
|
|
|
function wechatAuthLogin() {
|
|
|
let app = getApp();
|
|
|
|
|
|
return wx.login()
|
|
|
.then(res => {
|
|
|
if (res.code) {
|
|
|
return accountModel.wechatMiniAppLogin(res.code);
|
|
|
}
|
|
|
})
|
|
|
.then(data => {
|
|
|
if (data.code !== 200) {
|
|
|
return Promise.reject({
|
|
|
succeed: false,
|
|
|
message: data.message
|
|
|
});
|
|
|
return wx.login().then(res => {
|
|
|
if (res.code) {
|
|
|
return accountModel.wechatMiniAppLogin(res.code);
|
|
|
}
|
|
|
}).then(data => {
|
|
|
if (data.code !== 200) {
|
|
|
return Promise.reject({
|
|
|
succeed: false,
|
|
|
message: data.message
|
|
|
});
|
|
|
} else {
|
|
|
data = data.data;
|
|
|
app.setOpenID(data.openid);
|
|
|
app.setWechatThirdSession(data.srd_session);
|
|
|
|
|
|
if (data.unionid) {
|
|
|
app.setUnionID(data.unionid);
|
|
|
event.emit('enable-tap-login');
|
|
|
return wechatUserIsBind(data.unionid, '');
|
|
|
} else {
|
|
|
data = data.data;
|
|
|
app.setOpenID(data.openid);
|
|
|
app.setWechatThirdSession(data.srd_session);
|
|
|
|
|
|
// 如果unionID不存在(未使用过任何有货微信产品的全新用户),
|
|
|
// 调用getUnionID函数,再次获取unionID
|
|
|
if (!data.unionid) {
|
|
|
return getUnionID(data.srd_session, showMsg);
|
|
|
} else {
|
|
|
wx.getUserInfo();
|
|
|
app.setUnionID(data.unionid);
|
|
|
return wechatUserIsBind(data.unionid, '');
|
|
|
}
|
|
|
// 如果没有返回unionID,则把原来的清空
|
|
|
wx.setStorage({
|
|
|
key: 'unionID',
|
|
|
data: ''
|
|
|
});
|
|
|
}
|
|
|
})
|
|
|
.catch(err => {
|
|
|
return Promise.reject(err);
|
|
|
});
|
|
|
|
|
|
// return getUnionID(data.srd_session, showMsg);
|
|
|
}
|
|
|
}).catch(err => {
|
|
|
event.emit('enable-tap-login');
|
|
|
return Promise.reject(err);
|
|
|
});
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* [跳转手动登录页面]
|
|
|
* @return {[type]} [description]
|
|
|
*/
|
|
|
function tapGoLogin() {
|
|
|
return wx.navigateTo({
|
|
|
url: '/pages/account/bindMobile',
|
|
|
});
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 用户手动点击登录
|
|
|
* 用户手动点击登录<废弃>
|
|
|
* @returns {Promise.<T>}
|
|
|
*/
|
|
|
function tapToLogin() {
|
...
|
...
|
@@ -206,46 +232,45 @@ function tapToLogin() { |
|
|
let router = global.router;
|
|
|
|
|
|
if (wx.getSetting) {
|
|
|
return wx.getSetting()
|
|
|
.then(res => {
|
|
|
if (res.authSetting['scope.userInfo']) {
|
|
|
return router.go('bindMobile');
|
|
|
} else {
|
|
|
return wx.showModal({
|
|
|
title: '',
|
|
|
content: '检测到您未打开微信用户信息授权,开启后即可进行登录',
|
|
|
confirmText: '去开启',
|
|
|
confirmColor: '#000000'
|
|
|
});
|
|
|
}
|
|
|
})
|
|
|
.then(res => {
|
|
|
if (res.confirm) {
|
|
|
return wx.openSetting();
|
|
|
}
|
|
|
return wx.getSetting().then(res => {
|
|
|
if (res.authSetting['scope.userInfo']) {
|
|
|
return router.goUrl(`${config.MINI_APP_DOMAIN}/pages/account/bindMobile`);
|
|
|
} else {
|
|
|
return wx.showModal({
|
|
|
title: '',
|
|
|
content: '检测到您未打开微信用户信息授权,开启后即可进行登录',
|
|
|
confirmText: '去开启',
|
|
|
confirmColor: '#000000'
|
|
|
});
|
|
|
}
|
|
|
}).then(res => {
|
|
|
if (res.confirm) {
|
|
|
return wx.openSetting();
|
|
|
}
|
|
|
|
|
|
if (res.cancel) {
|
|
|
return router.go('bindMobile');
|
|
|
}
|
|
|
})
|
|
|
.then(res => {
|
|
|
if (res.authSetting['scope.userInfo'] && res.authSetting['scope.userInfo'] === true) {
|
|
|
return getUnionID(app.getWechatThirdSession());
|
|
|
}
|
|
|
})
|
|
|
.then(res => {
|
|
|
if (res.code === 10003) { // 已经绑定手机号
|
|
|
app.setUserInfo(res.data);
|
|
|
app.setSessionKey(res.data.sessionKey);
|
|
|
event.emit('user-login-success');
|
|
|
event.emit('login-type-report', {LOGIN_TYPE: 4});
|
|
|
}
|
|
|
if (res.cancel) {
|
|
|
return wx.navigateTo({
|
|
|
url: '/pages/account/bindMobile',
|
|
|
});
|
|
|
}
|
|
|
}).then(res => {
|
|
|
if (res.authSetting['scope.userInfo'] && res.authSetting['scope.userInfo'] === true) {
|
|
|
return getUnionID(app.getWechatThirdSession());
|
|
|
}
|
|
|
}).then(res => {
|
|
|
if (res.code === 10003) { // 已经绑定手机号
|
|
|
app._removeSync('disableAutoLogin');
|
|
|
app.setUserInfo(res.data);
|
|
|
app.setSessionKey(res.data.sessionKey);
|
|
|
event.emit('user-login-success');
|
|
|
}
|
|
|
|
|
|
if (res.code === 10004) { // 手动授权登录未绑定强制绑定
|
|
|
return router.go('bindMobile');
|
|
|
}
|
|
|
})
|
|
|
.catch(() => {});
|
|
|
if (res.code === 10004) { // 手动授权登录未绑定强制绑定
|
|
|
return wx.navigateTo({
|
|
|
url: '/pages/account/bindMobile',
|
|
|
});
|
|
|
}
|
|
|
}).catch(() => {});
|
|
|
} else {
|
|
|
return wx.showModal({
|
|
|
title: '提示',
|
...
|
...
|
@@ -286,7 +311,7 @@ function decodePhoneNumber(iv, encryptedData) { |
|
|
* @param e
|
|
|
*/
|
|
|
function getPhoneNumber(e) {
|
|
|
const app = getApp();
|
|
|
let app = getApp();
|
|
|
let router = global.router;
|
|
|
|
|
|
if (e.detail.errMsg === 'getPhoneNumber:ok') {
|
...
|
...
|
@@ -294,64 +319,142 @@ function getPhoneNumber(e) { |
|
|
let countryCode;
|
|
|
|
|
|
wx.showLoading();
|
|
|
decodePhoneNumber(e.detail.iv, e.detail.encryptedData)
|
|
|
.then(res => {
|
|
|
phoneNumber = res.data.phoneNumber;
|
|
|
countryCode = res.data.countryCode;
|
|
|
decodePhoneNumber(e.detail.iv, e.detail.encryptedData).then(res => {
|
|
|
phoneNumber = res.data.phoneNumber;
|
|
|
countryCode = res.data.countryCode;
|
|
|
|
|
|
if (res.data.phoneNumber && res.data.countryCode) {
|
|
|
return _bindMiniAppByAuto(res.data.phoneNumber, res.data.countryCode);
|
|
|
}
|
|
|
if (res.data.phoneNumber && res.data.countryCode) {
|
|
|
return _bindMiniAppByAuto(res.data.phoneNumber, res.data.countryCode);
|
|
|
}
|
|
|
return {};
|
|
|
}).then(res => {
|
|
|
wx.hideLoading();
|
|
|
if (res.code === 200) {
|
|
|
let userInfo = {};
|
|
|
|
|
|
return Promise.reject({});
|
|
|
})
|
|
|
.then(res => {
|
|
|
wx.hideLoading();
|
|
|
if (res.code === 200) {
|
|
|
let userInfo = {};
|
|
|
|
|
|
userInfo.uid = res.data.uid;
|
|
|
userInfo.is_bind = res.data.is_bind || '';
|
|
|
userInfo.mobile = res.data.profile;
|
|
|
userInfo.ssouid = res.data.ssouid;
|
|
|
userInfo.sessionKey = res.data.session_key;
|
|
|
|
|
|
app.setUserInfo(userInfo);
|
|
|
app.setSessionKey(userInfo.sessionKey);
|
|
|
|
|
|
event.emit('user-login-success');
|
|
|
|
|
|
if (res.data && res.data.is_register === 0) {
|
|
|
event.emit('bind-auto-register-type-report', {YB_REGISTER_SUCCESS: 5});
|
|
|
}
|
|
|
} else {
|
|
|
return wx.showModal({
|
|
|
title: '提示',
|
|
|
content: res.message || '该手机已是yoho账户,请使用手机号动态登录'
|
|
|
});
|
|
|
}
|
|
|
userInfo.uid = res.data.uid;
|
|
|
userInfo.is_bind = res.data.is_bind || '';
|
|
|
userInfo.mobile = res.data.profile;
|
|
|
userInfo.ssouid = res.data.ssouid;
|
|
|
userInfo.sessionKey = res.data.session_key;
|
|
|
|
|
|
app._removeSync('disableAutoLogin');
|
|
|
app.setUserInfo(userInfo);
|
|
|
app.setSessionKey(userInfo.sessionKey);
|
|
|
|
|
|
})
|
|
|
.then(res => {
|
|
|
if (res && res.confirm) {
|
|
|
router.go('bindMobile', {phone: phoneNumber, area: countryCode});
|
|
|
if (e.currentTarget && !e.currentTarget.dataset.notJump) {
|
|
|
wx.navigateBack(); // 先执行这个,然后在执行event
|
|
|
}
|
|
|
})
|
|
|
.catch(() => {
|
|
|
router.go('bindMobile', {phone: phoneNumber, area: countryCode});
|
|
|
});
|
|
|
|
|
|
event.emit('user-login-success');
|
|
|
event.emit('yas-user-register-success', { REG_TYPE: 5 });//注册成功调用埋点事件
|
|
|
wx.showToast({
|
|
|
icon: 'none',
|
|
|
title: '手机号授权成功!\r\n为享受更多权益,已为您注册有货会员!',
|
|
|
duration: 3000
|
|
|
});
|
|
|
} else {
|
|
|
return wx.showModal({
|
|
|
title: '提示',
|
|
|
content: res.message || '该手机已是yoho账户,请使用手机号动态登录'
|
|
|
});
|
|
|
}
|
|
|
}).then(res => {
|
|
|
if (res && res.confirm) {
|
|
|
router.goUrl(`${config.MINI_APP_DOMAIN}/pages/account/bindMobile?phone=${phoneNumber}&area=${countryCode}`);
|
|
|
}
|
|
|
}).catch(() => {
|
|
|
router.goUrl(`${config.MINI_APP_DOMAIN}/pages/account/bindMobile?phone=${phoneNumber}&area=${countryCode}`);
|
|
|
});
|
|
|
} else {
|
|
|
router.go('bindMobile');
|
|
|
wx.navigateTo({
|
|
|
url: '/pages/account/bindMobile'
|
|
|
});
|
|
|
}
|
|
|
}
|
|
|
|
|
|
function getUserInfo(e) {
|
|
|
let app = getApp();
|
|
|
|
|
|
if (e.detail.errMsg !== 'getUserInfo:ok') {
|
|
|
return Promise.reject({
|
|
|
code: 500,
|
|
|
message: 'getUserInfo authorize fail'
|
|
|
});
|
|
|
}
|
|
|
|
|
|
wx.showLoading();
|
|
|
event.emit('enable-tap-login');
|
|
|
|
|
|
return accountModel.decodeUserInfo(
|
|
|
app.getWechatThirdSession(),
|
|
|
e.detail.encryptedData,
|
|
|
e.detail.iv
|
|
|
).then(res => {
|
|
|
if (res.data && res.data.union_id) {
|
|
|
app.setUnionID(res.data.union_id);
|
|
|
return wechatUserIsBind(res.data.union_id, e.detail.userInfo.nickName);
|
|
|
}
|
|
|
return res;
|
|
|
}).then(res => {
|
|
|
wx.hideLoading();
|
|
|
if (res.code === 10003) { // 微信号已绑定手机号
|
|
|
event.emit('user-login-success');
|
|
|
wx.showToast({
|
|
|
icon: 'none',
|
|
|
title: '登录成功!',
|
|
|
duration: 3000
|
|
|
});
|
|
|
} else if (res.code === 10004) { // 微信用户未绑定手机号
|
|
|
wx.showToast({
|
|
|
icon: 'none',
|
|
|
title: '该微信号未绑定yoho手机账号,请再次单击绑定手机号',
|
|
|
duration: 3000
|
|
|
});
|
|
|
}
|
|
|
// 登录成功或者失败回调
|
|
|
event.emit('user-login-callback', res);
|
|
|
return res;
|
|
|
}).catch(err => {
|
|
|
console.log(err, 'err:getUserInfo--');
|
|
|
wx.hideLoading();
|
|
|
});
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* [获取登录按钮类型]
|
|
|
* @return {[type]} [description]
|
|
|
*/
|
|
|
function getLoginButtonType() {
|
|
|
let app = getApp() || {};
|
|
|
|
|
|
if (app && app.getUid()) {
|
|
|
return ''; // 已经是登录状态,合法用户
|
|
|
}
|
|
|
|
|
|
let isUnionID = app && app.globalData.unionID;// 如果有unionID情况
|
|
|
let isPhoneNumber = isUnionID ? !(app && app.getUid()) : false; // 如果有unionID且没有uid,可以获取手机号自动绑定登录
|
|
|
|
|
|
if (app._getSync('disableAutoLogin')) { // 手动退出,走手机号登录页面
|
|
|
return 'tapGoLogin';
|
|
|
} else if (!isUnionID) {
|
|
|
return 'getUserInfo';
|
|
|
} else if (isPhoneNumber) {
|
|
|
return 'getPhoneNumber';
|
|
|
}
|
|
|
|
|
|
return ''; // 已经是登录状态,合法用户
|
|
|
}
|
|
|
|
|
|
export {
|
|
|
module.exports = {
|
|
|
wechatAuthLogin,
|
|
|
isNeedImgCheck,
|
|
|
getVerifyCode,
|
|
|
bindMobileAction,
|
|
|
tapToLogin,
|
|
|
tapToLogin, // <废弃>
|
|
|
tapGoLogin,
|
|
|
verifySessionKey,
|
|
|
getPhoneNumber
|
|
|
getPhoneNumber,
|
|
|
getUserInfo,
|
|
|
getLoginButtonType
|
|
|
}; |
...
|
...
|
|