popup-control.vue 12.2 KB
Newer Older
DCloud_JSON's avatar
DCloud_JSON 已提交
1
<template>
2
	<view v-if="isShow" :style="{opacity}" class="popup-control">
3
		<view class="control-mark" @touchstart.prevent="closeMe" @click="closeMe">
DCloud_JSON's avatar
DCloud_JSON 已提交
4
		</view>
5
		<view ref="content" class="content" :style="{top:controlData.top,left:controlData.left,right:controlData.right}">
DCloud_JSON's avatar
DCloud_JSON 已提交
6 7 8 9 10 11 12
      <template v-for="(item,index) in controlList">
        <view :key="index" class="control-item" v-if="typeof item.canDisplay == 'function' ? item.canDisplay() : item.canDisplay " @click="item.action">
        	<uni-im-icons v-if="item.icon" :code="item.icon" size="16" color="#FFF"></uni-im-icons>
        	<text class="control-item-text">{{item.title}}</text>
        </view>
      </template>
		</view>
13
    <view class="icon" :class="{isInTop:controlData.isInTop}" :style="{right:iconBoxRight,left:iconBoxLeft,top:controlData.top}"></view>
DCloud_JSON's avatar
DCloud_JSON 已提交
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
	</view>
</template>

<script>
	import uniIm from '@/uni_modules/uni-im/sdk/index.js';
	export default {
		data(){
			return {
        controlList:[],
				isShow:false,
				controlData: {
					top:'',
          bottom:'',
					left:'unset',
					right:'unset',
					width:'',
					msg:{},
          msgContentDomInfo:{},
					isInTop:false
				},
        opacity:0
			}
		},
		computed: {
			...uniIm.mapState(['isWidescreen']),
			iconBoxLeft(){
        if(this.controlData.left != 'unset'){
          const {left:mLeft,width:mWidth} = this.controlData.msgContentDomInfo
          return mLeft + mWidth/2 + 'px'
        }else{
          return ''
        }
			},
			iconBoxRight(){
        if(this.controlData.right != 'unset'){
          const {left:mLeft,right:mRight,width:mWidth} = this.controlData.msgContentDomInfo
          const metrics = uniIm.utils.getScreenMetrics()
          return metrics.pageWidth - mRight + mWidth/2 + 'px'
        }else{
          return ''
        }
			}
		},
		mounted() {},
		methods:{
      chooseMore(){
        this.$emit('chooseMore',[this.controlData.msg])
      },
      share(e){
        // 仅支持web pc端
64
        if(!this.isWidescreen){
DCloud_JSON's avatar
DCloud_JSON 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
            uni.showToast({
                title:'仅支持web pc端',
                icon:'none'
            })
            return
        }
        this.$emit('share',[this.controlData.msg])
        
      },
      initControlList(msg){
        this.controlList = [
          {
            title:'回复',
            action:()=>this.answer(),
            canDisplay:msg._id != undefined,// 只有发送成功的消息才能回复
          },
81 82 83
          {
            title:'复制',
            action:()=>this.copyContent(),
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
84
            canDisplay: (uniIm.systemInfo.uniPlatform === "web" && ["userinfo-card","rich-text","image"].includes(msg.type) || msg.type == 'text' ),
85
          },
DCloud_JSON's avatar
DCloud_JSON 已提交
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
          {
            title:'撤回',
            action:()=>this.revokeMsg(),
            canDisplay:this.canRevoke,
          },
          // {
          //   title:'删除',
          //   action:()=>this.deleteMsg(),
          //   canDisplay:msg._id != undefined, 
          // },
          {
            title:'转发',
            action:()=>this.share(),
            canDisplay:this.isWidescreen && msg._id != undefined,
          },
          {
            title:'多选',
            action:()=>this.chooseMore(),
            canDisplay:this.isWidescreen && msg._id != undefined,
          }
        ]
        
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
108 109 110 111 112 113 114 115
        if(msg.about_msg_id){
          this.controlList.push({
            title:'进入会话',
            action:()=> this.$emit('intoTopic', msg._id),
            canDisplay:true,
          })
        }
        
DCloud_JSON's avatar
DCloud_JSON 已提交
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
        // 拿到扩展点的数据
        let extensionsControlList = uniIm.extensions.invokeExts('msg-popup-controls',msg)
        this.controlList = this.controlList.concat(...extensionsControlList)
        
        this.controlList.map(item=>{
          const oldAction = item.action
          item.action = ()=>{
            this.isShow = false
            oldAction()
          }
        })
      },
      async show({isSelf,msg,msgContentDomInfo}){
        this.initControlList(msg)
        // 先显示出来,设置透明度为0,拿到宽度,再设置透明度为1
        this.opacity = 0
        this.controlData.msg = msg
        this.isShow = true
        await this.$nextTick()
        // #ifdef H5
        // 当蒙版弹出,鼠标右键就关闭msg-popup-control
137
        const popupControl = document.querySelector('.popup-control')
DCloud_JSON's avatar
DCloud_JSON 已提交
138
        popupControl.addEventListener('contextmenu',(e) => {
139
          if(this.isShow && this.isWidescreen){
DCloud_JSON's avatar
DCloud_JSON 已提交
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
            this.isShow = false
          }
          e.preventDefault()
        })
        // #endif
        
        const controlData = {
          msgContentDomInfo,
          msg,
          isInTop: false
        }
        
        const query = uni.createSelectorQuery().in(this);
        await new Promise(resolve => {
          query.selectAll('.content').boundingClientRect(data => {
            controlData.width = data[0].width + 'px'
            resolve()
          }).exec();
        })
        
        // console.error('controlData.width',controlData.width)
        
        let metrics = uniIm.utils.getScreenMetrics()
        if (isSelf) {
          controlData.left = 'unset'
          const metrics = uniIm.utils.getScreenMetrics() 
          // console.log('msgContentDomInfo',msgContentDomInfo)
          controlData.right = metrics.pageWidth - msgContentDomInfo.right + msgContentDomInfo.width/2 - parseInt(controlData.width)/2 + 'px'
        } else {
          controlData.left = msgContentDomInfo.left + msgContentDomInfo.width / 2 - parseInt(controlData.width)/2 + 'px'
          controlData.right = 'unset'
        }
        
        controlData.isInTop = msgContentDomInfo.top > 60
        if (controlData.isInTop) {
          // #ifdef H5
          let n = -20
          // #endif
          // #ifndef H5
          let n = -65
          // #endif
          controlData.top = msgContentDomInfo.top + n + 'px'
        } else {
          // #ifdef APP
          let n = 8
          // #endif
          // #ifdef H5
          let n = 55
          // #endif
          // #ifdef MP
          let n = 10
          // #endif
          controlData.top = msgContentDomInfo.bottom + n + 'px'
        }
        
        
        if(parseInt(controlData.right) < 60){
          controlData.right = '60px'
        }
        if(parseInt(controlData.left) < msgContentDomInfo.left){
          controlData.left = msgContentDomInfo.left + 'px'
        }
        
        // console.error('dddddddta',controlData)
				this.controlData = controlData
        this.$nextTick(()=>{
          this.opacity = 1
        })
			},
209
			async copyContent(){
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
        let data = ''// 需要复制的数据
        const msgBody = this.controlData.msg.body
        const action = {
          text(){
            data = msgBody
          },
          'userinfo-card'(){
            data = location.origin + '/#/?user_id='+msgBody.user_id
          },
          // #ifdef H5
          async image(){
            let url = msgBody.url
            url = await uniIm.utils.getTempFileURL(url)
            fetch(url).then(response => response.blob()).then(blob => {
              let mime = blob.type
              let file = new File([blob], mime.replace('/', '.'), { type: mime })
              let clipboardItem = new ClipboardItem({ [mime]: file })
              navigator.clipboard.write([clipboardItem])
            })
          },
          async 'rich-text'(){
            let html = ""
            for(let i of msgBody){
              if(i.name == 'img'){
                const url = await uniIm.utils.getTempFileURL(i.attrs.src)
                html += `<img src="${url}">`
              }else if(i.name == 'a'){
                html += `<a href="${i.attrs.href}">${i.children[0].text}</a>`
              }else{
                html += i.text
240
              }
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
            }
            data = html
            // 把data写到剪切板
            // 将HTML字符串转换为ArrayBuffer,确保UTF-8编码
            const encoder = new TextEncoder();
            const htmlArrayBuffer = encoder.encode(html);
            // 创建一个包含HTML内容的ClipboardItem
            const clipboardItem = new ClipboardItem({
              'text/html': new Blob([htmlArrayBuffer], { type: 'text/html' }),
            });
            data = clipboardItem
            // 尝试将ClipboardItem写入剪切板
            try {
              await navigator.clipboard.write([clipboardItem]);
              // console.log('HTML内容已写入剪切板');
            } catch (err) {
              console.error('写入剪切板失败: ', err);
            }
          }
          // #endif
261
        }
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
262
        await action[this.controlData.msg.type]?.()
DCloud_JSON's avatar
DCloud_JSON 已提交
263

DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
264 265 266 267 268 269 270 271 272 273 274
				if(typeof data === 'string'){
          uni.setClipboardData({
          	data,
          	complete:(e)=> {
          		uni.hideToast()
          		// console.log(e);
          	}
          })
        }else{
          // console.log('非字符串',data);
        }
DCloud_JSON's avatar
DCloud_JSON 已提交
275 276
			},
			canRevoke() {
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
277
				let current_uid = uniIm.currentUser._id
DCloud_JSON's avatar
DCloud_JSON 已提交
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
				let {group_id,from_uid,conversation_id,create_time} = this.controlData.msg || {}
				// console.log('create_time',create_time,'group_id',group_id);
				// console.log('from_uid current_uid',this.controlData.msg,from_uid,current_uid);
        // console.log('this.controlData.msg.state',this.controlData.msg);

        //自己发的,状态 不是发送成功不能撤回 (发送成功的数据才有_id)
        if(!this.controlData.msg._id){
          return false
        }
        
        if(this.uniIDHasRole('uni-im-admin')){
          // 如果是管理员
          return true
        }
        
				let isGroupAdmin = false
				if(group_id){
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
295 296
					let conversation = uniIm.conversation.find(conversation_id)
					isGroupAdmin = conversation.group.user_id == current_uid
DCloud_JSON's avatar
DCloud_JSON 已提交
297 298 299 300 301 302 303 304 305 306 307 308 309 310
				}
				// console.log('isGroupAdmin',isGroupAdmin);
				// 如果是群主
				if(isGroupAdmin){
					return true
				}
        
        // 以上都不是,这需要是消息的发送者,且消息创建时间小于2分钟
        return from_uid == current_uid && ( Date.now() - create_time < 1000*60*2 )
			},
			async revokeMsg(){
				// 再判断一遍防止,分钟在2分钟的时候右键了,然后到了第3分钟才点下的情况
				if(this.canRevoke()){
					const {conversation_id,_id:msg_id} = this.controlData.msg
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
311
          uniIm.conversation.find(conversation_id).revokeMsg(msg_id)
DCloud_JSON's avatar
DCloud_JSON 已提交
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
				}else{
					uni.showToast({
						title: '已超过2分钟,不能撤回',
						icon: 'none'
					});
				}
				// console.log('this.controlData.msg',this.controlData.msg);
			},
			async answer(){
				// console.log('answer')
				this.$emit('answer',this.controlData.msg._id)
				
			},
			async deleteMsg(){
				// #ifndef H5
				return this.other()
				// #endif
				this.controlData.msg.is_delete = true
				// 存到本地
				let conversation = await uniIm.conversation.get(this.controlData.msg.conversation_id)
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
332
				// conversation.msgManager.localMsg.update(this.controlData.msg.unique_id,this.controlData.msg)
DCloud_JSON's avatar
DCloud_JSON 已提交
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
				
			},
			other(){
				uni.showToast({
					title: '暂不支持',
					icon: 'none'
				});
			},
			closeMe(evt){
				// 触摸屏忽略 click 事件
				if (uniIm.isTouchable && evt.type === 'click') return
				this.isShow = false
			}
		}
	}
