fix: 微信登录绑定账号功能

main
wx-jincw 3 weeks ago
parent 7fa05c4d1b
commit 71aa31628d

@ -45,5 +45,5 @@ module.exports = {
* false=关闭跳转账号密码页 register/binding/account 绑定已有账号
* 可被后端 wechat/getLogo 返回的 routineLoginRequirePhone 覆盖
*/
MP_WECHAT_REQUIRE_PHONE: true
MP_WECHAT_REQUIRE_PHONE: false
};

@ -38,46 +38,36 @@ export function _toLogin(push, pathLogin) {
pathLogin = '/page/users/login/index'
Cache.set('login_back_url',path);
}
// // #ifdef H5
// if (isWeixin()) {
// let urlData = location.pathname + location.search
// if (urlData.indexOf('?') !== -1) {
// urlData += '&go_longin=1';
// } else {
// urlData += '?go_longin=1';
// }
// if (!Cache.has('snsapiKey')) {
// auth.oAuth('snsapi_base', urlData);
// } else {
// if (['/pages/user/index'].indexOf(login_back_url) == -1) {
// uni.navigateTo({
// url: '/pages/users/wechat_login/index'
// })
// }
// }
// } else {
// if (['/pages/user/index'].indexOf(login_back_url) == -1) {
// uni.navigateTo({
// url: '/pages/users/login/index'
// })
// }
// }
// // #endif
// if (['pages/user/index','/pages/user/index'].indexOf(login_back_url) == -1) {
// // #ifdef MP
// uni.navigateTo({
// url: '/pages/users/wechat_login/index'
// })
// // #endif
// }
if (['pages/user/index','/pages/user/index'].indexOf(login_back_url) == -1) {
// #ifdef H5
if (isWeixin()) {
let urlData = location.pathname + location.search
if (urlData.indexOf('?') !== -1) {
urlData += '&go_longin=1';
} else {
urlData += '?go_longin=1';
}
if (!Cache.has('snsapiKey')) {
auth.oAuth('snsapi_base', urlData);
} else {
uni.navigateTo({
url: '/pages/users/wechat_login/index'
})
}
} else {
uni.navigateTo({
url: '/pages/users/login/index'
})
}
// #endif
// #ifdef MP
uni.navigateTo({
url: '/pages/users/login/index'
url: '/pages/users/wechat_login/index'
})
}
// #endif
}
}

