完成前端个人信息头像上传

This commit is contained in:
qujay
2022-10-17 00:13:29 +08:00
parent 72f2af6faa
commit d59dfda1f6
7 changed files with 52 additions and 48 deletions
+5 -1
View File
@@ -6,7 +6,7 @@
* For details, please see
* https://pro.ant.design/docs/deploy
*/
export default {
export default {
dev: {
'/api-uaa/': {
target: 'http://127.0.0.1:9900',
@@ -28,6 +28,10 @@ export default {
target: 'http://127.0.0.1:9900',
changeOrigin: true,
},
'/api-file/': {
target: 'http://127.0.0.1:9900',
changeOrigin: true,
},
},
test: {
'/api/': {
@@ -82,23 +82,6 @@
icon: 'smile',
component: './Welcome',
},
{
path: '/admin',
name: 'admin',
icon: 'crown',
access: 'canAdmin',
routes: [
{
path: '/admin/sub-page',
name: 'sub-page',
icon: 'smile',
component: './Welcome',
},
{
component: './404',
},
],
},
{
path: '/',
redirect: '/welcome',
+2 -2
View File
@@ -1,9 +1,9 @@
/**
* @see https://umijs.org/zh-CN/plugins/plugin-access
* */
export default function access(initialState: { currentUser?: API.CurrentUser } | undefined) {
export default function access(initialState: { currentUser?: API.CurrentUser } | undefined) {
const { currentUser } = initialState ?? {};
return {
canAdmin: currentUser && currentUser.access === 'admin',
canAdmin: currentUser ,
};
}
@@ -1,11 +1,10 @@
import { currentUser2 } from '@/services/login/api';
import { importImage, importUser, saveOrUpdateUser } from '@/services/system/api';
import { importImage, saveOrUpdateUser } from '@/services/system/api';
import { UploadOutlined } from '@ant-design/icons';
import type { ProFormInstance } from '@ant-design/pro-components';
import { PageContainer, ProFormText, ProForm, ProFormRadio } from '@ant-design/pro-components';
import { Button, message, Upload } from 'antd';
import { RcFile } from 'antd/lib/upload';
import React, { useRef } from 'react';
import type { RcFile } from 'antd/lib/upload';
import React, { useRef, useState } from 'react';
import { useModel } from 'umi';
import styles from './index.less';
@@ -35,18 +34,16 @@ const handleImport = async (file: RcFile) => {
formData.append('file', file);
const result = await importImage(formData);
hide();
if (result.resp_code === 0) {
message.success('上传头像成功');
} else {
message.error(result.resp_msg);
}
message.success('上传头像成功');
return result;
} catch (error) {
hide();
message.error('上传头像失败');
return null;
}
};
const AvatarView = ({ avatar }: { avatar: string }) => (
const AvatarUpload = ({ avatar, setImage }: { avatar: string, setImage: (headImgUrl: string) => void }) => (
<>
<div className={styles.avatar_title}>头像</div>
<div className={styles.avatar}>
@@ -57,7 +54,10 @@ const AvatarView = ({ avatar }: { avatar: string }) => (
action="/api-file/files-anon"
showUploadList={false}
beforeUpload={async (file) => {
await handleImport(file);
const result = await handleImport(file);
if(result && result.url) {
setImage(result.url);
}
return false;
}}
>
@@ -72,20 +72,27 @@ const AvatarView = ({ avatar }: { avatar: string }) => (
);
const UserInfo: React.FC = () => {
const formRef = useRef<ProFormInstance<API.CurrentUser>>();
const formRef = useRef<ProFormInstance<SYSTEM.User>>();
const { initialState } = useModel('@@initialState');
const { currentUser } = initialState;
const { currentUser } = initialState ?? {};
const [user, setUser] = useState<SYSTEM.User>({...currentUser});
const setImage = (headImgUrl: string) => {
setUser({...user, headImgUrl});
}
if (!initialState || !currentUser) {
return null;
}
formRef.current?.setFieldsValue(user);
return (
<PageContainer header={{ subTitle: '更新个人信息' }}>
<div className={styles.baseView}>
<div className={styles.left}>
<ProForm<API.CurrentUser>
<ProForm<SYSTEM.User>
formRef={formRef}
//layout="horizontal"
request={currentUser2}
onFinish={async (values) => {
const user = { ...currentUser, ...values };
await handleEdit(user);
const saveUser = { ...user, ...values };
await handleEdit(saveUser);
message.success('修改个人信息成功');
}}
// labelCol={{ span: 8 }}
@@ -134,7 +141,7 @@ const UserInfo: React.FC = () => {
</ProForm>
</div>
<div className={styles.right}>
<AvatarView avatar={currentUser.headImgUrl} />
{user.headImgUrl && <AvatarUpload avatar={user.headImgUrl} setImage={setImage}/>}
</div>
</div>
</PageContainer>
+2 -7
View File
@@ -14,13 +14,8 @@ declare namespace API {
datas?: T;
};
type CurrentUser = {
userId?: string;
username?: string;
nickname?: string;
headImgUrl?: string;
sex?: number;
type?: string;
type CurrentUser = SYSTEM.User & {
permissions: string[];
};
type Menu = {
@@ -70,7 +70,7 @@ export async function importUser(formData: FormData) {
}
export async function importImage(formData: FormData) {
const result = await request<API.Result<void>>("/api-file/files-anon", {
const result = await request<SYSTEM.FileInfo>("/api-file/files-anon", {
method: 'POST',
requestType: 'form',
data: formData,
+17 -2
View File
@@ -18,7 +18,7 @@ declare namespace SYSTEM {
};
type User = {
id: number;
id?: number;
username?: string;
nickname?: string;
headImgUrl?: string;
@@ -26,10 +26,11 @@ declare namespace SYSTEM {
type?: string;
mobile?: string;
createTime?: Date;
enabled: boolean;
enabled?: boolean;
del?: boolean;
roles?: Role[];
roleId?: string;
headImgUrl?: string;
};
type Menu = {
@@ -87,4 +88,18 @@ declare namespace SYSTEM {
tableComment?: string;
tableName?: string;
};
type FileInfo = {
id: number;
name?: string;
isImg: boolean;
url?: string;
contentType?: string;
size?: number;
path?: string;
source?: string;
createTime?: string;
tenantId?: string;
updateTime?: string;
}
}