|
|
|
|
@ -84,7 +84,10 @@
|
|
|
|
|
</uv-button>
|
|
|
|
|
</view>
|
|
|
|
|
</view>
|
|
|
|
|
</uv-popup>
|
|
|
|
|
</uv-popup>
|
|
|
|
|
<a style="display: none;" ref="ToLoginRef" href="weixin://dl/business/?appid=wxc504638e03981812&path=pages/login/login?from=h5ToWechat">
|
|
|
|
|
打开小程序并登录
|
|
|
|
|
</a>
|
|
|
|
|
</view>
|
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
@ -133,9 +136,9 @@
|
|
|
|
|
trigger: ["blur", "change"],
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
pattern: /^1[3-9]\d{9}$/,
|
|
|
|
|
message: "请输入正确的手机号",
|
|
|
|
|
trigger: ["blur", "change"],
|
|
|
|
|
pattern: /^1[3-9]\d{9}$/,
|
|
|
|
|
message: "请输入正确的手机号",
|
|
|
|
|
trigger: ["blur", "change"],
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
code: {
|
|
|
|
|
@ -143,7 +146,9 @@
|
|
|
|
|
message: "请输入验证码",
|
|
|
|
|
trigger: ["blur", "change"],
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const ToLoginRef = ref(null)
|
|
|
|
|
|
|
|
|
|
const tips = ref("获取验证码");
|
|
|
|
|
const codeChange = (text) => {
|
|
|
|
|
@ -208,6 +213,9 @@
|
|
|
|
|
onLoad((options) => {
|
|
|
|
|
if (options.redirect && removeQueryString(options.redirect) !== LOGIN_PATH) {
|
|
|
|
|
redirect = decodeURIComponent(options.redirect);
|
|
|
|
|
}
|
|
|
|
|
if(options.from && options.from === 'h5ToWechat') {
|
|
|
|
|
XcxLogin()
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
@ -231,47 +239,60 @@
|
|
|
|
|
// #endif
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const XcxLogin = () => {
|
|
|
|
|
uni.showLoading({
|
|
|
|
|
title: "登录中...",
|
|
|
|
|
});
|
|
|
|
|
uni.login({
|
|
|
|
|
success: function(loginRes) {
|
|
|
|
|
console.log('loginRes', loginRes);
|
|
|
|
|
RPC.post('/login/wechat_mini', {
|
|
|
|
|
js_code: loginRes.code
|
|
|
|
|
}).then(res => {
|
|
|
|
|
console.log('phonelogin', res)
|
|
|
|
|
authStore.setAccessToken(res.token)
|
|
|
|
|
userStore.setUserInfo(res.user)
|
|
|
|
|
if(!res.user.phone) {
|
|
|
|
|
uni.showToast({
|
|
|
|
|
title: "登录成功",
|
|
|
|
|
icon: "success",
|
|
|
|
|
});
|
|
|
|
|
uni.$uv.route({
|
|
|
|
|
type: isTabBarPath(redirect) ? "switchTab" : "redirectTo",
|
|
|
|
|
url: redirect,
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
popup.value.open()
|
|
|
|
|
}
|
|
|
|
|
}).catch(() => {
|
|
|
|
|
uni.showToast({
|
|
|
|
|
title: "登录失败",
|
|
|
|
|
icon: "error",
|
|
|
|
|
});
|
|
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
fail: () => {
|
|
|
|
|
uni.hideLoading();
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 微信登录
|
|
|
|
|
const wechatLogin = () => {
|
|
|
|
|
// #ifdef MP-WEIXIN
|
|
|
|
|
// 微信小程序登录
|
|
|
|
|
// uni.login({
|
|
|
|
|
// provider: "weixin",
|
|
|
|
|
// success: (loginRes) => {
|
|
|
|
|
// console.log("微信登录成功:", loginRes);
|
|
|
|
|
// // 获取用户信息
|
|
|
|
|
// uni.getUserProfile({
|
|
|
|
|
// desc: "用于完善用户资料",
|
|
|
|
|
// success: (userRes) => {
|
|
|
|
|
// console.log("获取用户信息成功:", userRes);
|
|
|
|
|
// // 显示绑定手机号弹窗
|
|
|
|
|
// showBindPhoneModal.value = true;
|
|
|
|
|
// },
|
|
|
|
|
// fail: (err) => {
|
|
|
|
|
// console.error("获取用户信息失败:", err);
|
|
|
|
|
// uni.showToast({
|
|
|
|
|
// title: "获取用户信息失败",
|
|
|
|
|
// icon: "none",
|
|
|
|
|
// });
|
|
|
|
|
// },
|
|
|
|
|
// });
|
|
|
|
|
// },
|
|
|
|
|
// fail: (err) => {
|
|
|
|
|
// console.error("微信登录失败:", err);
|
|
|
|
|
// uni.showToast({
|
|
|
|
|
// title: "微信登录失败",
|
|
|
|
|
// icon: "none",
|
|
|
|
|
// });
|
|
|
|
|
// },
|
|
|
|
|
// });
|
|
|
|
|
popup.value.open();
|
|
|
|
|
XcxLogin()
|
|
|
|
|
// popup.value.open();
|
|
|
|
|
// #endif
|
|
|
|
|
|
|
|
|
|
// #ifdef H5
|
|
|
|
|
// H5微信登录
|
|
|
|
|
if (typeof WeixinJSBridge !== "undefined") {
|
|
|
|
|
// 在微信内,使用微信JS-SDK进行授权登录
|
|
|
|
|
wechatH5Login();
|
|
|
|
|
// wechatH5Login();
|
|
|
|
|
ToLoginRef.value.click()
|
|
|
|
|
} else {
|
|
|
|
|
uni.showToast({
|
|
|
|
|
title: "请在微信中打开",
|
|
|
|
|
@ -349,7 +370,7 @@
|
|
|
|
|
const wechatH5Login = () => {
|
|
|
|
|
// #ifdef H5
|
|
|
|
|
// 构建微信授权URL
|
|
|
|
|
const appId = "YOUR_WECHAT_APP_ID"; // 替换为你的微信公众号AppID
|
|
|
|
|
const appId = "wxc504638e03981812"; // 替换为你的微信公众号AppID
|
|
|
|
|
const redirectUri = encodeURIComponent(
|
|
|
|
|
window.location.origin + "/pages/login/login"
|
|
|
|
|
); // 授权回调地址
|
|
|
|
|
@ -389,33 +410,57 @@
|
|
|
|
|
// 调用后端接口,通过code获取用户信息
|
|
|
|
|
// const result = await RPC.post('/api/wechat/h5-login', {
|
|
|
|
|
// code: code
|
|
|
|
|
// });
|
|
|
|
|
// });
|
|
|
|
|
RPC.post('/login/wechat_mini', {
|
|
|
|
|
js_code: loginRes.code
|
|
|
|
|
}).then(res => {
|
|
|
|
|
console.log('phonelogin', res)
|
|
|
|
|
authStore.setAccessToken(res.token)
|
|
|
|
|
userStore.setUserInfo(res.user)
|
|
|
|
|
if(!res.user.phone) {
|
|
|
|
|
uni.showToast({
|
|
|
|
|
title: "登录成功",
|
|
|
|
|
icon: "success",
|
|
|
|
|
});
|
|
|
|
|
uni.$uv.route({
|
|
|
|
|
type: isTabBarPath(redirect) ? "switchTab" : "redirectTo",
|
|
|
|
|
url: redirect,
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
popup.value.open()
|
|
|
|
|
}
|
|
|
|
|
}).catch(() => {
|
|
|
|
|
uni.showToast({
|
|
|
|
|
title: "登录失败",
|
|
|
|
|
icon: "error",
|
|
|
|
|
});
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// 模拟获取用户信息
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
uni.hideLoading();
|
|
|
|
|
|
|
|
|
|
// 模拟获取到的用户信息
|
|
|
|
|
const userInfo = {
|
|
|
|
|
openid: "mock_openid_" + Date.now(),
|
|
|
|
|
nickname: "微信用户",
|
|
|
|
|
headimgurl: "/static/imgs/avatar.png",
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
console.log("微信H5登录成功,用户信息:", userInfo);
|
|
|
|
|
|
|
|
|
|
// 显示绑定手机号弹窗
|
|
|
|
|
popup.value.open();
|
|
|
|
|
|
|
|
|
|
// 清除URL中的code参数
|
|
|
|
|
const url = new URL(window.location);
|
|
|
|
|
url.searchParams.delete("code");
|
|
|
|
|
url.searchParams.delete("state");
|
|
|
|
|
window.history.replaceState({},
|
|
|
|
|
document.title,
|
|
|
|
|
url.pathname + url.search
|
|
|
|
|
);
|
|
|
|
|
}, 1500);
|
|
|
|
|
// setTimeout(() => {
|
|
|
|
|
// uni.hideLoading();
|
|
|
|
|
|
|
|
|
|
// // 模拟获取到的用户信息
|
|
|
|
|
// const userInfo = {
|
|
|
|
|
// openid: "mock_openid_" + Date.now(),
|
|
|
|
|
// nickname: "微信用户",
|
|
|
|
|
// headimgurl: "/static/imgs/avatar.png",
|
|
|
|
|
// };
|
|
|
|
|
|
|
|
|
|
// console.log("微信H5登录成功,用户信息:", userInfo);
|
|
|
|
|
|
|
|
|
|
// // 显示绑定手机号弹窗
|
|
|
|
|
// popup.value.open();
|
|
|
|
|
|
|
|
|
|
// // 清除URL中的code参数
|
|
|
|
|
// const url = new URL(window.location);
|
|
|
|
|
// url.searchParams.delete("code");
|
|
|
|
|
// url.searchParams.delete("state");
|
|
|
|
|
// window.history.replaceState({},
|
|
|
|
|
// document.title,
|
|
|
|
|
// url.pathname + url.search
|
|
|
|
|
// );
|
|
|
|
|
// }, 1500);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
uni.hideLoading();
|
|
|
|
|
console.error("微信H5登录失败:", error);
|
|
|
|
|
@ -559,11 +604,13 @@
|
|
|
|
|
onMounted(() => {
|
|
|
|
|
checkEnvironment();
|
|
|
|
|
|
|
|
|
|
// #ifdef H5
|
|
|
|
|
// #ifdef MP-WEIXIN
|
|
|
|
|
// 检查是否是微信授权回调
|
|
|
|
|
const urlParams = new URLSearchParams(window.location.search);
|
|
|
|
|
const code = urlParams.get("code");
|
|
|
|
|
if (code && showWechatLogin.value) {
|
|
|
|
|
const code = urlParams.get("code");
|
|
|
|
|
const from = urlParams.get("from");
|
|
|
|
|
console.log('from',from)
|
|
|
|
|
if (from && from === 'h5') {
|
|
|
|
|
handleWechatH5Callback(code);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|