mirror of
https://github.com/continew-org/continew-admin-ui.git
synced 2025-09-09 20:57:17 +08:00
feat(tenant):增加通过域名查询租户编码和查询租户开关状态的功能
This commit is contained in:
@@ -38,3 +38,8 @@ export function listSiteOptionDict() {
|
|||||||
export function upload(data: FormData) {
|
export function upload(data: FormData) {
|
||||||
return http.post(`${BASE_URL}/file`, data)
|
return http.post(`${BASE_URL}/file`, data)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @desc 查询租户开启状态 */
|
||||||
|
export function getTenantStatus() {
|
||||||
|
return http.get<boolean>(`${BASE_URL}/dict/option/tenant`)
|
||||||
|
}
|
||||||
|
@@ -335,7 +335,6 @@ export interface BasicConfig {
|
|||||||
SITE_TITLE: string
|
SITE_TITLE: string
|
||||||
SITE_COPYRIGHT: string
|
SITE_COPYRIGHT: string
|
||||||
SITE_BEIAN: string
|
SITE_BEIAN: string
|
||||||
TENANT_ENABLED: boolean
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** 基础配置类型 */
|
/** 基础配置类型 */
|
||||||
|
@@ -7,3 +7,8 @@ const BASE_URL = '/tenant/common'
|
|||||||
export function listTenantPackageDict(query?: { description: string, status: number }) {
|
export function listTenantPackageDict(query?: { description: string, status: number }) {
|
||||||
return http.get<LabelValueState[]>(`${BASE_URL}/dict/package`, query)
|
return http.get<LabelValueState[]>(`${BASE_URL}/dict/package`, query)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @desc 根据域名查询租户编码 */
|
||||||
|
export function getTenantCodeByDomain(domain: string) {
|
||||||
|
return http.get<string>(`${BASE_URL}/code/domain`, { domain })
|
||||||
|
}
|
||||||
|
@@ -6,6 +6,7 @@ export * from './modules/route'
|
|||||||
export * from './modules/tabs'
|
export * from './modules/tabs'
|
||||||
export * from './modules/dict'
|
export * from './modules/dict'
|
||||||
export * from './modules/user'
|
export * from './modules/user'
|
||||||
|
export * from './modules/tenant'
|
||||||
|
|
||||||
const pinia = createPinia()
|
const pinia = createPinia()
|
||||||
pinia.use(piniaPluginPersistedstate)
|
pinia.use(piniaPluginPersistedstate)
|
||||||
|
@@ -68,7 +68,6 @@ const storeSetup = () => {
|
|||||||
siteConfig.SITE_TITLE = resMap.get('SITE_TITLE')
|
siteConfig.SITE_TITLE = resMap.get('SITE_TITLE')
|
||||||
siteConfig.SITE_COPYRIGHT = resMap.get('SITE_COPYRIGHT')
|
siteConfig.SITE_COPYRIGHT = resMap.get('SITE_COPYRIGHT')
|
||||||
siteConfig.SITE_BEIAN = resMap.get('SITE_BEIAN')
|
siteConfig.SITE_BEIAN = resMap.get('SITE_BEIAN')
|
||||||
siteConfig.TENANT_ENABLED = resMap.get('TENANT_ENABLED') === 'true'
|
|
||||||
document.title = resMap.get('SITE_TITLE')
|
document.title = resMap.get('SITE_TITLE')
|
||||||
document
|
document
|
||||||
.querySelector('link[rel="shortcut icon"]')
|
.querySelector('link[rel="shortcut icon"]')
|
||||||
@@ -123,10 +122,6 @@ const storeSetup = () => {
|
|||||||
const getForRecord = () => {
|
const getForRecord = () => {
|
||||||
return siteConfig.SITE_BEIAN
|
return siteConfig.SITE_BEIAN
|
||||||
}
|
}
|
||||||
|
|
||||||
const getTenantEnabled = () => {
|
|
||||||
return siteConfig.TENANT_ENABLED
|
|
||||||
}
|
|
||||||
return {
|
return {
|
||||||
...toRefs(settingConfig),
|
...toRefs(settingConfig),
|
||||||
...toRefs(siteConfig),
|
...toRefs(siteConfig),
|
||||||
@@ -143,7 +138,6 @@ const storeSetup = () => {
|
|||||||
getTitle,
|
getTitle,
|
||||||
getCopyright,
|
getCopyright,
|
||||||
getForRecord,
|
getForRecord,
|
||||||
getTenantEnabled,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
31
src/stores/modules/tenant.ts
Normal file
31
src/stores/modules/tenant.ts
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
import { defineStore } from 'pinia'
|
||||||
|
import { computed, reactive } from 'vue'
|
||||||
|
|
||||||
|
const storeSetup = () => {
|
||||||
|
interface TenantInfo {
|
||||||
|
tenantEnabled: boolean
|
||||||
|
tenantCode: string
|
||||||
|
}
|
||||||
|
const tenantInfo = reactive<TenantInfo>({
|
||||||
|
tenantEnabled: false,
|
||||||
|
tenantCode: '',
|
||||||
|
})
|
||||||
|
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 },
|
||||||
|
})
|
@@ -8,8 +8,8 @@
|
|||||||
size="large"
|
size="large"
|
||||||
@submit="handleLogin"
|
@submit="handleLogin"
|
||||||
>
|
>
|
||||||
<a-form-item v-if="tenantEnabled" field="tenantCode" hide-label>
|
<a-form-item v-if="tenantStore.tenantEnabled && !tenantStore.tenantCode" field="tenantCode" hide-label>
|
||||||
<a-input v-model="tenantCode" placeholder="请输入租户编码" allow-clear />
|
<a-input v-model="tenantCodeInput" placeholder="请输入租户编码" allow-clear />
|
||||||
</a-form-item>
|
</a-form-item>
|
||||||
<a-form-item field="username" hide-label>
|
<a-form-item field="username" hide-label>
|
||||||
<a-input v-model="form.username" placeholder="请输入用户名" allow-clear />
|
<a-input v-model="form.username" placeholder="请输入用户名" allow-clear />
|
||||||
@@ -43,17 +43,15 @@
|
|||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { type FormInstance, Message } from '@arco-design/web-vue'
|
import { type FormInstance, Message } from '@arco-design/web-vue'
|
||||||
import { useStorage } from '@vueuse/core'
|
import { useStorage } from '@vueuse/core'
|
||||||
import { computed } from 'vue'
|
|
||||||
import { getImageCaptcha } from '@/apis/common'
|
import { getImageCaptcha } from '@/apis/common'
|
||||||
import { useAppStore, useTabsStore, useUserStore } from '@/stores'
|
import { useTabsStore, useTenantStore, useUserStore } from '@/stores'
|
||||||
import { encryptByRsa } from '@/utils/encrypt'
|
import { encryptByRsa } from '@/utils/encrypt'
|
||||||
|
|
||||||
const appStore = useAppStore()
|
const tenantStore = useTenantStore()
|
||||||
const tenantEnabled = computed(() => appStore.getTenantEnabled())
|
|
||||||
|
|
||||||
const loginConfig = useStorage('login-config', {
|
const loginConfig = useStorage('login-config', {
|
||||||
rememberMe: true,
|
rememberMe: true,
|
||||||
tenantCode: '',
|
tenantCode: tenantStore.tenantCode,
|
||||||
username: 'admin', // 演示默认值
|
username: 'admin', // 演示默认值
|
||||||
password: 'admin123', // 演示默认值
|
password: 'admin123', // 演示默认值
|
||||||
// username: debug ? 'admin' : '', // 演示默认值
|
// username: debug ? 'admin' : '', // 演示默认值
|
||||||
@@ -65,6 +63,7 @@ const isCaptchaEnabled = ref(true)
|
|||||||
const captchaImgBase64 = ref()
|
const captchaImgBase64 = ref()
|
||||||
|
|
||||||
const formRef = ref<FormInstance>()
|
const formRef = ref<FormInstance>()
|
||||||
|
const tenantCodeInput = ref('') // 用户手动输入的租户编码
|
||||||
const form = reactive({
|
const form = reactive({
|
||||||
username: loginConfig.value.username,
|
username: loginConfig.value.username,
|
||||||
password: loginConfig.value.password,
|
password: loginConfig.value.password,
|
||||||
@@ -72,11 +71,16 @@ const form = reactive({
|
|||||||
uuid: '',
|
uuid: '',
|
||||||
expired: false,
|
expired: false,
|
||||||
})
|
})
|
||||||
const tenantCode = ref()
|
|
||||||
const rules: FormInstance['rules'] = {
|
const rules: FormInstance['rules'] = {
|
||||||
username: [{ required: true, message: '请输入用户名' }],
|
username: [{ required: true, message: '请输入用户名' }],
|
||||||
password: [{ required: true, message: '请输入密码' }],
|
password: [{ required: true, message: '请输入密码' }],
|
||||||
captcha: [{ required: isCaptchaEnabled.value, message: '请输入验证码' }],
|
captcha: [{ required: isCaptchaEnabled.value, message: '请输入验证码' }],
|
||||||
|
tenantCode: [
|
||||||
|
{
|
||||||
|
required: tenantStore.tenantEnabled && !tenantStore.tenantCode,
|
||||||
|
message: '请输入租户编码',
|
||||||
|
},
|
||||||
|
],
|
||||||
}
|
}
|
||||||
|
|
||||||
// 验证码过期定时器
|
// 验证码过期定时器
|
||||||
@@ -123,16 +127,23 @@ const handleLogin = async () => {
|
|||||||
const isInvalid = await formRef.value?.validate()
|
const isInvalid = await formRef.value?.validate()
|
||||||
if (isInvalid) return
|
if (isInvalid) return
|
||||||
loading.value = true
|
loading.value = true
|
||||||
|
|
||||||
|
// 计算最终要传递的租户编码
|
||||||
|
let finalTenantCode = ''
|
||||||
|
if (tenantStore.tenantEnabled) {
|
||||||
|
finalTenantCode = tenantStore.tenantCode || tenantCodeInput.value
|
||||||
|
}
|
||||||
|
|
||||||
await userStore.accountLogin({
|
await userStore.accountLogin({
|
||||||
username: form.username,
|
username: form.username,
|
||||||
password: encryptByRsa(form.password) || '',
|
password: encryptByRsa(form.password) || '',
|
||||||
captcha: form.captcha,
|
captcha: form.captcha,
|
||||||
uuid: form.uuid,
|
uuid: form.uuid,
|
||||||
}, tenantCode.value)
|
}, finalTenantCode)
|
||||||
tabsStore.reset()
|
tabsStore.reset()
|
||||||
const { redirect, ...othersQuery } = router.currentRoute.value.query
|
const { redirect, ...othersQuery } = router.currentRoute.value.query
|
||||||
const { rememberMe } = loginConfig.value
|
const { rememberMe } = loginConfig.value
|
||||||
loginConfig.value.tenantCode = rememberMe ? tenantCode.value : ''
|
loginConfig.value.tenantCode = rememberMe ? finalTenantCode : ''
|
||||||
loginConfig.value.username = rememberMe ? form.username : ''
|
loginConfig.value.username = rememberMe ? form.username : ''
|
||||||
|
|
||||||
// 如果有重定向参数,解码并直接跳转到完整路径
|
// 如果有重定向参数,解码并直接跳转到完整路径
|
||||||
|
@@ -87,7 +87,6 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<SelectTenant />
|
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
@@ -97,11 +96,12 @@ import AccountLogin from './components/account/index.vue'
|
|||||||
import PhoneLogin from './components/phone/index.vue'
|
import PhoneLogin from './components/phone/index.vue'
|
||||||
import EmailLogin from './components/email/index.vue'
|
import EmailLogin from './components/email/index.vue'
|
||||||
import { socialAuth } from '@/apis/auth'
|
import { socialAuth } from '@/apis/auth'
|
||||||
import { useAppStore } from '@/stores'
|
import { useAppStore, useTenantStore } from '@/stores'
|
||||||
import { useDevice } from '@/hooks'
|
import { useDevice } from '@/hooks'
|
||||||
|
import { getTenantCodeByDomain, getTenantStatus } from '@/apis'
|
||||||
|
|
||||||
defineOptions({ name: 'Login' })
|
defineOptions({ name: 'Login' })
|
||||||
|
const tenantStore = useTenantStore()
|
||||||
const { isDesktop } = useDevice()
|
const { isDesktop } = useDevice()
|
||||||
const appStore = useAppStore()
|
const appStore = useAppStore()
|
||||||
const title = computed(() => appStore.getTitle())
|
const title = computed(() => appStore.getTitle())
|
||||||
@@ -120,6 +120,21 @@ const onOauth = async (source: string) => {
|
|||||||
const { data } = await socialAuth(source)
|
const { data } = await socialAuth(source)
|
||||||
window.location.href = data.authorizeUrl
|
window.location.href = data.authorizeUrl
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 查询租户状态和租户编码
|
||||||
|
const onGetTenant = async () => {
|
||||||
|
const { data } = await getTenantStatus()
|
||||||
|
tenantStore.setTenantEnable(data)
|
||||||
|
// 开启租户 根据地址(域名)查询租户code
|
||||||
|
if (data) {
|
||||||
|
const domain = window.location.hostname
|
||||||
|
const { data } = await getTenantCodeByDomain(domain)
|
||||||
|
tenantStore.setTenantCode(data)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
onMounted(() => {
|
||||||
|
onGetTenant()
|
||||||
|
})
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<style scoped lang="scss">
|
<style scoped lang="scss">
|
||||||
|
Reference in New Issue
Block a user