Skip to content

Commit d4397ff

Browse files
committed
chore(login): 调整租户登录逻辑
1 parent c7a1c1d commit d4397ff

10 files changed

Lines changed: 53 additions & 67 deletions

File tree

‎src/apis/auth/index.ts‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,7 @@ const login = (req: T.AccountLoginReq | T.PhoneLoginReq | T.EmailLoginReq, tenan
1010
if (tenantCode) {
1111
headers['X-Tenant-Code'] = tenantCode
1212
}
13-
return http.requestNative({
14-
url: `${BASE_URL}/login`,
15-
data: req,
16-
method: 'post',
13+
return http.post<T.LoginResp>(`${BASE_URL}/login`, req, {
1714
headers,
1815
})
1916
}

‎src/apis/auth/type.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ export interface EmailLoginReq extends AuthReq {
8080
/** 登录响应类型 */
8181
export interface LoginResp {
8282
token: string
83+
tenantId: string
8384
}
8485

8586
/** 第三方登录授权类型 */

‎src/apis/tenant/common.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export function listTenantPackageDict(query?: { description: string, status: num
88
return http.get<LabelValueState[]>(`${BASE_URL}/dict/package`, query)
99
}
1010

11-
/** @desc 根据域名查询租户编码 */
12-
export function getTenantCodeByDomain(domain: string) {
13-
return http.get<string>(`${BASE_URL}/code/domain`, { domain })
11+
/** @desc 根据域名查询租户ID */
12+
export function getTenantIdByDomain(domain: string) {
13+
return http.get<string>(`${BASE_URL}/id/domain`, { domain })
1414
}

‎src/stores/index.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ export * from './modules/route'
66
export * from './modules/tabs'
77
export * from './modules/dict'
88
export * from './modules/user'
9-
export * from './modules/tenant'
109

1110
const pinia = createPinia()
1211
pinia.use(piniaPluginPersistedstate)

‎src/stores/modules/tenant.ts‎

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,29 @@
11
import { defineStore } from 'pinia'
2-
import { computed, reactive } from 'vue'
2+
import { computed, ref } from 'vue'
33

4-
const storeSetup = () => {
5-
interface TenantInfo {
6-
tenantEnabled: boolean
7-
tenantCode: string
8-
}
9-
const tenantInfo = reactive<TenantInfo>({
10-
tenantEnabled: false,
11-
tenantCode: '',
12-
})
13-
const tenantEnabled = computed(() => tenantInfo.tenantEnabled)
14-
const tenantCode = computed(() => tenantInfo.tenantCode)
15-
const setTenantEnable = (tenantStatus: boolean) => {
16-
tenantInfo.tenantEnabled = tenantStatus
4+
export const useTenantStore = defineStore('tenant', () => {
5+
const tenantEnabled = ref(false)
6+
const tenantId = ref('')
7+
8+
const setTenantEnable = (status: boolean) => {
9+
tenantEnabled.value = status
1710
}
18-
const setTenantCode = (tenantCode: string) => {
19-
tenantInfo.tenantCode = tenantCode
11+
const setTenantId = (id: string) => {
12+
tenantId.value = id
2013
}
14+
15+
// 判断是否需要用户输入租户编码
16+
const needInputTenantId = computed(() => {
17+
return tenantEnabled.value && !tenantId.value
18+
})
19+
2120
return {
22-
tenantCode,
2321
tenantEnabled,
24-
setTenantCode,
22+
tenantId,
2523
setTenantEnable,
24+
setTenantId,
25+
needInputTenantId,
2626
}
27-
}
28-
29-
export const useTenantStore = defineStore('tenant', storeSetup, {
30-
persist: { paths: ['tenantInfo'], storage: localStorage },
27+
}, {
28+
persist: { paths: ['tenantEnabled', 'tenantId'], storage: localStorage },
3129
})

‎src/stores/modules/user.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { defineStore } from 'pinia'
22
import { computed, reactive, ref } from 'vue'
3+
import { useTenantStore } from './tenant'
34
import { resetRouter } from '@/router'
45
import {
56
type AccountLoginReq,
@@ -18,6 +19,7 @@ import { clearToken, getToken, setToken } from '@/utils/auth'
1819
import { resetHasRouteFlag } from '@/router/guard'
1920

2021
const storeSetup = () => {
22+
const tenantStore = useTenantStore()
2123
const userInfo = reactive<UserInfo>({
2224
id: '',
2325
username: '',
@@ -41,7 +43,6 @@ const storeSetup = () => {
4143
const pwdExpiredShow = ref<boolean>(true)
4244
const roles = ref<string[]>([]) // 当前用户角色
4345
const permissions = ref<string[]>([]) // 当前角色权限标识集合
44-
4546
// 重置token
4647
const resetToken = () => {
4748
token.value = ''
@@ -53,27 +54,31 @@ const storeSetup = () => {
5354
const accountLogin = async (req: AccountLoginReq, tenantCode?: string) => {
5455
const res = await accountLoginApi({ ...req, clientId: import.meta.env.VITE_CLIENT_ID, authType: AuthTypeConstants.ACCOUNT }, tenantCode)
5556
setToken(res.data.token)
57+
tenantStore.setTenantId(res.data.tenantId)
5658
token.value = res.data.token
5759
}
5860

5961
// 邮箱登录
6062
const emailLogin = async (req: EmailLoginReq, tenantCode?: string) => {
6163
const res = await emailLoginApi({ ...req, clientId: import.meta.env.VITE_CLIENT_ID, authType: AuthTypeConstants.EMAIL }, tenantCode)
6264
setToken(res.data.token)
65+
tenantStore.setTenantId(res.data.tenantId)
6366
token.value = res.data.token
6467
}
6568

6669
// 手机号登录
6770
const phoneLogin = async (req: PhoneLoginReq, tenantCode?: string) => {
6871
const res = await phoneLoginApi({ ...req, clientId: import.meta.env.VITE_CLIENT_ID, authType: AuthTypeConstants.PHONE }, tenantCode)
6972
setToken(res.data.token)
73+
tenantStore.setTenantId(res.data.tenantId)
7074
token.value = res.data.token
7175
}
7276

7377
// 三方账号登录
7478
const socialLogin = async (source: string, req: any) => {
75-
const res = await socialLoginApi({ ...req, source, clientId: import.meta.env.VITE_CLIENT_ID, authType: AuthTypeConstants.SOCIAL })
79+
const res: any = await socialLoginApi({ ...req, source, clientId: import.meta.env.VITE_CLIENT_ID, authType: AuthTypeConstants.SOCIAL })
7680
setToken(res.data.token)
81+
tenantStore.setTenantId(res.data.tenantId)
7782
token.value = res.data.token
7883
}
7984

‎src/utils/http.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import axios from 'axios'
22
import qs from 'query-string'
33
import type { AxiosError, AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios'
4+
import { useTenantStore } from '@/stores/modules/tenant'
45
import { useUserStore } from '@/stores'
56
import { getToken } from '@/utils/auth'
67
import modalErrorWrapper from '@/utils/modal-error-wrapper'
78
import messageErrorWrapper from '@/utils/message-error-wrapper'
89
import notificationErrorWrapper from '@/utils/notification-error-wrapper'
910
import router from '@/router'
10-
import { getTenantId } from '@/utils/tenant'
1111

1212
interface ICodeMessage {
1313
[propName: number]: string
@@ -58,9 +58,9 @@ http.interceptors.request.use(
5858
if (token) {
5959
config.headers.Authorization = `Bearer ${token}`
6060
}
61-
const tenantId = getTenantId()
62-
if (tenantId) {
63-
config.headers['X-Tenant-Id'] = tenantId
61+
const tenantStore = useTenantStore()
62+
if (tenantStore.tenantEnabled && tenantStore.tenantId) {
63+
config.headers['X-Tenant-Id'] = tenantStore.tenantId
6464
}
6565
return config
6666
},

‎src/utils/tenant.ts‎

Lines changed: 0 additions & 14 deletions
This file was deleted.

‎src/views/login/components/account/index.vue‎

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
size="large"
99
@submit="handleLogin"
1010
>
11-
<a-form-item v-if="tenantStore.tenantEnabled && !tenantStore.tenantCode" field="tenantCode" hide-label>
12-
<a-input v-model="tenantCodeInput" placeholder="请输入租户编码" allow-clear />
11+
<a-form-item v-if="tenantStore.needInputTenantId" field="tenantCode" hide-label>
12+
<a-input v-model="form.tenantCode" placeholder="请输入租户编码" allow-clear />
1313
</a-form-item>
1414
<a-form-item field="username" hide-label>
1515
<a-input v-model="form.username" placeholder="请输入用户名" allow-clear />
@@ -44,14 +44,14 @@
4444
import { type FormInstance, Message } from '@arco-design/web-vue'
4545
import { useStorage } from '@vueuse/core'
4646
import { getImageCaptcha } from '@/apis/common'
47-
import { useTabsStore, useTenantStore, useUserStore } from '@/stores'
47+
import { useTabsStore, useUserStore } from '@/stores'
4848
import { encryptByRsa } from '@/utils/encrypt'
49+
import { useTenantStore } from '@/stores/modules/tenant'
4950
5051
const tenantStore = useTenantStore()
5152
5253
const loginConfig = useStorage('login-config', {
5354
rememberMe: true,
54-
tenantCode: tenantStore.tenantCode,
5555
username: 'admin', // 演示默认值
5656
password: 'admin123', // 演示默认值
5757
// username: debug ? 'admin' : '', // 演示默认值
@@ -63,21 +63,22 @@ const isCaptchaEnabled = ref(true)
6363
const captchaImgBase64 = ref()
6464
6565
const formRef = ref<FormInstance>()
66-
const tenantCodeInput = ref('') // 用户手动输入的租户编码
6766
const form = reactive({
6867
username: loginConfig.value.username,
6968
password: loginConfig.value.password,
7069
captcha: '',
7170
uuid: '',
7271
expired: false,
72+
tenantCode: '', // 新增
7373
})
74+
// 校验规则部分
7475
const rules: FormInstance['rules'] = {
7576
username: [{ required: true, message: '请输入用户名' }],
7677
password: [{ required: true, message: '请输入密码' }],
7778
captcha: [{ required: isCaptchaEnabled.value, message: '请输入验证码' }],
7879
tenantCode: [
7980
{
80-
required: tenantStore.tenantEnabled && !tenantStore.tenantCode,
81+
required: tenantStore.needInputTenantId,
8182
message: '请输入租户编码',
8283
},
8384
],
@@ -128,22 +129,20 @@ const handleLogin = async () => {
128129
if (isInvalid) return
129130
loading.value = true
130131
131-
// 计算最终要传递的租户编码
132-
let finalTenantCode = ''
133-
if (tenantStore.tenantEnabled) {
134-
finalTenantCode = tenantStore.tenantCode || tenantCodeInput.value
132+
let tenantCode
133+
if (tenantStore.needInputTenantId) {
134+
tenantCode = form.tenantCode
135135
}
136136
137137
await userStore.accountLogin({
138138
username: form.username,
139139
password: encryptByRsa(form.password) || '',
140140
captcha: form.captcha,
141141
uuid: form.uuid,
142-
}, finalTenantCode)
142+
}, tenantCode)
143143
tabsStore.reset()
144144
const { redirect, ...othersQuery } = router.currentRoute.value.query
145145
const { rememberMe } = loginConfig.value
146-
loginConfig.value.tenantCode = rememberMe ? finalTenantCode : ''
147146
loginConfig.value.username = rememberMe ? form.username : ''
148147
149148
// 如果有重定向参数,解码并直接跳转到完整路径

‎src/views/login/index.vue‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,15 @@ import AccountLogin from './components/account/index.vue'
9696
import PhoneLogin from './components/phone/index.vue'
9797
import EmailLogin from './components/email/index.vue'
9898
import { socialAuth } from '@/apis/auth'
99-
import { useAppStore, useTenantStore } from '@/stores'
99+
import { useAppStore } from '@/stores'
100+
import { useTenantStore } from '@/stores/modules/tenant'
100101
import { useDevice } from '@/hooks'
101-
import { getTenantCodeByDomain, getTenantStatus } from '@/apis'
102+
import { getTenantIdByDomain, getTenantStatus } from '@/apis'
102103
103104
defineOptions({ name: 'Login' })
105+
const appStore = useAppStore()
104106
const tenantStore = useTenantStore()
105107
const { isDesktop } = useDevice()
106-
const appStore = useAppStore()
107108
const title = computed(() => appStore.getTitle())
108109
const logo = computed(() => appStore.getLogo())
109110
@@ -128,8 +129,8 @@ const onGetTenant = async () => {
128129
// 开启租户 根据地址(域名)查询租户code
129130
if (data) {
130131
const domain = window.location.hostname
131-
const { data } = await getTenantCodeByDomain(domain)
132-
tenantStore.setTenantCode(data)
132+
const { data: tenantId } = await getTenantIdByDomain(domain)
133+
tenantStore.setTenantId(tenantId)
133134
}
134135
}
135136
onMounted(() => {

0 commit comments

Comments
 (0)