#
xjj
2024-02-27 c3181d170002530b0ff3fe882c536d61500cc484
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import {
  ProForm,
  ProFormText,
  ProFormTextArea,
} from '@ant-design/pro-components';
import { Button, Input, message, Upload } from 'antd';
import React from 'react';
import useStyles from './index.style';
import Http from '@/utils/http';
 
import defaultAvat from '/public/img/defaultAva.png'
 
const handleUpdate = async (val) => {
  const hide = message.loading('正在更新');
  try {
    const resp = await Http.doPost('api/user/update', val);
    if (resp.code === 200) {
      message.success('更新成功');
      return true;
    } else {
      message.error(resp.msg);
      return false;
    }
  } catch (error) {
    message.error('配置失败请重试!');
    return false;
  } finally {
    hide();
  }
};
 
const BaseView = () => {
  const { styles } = useStyles();
  const [loading, setLoading] = React.useState(false);
  const [currentUser, setCurrentUser] = React.useState({});
 
  const AvatarView = ({ avatar }) => (
    <>
      <div className={styles.avatar_title}>头像</div>
      <div className={styles.avatar}>
        <img src={avatar} alt="avatar" />
      </div>
      <Upload showUploadList={false}>
        <div className={styles.button_view}>
          {/* <Button>
            <UploadOutlined />
            更换头像
          </Button> */}
        </div>
      </Upload>
    </>
  );
 
  const queryCurrent = () => {
    setLoading(true);
    Http.doGetPromise('api/auth/user', {}, (res) => {
      setLoading(false);
      setCurrentUser(res.data);
    }).catch((err) => {
      console.error(err);
      setLoading(false);
    })
  }
 
  React.useEffect(() => {
    queryCurrent();
  }, []);
 
  const getAvatarURL = () => {
    if (currentUser) {
      if (currentUser.avatar) {
        return currentUser.avatar;
      }
      const url = { defaultAvat };
      return url;
    }
    return defaultAvat;
  };
 
  const handleFinish = async () => {
    handleUpdate();
  };
 
  return (
    <div className={styles.baseView}>
      {loading ? null : (
        <>
          <div className={styles.left}>
            <ProForm
              layout="vertical"
              onFinish={handleFinish}
              submitter={{
                searchConfig: {
                  submitText: '更新基本信息',
                },
                render: (_, dom) => dom[1],
              }}
              initialValues={{
                ...currentUser,
                // phone: currentUser?.phone.split('-'),
              }}
              hideRequiredMark
            >
              <ProFormText
                width="md"
                name="email"
                label="邮箱"
                rules={[
                  {
                    required: true,
                    message: '请输入您的邮箱!',
                  },
                ]}
              />
              <ProFormText
                width="md"
                name="name"
                label="昵称"
                rules={[
                  {
                    required: true,
                    message: '请输入您的昵称!',
                  },
                ]}
              />
              <ProFormTextArea
                name="profile"
                label="个人简介"
                rules={[
                  {
                    required: true,
                    message: '请输入个人简介!',
                  },
                ]}
                placeholder="个人简介"
              />
 
              <ProFormText
                width="md"
                name="address"
                label="地址"
                rules={[
                  {
                    required: true,
                    message: '请输入您的地址!',
                  },
                ]}
              />
            </ProForm>
          </div>
          <div className={styles.right}>
            <AvatarView avatar={getAvatarURL()} />
          </div>
        </>
      )}
    </div>
  );
};
 
export default BaseView;