feat: 测试

This commit is contained in:
luchang 2024-05-31 13:37:52 +08:00
parent 47941ad9c0
commit 40b3be8a8d
13 changed files with 70 additions and 169 deletions

View File

@ -3,7 +3,7 @@ import { defineConfig } from '@umijs/max';
export default defineConfig({
define: {
'process.env': {
API_HOST_URL: 'http://localhost:4399',
API_HOST_URL: 'http://127.0.0.1:4399',
},
},
});

View File

@ -3,7 +3,7 @@ import { defineConfig } from '@umijs/max';
export default defineConfig({
define: {
'process.env': {
API_HOST_URL: 'http://localhost:4399',
API_HOST_URL: 'https://api.easy-trip.world/api',
},
},
});

View File

@ -15,7 +15,7 @@ const Settings: ProLayoutProps & {
fixedHeader: false,
fixSiderbar: true,
colorWeak: false,
title: '依',
title: '依',
pwa: true,
logo: 'https://polaris-frontend.oss-cn-shanghai.aliyuncs.com/yitu_image/logo.png',
iconfontUrl: '',

View File

@ -9,7 +9,7 @@
*
* @doc https://umijs.org/docs/guides/proxy
*/
const { API_HOST_URL = 'localhost:3000' } = process.env;
const { API_HOST_URL = 'localhost:4399' } = process.env;
export default {
// 如果需要自定义本地开发服务器 请取消注释按需调整
dev: {
@ -23,5 +23,4 @@ export default {
pathRewrite: { '^': '' },
},
},
};

View File

@ -22,9 +22,7 @@ export async function getInitialState(): Promise<{
}> {
const fetchUserInfo = async () => {
try {
const msg = await queryCurrentUser({
skipErrorHandler: true,
});
const msg = await queryCurrentUser();
return msg.data;
} catch (error) {
// history.push(loginPath);
@ -33,14 +31,14 @@ export async function getInitialState(): Promise<{
};
// 如果不是登录页面,执行
const { location } = history;
// if (location.pathname !== loginPath) {
// const currentUser = await fetchUserInfo();
// return {
// fetchUserInfo,
// currentUser,
// settings: defaultSettings as Partial<LayoutSettings>,
// };
// }
if (location.pathname !== loginPath) {
const currentUser = await fetchUserInfo();
return {
fetchUserInfo,
currentUser,
settings: defaultSettings as Partial<LayoutSettings>,
};
}
return {
fetchUserInfo,
settings: defaultSettings as Partial<LayoutSettings>,
@ -55,8 +53,7 @@ export const layout: RunTimeLayoutConfig = ({ initialState, setInitialState }) =
src: initialState?.currentUser?.avatar,
title: <AvatarName />,
render: (_, avatarChildren) => {
return <>admin</>
// return <AvatarDropdown>{avatarChildren}</AvatarDropdown>;
return <AvatarDropdown>{avatarChildren}</AvatarDropdown>;
},
},
waterMarkProps: {
@ -66,9 +63,9 @@ export const layout: RunTimeLayoutConfig = ({ initialState, setInitialState }) =
onPageChange: () => {
const { location } = history;
// 如果没有登录,重定向到 login
// if (!initialState?.currentUser && location.pathname !== loginPath) {
// history.push(loginPath);
// }
if (!initialState?.currentUser && location.pathname !== loginPath) {
history.push(loginPath);
}
},
bgLayoutImgList: [
{
@ -92,10 +89,10 @@ export const layout: RunTimeLayoutConfig = ({ initialState, setInitialState }) =
],
links: isDev
? [
<Link key="openapi" to="/umi/plugin/openapi" target="_blank">
<LinkOutlined />
<span>OpenAPI </span>
</Link>,
// <Link key="openapi" to="/umi/plugin/openapi" target="_blank">
// <LinkOutlined />
// <span>OpenAPI 文档</span>
// </Link>,
]
: [],
menuHeaderRender: undefined,

View File

@ -21,16 +21,16 @@ const FormUpload: React.FC<IFormUpload> = ({ onChange }) => {
const [imageUrl, setImageUrl] = useState('');
const handleUpload = async (info: any) => {
if (info.file.status === 'uploading') {
setLoading(true);
return;
}
if (info.file.status === 'done') {
getBase64(info.file.originFileObj as FileType, (url) => {
setLoading(false);
setImageUrl(url);
});
}
// if (info.file.status === 'uploading') {
// setLoading(true);
// return;
// }
// if (info.file.status === 'done') {
getBase64(info.file.originFileObj as FileType, (url) => {
setLoading(false);
setImageUrl(url);
});
// }
onChange?.(info);
};
@ -58,6 +58,9 @@ const FormUpload: React.FC<IFormUpload> = ({ onChange }) => {
className={`avatar-uploader uploadBtn`}
showUploadList={false}
onChange={handleUpload}
customRequest={(file: any) => {
console.log('>>>>>测试', file);
}}
>
{uploadButton}
</Upload>

View File

@ -17,7 +17,7 @@ export type GlobalHeaderRightProps = {
export const AvatarName = () => {
const { initialState } = useModel('@@initialState');
const { currentUser } = initialState || {};
return <span className="anticon">{currentUser?.name}</span>;
return <span className="anticon">{currentUser?.username}</span>;
};
const useStyles = createStyles(({ token }) => {
@ -43,7 +43,6 @@ export const AvatarDropdown: React.FC<GlobalHeaderRightProps> = ({ menu, childre
* 退 url
*/
const loginOut = async () => {
await outLogin();
const { search, pathname } = window.location;
const urlParams = new URL(window.location.href).searchParams;
/** 此方法会跳转到 redirect 参数所在的位置 */
@ -95,7 +94,7 @@ export const AvatarDropdown: React.FC<GlobalHeaderRightProps> = ({ menu, childre
const { currentUser } = initialState;
if (!currentUser || !currentUser.name) {
if (!currentUser || !currentUser.username) {
return loading;
}

View File

@ -5,9 +5,9 @@ export default {
'pages.login.accountLogin.errorMessage': 'Incorrect username/password(admin/ant.design)',
'pages.login.failure': 'Login failed, please try again!',
'pages.login.success': 'Login successful!',
'pages.login.username.placeholder': 'Username: admin or user',
'pages.login.username.placeholder': 'Username: admin',
'pages.login.username.required': 'Please input your username!',
'pages.login.password.placeholder': 'Password: ant.design',
'pages.login.password.placeholder': 'Password',
'pages.login.password.required': 'Please input your password!',
'pages.login.phoneLogin.tab': 'Phone Login',
'pages.login.phoneLogin.errorMessage': 'Verification Code Error',

View File

@ -4,9 +4,9 @@ export default {
'pages.login.accountLogin.errorMessage': '错误的用户名和密码(admin/ant.design)',
'pages.login.failure': '登录失败,请重试!',
'pages.login.success': '登录成功!',
'pages.login.username.placeholder': '用户名: admin or user',
'pages.login.username.placeholder': '用户名: admin',
'pages.login.username.required': '用户名是必填项!',
'pages.login.password.placeholder': '密码: ant.design',
'pages.login.password.placeholder': '密码',
'pages.login.password.required': '密码是必填项!',
'pages.login.phoneLogin.tab': '手机号登录',
'pages.login.phoneLogin.errorMessage': '验证码错误',

View File

@ -91,18 +91,19 @@ const Login: React.FC = () => {
const handleSubmit = async (values: API.LoginParams) => {
try {
// 登录
const msg = await login({ ...values, type });
if (msg.status === 'ok') {
const result = await login({ ...values, type });
if (result.code === 0) {
const defaultLoginSuccessMessage = intl.formatMessage({
id: 'pages.login.success',
defaultMessage: '登录成功!',
});
message.success(defaultLoginSuccessMessage);
// await fetchUserInfo();
localStorage.setItem('token', result.data.token);
await fetchUserInfo();
history.push('/userList');
return;
}
console.log(msg);
console.log(result);
// 如果失败去设置用户错误信息
setUserLoginState(msg);
} catch (error) {
@ -151,28 +152,6 @@ const Login: React.FC = () => {
await handleSubmit(values as API.LoginParams);
}}
>
{/* <Tabs
activeKey={type}
onChange={setType}
centered
items={[
{
key: 'account',
label: intl.formatMessage({
id: 'pages.login.accountLogin.tab',
defaultMessage: '账户密码登录',
}),
},
{
key: 'mobile',
label: intl.formatMessage({
id: 'pages.login.phoneLogin.tab',
defaultMessage: '手机号登录',
}),
},
]}
/> */}
{status === 'error' && loginType === 'account' && (
<LoginMessage
content={intl.formatMessage({
@ -191,7 +170,7 @@ const Login: React.FC = () => {
}}
placeholder={intl.formatMessage({
id: 'pages.login.username.placeholder',
defaultMessage: '用户名: admin or user',
defaultMessage: '请输入用户名!',
})}
rules={[
{
@ -213,7 +192,7 @@ const Login: React.FC = () => {
}}
placeholder={intl.formatMessage({
id: 'pages.login.password.placeholder',
defaultMessage: '密码: ant.design',
defaultMessage: '请输入密码',
})}
rules={[
{
@ -229,103 +208,18 @@ const Login: React.FC = () => {
/>
</>
)}
{/* {status === 'error' && loginType === 'mobile' && <LoginMessage content="验证码错误" />}
{type === 'mobile' && (
<>
<ProFormText
fieldProps={{
size: 'large',
prefix: <MobileOutlined />,
}}
name="mobile"
placeholder={intl.formatMessage({
id: 'pages.login.phoneNumber.placeholder',
defaultMessage: '手机号',
})}
rules={[
{
required: true,
message: (
<FormattedMessage
id="pages.login.phoneNumber.required"
defaultMessage="请输入手机号!"
/>
),
},
{
pattern: /^1\d{10}$/,
message: (
<FormattedMessage
id="pages.login.phoneNumber.invalid"
defaultMessage="手机号格式错误!"
/>
),
},
]}
/>
<ProFormCaptcha
fieldProps={{
size: 'large',
prefix: <LockOutlined />,
}}
captchaProps={{
size: 'large',
}}
placeholder={intl.formatMessage({
id: 'pages.login.captcha.placeholder',
defaultMessage: '请输入验证码',
})}
captchaTextRender={(timing, count) => {
if (timing) {
return `${count} ${intl.formatMessage({
id: 'pages.getCaptchaSecondText',
defaultMessage: '获取验证码',
})}`;
}
return intl.formatMessage({
id: 'pages.login.phoneLogin.getVerificationCode',
defaultMessage: '获取验证码',
});
}}
name="captcha"
rules={[
{
required: true,
message: (
<FormattedMessage
id="pages.login.captcha.required"
defaultMessage="请输入验证码!"
/>
),
},
]}
onGetCaptcha={async (phone) => {
const result = await getFakeCaptcha({
phone,
});
if (!result) {
return;
}
message.success('获取验证码成功验证码为1234');
}}
/>
</>
)} */}
{/* <div
{/*
<div
style={{
marginBottom: 24,
}}
>
<ProFormCheckbox noStyle name="autoLogin">
<FormattedMessage id="pages.login.rememberMe" defaultMessage="自动登录" />
</ProFormCheckbox>
<a
style={{
float: 'right',
}}
>
<FormattedMessage id="pages.login.forgotPassword" defaultMessage="忘记密码" />
<FormattedMessage id="pages.login.registerAccount" defaultMessage="注册账号" />
</a>
</div> */}
</LoginForm>

View File

@ -110,6 +110,7 @@ const Welcome: React.FC = () => {
scroll={{
x: 960,
}}
recordCreatorProps={false}
loading={false}
columns={columns}
request={request}

View File

@ -1,5 +1,5 @@
import type { RequestOptions } from '@@/plugin-request/request';
import type { RequestConfig } from '@umijs/max';
import type { RequestConfig, history } from '@umijs/max';
import { message, notification } from 'antd';
// 错误处理方案: 错误类型
@ -32,6 +32,7 @@ export const errorConfig: RequestConfig = {
errorThrower: (res) => {
const { success, data, errorCode, errorMessage, showType } =
res as unknown as ResponseStructure;
if (!success) {
const error: any = new Error(errorMessage);
error.name = 'BizError';
@ -75,6 +76,9 @@ export const errorConfig: RequestConfig = {
// Axios 的错误
// 请求成功发出且服务器也响应了状态码,但状态代码超出了 2xx 的范围
message.error(`Response status:${error.response.status}`);
if (error.response.status === 401) {
window.location.href = '/user/login';
}
} else if (error.request) {
// 请求已经成功发起,但没有收到响应
// \`error.request\` 在浏览器中是 XMLHttpRequest 的实例,
@ -92,6 +96,10 @@ export const errorConfig: RequestConfig = {
(config: RequestOptions) => {
// 拦截请求配置,进行个性化处理。
const url = config?.url?.concat();
const token = localStorage.getItem('token');
if (token && config.headers) {
config.headers.Authorization = `Bearer ${token}`;
}
return { ...config, url };
},
],

View File

@ -6,7 +6,7 @@ import { request } from '@umijs/max';
export async function currentUser(options?: { [key: string]: any }) {
return request<{
data: API.CurrentUser;
}>('/api/currentUser', {
}>('/auth/user', {
method: 'GET',
...(options || {}),
});
@ -22,7 +22,7 @@ export async function outLogin(options?: { [key: string]: any }) {
/** 登录接口 POST /api/login/account */
export async function login(body: API.LoginParams, options?: { [key: string]: any }) {
return request<API.LoginResult>('/api/user/login', {
return request<API.LoginResult>('/auth/login', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
@ -34,7 +34,7 @@ export async function login(body: API.LoginParams, options?: { [key: string]: an
/** 获取用户列表 */
export async function getUserList(options?: { [key: string]: any }) {
return request<any>('/api/user', {
return request<any>('/user', {
method: 'GET',
...(options || {}),
});
@ -42,7 +42,7 @@ export async function getUserList(options?: { [key: string]: any }) {
/** 编辑用户 */
export async function updateUser(body: any, options?: { [key: string]: any }) {
return request<any>('/api/user/' + body?.id, {
return request<any>('/user/' + body?.id, {
method: 'PATCH',
data: body,
...(options || {}),
@ -51,7 +51,7 @@ export async function updateUser(body: any, options?: { [key: string]: any }) {
/** 删除用户 */
export async function deleteUser(options?: { [key: string]: any }) {
return request<any>('/api/user/' + options?.id, {
return request<any>('/user/' + options?.id, {
method: 'DELETE',
...(options || {}),
});
@ -59,7 +59,7 @@ export async function deleteUser(options?: { [key: string]: any }) {
/** 获取oss签名 */
export async function getSignature(options?: { [key: string]: any }) {
return request<any>('/api/signature', {
return request<any>('/signature', {
method: 'GET',
...(options || {}),
});
@ -75,7 +75,7 @@ export async function upload(url: string, formData: any) {
/** 上传banner图 */
export async function uploadBanner(body: any, options?: { [key: string]: any }) {
return request<any>('/api/banner', {
return request<any>('/banner', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
@ -87,7 +87,7 @@ export async function uploadBanner(body: any, options?: { [key: string]: any })
/** 删除 */
export async function getBanners(options?: { [key: string]: any }) {
return request<any>('/api/banner', {
return request<any>('/banner', {
method: 'GET',
...(options || {}),
});
@ -95,7 +95,7 @@ export async function getBanners(options?: { [key: string]: any }) {
/** 删除 */
export async function deleteBanner(id: number) {
return request<any>('/api/banner/' + id, {
return request<any>('/banner/' + id, {
method: 'DELETE',
headers: {
'Content-Type': 'application/json',
@ -105,7 +105,7 @@ export async function deleteBanner(id: number) {
/** 此处后端没有提供注释 GET /api/notices */
export async function getNotices(options?: { [key: string]: any }) {
return request<API.NoticeIconList>('/api/notices', {
return request<API.NoticeIconList>('/notices', {
method: 'GET',
...(options || {}),
});