index.tsx 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. import { postForm } from '@/request';
  2. import * as api from '@/services/login';
  3. import { message } from '@/utils/antdAppInstance';
  4. import { userKey } from '@/utils/authUtils';
  5. import { secureLocalStorage as ls } from '@/utils/localUtils';
  6. import {
  7. CloseOutlined,
  8. EditOutlined,
  9. LoadingOutlined,
  10. PlusOutlined,
  11. SaveOutlined,
  12. } from '@ant-design/icons';
  13. import { ProFormText } from '@ant-design/pro-components';
  14. import { useModel } from '@umijs/max';
  15. import { Avatar, Button, Card, Col, Form, Row, Space, Upload } from 'antd';
  16. import React, { useEffect, useState } from 'react';
  17. const UserInfo: React.FC = () => {
  18. const { initialState, setInitialState } = useModel('@@initialState');
  19. const [form] = Form.useForm();
  20. const [isEditing, setIsEditing] = useState(false);
  21. const [loading, setLoading] = useState(false);
  22. const [uploading, setUploading] = useState(false);
  23. const [userInfo, setUserInfo] = useState<API.UserInfo | null>(null);
  24. useEffect(() => {
  25. if (initialState?.currentUser) {
  26. setUserInfo(initialState.currentUser);
  27. form.setFieldsValue({
  28. username: initialState.currentUser.username,
  29. nickname: initialState.currentUser.nickname,
  30. avatar: initialState.currentUser.avatar,
  31. email: initialState.currentUser.email,
  32. phone: initialState.currentUser.phone,
  33. });
  34. }
  35. }, [initialState?.currentUser, form]);
  36. const handleEdit = () => {
  37. setIsEditing(true);
  38. };
  39. const handleCancel = () => {
  40. setIsEditing(false);
  41. if (userInfo) {
  42. form.setFieldsValue({
  43. username: userInfo.username,
  44. nickname: userInfo.nickname,
  45. avatar: userInfo.avatar,
  46. email: userInfo.email,
  47. phone: userInfo.phone,
  48. });
  49. }
  50. };
  51. const handleSave = async () => {
  52. try {
  53. const values = await form.validateFields();
  54. setLoading(true);
  55. const result = await api.fetchUserUpdateInfo({
  56. username: values.username,
  57. nickname: values.nickname,
  58. avatar: values.avatar,
  59. email: values.email,
  60. phone: values.phone,
  61. });
  62. if (result.success) {
  63. message.success('修改成功');
  64. setIsEditing(false);
  65. setInitialState((prev) => ({
  66. ...prev,
  67. currentUser: {
  68. ...prev?.currentUser,
  69. ...values,
  70. },
  71. }));
  72. const localUserInfo = ls.getLocal<API.UserInfo>(userKey);
  73. const newUserInfo = {
  74. ...localUserInfo,
  75. ...values,
  76. };
  77. ls.setLocal<API.UserInfo>(userKey, newUserInfo);
  78. setUserInfo(newUserInfo);
  79. } else {
  80. message.error(result.errorMessage || '修改失败');
  81. }
  82. } catch (error) {
  83. message.error('修改失败');
  84. } finally {
  85. setLoading(false);
  86. }
  87. };
  88. const handleAvatarUpload = async (options: any) => {
  89. const { file } = options;
  90. const isImage = file.type.startsWith('image/');
  91. if (!isImage) {
  92. message.error('只能上传图片文件!');
  93. return;
  94. }
  95. if (file.size / 1024 / 1024 > 1) {
  96. message.error('头像大小不能超过1MB');
  97. return;
  98. }
  99. setUploading(true);
  100. try {
  101. const formData = new FormData();
  102. formData.append('file', file);
  103. formData.append('fileType', 'avatar');
  104. const res = await postForm<API.Result<{ url: string; path: string }>>(
  105. '/minio/upload',
  106. formData,
  107. );
  108. if (res.success && res.data) {
  109. form.setFieldValue('avatar', res.data.path);
  110. setUserInfo((prev) => (prev ? { ...prev, avatar: res.data!.url } : prev));
  111. message.success('上传成功');
  112. } else {
  113. message.error(res.errorMessage || '上传失败');
  114. }
  115. } catch {
  116. message.error('上传失败');
  117. } finally {
  118. setUploading(false);
  119. }
  120. };
  121. if (!userInfo) {
  122. return <div>加载中...</div>;
  123. }
  124. return (
  125. <div style={{ padding: '24px' }}>
  126. <Card title="用户信息" style={{ maxWidth: 800, margin: '0 auto' }}>
  127. <Form form={form} layout="vertical">
  128. <Row gutter={24}>
  129. <Col span={12}>
  130. <ProFormText
  131. name="username"
  132. label="用户名"
  133. readonly={!isEditing}
  134. rules={[{ required: true, message: '请输入用户名' }]}
  135. formItemProps={{ layout: 'horizontal' }}
  136. fieldProps={{ disabled: true }}
  137. />
  138. <ProFormText
  139. name="nickname"
  140. label="昵称"
  141. readonly={!isEditing}
  142. rules={[{ required: true, message: '请输入昵称' }]}
  143. formItemProps={{ layout: 'horizontal' }}
  144. placeholder="请输入昵称"
  145. />
  146. <ProFormText
  147. name="email"
  148. label="邮箱"
  149. readonly={!isEditing}
  150. rules={[{ type: 'email', message: '请输入正确的邮箱格式' }]}
  151. formItemProps={{ layout: 'horizontal' }}
  152. placeholder="请输入邮箱"
  153. />
  154. <ProFormText
  155. name="phone"
  156. label="电话"
  157. readonly={!isEditing}
  158. formItemProps={{ layout: 'horizontal' }}
  159. placeholder="请输入电话"
  160. />
  161. </Col>
  162. <Col span={12}>
  163. <Form.Item name="avatar" hidden />
  164. <div style={{ textAlign: 'center' }}>
  165. <div style={{ marginBottom: 16 }}>
  166. <Avatar
  167. size={120}
  168. src={userInfo.avatar}
  169. style={{ border: '2px solid #f0f0f0' }}
  170. />
  171. </div>
  172. {isEditing && (
  173. <Upload
  174. showUploadList={false}
  175. customRequest={handleAvatarUpload}
  176. accept="image/*"
  177. >
  178. <Button icon={uploading ? <LoadingOutlined /> : <PlusOutlined />}>
  179. 上传头像
  180. </Button>
  181. </Upload>
  182. )}
  183. </div>
  184. </Col>
  185. </Row>
  186. </Form>
  187. <div style={{ textAlign: 'center', marginTop: 24 }}>
  188. {!isEditing ? (
  189. <Button type="primary" icon={<EditOutlined />} onClick={handleEdit}>
  190. 修改信息
  191. </Button>
  192. ) : (
  193. <Space>
  194. <Button type="primary" icon={<SaveOutlined />} onClick={handleSave} loading={loading}>
  195. 确定修改
  196. </Button>
  197. <Button icon={<CloseOutlined />} onClick={handleCancel}>
  198. 取消
  199. </Button>
  200. </Space>
  201. )}
  202. </div>
  203. </Card>
  204. </div>
  205. );
  206. };
  207. export default UserInfo;