SettingAccount.tsx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import {
  2. ProForm,
  3. ProFormText,
  4. ProFormUploadButton,
  5. } from "@ant-design/pro-components";
  6. import { message } from "antd";
  7. import { useAppSelector } from "../../../hooks";
  8. import { currentUser } from "../../../reducers/current-user";
  9. import { API_HOST, delete_, get, put } from "../../../request";
  10. import type { IUserRequest, IUserResponse } from "../../../api/Auth";
  11. import type { UploadChangeParam, UploadFile } from "antd/es/upload/interface";
  12. import { get as getToken } from "../../../reducers/current-user";
  13. import type { IAttachmentResponse } from "../../../api/Attachments";
  14. import { useIntl } from "react-intl";
  15. import type { IDeleteResponse } from "../../../api/Group";
  16. interface IAccount {
  17. id: string;
  18. realName: string;
  19. nickName: string;
  20. email: string;
  21. avatar?: UploadFile<IAttachmentResponse>[];
  22. }
  23. const SettingAccountWidget = () => {
  24. const user = useAppSelector(currentUser);
  25. const intl = useIntl();
  26. return (
  27. <ProForm<IAccount>
  28. onFinish={async (values: IAccount) => {
  29. console.log(values);
  30. let _avatar: string = "";
  31. if (
  32. typeof values.avatar === "undefined" ||
  33. values.avatar.length === 0
  34. ) {
  35. _avatar = "";
  36. } else if (typeof values.avatar[0].response === "undefined") {
  37. _avatar = values.avatar[0].uid;
  38. } else {
  39. console.debug("upload ", values.avatar[0].response);
  40. _avatar = values.avatar[0].response.data.name;
  41. }
  42. const url = `/v2/user/${user?.id}`;
  43. const postData = {
  44. nickName: values.nickName,
  45. avatar: _avatar,
  46. email: values.email,
  47. };
  48. console.log("account put ", url, postData);
  49. const res = await put<IUserRequest, IUserResponse>(url, postData);
  50. if (res.ok) {
  51. message.success(intl.formatMessage({ id: "flashes.success" }));
  52. } else {
  53. message.error(res.message);
  54. }
  55. }}
  56. params={{}}
  57. request={async () => {
  58. const url = `/v2/user/${user?.id}`;
  59. console.log("url", url);
  60. const res = await get<IUserResponse>(url);
  61. if (res.ok) {
  62. }
  63. return {
  64. id: res.data.id,
  65. realName: res.data.userName,
  66. nickName: res.data.nickName,
  67. email: res.data.email,
  68. avatar:
  69. res.data.avatar && res.data.avatarName
  70. ? [
  71. {
  72. uid: res.data.avatarName,
  73. name: "avatar",
  74. thumbUrl: res.data.avatar,
  75. },
  76. ]
  77. : [],
  78. };
  79. }}
  80. >
  81. <ProFormUploadButton
  82. name="avatar"
  83. label="头像"
  84. max={1}
  85. fieldProps={{
  86. name: "file",
  87. listType: "picture-card",
  88. className: "avatar-uploader",
  89. headers: {
  90. Authorization: `Bearer ${getToken()}`,
  91. },
  92. onRemove: (file: UploadFile<any>): boolean => {
  93. console.log("remove", file);
  94. const url = `/v2/attachment/1?name=${file.uid}`;
  95. console.info("avatar delete url", url);
  96. delete_<IDeleteResponse>(url)
  97. .then((json) => {
  98. if (json.ok) {
  99. message.success("删除成功");
  100. } else {
  101. message.error(json.message);
  102. }
  103. })
  104. .catch((e) => console.log("Oops errors!", e));
  105. return true;
  106. },
  107. }}
  108. action={`${API_HOST}/api/v2/attachment?type=avatar`}
  109. extra="必须为正方形。最大512*512"
  110. onChange={(_info: UploadChangeParam<UploadFile<any>>) => {}}
  111. />
  112. <ProFormText
  113. width="md"
  114. readonly
  115. name="realName"
  116. label="realName"
  117. tooltip="最长为 24 位"
  118. />
  119. <ProFormText width="md" name="nickName" label="nickName" />
  120. <ProFormText readonly name="email" width="md" label="email" />
  121. </ProForm>
  122. );
  123. };
  124. export default SettingAccountWidget;