@ -47,9 +47,12 @@
</div>
</div>
<div class="logon" @click="loginMobile" v-if="current !== 0"></div>
<div class="logon" @click="submit" v-if="current === 0"></div>
<div class="logon" @click="submit" v-if="current === 0">{{ bindWechatMode ? '' : '' }}</div>
<!-- #ifdef MP-WEIXIN -->
<div class="logon logon-wechat" v-if="current === 0" @click="goWechatLogin"></div>
<div class="logon logon-wechat" v-if="current === 0 && !bindWechatMode" @click="goWechatLogin"></div>
<!-- #endif -->
<!-- #ifdef MP-WEIXIN -->
<div class="bind-wechat-tip" v-if="bindWechatMode">使</div>
<!-- #endif -->
<div class="tips">
<!-- <div v-if="current==0" @click="current = 1"></div> -->
@ -80,7 +83,7 @@
validatorDefaultCatch
} from "@/utils/dialog";
import {
getLogo, appAuth, appleLogin
getLogo, appAuth, appleLogin, bindRoutineAccount
} from "@/api/public";
import {
VUE_APP_API_URL
@ -116,7 +119,10 @@
appleLoginStatus: false, //
appleUserInfo: null,
appleShow: false, // ios13
pendingGzhCode: '' //
pendingGzhCode: '', //
// wechat_login register
bindWechatMode: false,
wechatBindAuthKey: ''
};
},
watch:{
@ -132,7 +138,13 @@
this.getCode();
this.getLogoImage();
},
onLoad() {
onLoad(options) {
// #ifdef MP-WEIXIN
if (options.bindWechat === '1' && options.authKey) {
this.bindWechatMode = true;
this.wechatBindAuthKey = decodeURIComponent(options.authKey);
}
// #endif
let self = this
uni.getSystemInfo({
success: function(res) {
@ -445,6 +457,43 @@
if (!that.password) return that.$util.Tips({
title: '请填写密码'
});
// openId
// #ifdef MP-WEIXIN
if (that.bindWechatMode) {
if (!/^1(3|4|5|7|8|9|6)\d{9}$/i.test(that.account)) {
return that.$util.Tips({
title: '请输入正确的手机号码'
});
}
if (!that.wechatBindAuthKey) {
return that.$util.Tips({
title: '微信授权已失效,请返回重新授权'
});
}
uni.showLoading({ title: '绑定中' });
bindRoutineAccount({
account: that.account,
password: that.password,
key: that.wechatBindAuthKey
}).then(res => {
uni.hideLoading();
that.wechatBindAuthKey = '';
that.bindWechatMode = false;
that.$store.commit('LOGIN', {
token: res.data.token
});
that.getUserInfo(res.data);
}).catch(e => {
uni.hideLoading();
that.$util.Tips({
title: e
});
});
return;
}
// #endif
const loginPayload = {
account: that.account,
password: that.password,
@ -686,6 +735,15 @@
margin-top: 32rpx;
background-color: #07c160;
}
.bind-wechat-tip {
margin-top: 24rpx;
padding: 0 20rpx;
text-align: center;
font-size: 24rpx;
color: #999;
line-height: 1.6;
}
.tips {
margin: 30rpx;

@ -23,7 +23,11 @@
<!-- #ifdef MP -->
<button hover-class="none" @tap="getUserProfile" class="bg-green btn1">微信登录</button>
<!-- #endif -->
<!-- <button hover-class="none" @click="isUp = true" class="btn2">手机号登录</button> -->
<!-- 关闭强制手机号时引导已有账号用户去绑定 -->
<!-- #ifdef MP -->
<button hover-class="none" v-if="!requirePhone" @click="goBindAccountLogin" class="btn2"></button>
<!-- #endif -->
<button hover-class="none" @click="goAccountLogin" class="btn2">账号密码登录</button>
</view>
</view>
<block v-if="isUp">
@ -72,7 +76,11 @@
authKey: '',
options: '',
userInfo: {},
codeNum: 0
codeNum: 0,
// true=false=
requirePhone: false,
// key使
pendingBindAuthKey: ''
}
},
components: {
@ -80,9 +88,11 @@
routinePhone
},
onLoad(options) {
// getLogo().then(res => {
// this.logoUrl = res.data.logoUrl
// })
// #ifdef MP
// getLogo requirePhone
this.requirePhone = this.parseRequirePhoneFlag(this.$config.MP_WECHAT_REQUIRE_PHONE);
this.loadMpLoginConfig();
// #endif
let that = this
// #ifdef H5
document.body.addEventListener("focusout", () => {
@ -131,6 +141,66 @@
},
methods: {
// #ifdef MP
/**
* 是否强制获取微信手机号 "1" / true 为开启其余均为关闭
*/
parseRequirePhoneFlag(value) {
return value === true || value === 1 || String(value) === '1';
},
/**
* 拉取小程序微信登录配置是否强制获取手机号
* 后端 system_config.routine_login_require_phone1=开启0=关闭默认
* 未配置时使用 app/config/app.js 中的 MP_WECHAT_REQUIRE_PHONE
*/
loadMpLoginConfig() {
const localDefault = this.parseRequirePhoneFlag(this.$config.MP_WECHAT_REQUIRE_PHONE);
return getLogo().then(res => {
const cfg = (res && res.data) ? res.data : {};
if (cfg.logoUrl) {
this.logoUrl = cfg.logoUrl;
}
// routineLoginRequirePhone
if (cfg.routineLoginRequirePhone !== undefined && cfg.routineLoginRequirePhone !== null && cfg.routineLoginRequirePhone !== '') {
this.requirePhone = this.parseRequirePhoneFlag(cfg.routineLoginRequirePhone);
} else {
this.requirePhone = localDefault;
}
return this.requirePhone;
}).catch(() => {
this.requirePhone = localDefault;
return this.requirePhone;
});
},
/**
* 关闭强制手机号时跳转账号密码页进行微信绑定
* 若尚未微信授权提示先点微信登录
*/
goBindAccountLogin() {
if (!this.pendingBindAuthKey) {
return this.$util.Tips({
title: '请先点击上方「微信登录」完成授权'
});
}
uni.navigateTo({
url: '/pages/users/login/index?bindWechat=1&authKey=' + encodeURIComponent(this.pendingBindAuthKey)
});
},
/**
* 新微信用户且关闭强制手机号跳转绑定页
*/
goWechatBindLogin(authKey) {
this.pendingBindAuthKey = authKey;
uni.navigateTo({
url: '/pages/users/login/index?bindWechat=1&authKey=' + encodeURIComponent(authKey)
});
},
// #endif
goAccountLogin() {
uni.navigateTo({
url: '/pages/users/login/index'
});
},
back() {
uni.navigateBack();
},
@ -224,19 +294,22 @@
uni.showLoading({
title: '正在登录中'
});
Routine.getUserProfile()
.then(res => {
Routine.getCode()
.then(code => {
self.getWxUser(code, res);
})
.catch(res => {
uni.hideLoading();
});
})
.catch(res => {
uni.hideLoading();
});
// requirePhone
self.loadMpLoginConfig().finally(() => {
Routine.getUserProfile()
.then(res => {
Routine.getCode()
.then(code => {
self.getWxUser(code, res);
})
.catch(() => {
uni.hideLoading();
});
})
.catch(() => {
uni.hideLoading();
});
});
},
getWxUser(code, res) {
@ -254,17 +327,24 @@
userInfo.type = 'routine'
Routine.authUserInfo(userInfo.code, userInfo)
.then(res => {
self.authKey = res.data.key;
if (res.data.type === 'register') {
const payload = (res && res.data) ? res.data : (res || {});
self.authKey = payload.key;
if (payload.type === 'register') {
uni.hideLoading();
self.isPhoneBox = true
self.isPhoneBox = false;
//
if (self.requirePhone) {
self.isPhoneBox = true;
} else {
self.goWechatBindLogin(payload.key);
}
}
if (res.data.type === 'login') {
if (payload.type === 'login') {
uni.hideLoading();
self.$store.commit('LOGIN', {
token: res.data.token
token: payload.token
});
self.$store.commit("SETUID", res.data.uid);
self.$store.commit("SETUID", payload.uid);
self.getUserInfo();
self.$util.Tips({
title: res,

Loading…
Cancel
Save