index.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <template>
  2. <transition name="msgbox-fade">
  3. <div class="pub-wrapper" v-if="visible">
  4. <div class="pub-mask" @click="visible = false"></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. let userId = localStorage.getItem('user_id')
  50. let token = localStorage.getItem('token')
  51. if (window.location.port === '8080') {
  52. host = '//test.mee.chat/'
  53. }
  54. return this.isMe ? `${host}user/changePhoto?user_id=${userId}&token=${token}` : `${host}group/changeCover?user_id=${userId}&token=${token}`
  55. },
  56. uploadData () {
  57. let authData = {
  58. user_id: this.$store.state.userId,
  59. token: this.$store.state.token
  60. }
  61. return this.isMe
  62. ? authData
  63. : Object.assign(authData, { group_id: this.$store.state.curSession })
  64. }
  65. },
  66. methods: {
  67. handleAvatarSuccess (res, file) {
  68. this.imageUrl = URL.createObjectURL(file.raw)
  69. this.$showTips(this.$t('public.uploadSucc'))
  70. if (this.isMe) {
  71. // 用户头像
  72. this.$store.commit('updateUserPhoto', this.imageUrl)
  73. this.$store.commit('updateMemberInfo', {
  74. userId: this.$store.state.userId,
  75. imageUrl: this.imageUrl
  76. })
  77. } else {
  78. // 群头像
  79. this.$store.dispatch('updateSessionItem', {
  80. sessionId: this.$store.state.curSession,
  81. data: {
  82. cover_photo: this.imageUrl
  83. }
  84. })
  85. this.$store.commit('updateGroup', {
  86. key: 'coverPhoto',
  87. data: this.imageUrl
  88. })
  89. }
  90. },
  91. beforeAvatarUpload (file) {
  92. const isIMG = file.type.indexOf('image') > -1
  93. const isLt1M = file.size / 1024 / 1024 < 2
  94. if (!isIMG || !isLt1M) {
  95. this.$showTips(this.$t('userinfo.maxUploadTips'))
  96. }
  97. return isIMG && isLt1M
  98. }
  99. },
  100. created () {
  101. }
  102. }
  103. </script>
  104. <style lang="scss" scoped>
  105. @import "./style.scss";
  106. </style>