From d4397ffd570635c72eb1e214f9aaa9d8256f9dfc Mon Sep 17 00:00:00 2001 From: MoChou <1373639299@qq.com> Date: Sun, 20 Jul 2025 00:48:52 +0800 Subject: [PATCH] =?UTF-8?q?chore(login):=20=E8=B0=83=E6=95=B4=E7=A7=9F?= =?UTF-8?q?=E6=88=B7=E7=99=BB=E5=BD=95=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/apis/auth/index.ts | 5 +- src/apis/auth/type.ts | 1 + src/apis/tenant/common.ts | 6 +-- src/stores/index.ts | 1 - src/stores/modules/tenant.ts | 48 ++++++++++---------- src/stores/modules/user.ts | 9 +++- src/utils/http.ts | 8 ++-- src/utils/tenant.ts | 14 ------ src/views/login/components/account/index.vue | 23 +++++----- src/views/login/index.vue | 11 +++-- 10 files changed, 56 insertions(+), 70 deletions(-) delete mode 100644 src/utils/tenant.ts diff --git a/src/apis/auth/index.ts b/src/apis/auth/index.ts index 1547e70..f804d2b 100644 --- a/src/apis/auth/index.ts +++ b/src/apis/auth/index.ts @@ -10,10 +10,7 @@ const login = (req: T.AccountLoginReq | T.PhoneLoginReq | T.EmailLoginReq, tenan if (tenantCode) { headers['X-Tenant-Code'] = tenantCode } - return http.requestNative({ - url: `${BASE_URL}/login`, - data: req, - method: 'post', + return http.post(`${BASE_URL}/login`, req, { headers, }) } diff --git a/src/apis/auth/type.ts b/src/apis/auth/type.ts index 36b187d..2db1fd5 100644 --- a/src/apis/auth/type.ts +++ b/src/apis/auth/type.ts @@ -80,6 +80,7 @@ export interface EmailLoginReq extends AuthReq { /** 登录响应类型 */ export interface LoginResp { token: string + tenantId: string } /** 第三方登录授权类型 */ diff --git a/src/apis/tenant/common.ts b/src/apis/tenant/common.ts index 0871075..4e31d86 100644 --- a/src/apis/tenant/common.ts +++ b/src/apis/tenant/common.ts @@ -8,7 +8,7 @@ export function listTenantPackageDict(query?: { description: string, status: num return http.get(`${BASE_URL}/dict/package`, query) } -/** @desc 根据域名查询租户编码 */ -export function getTenantCodeByDomain(domain: string) { - return http.get(`${BASE_URL}/code/domain`, { domain }) +/** @desc 根据域名查询租户ID */ +export function getTenantIdByDomain(domain: string) { + return http.get(`${BASE_URL}/id/domain`, { domain }) } diff --git a/src/stores/index.ts b/src/stores/index.ts index 2ec60fa..875abe8 100644 --- a/src/stores/index.ts +++ b/src/stores/index.ts @@ -6,7 +6,6 @@ export * from './modules/route' export * from './modules/tabs' export * from './modules/dict' export * from './modules/user' -export * from './modules/tenant' const pinia = createPinia() pinia.use(piniaPluginPersistedstate) diff --git a/src/stores/modules/tenant.ts b/src/stores/modules/tenant.ts index 732f66e..94723a9 100644 --- a/src/stores/modules/tenant.ts +++ b/src/stores/modules/tenant.ts @@ -1,31 +1,29 @@ import { defineStore } from 'pinia' -import { computed, reactive } from 'vue' +import { computed, ref } from 'vue' -const storeSetup = () => { - interface TenantInfo { - tenantEnabled: boolean - tenantCode: string +export const useTenantStore = defineStore('tenant', () => { + const tenantEnabled = ref(false) + const tenantId = ref('') + + const setTenantEnable = (status: boolean) => { + tenantEnabled.value = status } - const tenantInfo = reactive({ - tenantEnabled: false, - tenantCode: '', + const setTenantId = (id: string) => { + tenantId.value = id + } + + // 判断是否需要用户输入租户编码 + const needInputTenantId = computed(() => { + return tenantEnabled.value && !tenantId.value }) - const tenantEnabled = computed(() => tenantInfo.tenantEnabled) - const tenantCode = computed(() => tenantInfo.tenantCode) - const setTenantEnable = (tenantStatus: boolean) => { - tenantInfo.tenantEnabled = tenantStatus - } - const setTenantCode = (tenantCode: string) => { - tenantInfo.tenantCode = tenantCode - } - return { - tenantCode, - tenantEnabled, - setTenantCode, - setTenantEnable, - } -} -export const useTenantStore = defineStore('tenant', storeSetup, { - persist: { paths: ['tenantInfo'], storage: localStorage }, + return { + tenantEnabled, + tenantId, + setTenantEnable, + setTenantId, + needInputTenantId, + } +}, { + persist: { paths: ['tenantEnabled', 'tenantId'], storage: localStorage }, }) diff --git a/src/stores/modules/user.ts b/src/stores/modules/user.ts index a281342..1bb8b30 100644 --- a/src/stores/modules/user.ts +++ b/src/stores/modules/user.ts @@ -1,5 +1,6 @@ import { defineStore } from 'pinia' import { computed, reactive, ref } from 'vue' +import { useTenantStore } from './tenant' import { resetRouter } from '@/router' import { type AccountLoginReq, @@ -18,6 +19,7 @@ import { clearToken, getToken, setToken } from '@/utils/auth' import { resetHasRouteFlag } from '@/router/guard' const storeSetup = () => { + const tenantStore = useTenantStore() const userInfo = reactive({ id: '', username: '', @@ -41,7 +43,6 @@ const storeSetup = () => { const pwdExpiredShow = ref(true) const roles = ref([]) // 当前用户角色 const permissions = ref([]) // 当前角色权限标识集合 - // 重置token const resetToken = () => { token.value = '' @@ -53,6 +54,7 @@ const storeSetup = () => { const accountLogin = async (req: AccountLoginReq, tenantCode?: string) => { const res = await accountLoginApi({ ...req, clientId: import.meta.env.VITE_CLIENT_ID, authType: AuthTypeConstants.ACCOUNT }, tenantCode) setToken(res.data.token) + tenantStore.setTenantId(res.data.tenantId) token.value = res.data.token } @@ -60,6 +62,7 @@ const storeSetup = () => { const emailLogin = async (req: EmailLoginReq, tenantCode?: string) => { const res = await emailLoginApi({ ...req, clientId: import.meta.env.VITE_CLIENT_ID, authType: AuthTypeConstants.EMAIL }, tenantCode) setToken(res.data.token) + tenantStore.setTenantId(res.data.tenantId) token.value = res.data.token } @@ -67,13 +70,15 @@ const storeSetup = () => { const phoneLogin = async (req: PhoneLoginReq, tenantCode?: string) => { const res = await phoneLoginApi({ ...req, clientId: import.meta.env.VITE_CLIENT_ID, authType: AuthTypeConstants.PHONE }, tenantCode) setToken(res.data.token) + tenantStore.setTenantId(res.data.tenantId) 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: AuthTypeConstants.SOCIAL }) + const res: any = await socialLoginApi({ ...req, source, clientId: import.meta.env.VITE_CLIENT_ID, authType: AuthTypeConstants.SOCIAL }) setToken(res.data.token) + tenantStore.setTenantId(res.data.tenantId) token.value = res.data.token } diff --git a/src/utils/http.ts b/src/utils/http.ts index 54aee08..61b5d28 100644 --- a/src/utils/http.ts +++ b/src/utils/http.ts @@ -1,13 +1,13 @@ import axios from 'axios' import qs from 'query-string' import type { AxiosError, AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios' +import { useTenantStore } from '@/stores/modules/tenant' import { useUserStore } from '@/stores' import { getToken } from '@/utils/auth' import modalErrorWrapper from '@/utils/modal-error-wrapper' import messageErrorWrapper from '@/utils/message-error-wrapper' import notificationErrorWrapper from '@/utils/notification-error-wrapper' import router from '@/router' -import { getTenantId } from '@/utils/tenant' interface ICodeMessage { [propName: number]: string @@ -58,9 +58,9 @@ http.interceptors.request.use( if (token) { config.headers.Authorization = `Bearer ${token}` } - const tenantId = getTenantId() - if (tenantId) { - config.headers['X-Tenant-Id'] = tenantId + const tenantStore = useTenantStore() + if (tenantStore.tenantEnabled && tenantStore.tenantId) { + config.headers['X-Tenant-Id'] = tenantStore.tenantId } return config }, diff --git a/src/utils/tenant.ts b/src/utils/tenant.ts deleted file mode 100644 index 7deeb18..0000000 --- a/src/utils/tenant.ts +++ /dev/null @@ -1,14 +0,0 @@ -const CURRENT_TENANT = 'current_tenant' - -export interface TenantCommon { - isEnabled: boolean - availableList: any[] -} - -export const getTenantId = () => { - return localStorage.getItem(CURRENT_TENANT) -} - -export const setTenantId = (tenantId: string) => { - localStorage.setItem(CURRENT_TENANT, tenantId) -} diff --git a/src/views/login/components/account/index.vue b/src/views/login/components/account/index.vue index da3eaab..648f3ba 100644 --- a/src/views/login/components/account/index.vue +++ b/src/views/login/components/account/index.vue @@ -8,8 +8,8 @@ size="large" @submit="handleLogin" > - - + + @@ -44,14 +44,14 @@ import { type FormInstance, Message } from '@arco-design/web-vue' import { useStorage } from '@vueuse/core' import { getImageCaptcha } from '@/apis/common' -import { useTabsStore, useTenantStore, useUserStore } from '@/stores' +import { useTabsStore, useUserStore } from '@/stores' import { encryptByRsa } from '@/utils/encrypt' +import { useTenantStore } from '@/stores/modules/tenant' const tenantStore = useTenantStore() const loginConfig = useStorage('login-config', { rememberMe: true, - tenantCode: tenantStore.tenantCode, username: 'admin', // 演示默认值 password: 'admin123', // 演示默认值 // username: debug ? 'admin' : '', // 演示默认值 @@ -63,21 +63,22 @@ const isCaptchaEnabled = ref(true) const captchaImgBase64 = ref() const formRef = ref() -const tenantCodeInput = ref('') // 用户手动输入的租户编码 const form = reactive({ username: loginConfig.value.username, password: loginConfig.value.password, captcha: '', uuid: '', expired: false, + tenantCode: '', // 新增 }) +// 校验规则部分 const rules: FormInstance['rules'] = { username: [{ required: true, message: '请输入用户名' }], password: [{ required: true, message: '请输入密码' }], captcha: [{ required: isCaptchaEnabled.value, message: '请输入验证码' }], tenantCode: [ { - required: tenantStore.tenantEnabled && !tenantStore.tenantCode, + required: tenantStore.needInputTenantId, message: '请输入租户编码', }, ], @@ -128,10 +129,9 @@ const handleLogin = async () => { if (isInvalid) return loading.value = true - // 计算最终要传递的租户编码 - let finalTenantCode = '' - if (tenantStore.tenantEnabled) { - finalTenantCode = tenantStore.tenantCode || tenantCodeInput.value + let tenantCode + if (tenantStore.needInputTenantId) { + tenantCode = form.tenantCode } await userStore.accountLogin({ @@ -139,11 +139,10 @@ const handleLogin = async () => { password: encryptByRsa(form.password) || '', captcha: form.captcha, uuid: form.uuid, - }, finalTenantCode) + }, tenantCode) tabsStore.reset() const { redirect, ...othersQuery } = router.currentRoute.value.query const { rememberMe } = loginConfig.value - loginConfig.value.tenantCode = rememberMe ? finalTenantCode : '' loginConfig.value.username = rememberMe ? form.username : '' // 如果有重定向参数,解码并直接跳转到完整路径 diff --git a/src/views/login/index.vue b/src/views/login/index.vue index 8562e2e..1e6bd1d 100644 --- a/src/views/login/index.vue +++ b/src/views/login/index.vue @@ -96,14 +96,15 @@ import AccountLogin from './components/account/index.vue' import PhoneLogin from './components/phone/index.vue' import EmailLogin from './components/email/index.vue' import { socialAuth } from '@/apis/auth' -import { useAppStore, useTenantStore } from '@/stores' +import { useAppStore } from '@/stores' +import { useTenantStore } from '@/stores/modules/tenant' import { useDevice } from '@/hooks' -import { getTenantCodeByDomain, getTenantStatus } from '@/apis' +import { getTenantIdByDomain, getTenantStatus } from '@/apis' defineOptions({ name: 'Login' }) +const appStore = useAppStore() const tenantStore = useTenantStore() const { isDesktop } = useDevice() -const appStore = useAppStore() const title = computed(() => appStore.getTitle()) const logo = computed(() => appStore.getLogo()) @@ -128,8 +129,8 @@ const onGetTenant = async () => { // 开启租户 根据地址(域名)查询租户code if (data) { const domain = window.location.hostname - const { data } = await getTenantCodeByDomain(domain) - tenantStore.setTenantCode(data) + const { data: tenantId } = await getTenantIdByDomain(domain) + tenantStore.setTenantId(tenantId) } } onMounted(() => {