diff --git a/.env.development b/.env.development index dd2491112fd1dca7a8fb3563584a61f6b2ba34c8..205fdd4c6253f745c081ba42466acd3af4ad3354 100644 --- a/.env.development +++ b/.env.development @@ -1,6 +1,6 @@ # 环境变量 (命名必须以 VITE_ 开头) # 接口前缀 -VITE_API_PREFIX = '/api' +VITE_API_PREFIX = '/dev-api' # 接口地址 VITE_API_BASE_URL = 'http://localhost:8000' diff --git a/.env.test b/.env.test index 20919c293fe3e7d813db217f2766e6f38bbf6367..b597c5fc4a6a217b17c5aecc59027b8b0b9ef84f 100644 --- a/.env.test +++ b/.env.test @@ -4,7 +4,7 @@ VITE_BUILD_MOCK = true # 接口前缀 -VITE_API_PREFIX = '/api' +VITE_API_PREFIX = '/test-api' # 接口地址 VITE_API_BASE_URL = 'http://localhost:8000' diff --git a/src/apis/auth/type.ts b/src/apis/auth/type.ts index 7b9b9ce991e5d800bf9e28668573985e8512b786..36b187d4799e60f7bd9c94edf403e48f38415d47 100644 --- a/src/apis/auth/type.ts +++ b/src/apis/auth/type.ts @@ -42,15 +42,19 @@ export interface RouteItem { } /** 认证类型 */ -export enum AuthTypeEnum { - ACCOUNT = 'ACCOUNT', - PHONE = 'PHONE', - EMAIL = 'EMAIL', - SOCIAL = 'SOCIAL', -} +export type AuthType = 'ACCOUNT' | 'PHONE' | 'EMAIL' | 'SOCIAL' + +export const AuthTypeConstants = { + ACCOUNT: 'ACCOUNT', + PHONE: 'PHONE', + EMAIL: 'EMAIL', + SOCIAL: 'SOCIAL', +} as const + +/** 基础认证请求接口 */ export interface AuthReq { clientId?: string - authType?: string + authType?: AuthType } /** 账号登录请求参数 */ @@ -73,12 +77,12 @@ export interface EmailLoginReq extends AuthReq { captcha: string } -// 登录响应类型 +/** 登录响应类型 */ export interface LoginResp { token: string } -// 第三方登录授权类型 +/** 第三方登录授权类型 */ export interface SocialAuthAuthorizeResp { authorizeUrl: string } diff --git a/src/stores/modules/user.ts b/src/stores/modules/user.ts index 7599a26cdae07519859401c8ba8efd61d8bbe4dc..a363b3829cd90b08713b5718e6175e7003fb9594 100644 --- a/src/stores/modules/user.ts +++ b/src/stores/modules/user.ts @@ -3,7 +3,7 @@ import { computed, reactive, ref } from 'vue' import { resetRouter } from '@/router' import { type AccountLoginReq, - AuthTypeEnum, + AuthTypeConstants, type EmailLoginReq, type PhoneLoginReq, type UserInfo, @@ -51,28 +51,28 @@ const storeSetup = () => { // 登录 const accountLogin = async (req: AccountLoginReq) => { - const res = await accountLoginApi({ ...req, clientId: import.meta.env.VITE_CLIENT_ID, authType: AuthTypeEnum.ACCOUNT }) + const res = await accountLoginApi({ ...req, clientId: import.meta.env.VITE_CLIENT_ID, authType: AuthTypeConstants.ACCOUNT }) setToken(res.data.token) token.value = res.data.token } // 邮箱登录 const emailLogin = async (req: EmailLoginReq) => { - const res = await emailLoginApi({ ...req, clientId: import.meta.env.VITE_CLIENT_ID, authType: AuthTypeEnum.EMAIL }) + const res = await emailLoginApi({ ...req, clientId: import.meta.env.VITE_CLIENT_ID, authType: AuthTypeConstants.EMAIL }) setToken(res.data.token) token.value = res.data.token } // 手机号登录 const phoneLogin = async (req: PhoneLoginReq) => { - const res = await phoneLoginApi({ ...req, clientId: import.meta.env.VITE_CLIENT_ID, authType: AuthTypeEnum.PHONE }) + const res = await phoneLoginApi({ ...req, clientId: import.meta.env.VITE_CLIENT_ID, authType: AuthTypeConstants.PHONE }) setToken(res.data.token) token.value = res.data.token } // 三方账号登录 const socialLogin = async (source: string, req: any) => { - const res = await socialLoginApi({ ...req, source, clientId: import.meta.env.VITE_CLIENT_ID, authType: AuthTypeEnum.SOCIAL }) + const res = await socialLoginApi({ ...req, source, clientId: import.meta.env.VITE_CLIENT_ID, authType: AuthTypeConstants.SOCIAL }) setToken(res.data.token) token.value = res.data.token } diff --git a/vite.config.ts b/vite.config.ts index 2f12042d1bbb59990eb0527518d8053cd1ee3e68..a38aa211b35758f5c588679df26e7ebb0bbc2086 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -33,11 +33,11 @@ export default defineConfig(({ command, mode }) => { open: true, // 本地跨域代理 -> 代理到服务器的接口地址 proxy: { - '/api': { + [env.VITE_API_PREFIX]: { target: env.VITE_API_BASE_URL, // 后台服务器地址 changeOrigin: true, // 是否允许不同源 secure: false, // 支持https - rewrite: (path) => path.replace(/^\/api/, ''), + rewrite: (path) => path.replace(new RegExp(`^${env.VITE_API_PREFIX}`), ''), }, }, },