userAvatar.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <template>
  2. <div>
  3. <div class="user-info-head" @click="editCropper()"><img v-bind:src="options.img" title="点击上传头像" class="img-circle img-lg" /></div>
  4. <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body @opened="modalOpened" @close="closeDialog()">
  5. <el-row>
  6. <el-col :xs="24" :md="12" :style="{height: '350px'}">
  7. <vue-cropper
  8. ref="cropper"
  9. :img="options.img"
  10. :info="true"
  11. :autoCrop="options.autoCrop"
  12. :autoCropWidth="options.autoCropWidth"
  13. :autoCropHeight="options.autoCropHeight"
  14. :fixedBox="options.fixedBox"
  15. @realTime="realTime"
  16. v-if="visible"
  17. />
  18. </el-col>
  19. <el-col :xs="24" :md="12" :style="{height: '350px'}">
  20. <div class="avatar-upload-preview">
  21. <img :src="previews.url" :style="previews.img" />
  22. </div>
  23. </el-col>
  24. </el-row>
  25. <br />
  26. <el-row>
  27. <el-col :lg="2" :md="2">
  28. <el-upload action="#" :http-request="requestUpload" :show-file-list="false" :before-upload="beforeUpload">
  29. <el-button size="small">
  30. 选择
  31. <i class="el-icon-upload el-icon--right"></i>
  32. </el-button>
  33. </el-upload>
  34. </el-col>
  35. <el-col :lg="{span: 1, offset: 2}" :md="2">
  36. <el-button icon="el-icon-plus" size="small" @click="changeScale(1)"></el-button>
  37. </el-col>
  38. <el-col :lg="{span: 1, offset: 1}" :md="2">
  39. <el-button icon="el-icon-minus" size="small" @click="changeScale(-1)"></el-button>
  40. </el-col>
  41. <el-col :lg="{span: 1, offset: 1}" :md="2">
  42. <el-button icon="el-icon-refresh-left" size="small" @click="rotateLeft()"></el-button>
  43. </el-col>
  44. <el-col :lg="{span: 1, offset: 1}" :md="2">
  45. <el-button icon="el-icon-refresh-right" size="small" @click="rotateRight()"></el-button>
  46. </el-col>
  47. <el-col :lg="{span: 2, offset: 6}" :md="2">
  48. <el-button type="primary" size="small" @click="uploadImg()">提 交</el-button>
  49. </el-col>
  50. </el-row>
  51. </el-dialog>
  52. </div>
  53. </template>
  54. <script>
  55. import store from "@/store";
  56. import { VueCropper } from "vue-cropper";
  57. import { uploadAvatar } from "@/api/system/user";
  58. import { publicFileGetUrl} from '@/api/common'
  59. export default {
  60. components: { VueCropper },
  61. props: {
  62. user: {
  63. type: Object
  64. }
  65. },
  66. data() {
  67. return {
  68. // 是否显示弹出层
  69. open: false,
  70. // 是否显示cropper
  71. visible: false,
  72. // 弹出层标题
  73. title: "修改头像",
  74. options: {
  75. img: store.getters.avatar, //裁剪图片的地址
  76. autoCrop: true, // 是否默认生成截图框
  77. autoCropWidth: 200, // 默认生成截图框宽度
  78. autoCropHeight: 200, // 默认生成截图框高度
  79. fixedBox: true // 固定截图框大小 不允许改变
  80. },
  81. previews: {}
  82. };
  83. },
  84. computed: {
  85. publicFileGetUrl() {
  86. return publicFileGetUrl
  87. }
  88. },
  89. methods: {
  90. // 编辑头像
  91. editCropper() {
  92. this.open = true;
  93. },
  94. // 打开弹出层结束时的回调
  95. modalOpened() {
  96. this.visible = true;
  97. let _this = this;
  98. // 设置头像base64
  99. // 其中this.avatar为当前头像
  100. this.setAvatarBase64(this.options.img, (base64) => {
  101. _this.options.img = base64;
  102. });
  103. },
  104. // 覆盖默认的上传行为
  105. requestUpload() {
  106. },
  107. // 向左旋转
  108. rotateLeft() {
  109. this.$refs.cropper.rotateLeft();
  110. },
  111. // 向右旋转
  112. rotateRight() {
  113. this.$refs.cropper.rotateRight();
  114. },
  115. // 图片缩放
  116. changeScale(num) {
  117. num = num || 1;
  118. this.$refs.cropper.changeScale(num);
  119. },
  120. // 上传预处理
  121. beforeUpload(file) {
  122. if (file.type.indexOf("image/") == -1) {
  123. this.msgError("文件格式错误,请上传图片类型,如:JPG,PNG后缀的文件。");
  124. } else {
  125. const reader = new FileReader();
  126. reader.readAsDataURL(file);
  127. reader.onload = () => {
  128. this.options.img = reader.result;
  129. };
  130. }
  131. },
  132. // 上传图片
  133. uploadImg() {
  134. this.$refs.cropper.getCropBlob(data => {
  135. let formData = new FormData();
  136. formData.append("avatarfile", data);
  137. uploadAvatar(formData).then(response => {
  138. this.open = false;
  139. this.options.img = this.publicFileGetUrl + response.imgUrl;
  140. store.commit('SET_AVATAR', this.options.img);
  141. this.msgSuccess("修改成功");
  142. this.visible = false;
  143. });
  144. });
  145. },
  146. // 实时预览
  147. realTime(data) {
  148. this.previews = data;
  149. },
  150. // 关闭窗口
  151. closeDialog() {
  152. this.options.img = store.getters.avatar
  153. this.visible = false;
  154. },
  155. // 设置头像base64
  156. setAvatarBase64(src, callback) {
  157. // console.log("setAvatarBase64 src == "+src)
  158. let _this = this;
  159. let image = new Image();
  160. // 处理缓存
  161. image.src = src;
  162. // 支持跨域图片
  163. image.crossOrigin = "*";
  164. image.onload = function () {
  165. let base64 = _this.transBase64FromImage(image);
  166. // console.log("setAvatarBase64 base64 == "+base64)
  167. callback && callback(base64);
  168. }
  169. },
  170. // 将网络图片转换成base64格式
  171. transBase64FromImage(image) {
  172. let canvas = document.createElement("canvas");
  173. canvas.width = image.width;
  174. canvas.height = image.height;
  175. let ctx = canvas.getContext("2d");
  176. ctx.drawImage(image, 0, 0, image.width, image.height);
  177. // 可选其他值 image/jpeg
  178. return canvas.toDataURL("image/png");
  179. }
  180. }
  181. };
  182. </script>
  183. <style scoped lang="scss">
  184. .user-info-head {
  185. position: relative;
  186. display: inline-block;
  187. height: 120px;
  188. }
  189. .user-info-head:hover:after {
  190. content: '+';
  191. position: absolute;
  192. left: 0;
  193. right: 0;
  194. top: 0;
  195. bottom: 0;
  196. color: #eee;
  197. background: rgba(0, 0, 0, 0.5);
  198. font-size: 24px;
  199. font-style: normal;
  200. -webkit-font-smoothing: antialiased;
  201. -moz-osx-font-smoothing: grayscale;
  202. cursor: pointer;
  203. line-height: 110px;
  204. border-radius: 50%;
  205. }
  206. </style>