登录失败问题修复
This commit is contained in:
@@ -127,6 +127,7 @@ import { ref, inject, onMounted, computed } from 'vue';
|
||||
import { onLoad } from '@dcloudio/uni-app';
|
||||
import useUserStore from '@/stores/useUserStore';
|
||||
import useDictStore from '@/stores/useDictStore';
|
||||
import { refreshWxLoginCode, takeWxLoginCode } from '@/utils/loginHelper';
|
||||
|
||||
const { $api } = inject('globalFunction');
|
||||
const userStore = useUserStore();
|
||||
@@ -243,82 +244,94 @@ const checkBeforeWxAuth = (e) => {
|
||||
}
|
||||
};
|
||||
|
||||
// 微信获取手机号
|
||||
// 微信获取手机号(使用授权前预取的 login code,避免授权后再 login 导致 session_key 不匹配)
|
||||
const onWxGetPhoneNumber = async (e) => {
|
||||
const { encryptedData, iv } = e.detail;
|
||||
// 使用通用验证函数
|
||||
if (!validateForm()) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
if (e.detail.errMsg === 'getPhoneNumber:ok') {
|
||||
uni.login({
|
||||
provider: 'weixin',
|
||||
success: (loginRes) => {
|
||||
const code = loginRes.code;
|
||||
|
||||
// 调用接口 /app/appWxphoneSmsCode
|
||||
uni.showLoading({ title: '获取验证码中...' });
|
||||
|
||||
// 根据用户类型构建参数
|
||||
const requestParams = {
|
||||
code,
|
||||
encryptedData,
|
||||
iv,
|
||||
userType: userType.value
|
||||
};
|
||||
// 只有单位用户才传递机构类型
|
||||
if (userType.value === 0) {
|
||||
requestParams.orgType = orgType.value;
|
||||
}
|
||||
|
||||
$api.createRequest('/app/appWxphoneSmsCode', requestParams, 'post').then((resData) => {
|
||||
uni.hideLoading();
|
||||
// 检查可能的手机号字段
|
||||
const possiblePhoneFields = ['phone', 'mobile', 'phoneNumber', 'tel', 'mobilePhone'];
|
||||
let phoneValue = '';
|
||||
for (const field of possiblePhoneFields) {
|
||||
if (resData[field]) {
|
||||
phoneValue = resData[field];
|
||||
console.log(`从接口返回中找到手机号字段 "${field}": ${phoneValue}`);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (resData.code === 200 || resData.success) {
|
||||
// 跳转到短信验证页面,传递参数
|
||||
const params = {
|
||||
phone: phoneValue || '', // 接口返回的手机号
|
||||
openid: resData.openid || '',
|
||||
unionid: resData.unionid || '',
|
||||
userType: userType.value
|
||||
};
|
||||
// 只有单位用户才传递机构类型
|
||||
if (userType.value === 0) {
|
||||
params.orgType = orgType.value;
|
||||
}
|
||||
|
||||
uni.navigateTo({
|
||||
url: '/pages/login/sms-verify?' + Object.keys(params)
|
||||
.map(key => `${key}=${encodeURIComponent(params[key])}`)
|
||||
.join('&')
|
||||
});
|
||||
} else {
|
||||
$api.msg(resData.msg || '获取验证码失败');
|
||||
}
|
||||
}).catch((err) => {
|
||||
uni.hideLoading();
|
||||
$api.msg(err.msg || '获取验证码失败,请重试');
|
||||
const { encryptedData, iv, code: wxPhoneCode } = e.detail;
|
||||
if (!encryptedData && !wxPhoneCode) {
|
||||
uni.showToast({ title: '获取手机号失败', icon: 'none', duration: 2000 });
|
||||
return;
|
||||
}
|
||||
|
||||
let loginCode = takeWxLoginCode();
|
||||
if (!loginCode) {
|
||||
if (encryptedData) {
|
||||
uni.showToast({
|
||||
title: '登录凭证未就绪,请稍后重新点击授权',
|
||||
icon: 'none',
|
||||
duration: 2000
|
||||
});
|
||||
},
|
||||
fail: (err) => {
|
||||
refreshWxLoginCode().catch(() => {});
|
||||
return;
|
||||
}
|
||||
try {
|
||||
loginCode = await refreshWxLoginCode();
|
||||
} catch {
|
||||
uni.showToast({
|
||||
title: '获取登录信息失败,请重试',
|
||||
icon: 'none',
|
||||
duration: 2000
|
||||
});
|
||||
return;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
uni.showLoading({ title: '获取验证码中...' });
|
||||
|
||||
const requestParams = {
|
||||
code: loginCode,
|
||||
userType: userType.value
|
||||
};
|
||||
if (encryptedData && iv) {
|
||||
requestParams.encryptedData = encryptedData;
|
||||
requestParams.iv = iv;
|
||||
}
|
||||
if (wxPhoneCode) {
|
||||
requestParams.phoneCode = wxPhoneCode;
|
||||
}
|
||||
if (userType.value === 0) {
|
||||
requestParams.orgType = orgType.value;
|
||||
}
|
||||
|
||||
try {
|
||||
const resData = await $api.createRequest('/app/appWxphoneSmsCode', requestParams, 'post');
|
||||
const possiblePhoneFields = ['phone', 'mobile', 'phoneNumber', 'tel', 'mobilePhone'];
|
||||
let phoneValue = '';
|
||||
for (const field of possiblePhoneFields) {
|
||||
if (resData[field]) {
|
||||
phoneValue = resData[field];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (resData.code === 200 || resData.success) {
|
||||
const params = {
|
||||
phone: phoneValue || '',
|
||||
openid: resData.openid || '',
|
||||
unionid: resData.unionid || '',
|
||||
userType: userType.value
|
||||
};
|
||||
if (userType.value === 0) {
|
||||
params.orgType = orgType.value;
|
||||
}
|
||||
uni.navigateTo({
|
||||
url: '/pages/login/sms-verify?' + Object.keys(params)
|
||||
.map(key => `${key}=${encodeURIComponent(params[key])}`)
|
||||
.join('&')
|
||||
});
|
||||
} else {
|
||||
$api.msg(resData.msg || '获取验证码失败');
|
||||
}
|
||||
} catch (err) {
|
||||
$api.msg(err.msg || '获取验证码失败,请重试');
|
||||
} finally {
|
||||
uni.hideLoading();
|
||||
refreshWxLoginCode().catch(() => {});
|
||||
}
|
||||
} else if (e.detail.errMsg === 'getPhoneNumber:fail user deny') {
|
||||
uni.showToast({
|
||||
title: '您取消了授权',
|
||||
@@ -494,9 +507,12 @@ const testLogin = () => {
|
||||
});
|
||||
};
|
||||
|
||||
// 页面加载时获取字典数据
|
||||
// 页面加载时获取字典数据,并预取 wx.login code
|
||||
onLoad(() => {
|
||||
getOrgTypeDict();
|
||||
// #ifdef MP-WEIXIN
|
||||
refreshWxLoginCode().catch(() => {});
|
||||
// #endif
|
||||
});
|
||||
</script>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user