From b3038281649556db3e6ac233e99a88aec5368f3b Mon Sep 17 00:00:00 2001 From: zzp <34701892@qq.com> Date: Sun, 17 Aug 2025 17:26:24 +0800 Subject: [PATCH] =?UTF-8?q?feat(login):=20=E6=B7=BB=E5=8A=A0=E5=BE=AE?= =?UTF-8?q?=E4=BF=A1=E6=8E=88=E6=9D=83=E7=99=BB=E5=BD=95=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增微信授权API接口 - 替换立即进入按钮为微信获取手机号按钮 - 实现微信授权登录流程,包括获取code和用户信息 - 处理授权成功后的token和用户信息存储 --- src/api/index.ts | 5 +++ src/pages/login/indexMini.vue | 64 ++++++++++++++++++++++++++++++++++- 2 files changed, 68 insertions(+), 1 deletion(-) diff --git a/src/api/index.ts b/src/api/index.ts index 0e466a5..ccf3ca3 100644 --- a/src/api/index.ts +++ b/src/api/index.ts @@ -62,3 +62,8 @@ export const searchNews = (data: any) => { return Request.post("/news/search", data); }; +// 微信授权 +export const doWxAuth = (data: any) => { + return Request.post("/common/auth", data); +}; + diff --git a/src/pages/login/indexMini.vue b/src/pages/login/indexMini.vue index 1de62d0..8ba0623 100644 --- a/src/pages/login/indexMini.vue +++ b/src/pages/login/indexMini.vue @@ -5,7 +5,18 @@ - 立即进入 + + + + @@ -28,6 +39,7 @@ import { onShareTimeline, onUnload, } from "@dcloudio/uni-app"; +import { doWxAuth } from "@/api/index"; import { onPullDownRefresh, onReachBottom } from "@dcloudio/uni-app"; import { useShareStore } from "@/stores/shareStore"; @@ -52,6 +64,56 @@ function goIndex() { }); } +// 微信获取手机号 +function getPhoneNumber(phoneObj) { + uni.showLoading(); + getWxCode(phoneObj.detail); +} +const codeWx = ref(); +function getWxCode(phoneObj) { + console.log("🚀 ~ getWxCode ~ phoneObj:", phoneObj); + uni.login({ + success: (res) => { + console.log("🚀 ~ getWxCode ~ res:", res); + if (res.code) { + //微信登录成功 已拿到code + codeWx.value = res.code; + + doWxAuthFn(phoneObj, res.code); + } else { + console.log("登录失败!" + res.errMsg); + } + }, + }); +} + +/** + * 微信授权 + * @param userInfo + */ +async function doWxAuthFn(phoneObj, jsCode) { + let { code, data } = await doWxAuth({ + jsCode: jsCode, + encryptedData: phoneObj.encryptedData, + siv: phoneObj.iv, + }); + uni.hideLoading(); + if (code == 200) { + uni.setStorageSync("token", data.token); + uni.setStorageSync("userId", data.userId); + // #ifdef MP-WEIXIN + uni.setStorageSync("unionId", data.unionId); + // #endif + // #ifdef MP-ALIPAY + uni.setStorageSync("openId", data.openId); + // #endif + + emit("authFinish", true); + } else { + emit("authFinish", false); + } +} + onShareAppMessage((res) => { return {}; });