From 9c658e9f9f0bcd816b56ca37ea4cdeb6017f647b Mon Sep 17 00:00:00 2001 From: zoujing Date: Tue, 6 Jan 2026 19:42:10 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E7=99=BB=E5=BD=95?= =?UTF-8?q?=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/hooks/useGoLogin.js | 24 ++++++++++++------- src/manager/LoginManager.js | 8 ++----- .../components/chat/ChatMainList/index.vue | 7 +++--- src/pages/login/index.vue | 12 ++++++---- src/request/api/LoginApi.js | 6 ++--- src/request/base/request.js | 3 +-- 6 files changed, 30 insertions(+), 30 deletions(-) diff --git a/src/hooks/useGoLogin.js b/src/hooks/useGoLogin.js index dd2579c..da33f54 100644 --- a/src/hooks/useGoLogin.js +++ b/src/hooks/useGoLogin.js @@ -1,7 +1,8 @@ -import { wxLogin, checkUserPhone } from "@/request/api/LoginApi"; +import { oauthToken, checkUserPhone } from "@/request/api/LoginApi"; import { loginAuth, bindPhone } from "@/manager/LoginManager"; import { clientId } from "@/constant/base"; -import { getAccessToken, setAccessToken } from "../constant/token"; +import { getAccessToken, removeAccessToken, setAccessToken } from "../constant/token"; +import { NOTICE_EVENT_LOGIN_SUCCESS } from "@/constant/constant"; // 跳转登录 export const goLogin = () => uni.navigateTo({ url: "/pages/login/index" }); @@ -40,7 +41,13 @@ export const onLogin = async (e) => { const params = { wechatPhoneCode: code, clientId: clientId }; const res = await bindPhone(params); if (res.data) { + // 登录成功后,触发登录成功事件 + uni.$emit(NOTICE_EVENT_LOGIN_SUCCESS); resolve(); + } else { + console.log("绑定手机号失败"); + removeAccessToken(); + reject(); } }); }); @@ -61,7 +68,7 @@ export const checkToken = () => { }; // 刷新token -export const refreshToken = (needLogin = false) => { +export const refreshToken = () => { return new Promise(async (resolve) => { uni.login({ provider: "weixin", //使用微信登录 @@ -75,18 +82,17 @@ export const refreshToken = (needLogin = false) => { }; console.log("获取到的微信授权params:", JSON.stringify(params)); - const response = await wxLogin(params); - - if (needLogin && response.access_token) { - setAccessToken(response.access_token); - } - + const response = await oauthToken(params); if (response.access_token) { + setAccessToken(response.access_token); + const checkRes = await checkUserPhone({ token: response.access_token, }); if (checkRes.data) { + // 登录成功后,触发登录成功事件 + uni.$emit(NOTICE_EVENT_LOGIN_SUCCESS); resolve(false); } else { resolve(true); diff --git a/src/manager/LoginManager.js b/src/manager/LoginManager.js index 7aac25c..7947b4c 100644 --- a/src/manager/LoginManager.js +++ b/src/manager/LoginManager.js @@ -1,4 +1,4 @@ -import { wxLogin, bindUserPhone } from "../request/api/LoginApi"; +import { oauthToken, bindUserPhone } from "../request/api/LoginApi"; import { getWeChatAuthCode } from "./AuthManager"; import { clientId } from "@/constant/base"; import { NOTICE_EVENT_LOGIN_SUCCESS } from "@/constant/constant"; @@ -17,15 +17,11 @@ const loginAuth = (e) => { clientId: clientId, }; console.log("获取到的微信授权params:", JSON.stringify(params)); - const response = await wxLogin(params); + const response = await oauthToken(params); console.log("获取到的微信授权response:", response); if (response.access_token) { - console.log("进入条件"); setAccessToken(response.access_token); - - // 登录成功后,触发登录成功事件 - uni.$emit(NOTICE_EVENT_LOGIN_SUCCESS); resolve(); } else { reject(response.message || "登录失败"); diff --git a/src/pages/index/components/chat/ChatMainList/index.vue b/src/pages/index/components/chat/ChatMainList/index.vue index e7d08b0..653c8fd 100644 --- a/src/pages/index/components/chat/ChatMainList/index.vue +++ b/src/pages/index/components/chat/ChatMainList/index.vue @@ -602,9 +602,6 @@ const sendChat = (message, isInstruct = false) => { const messageContent = isInstruct ? commonType : message; currentSessionMessageId = IdUtils.generateMessageId(); - // 重置消息状态,为新消息做准备 - resetMessageState(); - // 插入AI消息 const aiMsg = { msgId: `msg_${chatMsgList.value.length}`, @@ -627,8 +624,10 @@ const sendChat = (message, isInstruct = false) => { chatMsgList.value[aiMsgIndex].msg = "发送消息失败,请重试"; chatMsgList.value[aiMsgIndex].isLoading = false; isSessionActive.value = false; - resetMessageState(); } + + // 重置消息状态,为新消息做准备 + resetMessageState(); }; // 停止请求函数 diff --git a/src/pages/login/index.vue b/src/pages/login/index.vue index b4262c5..466c6d4 100644 --- a/src/pages/login/index.vue +++ b/src/pages/login/index.vue @@ -52,7 +52,6 @@ import CheckBox from "@/components/CheckBox/index.vue"; import AgreePopup from "./components/AgreePopup/index.vue"; import { zniconsMap } from "@/static/fonts/znicons"; import { getCurrentConfig } from "@/constant/base"; -import { NOTICE_EVENT_LOGIN_SUCCESS } from "@/constant/constant"; // 是否需要微信手机号授权登录 const needWxAuthLogin = ref(false); @@ -79,7 +78,7 @@ const handleAgreeAndGetPhone = () => { return; } - refreshToken(true).then(() => loginSuccess()); + refreshToken().then(() => loginSuccess()); }; /// 获取授权后绑定手机号登录 @@ -92,13 +91,16 @@ const getPhoneNumber = (e) => { return; } onLogin(e).then(() => loginSuccess()) - .catch(() => { }); + .catch(() => { + uni.showToast({ + title: "获取登录手机号失败", + icon: "none", + }); + }); }; /// 登录成功返回上一页 const loginSuccess = () => { - // 登录成功后,触发登录成功事件 - uni.$emit(NOTICE_EVENT_LOGIN_SUCCESS); uni.showToast({ title: "登录成功", icon: "success", diff --git a/src/request/api/LoginApi.js b/src/request/api/LoginApi.js index dbd4696..18910ad 100644 --- a/src/request/api/LoginApi.js +++ b/src/request/api/LoginApi.js @@ -1,16 +1,14 @@ import { removeAccessToken } from "@/constant/token"; import request from "../base/request"; -const wxLogin = (args) => { +const oauthToken = (args) => { const config = { header: { Authorization: "Basic Y3VzdG9tOmN1c3RvbQ==", // 可在此动态设置 token "Content-Type": "application/x-www-form-urlencoded", }, }; - removeAccessToken(); - return request.post("/auth/oauth2/token", args, config); }; @@ -40,7 +38,7 @@ const getPrivacyAgreement = (args) => { }; export { - wxLogin, + oauthToken, bindUserPhone, checkUserPhone, getLoginUserPhone, diff --git a/src/request/base/request.js b/src/request/base/request.js index 5ee8a16..dd7a0f0 100644 --- a/src/request/base/request.js +++ b/src/request/base/request.js @@ -59,9 +59,8 @@ function request(url, args = {}, method = "POST", customConfig = {}) { resolve(res.data); if (res.statusCode && res.statusCode === 424) { console.log("424错误,重新登录"); - // removeAccessToken(); + removeAccessToken(); uni.$emit(NOTICE_EVENT_LOGOUT); - // goLogin(); } }, fail: (err) => {