index.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. // 用户头像
  69. this.$store.commit('updateUserPhoto', this.imageUrl)
  70. this.$store.commit('updateMemberAvatar', {
  71. userId: this.$store.state.userId,
  72. imageUrl: this.imageUrl
  73. })
  74. } else {
  75. // 群头像
  76. this.$store.dispatch('updateSessionItem', {
  77. sessionId: this.$store.state.curSession,
  78. data: {
  79. cover_photo: this.imageUrl
  80. }
  81. })
  82. this.$store.commit('updateGroup', {
  83. key: 'coverPhoto',
  84. data: this.imageUrl
  85. })
  86. }
  87. },
  88. beforeAvatarUpload (file) {
  89. const isJPG = file.type === 'image/jpeg'
  90. const isLt1M = file.size / 1024 / 1024 < 1
  91. if (!isJPG || !isLt1M) {
  92. this.$showTips('请上传1M以内的JPG格式图片')
  93. }
  94. return isJPG && isLt1M
  95. }
  96. },
  97. created () {
  98. }
  99. }
  100. </script>
  101. <style lang="scss">
  102. @import "./style.scss";
  103. </style>