feat(多租户):多租户数据隔离功能实现

This commit is contained in:
2025-05-15 16:51:48 +08:00
parent 4beecb5d3d
commit d4c39ac6b5
15 changed files with 134 additions and 27 deletions
@@ -13,7 +13,7 @@ const Footer: React.FC = () => {
return (
<DefaultFooter
copyright={`${2018} ${defaultMessage}`}
copyright={`${2025} ${defaultMessage}`}
links={[
{
key: 'github',
@@ -11,7 +11,7 @@ export default {
'layout.user.link.help': '帮助',
'layout.user.link.privacy': '隐私',
'layout.user.link.terms': '条款',
'app.copyright.produced': 'zlt版权所有:https://gitee.com/zlt2000/microservices-platform',
'app.copyright.produced': '版权所有:https://gitee.com/L-Shilin/go-fly-platform-services.git',
'app.preview.down.block': '下载此页面到本地项目',
'app.welcome.link.fetch-blocks': '获取全部区块',
'app.welcome.link.block-list': '基于 block 开发,快速构建标准页面',
@@ -1,9 +1,9 @@
import Footer from '@/components/Footer';
import { login } from '@/services/login/api';
import { InfoCircleOutlined, LockOutlined, UserOutlined } from '@ant-design/icons';
import { InfoCircleOutlined, LockOutlined, UserOutlined, AppstoreOutlined } from '@ant-design/icons';
import { LoginForm, ProFormCaptcha, ProFormText, ProFormCheckbox } from '@ant-design/pro-components';
import { message, Tooltip, Spin } from 'antd';
import React, { useState } from 'react';
import { message, Tooltip, Spin, Form } from 'antd';
import React, { useState, useEffect } from 'react';
import { FormattedMessage, history, useIntl, useModel } from 'umi';
import { v4 as uuidv4 } from 'uuid';
import styles from './index.less';
@@ -13,6 +13,7 @@ const Login: React.FC = () => {
const [loading, setLoading] = useState<boolean>(false);
const { initialState, setInitialState } = useModel('@@initialState');
const intl = useIntl();
const [form] = Form.useForm();
const fetchUserInfo = async () => {
try {
@@ -28,11 +29,32 @@ const Login: React.FC = () => {
}
};
// 缓存租户标识
const cacheTenantId = (clientId: string) => {
localStorage.setItem('tenant_client_id', clientId);
};
// 获取缓存的租户标识
const getCachedTenantId = () => {
return localStorage.getItem('tenant_client_id');
};
// 自动填充缓存的租户标识
useEffect(() => {
const clientId = getCachedTenantId();
if (clientId) {
form.setFieldsValue({
clientId,
});
}
}, []);
const handleSubmit = async (values: API.LoginParams) => {
try {
setLoading(true);
const msg = await login({ ...values, deviceId: uuid });
debugger;
if (msg.resp_code === 0 && msg.data?.access_token) {
sessionStorage.setItem('access_token', msg.data.access_token);
message.success('登录成功!');
@@ -62,6 +84,7 @@ const Login: React.FC = () => {
title="microservices-platform"
subTitle={intl.formatMessage({ id: 'pages.layouts.userLayout.title' })}
initialValues={{ autoLogin: true }}
form={form}
onFinish={async (values) => {
if (!loading) {
await handleSubmit(values as API.LoginParams);
@@ -69,16 +92,30 @@ const Login: React.FC = () => {
}}
>
<ProFormText
name="username"
name="clientId"
fieldProps={{
size: 'large',
prefix: <UserOutlined className={styles.prefixIcon} />,
prefix: <AppstoreOutlined />,
}}
placeholder="请输入账号"
placeholder="请输入租户标识"
rules={[
{
required: true,
message: '请输入账号!',
message: '请输入租户标识!',
},
]}
/>
<ProFormText
name="username"
fieldProps={{
size: 'large',
prefix: <UserOutlined />,
}}
placeholder="请输入用户名"
rules={[
{
required: true,
message: '请输入用户名!',
},
]}
/>
@@ -86,7 +123,7 @@ const Login: React.FC = () => {
name="password"
fieldProps={{
size: 'large',
prefix: <LockOutlined className={styles.prefixIcon} />,
prefix: <LockOutlined />,
}}
placeholder="请输入密码"
rules={[
@@ -33,15 +33,36 @@ export async function outLogin(options?: { [key: string]: any }) {
});
}
// 修改生成 basicToken 的工具函数,只使用 clientId
const generateBasicToken = (clientId: string) => {
// 确保使用 UTF-8 编码
const token = `${clientId}:${clientId}`;
// 使用 encodeURIComponent 和 decodeURIComponent 处理中文字符
const encodedToken = btoa(unescape(encodeURIComponent(token)));
return `Basic ${encodedToken}`;
};
export async function login(body: API.LoginParams, options?: { [key: string]: any }) {
const clientId = body.clientId;
const basicToken = generateBasicToken(clientId);
body.clientId = undefined;
debugger;
return request<API.OptionalResult<API.LoginResult>>('/api-uaa/oauth/token', {
method: 'POST',
requestType: 'form',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
Authorization: AUTH_CONFIG.basicToken,
Authorization: basicToken,
},
data: {
username: body.username,
password: body.password,
validCode: body.validCode,
deviceId: body.deviceId,
grant_type: 'password_code',
},
data: { ...body, grant_type: 'password_code' },
...(options || {}),
});
}
@@ -41,6 +41,7 @@ declare namespace API {
password?: string;
validCode?: string;
deviceId?: string;
clientId?: string;
};
type ErrorResponse = {