index.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <template>
  2. <transition name="msgbox-fade">
  3. <div class="pub-wrapper" v-if="visible">
  4. <div class="pub-mask"></div>
  5. <div class="pub-modal avater-modal">
  6. <div class="modal-hd">
  7. <div class="title">
  8. {{isMe ? '编辑头像' : '编辑群头像' }}
  9. </div>
  10. <i class="el-icon-close" @click="visible = false"></i>
  11. </div>
  12. <div class="modal-bd">
  13. <el-upload
  14. class="avatar-uploader"
  15. :action="uploadUrl"
  16. :show-file-list="false"
  17. :data="uploadData"
  18. :on-success="handleAvatarSuccess"
  19. name="cover_photo"
  20. :before-upload="beforeAvatarUpload">
  21. <img v-if="imageUrl" :src="imageUrl" class="avatar">
  22. <i v-else class="el-icon-plus avatar-uploader-icon"></i>
  23. </el-upload>
  24. </div>
  25. <div class="modal-fd">
  26. <!-- <el-button type="primary">确认</el-button> -->
  27. <el-button @click="visible = false">取消</el-button>
  28. </div>
  29. </div>
  30. </div>
  31. </transition>
  32. </template>
  33. <script>
  34. import { Button, Upload, Message } from 'element-ui'
  35. import Vue from 'vue'
  36. Vue.component(Button.name, Button)
  37. Vue.component(Upload.name, Upload)
  38. Vue.component(Message.name, Message)
  39. export default {
  40. name: 'avatarPopup',
  41. data () {
  42. return {
  43. }
  44. },
  45. computed: {
  46. uploadUrl () {
  47. let host = ''
  48. if (window.location.port === '8080') {
  49. host = '//test.mee.chat/'
  50. }
  51. return this.isMe ? `${host}user/changePhoto` : `${host}group/changeCover`
  52. },
  53. uploadData () {
  54. let authData = {
  55. user_id: this.$store.state.userId,
  56. token: this.$store.state.token
  57. }
  58. return this.isMe
  59. ? authData
  60. : Object.assign(authData, { group_id: this.$store.state.curSession })
  61. }
  62. },
  63. methods: {
  64. handleAvatarSuccess (res, file) {
  65. this.imageUrl = URL.createObjectURL(file.raw)
  66. this.$showTips('上传成功')
  67. if (this.isMe) {
  68. this.$store.commit('updateUserPhoto', this.imageUrl)
  69. this.$store.commit('updateMemberAvatar', {
  70. userId: this.$store.state.userId,
  71. imageUrl: this.imageUrl
  72. })
  73. } else {
  74. this.$store.commit('updateGroup', {
  75. key: 'coverPhoto',
  76. data: this.imageUrl
  77. })
  78. }
  79. },
  80. beforeAvatarUpload (file) {
  81. const isJPG = file.type === 'image/jpeg'
  82. const isLt1M = file.size / 1024 / 1024 < 1
  83. if (!isJPG || !isLt1M) {
  84. this.$showTips('请上传1M以内的JPG格式图片')
  85. }
  86. return isJPG && isLt1M
  87. }
  88. },
  89. created () {
  90. }
  91. }
  92. </script>
  93. <style lang="scss">
  94. @import "./style.scss";
  95. </style>