From 6ed190d92ecaa1ea6c9d6c024fbe32786b938626 Mon Sep 17 00:00:00 2001 From: cclu <1106109051@qq.com> Date: Thu, 27 Feb 2025 19:50:31 +0800 Subject: [PATCH] =?UTF-8?q?chore(=E6=A8=A1=E5=9D=97):=20=E5=AF=B9=E8=84=9A?= =?UTF-8?q?=E6=89=8B=E6=9E=B6=E5=81=9A=E4=BA=86=E4=BA=9B=E6=9B=B4=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/config.ts | 102 ++++++++++++++++++++++++++++++++- config/proxy.ts | 5 +- mock/user.ts | 8 +-- package.json | 1 + src/layouts/index.tsx | 6 +- src/models/user.ts | 2 + src/pages/authority.ts | 5 +- src/pages/user/login/index.tsx | 5 +- src/services/user.ts | 18 +++++- src/utils/request.ts | 94 +++++++++++++++++++++++++++++- yarn.lock | 5 ++ 11 files changed, 233 insertions(+), 18 deletions(-) diff --git a/config/config.ts b/config/config.ts index 5dd89ca..5083fa4 100644 --- a/config/config.ts +++ b/config/config.ts @@ -1,5 +1,5 @@ // import { defineConfig } from "umi"; -// import proxy from './proxy'; +import proxy from './proxy'; //不使用defineConfig则能在其他地方获取这些配置 export default { @@ -11,5 +11,105 @@ export default { title: 'UMI4 Admin', favicons: [ '/favicon.svg' + ], + routes: [ + { + id: 1, + key: '1', + path: '/', + redirect: '', + name: '首页' + }, + { + id: 2, + key: '2', + name: '关于', + path: '/about', + redirect: '/about/m', + routes: [ + { + id: 21, + key: '2-1', + name: '关于你', + path: '/about/u', + redirect: '/about/u/1', + pid: 2, + routes: [ + { + id: 211, + key: '2-1-1', + name: '关于你1', + path: '/about/u/index', + redirect: '', + pid: 21 + }, + { + id: 212, + key: '2-1-2', + name: '关于你2', + path: '/about/u/2', + redirect: '', + pid: 21 + } + ] + }, + { + id: 22, + key: '2-2', + path: '/about/m', + redirect: '', + name: '(页面元素权限)关于我', + pid: 2 + }, + { + id: 23, + key: '2-3', + path: '/about/um', + redirect: '', + name: '关于你和我', + pid: 2 + } + ] + }, + { + id: 3, + key: '3', + name: '教师', + path: '/teacher', + redirect: '/teacher/u', + routes: [ + { + id: 31, + key: '3-1', + path: '/teacher/u', + redirect: '', + name: '(403)关于你教师', + pid: 3 + }, + { + id: 32, + key: '3-2', + path: '/teacher/m', + redirect: '', + name: '关于我教师', + pid: 3 + }, + { + id: 33, + key: '3-3', + path: '/teacher/um', + redirect: '', + name: '关于你和我教师', + pid: 3 + } + ] + }, + { + id: 4, + key: '4', + name: '(404)学生', + path: '/student', + redirect: '', + } ] }; diff --git a/config/proxy.ts b/config/proxy.ts index 91fdb9d..f75f782 100644 --- a/config/proxy.ts +++ b/config/proxy.ts @@ -1,7 +1,8 @@ const proxy = { '/api': { - target: 'https://preview.pro.ant.design', - changeOrigin: true + target: 'http://10.104.1.38:3000', + changeOrigin: true, + pathRewrite: { '^/api': '' }, } } diff --git a/mock/user.ts b/mock/user.ts index 974fb72..d2cd58e 100644 --- a/mock/user.ts +++ b/mock/user.ts @@ -20,12 +20,12 @@ const handleCommonRes = (data: Record | Record const userApi = { //登录 'POST /api/user/login': async (req: Request, res: Response) => { - const { UserPassWord, UserPassport, mobile, captcha } = req.body; + const { password, username, mobile, captcha } = req.body; await waitTime(2000); switch (true) { - case UserPassport === 'admin' && UserPassWord === 'ant.design': - case UserPassport === 'user' && UserPassWord === 'ant.design': + case username === 'admin' && password === 'ant.design': + case username === 'user' && password === 'ant.design': case /^1\d{10}$/.test(mobile) && Boolean(captcha): res.send( handleCommonRes({ @@ -159,7 +159,7 @@ const userApi = { id: 211, key: '2-1-1', name: '关于你1', - path: '/about/u/1', + path: '/about/u/index', redirect: '', pid: 21 }, diff --git a/package.json b/package.json index c3e95eb..5b0f60a 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,7 @@ "@ant-design/pro-components": "^2.8.6", "antd": "^5.21.2", "axios": "^1.7.7", + "crypto-js": "^4.2.0", "dva": "^3.0.0-alpha.1", "umi": "^4.3.24" }, diff --git a/src/layouts/index.tsx b/src/layouts/index.tsx index 2a2575c..530a4c4 100644 --- a/src/layouts/index.tsx +++ b/src/layouts/index.tsx @@ -49,6 +49,8 @@ const BasicLayout: FC<{ user: UserModelState, global: ProfileModelState, dispatc const [collapsed, setCollapsed] = useState(false); const [openKeys, setOpenKeys] = useState(['']); const { pathname } = useLocation(); + + const { dispatch, user: { @@ -139,6 +141,8 @@ const BasicLayout: FC<{ user: UserModelState, global: ProfileModelState, dispatc // ]; console.log('consumableMenu', consumableMenu); const location = useLocation(); + console.log('location', location); + // 改变panes const handleTabsPanes = (payload: any): void => { @@ -183,7 +187,7 @@ const BasicLayout: FC<{ user: UserModelState, global: ProfileModelState, dispatc guid: "2", hideInMenu: false, name: "关于你1", - path: "/about/u/1", + path: "/about/u/index", }, { SYSTEMMODULE_DESC: "", diff --git a/src/models/user.ts b/src/models/user.ts index af6b74c..ed0c0be 100644 --- a/src/models/user.ts +++ b/src/models/user.ts @@ -108,6 +108,7 @@ const UserModel: UserModelType = { effects: { //登录 *login({ payload }, { call, put }) { + console.log('payload', payload); yield put({ type: 'save', @@ -117,6 +118,7 @@ const UserModel: UserModelType = { }); const res: API.LoginResponse = yield call(userLogin, payload); + console.log('res', res); //登录成功之后设置token并获取用户信息等数据 if (!res.code) { diff --git a/src/pages/authority.ts b/src/pages/authority.ts index 0a48292..77bec68 100644 --- a/src/pages/authority.ts +++ b/src/pages/authority.ts @@ -1,9 +1,10 @@ + const authority: PageAuthority = { '/': [ '/', ], - '/about/u/1': [ - '/about/u/1', + '/about/u/index': [ + '/about/u/index', ], '/about/u/2': [ '/about/u/2', diff --git a/src/pages/user/login/index.tsx b/src/pages/user/login/index.tsx index ec7cbae..beb6259 100644 --- a/src/pages/user/login/index.tsx +++ b/src/pages/user/login/index.tsx @@ -88,8 +88,7 @@ const Index: FC = (props) => { }} > , @@ -103,7 +102,7 @@ const Index: FC = (props) => { ]} /> , diff --git a/src/services/user.ts b/src/services/user.ts index e57df6f..6184672 100644 --- a/src/services/user.ts +++ b/src/services/user.ts @@ -1,9 +1,21 @@ import request from '@/utils/request'; //登录 -export const userLogin = (params: Record): Promise => ( - request.post('/api/user/login', params) -); +// export const userLogin = (params: Record): Promise => ( +// request.post('/api/user/login', params) +// ); + +// 登录 +export async function userLogin(params: any) { + console.log('paramsuserLogin', params); + + const data = await request.post('/api/user/login', params) + // const data = await request.post('/auth/login', params) + console.log('data', data); + + return data +} + //获取用户信息 export const retrieveUserInfo = (): Promise => ( diff --git a/src/utils/request.ts b/src/utils/request.ts index 07fe211..5f44238 100644 --- a/src/utils/request.ts +++ b/src/utils/request.ts @@ -2,15 +2,23 @@ import axios from 'axios'; import { getDvaApp } from 'umi'; import { notification } from 'antd'; import type { AxiosRequestHeaders } from 'axios/index'; +import CryptoJS from "crypto-js"; const { UMI_APP_BASEURL } = process.env; const instance = axios.create({ baseURL: UMI_APP_BASEURL }); // const instance = axios.create({ baseURL: 'https://api.eshangtech.com/EShangApiMain' }); +// const instance = axios.create({ baseURL: '/api' }); + instance.interceptors.request.use( (config) => { + // 对data数据进行加密 + // if (config.data) { + // config.data = preprocessData(JSON.stringify(config.data)); // 调用预处理函数 + // } + config.headers = { ...config.headers, Authorization: localStorage.getItem('Authorization') || '', @@ -27,13 +35,19 @@ instance.interceptors.response.use( const { data } = response; const { code } = data; - if (code) { + if (code !== 200) { notification.error({ message: data.message, }); } - return data; + const timestamp = getFormattedDate() + + // let res = JSON.parse(decryptAESECB(data.data, timestamp)) + // return res; + // console.log('resdsa', data); + + return data }, //状态码不为2xx的时候执行 (error) => { @@ -72,4 +86,80 @@ instance.interceptors.response.use( }, ); + +// 加密 +const encryptAESECB = (data: string, key: string) => { + // const cipher = CryptoJS.createCipheriv('aes-128-ecb', key, null); // ECB 模式不需要 IV + const newKey = CryptoJS.enc.Utf8.parse(key); // 密钥必须是 16 字节 + const cipher = CryptoJS.AES.encrypt(data, newKey, { + mode: CryptoJS.mode.ECB, + padding: CryptoJS.pad.Pkcs7 + }); + let encrypted = cipher.ciphertext.toString(CryptoJS.enc.Hex); + // let encrypted = cipher.update(data, 'utf8', 'hex'); + // encrypted += cipher.final('hex'); + return encrypted; +} + +// 解密 +const decryptAESECB = (data: string, key: string) => { + // const decipher = CryptoJS.createDecipheriv('aes-128-ecb', key, null); + // let decrypted = decipher.update(data, 'hex', 'utf8'); + // decrypted += decipher.final('utf8'); + const newKey = CryptoJS.enc.Utf8.parse(key); + + const encryptedData = CryptoJS.enc.Hex.parse(data); + + // 解密操作 + const decrypted = CryptoJS.AES.decrypt({ ciphertext: encryptedData }, newKey, { + mode: CryptoJS.mode.ECB, // ECB 模式 + padding: CryptoJS.pad.Pkcs7 // PKCS7 填充方式 + }); + // 将解密后的结果转为 UTF-8 字符串 + const decryptedText = decrypted.toString(CryptoJS.enc.Utf8); + return decryptedText; +} + +// md5 签名 +const md5 = (key: string, data: string, timestamp: string) => { + const text = "s" + key + data + timestamp; + return CryptoJS.MD5(text).toString(CryptoJS.enc.Hex); +} + +// 生成签名戳 +const getFormattedDate = () => { + const date = new Date(); + const year = date.getFullYear(); // 获取年份 (yyyy) + const month = String(date.getMonth() + 1).padStart(2, '0'); // 获取月份 (MM) + const day = String(date.getDate()).padStart(2, '0'); // 获取日期 (dd) + const hours = String(date.getHours()).padStart(2, '0'); // 获取小时 (HH) + return `es0${year}${month}${day}${hours}0es`; // 拼接成 yyyyMMddHH 格式 +} + +// 加密方法 +const preprocessData = (data: string) => { + console.log('data', data); + // YYYYMMDD + let timestamp = getFormattedDate() + console.log('timestamp', timestamp); + // 秒为单位的时间戳 + let timeSecond = parseInt((new Date().getTime() / 1000).toString()) + console.log('timeSecond', timeSecond); + // 数据的加密 + let encryptionData = encryptAESECB(data, timestamp) + console.log('encryptionData', encryptionData); + // md5签名方法 + let md5Data = md5(timestamp, encryptionData, timestamp) + console.log('md5Data', md5Data); + + let res = { + data: encryptionData, + timestamp: timeSecond, + sign: md5Data + } + console.log('res', res); + + return res +} + export default instance; diff --git a/yarn.lock b/yarn.lock index 815f994..af374f3 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4630,6 +4630,11 @@ crypto-browserify@^3.11.0: randombytes "^2.1.0" randomfill "^1.0.4" +crypto-js@^4.2.0: + version "4.2.0" + resolved "https://registry.npmmirror.com/crypto-js/-/crypto-js-4.2.0.tgz#4d931639ecdfd12ff80e8186dba6af2c2e856631" + integrity sha512-KALDyEYgpY+Rlob/iriUtjV6d5Eq+Y191A5g4UqLAi8CyGP9N1+FdVbkc1SxKc2r4YAYqG8JzO2KGL+AizD70Q== + css-blank-pseudo@^3.0.3: version "3.0.3" resolved "https://registry.npmmirror.com/css-blank-pseudo/-/css-blank-pseudo-3.0.3.tgz#36523b01c12a25d812df343a32c322d2a2324561"