index.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 ? $t('userinfo.editAvatar') : $t('userinfo.editGroupAvatar') }}
  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. accept="image/*"
  16. :action="uploadUrl"
  17. :show-file-list="false"
  18. :data="uploadData"
  19. :on-success="handleAvatarSuccess"
  20. name="cover_photo"
  21. :before-upload="beforeAvatarUpload">
  22. <img v-if="imageUrl" :src="imageUrl" class="avatar">
  23. <i v-else class="el-icon-plus avatar-uploader-icon"></i>
  24. </el-upload>
  25. </div>
  26. <div class="modal-fd">
  27. <!-- <el-button type="primary">确认</el-button> -->
  28. <el-button @click="visible = false">{{$t('public.cancel')}}</el-button>
  29. </div>
  30. </div>
  31. </div>
  32. </transition>
  33. </template>
  34. <script>
  35. import { Button, Upload, Message } from 'element-ui'
  36. import Vue from 'vue'
  37. Vue.component(Button.name, Button)
  38. Vue.component(Upload.name, Upload)
  39. Vue.component(Message.name, Message)
  40. export default {
  41. name: 'avatarPopup',
  42. data () {
  43. return {
  44. }
  45. },
  46. computed: {
  47. uploadUrl () {
  48. let host = ''
  49. if (window.location.port === '8080') {
  50. host = '//test.mee.chat/'
  51. }
  52. return this.isMe ? `${host}user/changePhoto` : `${host}group/changeCover`
  53. },
  54. uploadData () {
  55. let authData = {
  56. user_id: this.$store.state.userId,
  57. token: this.$store.state.token
  58. }
  59. return this.isMe
  60. ? authData
  61. : Object.assign(authData, { group_id: this.$store.state.curSession })
  62. }
  63. },
  64. methods: {
  65. handleAvatarSuccess (res, file) {
  66. this.imageUrl = URL.createObjectURL(file.raw)
  67. this.$showTips(this.$t('userinfo.uploadSucc'))
  68. if (this.isMe) {
  69. // 用户头像
  70. this.$store.commit('updateUserPhoto', this.imageUrl)
  71. this.$store.commit('updateMemberAvatar', {
  72. userId: this.$store.state.userId,
  73. imageUrl: this.imageUrl
  74. })
  75. } else {
  76. // 群头像
  77. this.$store.dispatch('updateSessionItem', {
  78. sessionId: this.$store.state.curSession,
  79. data: {
  80. cover_photo: this.imageUrl
  81. }
  82. })
  83. this.$store.commit('updateGroup', {
  84. key: 'coverPhoto',
  85. data: this.imageUrl
  86. })
  87. console.log('群头像update')
  88. }
  89. },
  90. beforeAvatarUpload (file) {
  91. const isIMG = file.type.indexOf('image') > -1
  92. const isLt1M = file.size / 1024 / 1024 < 2
  93. if (!isIMG || !isLt1M) {
  94. this.$showTips(this.$t('userinfo.maxUploadTips'))
  95. }
  96. return isIMG && isLt1M
  97. }
  98. },
  99. created () {
  100. }
  101. }
  102. </script>
  103. <style lang="scss">
  104. @import "./style.scss";
  105. </style>