修复微信小程序的一键登录

pull/6/head
YunaiV 2023-10-19 21:49:41 +08:00
parent 0b3882256e
commit 24c3709c95
2 changed files with 47 additions and 132 deletions

View File

@ -50,9 +50,6 @@
<div class="tips"> <div class="tips">
<div v-if="current === 0" @click="current = 1"></div> <div v-if="current === 0" @click="current = 1"></div>
<div v-if="current === 1" @click="current = 0"></div> <div v-if="current === 1" @click="current = 0"></div>
<!-- #ifdef MP-WEIXIN -->
<button open-type="getPhoneNumber" type="primary" @getphonenumber="getPhoneNumber"></button>
<!-- #endif -->
</div> </div>
</div> </div>
<div class="bottom"></div> <div class="bottom"></div>
@ -353,37 +350,6 @@
}); });
}); });
}, },
/**
* 微信一键登录
*/
async getPhoneNumber(e) {
//
const phoneCode = e.detail.code
if (!e.detail.code) {
uni.showModal({
title: '授权失败',
content: '您已拒绝获取绑定手机号登录授权,可以使用其他手机号验证登录',
confirmText: '知道了',
confirmColor: '#3C9CFFFF'
})
return;
}
//
const loginCode = await Routine.getCode()
AuthApi.weixinMiniAppLogin(phoneCode, loginCode).then(res => {
const data = res.data;
// TODO refreshToken
this.$store.commit("LOGIN", {
'token': data.accessToken
});
this.getUserInfo(data);
this.bindBrokerUser();
}).catch(e => {
this.$util.Tips({
title: e
});
});
},
getUserInfo(data) { getUserInfo(data) {
this.$store.commit("SETUID", data.userId); this.$store.commit("SETUID", data.userId);
this.$store.commit("OPENID", data.openid); this.$store.commit("OPENID", data.openid);

View File

@ -23,6 +23,9 @@
<!-- #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 -->
<!-- #ifdef MP-WEIXIN -->
<button open-type="getPhoneNumber" type="primary" @getphonenumber="getPhoneNumber"></button>
<!-- #endif -->
<!-- <button hover-class="none" @click="isUp = true" class="btn2">手机号登录</button> --> <!-- <button hover-class="none" @click="isUp = true" class="btn2">手机号登录</button> -->
</view> </view>
</view> </view>
@ -40,12 +43,11 @@
const app = getApp(); const app = getApp();
let statusBarHeight = uni.getSystemInfoSync().statusBarHeight + 'px'; let statusBarHeight = uni.getSystemInfoSync().statusBarHeight + 'px';
import * as UserApi from "@/api/member/user"; import * as UserApi from "@/api/member/user";
import * as AuthApi from "@/api/member/auth";
import mobileLogin from '@/components/login_mobile/index.vue' import mobileLogin from '@/components/login_mobile/index.vue'
import routinePhone from '@/components/login_mobile/routine_phone.vue' import routinePhone from '@/components/login_mobile/routine_phone.vue'
import { import * as BrokerageAPI from '@/api/trade/brokerage.js'
getLogo, import { getUserPhone } from '@/api/public';
getUserPhone
} from '@/api/public';
import Routine from '@/libs/routine'; import Routine from '@/libs/routine';
import wechat from "@/libs/wechat"; import wechat from "@/libs/wechat";
export default { export default {
@ -56,7 +58,7 @@
statusBarHeight: statusBarHeight, statusBarHeight: statusBarHeight,
isHome: false, isHome: false,
isPhoneBox: false, isPhoneBox: false,
logoUrl: '', // logoUrl: '',
code: '', code: '',
authKey: '', authKey: '',
options: '', options: '',
@ -72,9 +74,9 @@
routinePhone routinePhone
}, },
onLoad(options) { onLoad(options) {
getLogo().then(res => { // getLogo().then(res => {
this.logoUrl = res.data.logoUrl // this.logoUrl = res.data.logoUrl
}) // })
// #ifdef H5 // #ifdef H5
document.body.addEventListener("focusout", () => { document.body.addEventListener("focusout", () => {
@ -141,52 +143,14 @@
tab: 3 tab: 3
}) })
}); });
},
bindBrokerUser() {
const spread = this.$Cache.get("spread");
if (spread > 0) {
BrokerageAPI.bindBrokerageUser(spread)
}
}, },
// #ifdef MP // #ifdef MP
//
getphonenumber(e) {
uni.showLoading({
title: '正在登录中'
});
Routine.getCode()
.then(code => {
this.getUserPhoneNumber(e.detail.encryptedData, e.detail.iv, code);
})
.catch(error => {
uni.$emit('closePage', false)
uni.hideLoading();
});
},
//
getUserPhoneNumber(encryptedData, iv, code) {
getUserPhone({
encryptedData: encryptedData,
iv: iv,
code: code,
type: 'routine',
key: this.authKey
})
.then(res => {
this.$store.commit('LOGIN', {
token: res.data.token
});
this.$store.commit("SETUID", res.data.uid);
this.getUserInfo();
this.$util.Tips({
title: '登录成功',
icon: 'success'
}, {
tab: 3
})
})
.catch(res => {
uni.hideLoading();
that.$util.Tips({
title: res
});
});
},
getUserProfile() { getUserProfile() {
let self = this; let self = this;
uni.showLoading({ uni.showLoading({
@ -206,52 +170,37 @@
uni.hideLoading(); uni.hideLoading();
}); });
}, },
/**
getWxUser(code, res) { * 微信一键登录
let self = this */
let userInfo = res.userInfo; async getPhoneNumber(e) {
userInfo.code = code; //
userInfo.spread_spid = app.globalData.spid; //广ID const phoneCode = e.detail.code
userInfo.spread_code = app.globalData.code; //广ID if (!e.detail.code) {
userInfo.avatar = userInfo.userInfo.avatarUrl; uni.showModal({
userInfo.city = userInfo.userInfo.city; title: '授权失败',
userInfo.country = userInfo.userInfo.country; content: '您已拒绝获取绑定手机号登录授权,可以使用其他手机号验证登录',
userInfo.nickName = userInfo.userInfo.nickName; confirmText: '知道了',
userInfo.province = userInfo.userInfo.province; confirmColor: '#3C9CFFFF'
userInfo.sex = userInfo.userInfo.gender; })
userInfo.type = 'routine' return;
Routine.authUserInfo(userInfo.code, userInfo) }
.then(res => { //
self.authKey = res.data.key; const loginCode = await Routine.getCode()
if (res.data.type === 'register') { AuthApi.weixinMiniAppLogin(phoneCode, loginCode).then(res => {
uni.hideLoading(); const data = res.data;
self.isPhoneBox = true // TODO refreshToken
} this.$store.commit("LOGIN", {
if (res.data.type === 'login') { 'token': data.accessToken
uni.hideLoading(); });
self.$store.commit('LOGIN', { this.getUserInfo();
token: res.data.token this.bindBrokerUser();
}); }).catch(e => {
self.$store.commit("SETUID", res.data.uid); this.$util.Tips({
self.getUserInfo(); title: e
self.$util.Tips({ });
title: res, });
icon: 'success' },
}, {
tab: 3
})
}
})
.catch(res => {
uni.hideLoading();
uni.showToast({
title: res,
icon: 'none',
duration: 2000
});
});
},
// #endif // #endif
// #ifdef H5 // #ifdef H5