Skip to content

Commit 85ee558

Browse files
committed
多租户:登录界面,根据 host 域名获取对应的租户编号
1 parent c0d5c76 commit 85ee558

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed

src/api/login/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ export const getTenantIdByName = (name: string) => {
2727
return request.get({ url: '/system/tenant/get-id-by-name?name=' + name })
2828
}
2929

30+
// 使用租户域名,获得租户信息
31+
export const getTenantByWebsite = (website: string) => {
32+
return request.get({ url: '/system/tenant/get-by-website?website=' + website })
33+
}
34+
3035
// 登出
3136
export const loginOut = () => {
3237
return request.post({ url: '/system/auth/logout' })

src/views/Login/components/LoginForm.vue

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -193,10 +193,10 @@ const loginData = reactive({
193193
})
194194
195195
const socialList = [
196-
{ icon: 'ant-design:github-filled', type: 0 },
197196
{ icon: 'ant-design:wechat-filled', type: 30 },
198-
{ icon: 'ant-design:alipay-circle-filled', type: 0 },
199-
{ icon: 'ant-design:dingtalk-circle-filled', type: 20 }
197+
{ icon: 'ant-design:dingtalk-circle-filled', type: 20 },
198+
{ icon: 'ant-design:github-filled', type: 0 },
199+
{ icon: 'ant-design:alipay-circle-filled', type: 0 }
200200
]
201201
202202
// 获取验证码
@@ -210,7 +210,7 @@ const getCode = async () => {
210210
verify.value.show()
211211
}
212212
}
213-
//获取租户ID
213+
// 获取租户 ID
214214
const getTenantId = async () => {
215215
if (loginData.tenantEnable === 'true') {
216216
const res = await LoginApi.getTenantIdByName(loginData.loginForm.tenantName)
@@ -230,6 +230,15 @@ const getCookie = () => {
230230
}
231231
}
232232
}
233+
// 根据域名,获得租户信息
234+
const getTenantByWebsite = async () => {
235+
const website = location.host
236+
const res = await LoginApi.getTenantByWebsite(website)
237+
if (res) {
238+
loginData.loginForm.tenantName = res.name
239+
authUtil.setTenantId(res.id)
240+
}
241+
}
233242
const loading = ref() // ElLoading.service 返回的实例
234243
// 登录
235244
const handleLogin = async (params) => {
@@ -278,10 +287,15 @@ const doSocialLogin = async (type: number) => {
278287
} else {
279288
loginLoading.value = true
280289
if (loginData.tenantEnable === 'true') {
281-
await message.prompt('请输入租户名称', t('common.reminder')).then(async ({ value }) => {
282-
const res = await LoginApi.getTenantIdByName(value)
283-
authUtil.setTenantId(res)
284-
})
290+
// 尝试先通过 tenantName 获取租户
291+
await getTenantId()
292+
// 如果获取不到,则需要弹出提示,进行处理
293+
if (!authUtil.getTenantId()) {
294+
await message.prompt('请输入租户名称', t('common.reminder')).then(async ({ value }) => {
295+
const res = await LoginApi.getTenantIdByName(value)
296+
authUtil.setTenantId(res)
297+
})
298+
}
285299
}
286300
// 计算 redirectUri
287301
// tricky: type、redirect需要先encode一次,否则钉钉回调会丢失。
@@ -307,6 +321,7 @@ watch(
307321
)
308322
onMounted(() => {
309323
getCookie()
324+
getTenantByWebsite()
310325
})
311326
</script>
312327

0 commit comments

Comments
 (0)