123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- <template>
- <transition name="msgbox-fade">
- <div class="pub-wrapper" v-if="visible">
- <div class="pub-mask"></div>
- <div class="pub-modal avater-modal">
- <div class="modal-hd">
- <div class="title">
- {{isMe ? '编辑头像' : '编辑群头像' }}
- </div>
- <i class="el-icon-close" @click="visible = false"></i>
- </div>
- <div class="modal-bd">
- <el-upload
- class="avatar-uploader"
- :action="uploadUrl"
- :show-file-list="false"
- :data="uploadData"
- :on-success="handleAvatarSuccess"
- name="cover_photo"
- :before-upload="beforeAvatarUpload">
- <img v-if="imageUrl" :src="imageUrl" class="avatar">
- <i v-else class="el-icon-plus avatar-uploader-icon"></i>
- </el-upload>
- </div>
- <div class="modal-fd">
- <!-- <el-button type="primary">确认</el-button> -->
- <el-button @click="visible = false">取消</el-button>
- </div>
- </div>
- </div>
- </transition>
- </template>
- <script>
- import { Button, Upload, Message } from 'element-ui'
- import Vue from 'vue'
- Vue.component(Button.name, Button)
- Vue.component(Upload.name, Upload)
- Vue.component(Message.name, Message)
- export default {
- name: 'avatarPopup',
- data () {
- return {
- }
- },
- computed: {
- uploadUrl () {
- let host = ''
- if (window.location.port === '8080') {
- host = '//test.mee.chat/'
- }
- return this.isMe ? `${host}user/changePhoto` : `${host}group/changeCover`
- },
- uploadData () {
- let authData = {
- user_id: this.$store.state.userId,
- token: this.$store.state.token
- }
- return this.isMe
- ? authData
- : Object.assign(authData, { group_id: this.$store.state.curSession })
- }
- },
- methods: {
- handleAvatarSuccess (res, file) {
- this.imageUrl = URL.createObjectURL(file.raw)
- this.$showTips('上传成功')
- if (this.isMe) {
- // 用户头像
- this.$store.commit('updateUserPhoto', this.imageUrl)
- this.$store.commit('updateMemberAvatar', {
- userId: this.$store.state.userId,
- imageUrl: this.imageUrl
- })
- } else {
- // 群头像
- this.$store.dispatch('updateSessionItem', {
- sessionId: this.$store.state.curSession,
- data: {
- cover_photo: this.imageUrl
- }
- })
- this.$store.commit('updateGroup', {
- key: 'coverPhoto',
- data: this.imageUrl
- })
- }
- },
- beforeAvatarUpload (file) {
- const isJPG = file.type === 'image/jpeg'
- const isLt1M = file.size / 1024 / 1024 < 1
- if (!isJPG || !isLt1M) {
- this.$showTips('请上传1M以内的JPG格式图片')
- }
- return isJPG && isLt1M
- }
- },
- created () {
- }
- }
- </script>
- <style lang="scss">
- @import "./style.scss";
- </style>
|