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

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