index.tsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import React, { GetDerivedStateFromProps } from 'react'
  2. import Avatar from '../Avatar'
  3. import { Upload, message as Message, Button } from 'antd'
  4. const styles = require('./UploadAvatar.less')
  5. import api from 'utils/api'
  6. import { setToken, getToken } from 'utils/request'
  7. interface IUploadAvatar {
  8. id?: number
  9. callback?: (path: string) => any
  10. }
  11. interface IUploadAvatarProps {
  12. type: string
  13. path: string
  14. xhrParams: IUploadAvatar
  15. }
  16. interface IUploadAvatarState {
  17. currentPath: string
  18. }
  19. export class UploadAvatar extends React.PureComponent<
  20. IUploadAvatarProps,
  21. IUploadAvatarState
  22. > {
  23. constructor(props) {
  24. super(props)
  25. this.state = {
  26. currentPath: ''
  27. }
  28. }
  29. private beforeUpload = (file) => {
  30. const re = /image\/(png|jpg|jpeg|gif)/
  31. const isJPG = re.test(file.type)
  32. if (!isJPG) {
  33. Message.error('You can only upload JPG file!')
  34. }
  35. const isLt1MB = file.size / 1024 / 1024 > 1
  36. if (isLt1MB) {
  37. Message.error('Image must smaller than 1MB!')
  38. }
  39. return !!(isJPG && !isLt1MB)
  40. }
  41. private handleChange = (info) => {
  42. const { xhrParams } = this.props
  43. if (info.file.status === 'done') {
  44. const response = info.file.response
  45. if (response && response.header && response.header.code >= 200) {
  46. const avatar = response.payload.avatar
  47. const token = response.header.token
  48. if (xhrParams && typeof xhrParams.callback === 'function') {
  49. xhrParams.callback(avatar)
  50. }
  51. setToken(token)
  52. }
  53. }
  54. }
  55. public static getDerivedStateFromProps: GetDerivedStateFromProps<
  56. IUploadAvatarProps,
  57. IUploadAvatarState
  58. > = (nextProps, prevState) => {
  59. const { path } = nextProps
  60. if (path && path.length) {
  61. return { currentPath: path }
  62. }
  63. }
  64. public render() {
  65. const { type, xhrParams } = this.props
  66. const { currentPath } = this.state
  67. const TOKEN = { Authorization: getToken() }
  68. let action = ''
  69. if (type === 'profile') {
  70. if (xhrParams && xhrParams.id) {
  71. action = `${api.user}/${xhrParams.id}/avatar`
  72. }
  73. } else if (type === 'organization') {
  74. if (xhrParams && xhrParams.id) {
  75. action = `${api.organizations}/${xhrParams.id}/avatar`
  76. }
  77. }
  78. return (
  79. <div className={styles.avatar}>
  80. <Avatar path={currentPath} size="large" enlarge={true} />
  81. <div className={styles.uploadAvatar}>
  82. <div className={styles.uploadTitle}>上传新图像</div>
  83. <Upload
  84. name="file"
  85. showUploadList={false}
  86. headers={TOKEN}
  87. action={action}
  88. beforeUpload={this.beforeUpload}
  89. onChange={this.handleChange}
  90. >
  91. <Button size="large">选择图片</Button>
  92. </Upload>
  93. <p className={styles.uploadDesc}>图片大小不超过1MB</p>
  94. </div>
  95. </div>
  96. )
  97. }
  98. }
  99. export default UploadAvatar