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

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

@ -45,5 +45,5 @@ module.exports = {
* false=关闭跳转账号密码页 register/binding/account 绑定已有账号 * false=关闭跳转账号密码页 register/binding/account 绑定已有账号
* 可被后端 wechat/getLogo 返回的 routineLoginRequirePhone 覆盖 * 可被后端 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' pathLogin = '/page/users/login/index'
Cache.set('login_back_url',path); 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) { 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({ uni.navigateTo({
url: '/pages/users/login/index' url: '/pages/users/wechat_login/index'
}) })
} // #endif
}
} }

@ -47,9 +47,12 @@
</div> </div>
</div> </div>
<div class="logon" @click="loginMobile" v-if="current !== 0"></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 --> <!-- #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 --> <!-- #endif -->
<div class="tips"> <div class="tips">
<!-- <div v-if="current==0" @click="current = 1"></div> --> <!-- <div v-if="current==0" @click="current = 1"></div> -->
@ -80,7 +83,7 @@
validatorDefaultCatch validatorDefaultCatch
} from "@/utils/dialog"; } from "@/utils/dialog";
import { import {
getLogo, appAuth, appleLogin getLogo, appAuth, appleLogin, bindRoutineAccount
} from "@/api/public"; } from "@/api/public";
import { import {
VUE_APP_API_URL VUE_APP_API_URL
@ -116,7 +119,10 @@
appleLoginStatus: false, // appleLoginStatus: false, //
appleUserInfo: null, appleUserInfo: null,
appleShow: false, // ios13 appleShow: false, // ios13
pendingGzhCode: '' // pendingGzhCode: '', //
// wechat_login register
bindWechatMode: false,
wechatBindAuthKey: ''
}; };
}, },
watch:{ watch:{
@ -132,7 +138,13 @@
this.getCode(); this.getCode();
this.getLogoImage(); 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 let self = this
uni.getSystemInfo({ uni.getSystemInfo({
success: function(res) { success: function(res) {
@ -445,6 +457,43 @@
if (!that.password) return that.$util.Tips({ if (!that.password) return that.$util.Tips({
title: '请填写密码' 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 = { const loginPayload = {
account: that.account, account: that.account,
password: that.password, password: that.password,
@ -686,6 +735,15 @@
margin-top: 32rpx; margin-top: 32rpx;
background-color: #07c160; 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 { .tips {
margin: 30rpx; margin: 30rpx;

@ -23,7 +23,11 @@
<!-- #ifdef MP --> <!-- #ifdef MP -->
<button hover-class="none" @tap="getUserProfile" class="bg-green btn1">微信登录</button> <button hover-class="none" @tap="getUserProfile" class="bg-green btn1">微信登录</button>
<!-- #endif --> <!-- #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>
</view> </view>
<block v-if="isUp"> <block v-if="isUp">
@ -72,7 +76,11 @@
authKey: '', authKey: '',
options: '', options: '',
userInfo: {}, userInfo: {},
codeNum: 0 codeNum: 0,
// true=false=
requirePhone: false,
// key使
pendingBindAuthKey: ''
} }
}, },
components: { components: {
@ -80,9 +88,11 @@
routinePhone routinePhone
}, },
onLoad(options) { onLoad(options) {
// getLogo().then(res => { // #ifdef MP
// this.logoUrl = res.data.logoUrl // getLogo requirePhone
// }) this.requirePhone = this.parseRequirePhoneFlag(this.$config.MP_WECHAT_REQUIRE_PHONE);
this.loadMpLoginConfig();
// #endif
let that = this let that = this
// #ifdef H5 // #ifdef H5
document.body.addEventListener("focusout", () => { document.body.addEventListener("focusout", () => {
@ -131,6 +141,66 @@
}, },
methods: { 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() { back() {
uni.navigateBack(); uni.navigateBack();
}, },
@ -224,19 +294,22 @@
uni.showLoading({ uni.showLoading({
title: '正在登录中' title: '正在登录中'
}); });
Routine.getUserProfile() // requirePhone
.then(res => { self.loadMpLoginConfig().finally(() => {
Routine.getCode() Routine.getUserProfile()
.then(code => { .then(res => {
self.getWxUser(code, res); Routine.getCode()
}) .then(code => {
.catch(res => { self.getWxUser(code, res);
uni.hideLoading(); })
}); .catch(() => {
}) uni.hideLoading();
.catch(res => { });
uni.hideLoading(); })
}); .catch(() => {
uni.hideLoading();
});
});
}, },
getWxUser(code, res) { getWxUser(code, res) {
@ -254,17 +327,24 @@
userInfo.type = 'routine' userInfo.type = 'routine'
Routine.authUserInfo(userInfo.code, userInfo) Routine.authUserInfo(userInfo.code, userInfo)
.then(res => { .then(res => {
self.authKey = res.data.key; const payload = (res && res.data) ? res.data : (res || {});
if (res.data.type === 'register') { self.authKey = payload.key;
if (payload.type === 'register') {
uni.hideLoading(); 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(); uni.hideLoading();
self.$store.commit('LOGIN', { 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.getUserInfo();
self.$util.Tips({ self.$util.Tips({
title: res, title: res,

Loading…
Cancel
Save