avatarUpload.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <template>
  2. <div class="components-container">
  3. <pan-thumb :image="image" style="margin:auto; display: block;">头像</pan-thumb>
  4. <div style="text-align: center; padding:30px;">
  5. <el-button icon="upload" style="" @click="closeOPen">取消
  6. </el-button>
  7. <el-button type="primary" icon="upload" style="" @click="imagecropperShow=true">修改头像
  8. </el-button>
  9. </div>
  10. <image-cropper field="file" :width="300" :height="300" :url="'http://'+'integralsys.oss-cn-shenzhen.aliyuncs.com'" @close='close' @crop-upload-success="cropSuccess" langType="zh"
  11. :key="imagecropperKey" v-show="imagecropperShow"></image-cropper>
  12. </div>
  13. </template>
  14. <script>
  15. import ImageCropper from '@/components/ImageCropper'
  16. import PanThumb from '@/components/PanThumb'
  17. export default {
  18. name: 'avatarUpload-demo',
  19. components: { ImageCropper, PanThumb },
  20. data() {
  21. let image = this.$store.getters.user_info.img_url?this.$store.getters.user_info.img_url:this.serverdomain+'static/images/head_default.png'
  22. return {
  23. imagecropperShow: false,
  24. imagecropperKey: 0,
  25. image: image
  26. }
  27. },
  28. methods: {
  29. closeOPen() {
  30. this.$emit('send_success');
  31. },
  32. sendAvatarRequest: function (url) {
  33. let params = {
  34. employee_id: this.$store.getters.user_info.id,
  35. url: url
  36. }
  37. var self = this;
  38. this.$http('post','/integral.php/verify/update_avatar',params,'','',
  39. {transformRequest: [(data) => {
  40. let paramArray = new Array();
  41. for (let it in data) {
  42. paramArray.push(encodeURIComponent(it) + '=' + encodeURIComponent(data[it]));
  43. }
  44. return paramArray.join("&");
  45. }]},
  46. // headers: {
  47. // 'Content-Type': 'application/x-www-form-urlencoded'
  48. // }
  49. ).then(function (response) {
  50. let message = "";
  51. let status = 0;
  52. if (response.status == 200) {
  53. let data = response.data;
  54. status = isNaN(data.status) ? 0 : parseInt(data.status);
  55. switch (status) {
  56. case 1:
  57. message = "修改成功";
  58. break;
  59. case -1:
  60. message = "表单参数错误";
  61. break;
  62. default:
  63. message = data.info ? data.info : "未知错误";
  64. }
  65. } else {
  66. message = "服务器出现问题";
  67. }
  68. self.$message({
  69. message: message,
  70. type: status == 1 ? 'success' : 'error'
  71. });
  72. }).catch(function (error) {
  73. console.log(error);
  74. });
  75. },
  76. cropSuccess(resData) {
  77. console.log(resData)
  78. this.imagecropperShow = false
  79. this.imagecropperKey = this.imagecropperKey + 1
  80. this.image = resData.url
  81. this.$store.getters.user_info.img_url = this.image
  82. //更新头像
  83. this.sendAvatarRequest(this.$store.getters.user_info.img_url)
  84. },
  85. close() {
  86. this.imagecropperShow = false
  87. }
  88. },
  89. created(){
  90. this.$store.dispatch('setLanguage', 'zh')
  91. }
  92. }
  93. </script>
  94. <style scoped>
  95. .avatar{
  96. width: 200px;
  97. height: 200px;
  98. border-radius: 50%;
  99. }
  100. </style>