From c524d8a4cdb19ad1149a60f07d2078e49709c63c Mon Sep 17 00:00:00 2001 From: chenhao Date: Mon, 1 Dec 2025 16:25:32 +0800 Subject: [PATCH] =?UTF-8?q?feat(auth):=20=E5=AE=9E=E7=8E=B0=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E9=80=80=E5=87=BA=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 - 新增 logout API 接口用于服务端登出 - 在 auth store 中实现异步 logout 方法,调用后端接口并清理本地状态 - 在订单列表页面添加退出登录按钮及确认弹窗 - 更新 Vite 配置注释以支持多环境切换 - 在详情页展示产品折扣信息并格式化显示 --- - 增加了 `/logout` 后端接口调用 - 完善了前端登出逻辑,包括异常处理和状态清理 - 提供了用户登出确认交互流程 - 折扣率计算函数 `getProductDiscountRate` 已添加并应用 - 样式调整适配新增的顶部导航栏布局 --- src/api/auth.ts | 7 +++ src/store/auth.ts | 28 ++++++---- src/views/Detail/index.vue | 15 ++++++ src/views/List/index.vue | 103 ++++++++++++++++++++++++++++++++++++- 4 files changed, 141 insertions(+), 12 deletions(-) diff --git a/src/api/auth.ts b/src/api/auth.ts index d15088f..5e56bbe 100644 --- a/src/api/auth.ts +++ b/src/api/auth.ts @@ -17,4 +17,11 @@ export const login = (params: LoginParams): Promise>> => { + return http.post('/logout') } \ No newline at end of file diff --git a/src/store/auth.ts b/src/store/auth.ts index 760bcaf..0073b8f 100644 --- a/src/store/auth.ts +++ b/src/store/auth.ts @@ -1,5 +1,5 @@ import { defineStore } from 'pinia' -import { login } from '@/api/auth' +import { login, logout as logoutApi } from '@/api/auth' import type { LoginParams } from '@/types' interface AuthState { @@ -55,15 +55,23 @@ export const useAuthStore = defineStore('auth', { /** * 用户登出 */ - logout() { - this.token = null - this.userInfo = null - this.isAuthenticated = false - - // 清除localStorage中的认证状态 - localStorage.removeItem('isAuthenticated') - - // 注意:这里不清除保存的用户名和密码,以便下次自动填充 + async logout() { + try { + // 调用后端logout接口清除session + await logoutApi() + } catch (error) { + console.warn('服务器退出登录失败,仅清除本地状态:', error) + } finally { + // 清除本地状态 + this.token = null + this.userInfo = null + this.isAuthenticated = false + + // 清除localStorage中的认证状态 + localStorage.removeItem('isAuthenticated') + + // 注意:这里不清除保存的用户名和密码,以便下次自动填充 + } }, /** diff --git a/src/views/Detail/index.vue b/src/views/Detail/index.vue index 86493ba..f3123b0 100644 --- a/src/views/Detail/index.vue +++ b/src/views/Detail/index.vue @@ -188,6 +188,10 @@ 单价 {{ formatAmount(product.price) }} +
+ 折扣 + {{ getProductDiscountRate(product.discount) }} +
税率 @@ -233,6 +237,10 @@ 单价 {{ formatAmount(product.price) }}
+
+ 折扣 + {{ getProductDiscountRate(product.discount) }} +
税率 @@ -278,6 +286,10 @@ 单价 {{ formatAmount(product.price) }}
+
+ 折扣 + {{ getProductDiscountRate(product.discount) }} +
税率 @@ -617,6 +629,9 @@ const getTotalAmount = () => { return total } +const getProductDiscountRate=(discount: number)=>{ + return (discount * 100).toFixed(1) + '%' +} // 获取现金折扣率 const getDiscountRate = () => { if (!currentOrderInfo.value || !currentOrderInfo.value.discountFold) { diff --git a/src/views/List/index.vue b/src/views/List/index.vue index c88c084..1cf3d7f 100644 --- a/src/views/List/index.vue +++ b/src/views/List/index.vue @@ -1,5 +1,23 @@