</script>

350 351 352 353 354 355 356 357 358 359 360 361
<style lang="scss">
.popup-control {
  .content{
  	background-color:#252a30;
  	height: 55px;
  	// width: 375rpx;
  	position:fixed;
  	top:0;
  	border-radius: 5px;
  	flex-direction: row;
  	justify-content: space-around;
  	align-items: center;
362
    z-index: 10;
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
  }
  .control-item{
  	padding: 0 15px;
  	justify-content: center;
  	align-items: center;
  }
  .control-item-text{
  	font-size: 12px;
  	color:#FFFFFF;
  	margin-top: 1px;
  	/* #ifdef H5 */
  	cursor: pointer;
  	/* #endif */
  }
  /* #ifdef H5 */
  .control-item-text:hover{
  	color:#c9e7ff;
  }
  /* #endif */
  .control-mark{
  	position: fixed;
  	top: 0;
  	left: 0;
  	width: 100vw !important;
  	height: 100vh !important;
  	z-index: 9;
  	background-color: rgba(0,0,0,0.1);
  }
  
  .icon {
DCloud_JSON's avatar
DCloud_JSON 已提交
393
    position: fixed;
394
  	transform:translate(0,-5px) rotate(45deg);
DCloud_JSON's avatar
DCloud_JSON 已提交
395 396 397
    background-color: #252a30;
    width: 10px;
    height: 10px;
398
    z-index: 9;
399
  }
DCloud_JSON's avatar
DCloud_JSON 已提交
400 401 402 403 404 405 406 407 408 409 410 411
  
  .isInTop{
  	transform:translate(0,50px) rotate(45deg);
  }
  
  /* #ifdef H5 */
  @media screen and (min-device-width:960px){
  	.control-mark {
  		background-color: rgba(255,255,255,0.05);
  	}
  }
  /* #endif */
412
}
DCloud_JSON's avatar
DCloud_JSON 已提交
413
</style>