AvatarModal.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <template>
  2. <a-modal
  3. title="修改头像"
  4. :visible="visible"
  5. :maskClosable="false"
  6. :confirmLoading="confirmLoading"
  7. :width="800"
  8. :footer="null"
  9. @cancel="cancelHandel">
  10. <a-row>
  11. <a-col :xs="24" :md="12" :style="{height: '350px'}">
  12. <vue-cropper
  13. ref="cropper"
  14. :img="options.img"
  15. :info="true"
  16. :autoCrop="options.autoCrop"
  17. :autoCropWidth="options.autoCropWidth"
  18. :autoCropHeight="options.autoCropHeight"
  19. :fixedBox="options.fixedBox"
  20. @realTime="realTime"
  21. >
  22. </vue-cropper>
  23. </a-col>
  24. <a-col :xs="24" :md="12" :style="{height: '350px'}">
  25. <div class="avatar-upload-preview">
  26. <img :src="previews.url" :style="previews.img"/>
  27. </div>
  28. </a-col>
  29. </a-row>
  30. <br>
  31. <a-row>
  32. <a-col :lg="2" :md="2">
  33. <a-upload name="file" :beforeUpload="beforeUpload" :showUploadList="false">
  34. <a-button icon="upload">选择图片</a-button>
  35. </a-upload>
  36. </a-col>
  37. <a-col :lg="{span: 1, offset: 2}" :md="2">
  38. <a-button icon="plus" @click="changeScale(1)"/>
  39. </a-col>
  40. <a-col :lg="{span: 1, offset: 1}" :md="2">
  41. <a-button icon="minus" @click="changeScale(-1)"/>
  42. </a-col>
  43. <a-col :lg="{span: 1, offset: 1}" :md="2">
  44. <a-button icon="undo" @click="rotateLeft"/>
  45. </a-col>
  46. <a-col :lg="{span: 1, offset: 1}" :md="2">
  47. <a-button icon="redo" @click="rotateRight"/>
  48. </a-col>
  49. <a-col :lg="{span: 2, offset: 6}" :md="2">
  50. <a-button type="primary" @click="finish('blob')" :loading="uploading">保存</a-button>
  51. </a-col>
  52. </a-row>
  53. </a-modal>
  54. </template>
  55. <script>
  56. import { sysFileInfoUpload } from '@/api/modular/system/fileManage'
  57. import { sysUserUpdateAvatar } from '@/api/modular/system/userManage'
  58. export default {
  59. data () {
  60. return {
  61. visible: false,
  62. id: null,
  63. confirmLoading: false,
  64. fileList: [],
  65. uploading: false,
  66. options: {
  67. img: '',
  68. autoCrop: true,
  69. autoCropWidth: 200,
  70. autoCropHeight: 200,
  71. fixedBox: true
  72. },
  73. previews: {}
  74. }
  75. },
  76. methods: {
  77. edit (id) {
  78. this.visible = true
  79. this.id = id
  80. /* 获取原始头像 */
  81. },
  82. close () {
  83. this.id = null
  84. this.visible = false
  85. },
  86. cancelHandel () {
  87. this.close()
  88. },
  89. changeScale (num) {
  90. num = num || 1
  91. this.$refs.cropper.changeScale(num)
  92. },
  93. rotateLeft () {
  94. this.$refs.cropper.rotateLeft()
  95. },
  96. rotateRight () {
  97. this.$refs.cropper.rotateRight()
  98. },
  99. beforeUpload (file) {
  100. this.fileList = file
  101. const reader = new FileReader()
  102. // 把Array Buffer转化为blob 如果是base64不需要
  103. // 转化为base64
  104. reader.readAsDataURL(file)
  105. reader.onload = () => {
  106. this.options.img = reader.result
  107. }
  108. // 转化为blob
  109. // reader.readAsArrayBuffer(file)
  110. return false
  111. },
  112. // 上传图片(点击上传按钮)
  113. finish (type) {
  114. if (type === 'blob') {
  115. this.uploading = true
  116. this.$refs.cropper.getCropBlob((data) => {
  117. const files = new window.File(
  118. [data],
  119. this.fileList.name,
  120. { type: this.fileList.type }
  121. )
  122. const formData = new FormData()
  123. formData.append('file', files)
  124. sysFileInfoUpload(formData).then((res) => {
  125. if (res.success) {
  126. this.updateAvatar(res.data)
  127. this.$emit('ok', res.data)
  128. } else {
  129. this.uploading = false
  130. this.$message.error(res.message)
  131. }
  132. })
  133. })
  134. } else {
  135. this.$refs.cropper.getCropData((data) => {
  136. console.log(data)
  137. })
  138. }
  139. },
  140. updateAvatar (data) {
  141. const params = {
  142. id: this.id,
  143. avatar: data
  144. }
  145. sysUserUpdateAvatar(params).then((res) => {
  146. this.uploading = false
  147. if (res.success) {
  148. this.visible = false
  149. this.$message.success('头像上传修改成功')
  150. } else {
  151. this.$message.error(res.message)
  152. }
  153. })
  154. },
  155. realTime (data) {
  156. this.previews = data
  157. }
  158. }
  159. }
  160. </script>
  161. <style lang="less" scoped>
  162. .avatar-upload-preview {
  163. position: absolute;
  164. top: 50%;
  165. transform: translate(50%, -50%);
  166. width: 200px;
  167. height: 200px;
  168. border-radius: 50%;
  169. box-shadow: 0 0 4px #ccc;
  170. overflow: hidden;
  171. img {
  172. width: 100%;
  173. height: 100%;
  174. }
  175. }
  176. </style>