privateChatG.vue 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844
  1. <template>
  2. <view class="chatInterface" @contextmenu.prevent="" @click="chatBox">
  3. <view class="scroll-view">
  4. <image v-if="history.loading" class="history-loaded" src="/static/images/loading.svg"/>
  5. <view v-else :class="history.allLoaded ? 'history-loaded':'load'" @click="loadHistoryMessage(false)">
  6. <view>{{ history.allLoaded ? '已经没有更多的历史消息' : '点击获取历史消息' }}</view>
  7. </view>
  8. <checkbox-group @change="selectMessages">
  9. <!--消息记录-->
  10. <view v-for="(message,index) in history.messages" :key="message.messageId">
  11. <!--时间显示,类似于微信,隔5分钟不发言,才显示时间-->
  12. <view class="time-lag">
  13. {{ renderMessageDate(message, index) }}
  14. </view>
  15. <view class="message-recalled" v-if="message.recalled">
  16. <view v-if="message.recaller.id === currentUser.userId" class="message-recalled-self">
  17. <view>你撤回了一条消息</view>
  18. <span v-if="message.type === 'text' && Date.now()-message.timestamp< 60 * 1000 "
  19. @click="editRecalledMessage(message.payload.text)">重新编辑</span>
  20. </view>
  21. <view v-else>{{ message.recaller.data.name }}撤回了一条消息</view>
  22. </view>
  23. <view class="message-item" v-else>
  24. <view class="message-item-checkbox">
  25. <checkbox v-show="messageSelector.visible && message.status !== 'sending'" :value="message.messageId"
  26. :checked="messageSelector.messages.includes(message)"/>
  27. </view>
  28. <view class="message-item-content" :class="{'self' : message.senderId === currentUser.userId}">
  29. <view class="avatar">
  30. <!-- <image :src="message.senderId === currentUser.id? currentUser.avatar : friend.avatar"></image> -->
  31. <image v-if="message.senderId === currentUser.userId" src="/static/img/pic_def_ava@2x.png"></image>
  32. <image v-else src="/static/img/yonghu.png"></image>
  33. </view>
  34. <view class="content" @click.right="showActionPopup(message)" @longpress="showActionPopup(message)">
  35. <view class="message-payload">
  36. <b class="pending" v-if="message.status === 'sending'"></b>
  37. <b class="send-fail" v-if="message.status === 'fail'"></b>
  38. <view v-if="message.type === 'text'" class="text-content" v-html="renderTextMessage(message)"></view>
  39. <image v-if="message.type === 'image'"
  40. :data-url="message.payload.url"
  41. :src="message.payload.thumbnail"
  42. class="image-content"
  43. mode="heightFix"
  44. @click="showImageFullScreen"
  45. ></image>
  46. <view class="video-snapshot" v-if="message.type === 'video'" :data-url="message.payload.video.url"
  47. @click="playVideo">
  48. <image
  49. :src="message.payload.thumbnail.url"
  50. :style="{height: getImageHeight(message.payload.thumbnail.width,message.payload.thumbnail.height)+'rpx' }"
  51. mode="heightFix"
  52. ></image>
  53. <view class="video-play-icon"></view>
  54. </view>
  55. <view class="file-content" v-if="message.type === 'file'">
  56. <view class="file-info">
  57. <span class="file-name">{{ message.payload.name }}</span>
  58. <span class="file-size">{{ (message.payload.size / 1024).toFixed(2) }}KB</span>
  59. </view>
  60. <image class="file-img" src="/static/images/file-icon.png"></image>
  61. </view>
  62. <view v-if="message.type ==='audio'" class="audio-content" @click="playAudio(message)">
  63. <view class="audio-facade" :style="{width:Math.ceil(message.payload.duration)*7 + 50 + 'px'}">
  64. <view
  65. class="audio-facade-bg"
  66. :class="{'play-icon':audioPlayer.playingMessage && audioPlayer.playingMessage.messageId === message.messageId}"
  67. ></view>
  68. <view>{{Math.ceil(message.payload.duration) || 1}}<span>"</span></view>
  69. </view>
  70. </view>
  71. <view v-if="message.type === 'order'" class="order-content">
  72. <view class="order-id">订单号:{{ message.payload.id }}</view>
  73. <view class="order-body">
  74. <image :src="message.payload.url" class="order-img"></image>
  75. <view>
  76. <view class="order-name">{{ message.payload.name }}</view>
  77. <view class="order-info">
  78. <view class="order-price">{{ message.payload.price }}</view>
  79. <view class="order-count">共{{ message.payload.count }}件</view>
  80. </view>
  81. </view>
  82. </view>
  83. </view>
  84. <view v-if="message.type === 'car'" class="text-content" @click="gocar">
  85. <view class="mVin">{{message.payload.vin}}</view>
  86. <view class="mcar">{{message.payload.carModel}}</view>
  87. </view>
  88. </view>
  89. <view v-if="message.senderId === currentUser.userId" :class="message.read ?'message-read':'message-unread'">
  90. <view v-if="message.status === 'success'">{{ message.read ? '已读' : '未读' }}</view>
  91. </view>
  92. </view>
  93. </view>
  94. </view>
  95. </view>
  96. </checkbox-group>
  97. </view>
  98. <view class="action-box" v-if="!videoPlayer.visible && !messageSelector.visible">
  99. <view class="action-top">
  100. <view @click.stop="switchAudioKeyboard">
  101. <image class="more" v-if="audio.visible" src="/static/images/jianpan.png"></image>
  102. <image class="more" v-else src="/static/images/audio.png"></image>
  103. </view>
  104. <view v-if="audio.visible" class="record-input" @touchend.stop="onRecordEnd" @touchstart.stop="onRecordStart">
  105. {{ recorderManager.recording ? '松开发送' : '按住录音' }}
  106. </view>
  107. <!-- GoEasyIM最大支持3k的文本消息,如需发送长文本,需调整输入框maxlength值 -->
  108. <input v-else v-model="text" @confirm="sendTextMessage" class="consult-input" maxlength="700" placeholder="发送消息" type="text" />
  109. <view @click.stop="switchEmojiKeyboard">
  110. <image class="more" v-if="emoji.visible" src="/static/images/jianpan.png"></image>
  111. <image class="more" v-else src="/static/images/emoji.png"></image>
  112. </view>
  113. <view>
  114. <image @click.stop="showOtherTypesMessagePanel()" class="more" src="/static/images/more.png"/>
  115. </view>
  116. <view v-if="text" class="send-btn-box">
  117. <text class="btn" @click.stop="sendTextMessage()">发送</text>
  118. </view>
  119. </view>
  120. <!--展示表情列表-->
  121. <view class="action-bottom action-bottom-emoji" v-if="emoji.visible">
  122. <image class="emoji-item" v-for="(emojiItem, emojiKey, index) in emoji.map" :key="index"
  123. :src="emoji.url + emojiItem" @click.stop="chooseEmoji(emojiKey)"></image>
  124. </view>
  125. <!--其他类型消息面板-->
  126. <view v-if="otherTypesMessagePanelVisible" class="action-bottom">
  127. <view class="more-icon">
  128. <image @click.stop="sendImageMessage()" class="operation-icon" src="/static/images/picture.png"></image>
  129. <view class="operation-title">图片</view>
  130. </view>
  131. <view class="more-icon">
  132. <image @click.stop="sendVideoMessage()" class="operation-icon" src="/static/images/video.png"></image>
  133. <view class="operation-title">视频</view>
  134. </view>
  135. <!-- <view class="more-icon">
  136. <image @click="showOrderMessageList()" class="operation-icon" src="/static/images/order.png"></image>
  137. <view class="operation-title">订单</view>
  138. </view>
  139. <view class="more-icon">
  140. <image @click="privateCall()" class="operation-icon" src="/static/images/rtc.png"></image>
  141. <view class="operation-title">视频通话</view>
  142. </view> -->
  143. </view>
  144. </view>
  145. <view class="action-popup" @touchmove.stop.prevent v-if="actionPopup.visible">
  146. <view class="layer"></view>
  147. <view class="action-list">
  148. <view class="action-item" @click="deleteSingleMessage">删除</view>
  149. <view class="action-item" v-if="actionPopup.recallable" @click="recallMessage">撤回</view>
  150. <view class="action-item" @click="showCheckBox">多选</view>
  151. <view class="action-item" @click="hideActionPopup">取消</view>
  152. </view>
  153. </view>
  154. <view class="messageSelector-box" v-if="messageSelector.visible">
  155. <image class="messageSelector-btn" @click="deleteMultipleMessages" src="/static/images/delete.png"></image>
  156. </view>
  157. <view class="record-loading" v-if="recorderManager.recording"></view>
  158. <video v-if="videoPlayer.visible" :src="videoPlayer.url" id="videoPlayer"
  159. @fullscreenchange="onVideoFullScreenChange"></video>
  160. <view v-if="orderList.visible" class="order-list">
  161. <view class="orders-content">
  162. <view class="title">
  163. <view>请选择一个订单</view>
  164. <view class="close" @click="hideOrderMessageList">×</view>
  165. </view>
  166. <view class="orders">
  167. <view
  168. v-for="(order, index) in orderList.orders"
  169. :key="index" class="order-item"
  170. @click="sendOrderMessage(order)"
  171. >
  172. <view class="order-id">订单号:{{ order.id }}</view>
  173. <view class="order-body">
  174. <image :src="order.url" class="order-img"></image>
  175. <view class="order-name">{{ order.name }}</view>
  176. <view class="order-right">
  177. <view class="order-price">{{ order.price }}</view>
  178. <view class="order-count">共{{ order.count }}件</view>
  179. </view>
  180. </view>
  181. </view>
  182. </view>
  183. </view>
  184. </view>
  185. <view class="videoBox" v-if="videoShow" @click="videoShow=false">
  186. <view style="width: 100%;" @click.stop="">
  187. <video :src="videoPlayer.url" id="videoPlayer"
  188. :show-fullscreen-btn="false" ></video>
  189. </view>
  190. </view>
  191. </view>
  192. </template>
  193. <script>
  194. import EmojiDecoder from '../lib/EmojiDecoder';
  195. import restApi from '../lib/restapi';
  196. import {formatDate} from '../lib/utils';
  197. import RecorderManager from '../lib/RecorderManager';
  198. const IMAGE_MAX_WIDTH = 200;
  199. const IMAGE_MAX_HEIGHT = 150;
  200. const recorderManager = new RecorderManager();
  201. const GoEasy = uni.$GoEasy;
  202. const GRTC = uni.$GRTC;
  203. export default {
  204. name: 'privateChat',
  205. data() {
  206. const emojiUrl = 'https://imgcache.qq.com/open/qcloud/tim/assets/emoji/';
  207. const emojiMap = {
  208. '[么么哒]': 'emoji_3@2x.png',
  209. '[乒乓]': 'emoji_4@2x.png',
  210. '[便便]': 'emoji_5@2x.png',
  211. '[信封]': 'emoji_6@2x.png',
  212. '[偷笑]': 'emoji_7@2x.png',
  213. '[傲慢]': 'emoji_8@2x.png'
  214. };
  215. return {
  216. //聊天文本框
  217. text: '',
  218. friend: null,
  219. to: {},// 作为createMessage的参数
  220. currentUser: null,
  221. //定义表情列表
  222. emoji: {
  223. url: emojiUrl,
  224. map: emojiMap,
  225. visible: false,
  226. decoder: new EmojiDecoder(emojiUrl, emojiMap),
  227. },
  228. //是否展示‘其他消息类型面板’
  229. otherTypesMessagePanelVisible: false,
  230. orderList: {
  231. orders: [],
  232. visible: false
  233. },
  234. history: {
  235. messages: [],
  236. allLoaded: false,
  237. loading: false
  238. },
  239. recorderManager: recorderManager,
  240. audio: {
  241. //录音按钮展示
  242. visible: false
  243. },
  244. audioPlayer: {
  245. innerAudioContext: null,
  246. playingMessage: null,
  247. },
  248. videoPlayer: {
  249. visible: false,
  250. url: '',
  251. context: null
  252. },
  253. // 展示消息删除弹出框
  254. actionPopup: {
  255. visible: false,
  256. message: null,
  257. recallable: false,
  258. },
  259. // 消息选择
  260. messageSelector: {
  261. visible: false,
  262. messages: []
  263. },
  264. videoShow:false
  265. }
  266. },
  267. onLoad(options) {
  268. //聊天对象
  269. let id = options.to;
  270. this.friend = uni.getStorageSync("friend");
  271. //this.friend = restApi.findUserById(id);
  272. //this.currentUser = uni.$currentUser;
  273. this.currentUser = uni.getStorageSync("loginInfo");
  274. this.to = {
  275. id: this.friend.id,
  276. type: GoEasy.IM_SCENE.PRIVATE,
  277. data: {
  278. name: this.friend.supplierName,
  279. avatar: '/static/images/Avatar-1.png'
  280. }
  281. };
  282. this.initGoEasyListeners();
  283. // 语音播放器
  284. this.initialAudioPlayer();
  285. // 录音监听器
  286. this.initRecorderListeners();
  287. },
  288. onShow() {
  289. this.otherTypesMessagePanelVisible = false;
  290. this.emoji.visible = false;
  291. },
  292. onReady() {
  293. this.loadHistoryMessage(true);
  294. this.videoPlayer.context = uni.createVideoContext('videoPlayer', this);
  295. // https://uniapp.dcloud.io/api/ui/navigationbar?id=setnavigationbartitle
  296. uni.setNavigationBarTitle({ title: this.friend.supplierName });
  297. },
  298. onPullDownRefresh(e) {
  299. this.loadHistoryMessage(false);
  300. },
  301. onUnload() {
  302. //退出聊天页面之前,清空监听器
  303. GoEasy.im.off(GoEasy.IM_EVENT.PRIVATE_MESSAGE_RECEIVED, this.onMessageReceived);
  304. GoEasy.im.off(GoEasy.IM_EVENT.MESSAGE_DELETED, this.onMessageDeleted);
  305. GoEasy.im.off(GoEasy.IM_EVENT.HISTORY_EXPIRED, this.onHistoryExpired);
  306. },
  307. methods: {
  308. chatBox(){
  309. console.log("fas")
  310. this.otherTypesMessagePanelVisible = false;
  311. this.emoji.visible = false;
  312. },
  313. //渲染文本消息,如果包含表情,替换为图片
  314. //todo:本不需要该方法,可以在标签里完成,但小程序有兼容性问题,被迫这样实现
  315. renderTextMessage(message) {
  316. return '<span>' + this.emoji.decoder.decode(message.payload.text) + '</span>'
  317. },
  318. //像微信那样显示时间,如果有几分钟没发消息了,才显示时间
  319. //todo:本不需要该方法,可以在标签里完成,但小程序有兼容性问题,被迫这样实现
  320. renderMessageDate(message, index) {
  321. if (index === 0) {
  322. return formatDate(message.timestamp)
  323. } else {
  324. if (message.timestamp - this.history.messages[index - 1].timestamp > 5 * 60 * 1000) {
  325. return formatDate(message.timestamp)
  326. }
  327. }
  328. return '';
  329. },
  330. initGoEasyListeners() {
  331. // 监听私聊消息
  332. GoEasy.im.on(GoEasy.IM_EVENT.PRIVATE_MESSAGE_RECEIVED, this.onMessageReceived);
  333. //监听消息删除
  334. GoEasy.im.on(GoEasy.IM_EVENT.MESSAGE_DELETED, this.onMessageDeleted);
  335. // 监听断网重连
  336. GoEasy.im.on(GoEasy.IM_EVENT.HISTORY_EXPIRED, this.onHistoryExpired);
  337. },
  338. onMessageReceived (message) {
  339. let senderId = message.senderId;
  340. let receiverId = message.receiverId;
  341. let friendId = this.currentUser.userId === senderId ? receiverId : senderId;
  342. if (friendId === this.friend.id) {
  343. this.history.messages.push(message);
  344. //聊天时,收到消息标记为已读
  345. this.markPrivateMessageAsRead();
  346. //收到新消息,是滚动到最底部
  347. this.scrollToBottom();
  348. }
  349. },
  350. onMessageDeleted (deletedMessages) {
  351. deletedMessages.forEach(message => {
  352. let senderId = message.senderId;
  353. let receiverId = message.receiverId;
  354. let friendId = this.currentUser.userId === senderId ? receiverId : senderId;
  355. if (friendId === this.friend.id) {
  356. let index = this.history.messages.indexOf(message);
  357. if (index > -1) {
  358. this.history.messages.splice(index, 1);
  359. }
  360. }
  361. });
  362. },
  363. onHistoryExpired() {
  364. this.history.messages = [];
  365. this.loadHistoryMessage(true);
  366. },
  367. initialAudioPlayer () {
  368. this.audioPlayer.innerAudioContext = uni.createInnerAudioContext();
  369. this.audioPlayer.innerAudioContext.onEnded(() => {
  370. this.audioPlayer.playingMessage = null;
  371. });
  372. this.audioPlayer.innerAudioContext.onStop(() => {
  373. this.audioPlayer.playingMessage = null;
  374. });
  375. },
  376. initRecorderListeners() {
  377. recorderManager.onRecordComplete((file, duration) => {
  378. if (duration < 1000) {
  379. uni.showToast({
  380. icon: 'none',
  381. title: '录音时间太短',
  382. duration: 500
  383. });
  384. return;
  385. }
  386. GoEasy.im.createAudioMessage({
  387. to: this.to,
  388. file: file,
  389. notification: {
  390. title: this.currentUser.supplierName + '发来一段语音',
  391. body: '[语音消息]', // 字段最长 50 字符
  392. sound: 'message',
  393. badge: '+1'
  394. },
  395. onProgress: function (progress) {
  396. console.log(progress)
  397. },
  398. onSuccess: (message) => {
  399. this.sendMessage(message);
  400. },
  401. onFailed: (e) => {
  402. console.log('error :', e);
  403. }
  404. });
  405. });
  406. },
  407. /**
  408. * 核心就是设置高度,产生明确占位
  409. *
  410. * 小 (宽度和高度都小于预设尺寸)
  411. * 设高=原始高度
  412. * 宽 (宽度>高度)
  413. * 高度= 根据宽度等比缩放
  414. * 窄 (宽度<高度)或方(宽度=高度)
  415. * 设高=MAX height
  416. *
  417. * @param width,height
  418. * @returns number
  419. */
  420. getImageHeight(width, height) {
  421. if (width < IMAGE_MAX_WIDTH && height < IMAGE_MAX_HEIGHT) {
  422. return height * 2;
  423. } else if (width > height) {
  424. return (IMAGE_MAX_WIDTH / width * height) * 2;
  425. } else if (width === height || width < height) {
  426. return IMAGE_MAX_HEIGHT * 2;
  427. }
  428. },
  429. sendMessage(message) {
  430. this.history.messages.push(message);
  431. this.scrollToBottom();
  432. GoEasy.im.sendMessage({
  433. message: message,
  434. onSuccess: function () {
  435. console.log('发送成功.', message);
  436. },
  437. onFailed: function (error) {
  438. if (error.code === 507) {
  439. console.log('发送语音/图片/视频/文件失败,没有配置OSS存储,详情参考:https://docs.goeasy.io/2.x/im/message/media/alioss');
  440. } else {
  441. console.log('发送失败:', error);
  442. }
  443. }
  444. });
  445. },
  446. sendTextMessage() {
  447. if (this.text.trim() !== '') {
  448. let body = this.text;
  449. if (this.text.length >= 50) {
  450. body = this.text.substring(0, 30) + '...';
  451. }
  452. GoEasy.im.createTextMessage({
  453. text: this.text,
  454. to: this.to,
  455. notification: {
  456. title: this.currentUser.supplierName + '发来一段文字',
  457. body: body,
  458. sound: 'message',
  459. badge: '+1'
  460. },
  461. onSuccess: (message) => {
  462. this.sendMessage(message);
  463. },
  464. onFailed: (e) => {
  465. console.log('error :', e);
  466. }
  467. });
  468. }
  469. this.text = '';
  470. },
  471. sendVideoMessage() {
  472. uni.chooseVideo({
  473. success: (res) => {
  474. GoEasy.im.createVideoMessage({
  475. to: this.to,
  476. file: res,
  477. notification: {
  478. title: this.currentUser.supplierName + '发来一个视频',
  479. body: '[视频消息]', // 字段最长 50 字符
  480. sound: 'message',
  481. badge: '+1'
  482. },
  483. onProgress: function (progress) {
  484. console.log(progress)
  485. },
  486. onSuccess: (message) => {
  487. this.otherTypesMessagePanelVisible = false;
  488. this.sendMessage(message);
  489. },
  490. onFailed: (e) => {
  491. console.log('error :', e);
  492. }
  493. });
  494. }
  495. })
  496. },
  497. sendImageMessage() {
  498. uni.chooseImage({
  499. count: 9,
  500. success: (res) => {
  501. res.tempFiles.forEach(file => {
  502. GoEasy.im.createImageMessage({
  503. to: this.to,
  504. file: file,
  505. notification: {
  506. title: this.currentUser.supplierName + '发来一张图片',
  507. body: '[图片消息]', // 字段最长 50 字符
  508. sound: 'message',
  509. badge: '+1'
  510. },
  511. onProgress: function (progress) {
  512. console.log(progress)
  513. },
  514. onSuccess: (message) => {
  515. this.otherTypesMessagePanelVisible = false;
  516. this.sendMessage(message);
  517. },
  518. onFailed: (e) => {
  519. console.log('error :', e);
  520. }
  521. });
  522. })
  523. }
  524. });
  525. },
  526. sendOrderMessage(order) {
  527. //GoEasyIM自定义消息,实现订单发送
  528. GoEasy.im.createCustomMessage({
  529. type: 'order',
  530. payload: order,
  531. to: this.to,
  532. notification: {
  533. title: this.currentUser.supplierName + '发来一个订单',
  534. body: '[订单消息]',
  535. sound: 'message',
  536. badge: '+1'
  537. },
  538. onSuccess: (message) => {
  539. this.otherTypesMessagePanelVisible = false;
  540. this.sendMessage(message);
  541. },
  542. onFailed: (e) => {
  543. console.log('error :', e);
  544. }
  545. });
  546. this.orderList.visible = false;
  547. },
  548. showActionPopup(message) {
  549. const MAX_RECALLABLE_TIME = 3 * 60 * 1000; //3分钟以内的消息才可以撤回
  550. this.messageSelector.messages = [message];
  551. if ((Date.now() - message.timestamp) < MAX_RECALLABLE_TIME && message.senderId === this.currentUser.userId && message.status === 'success') {
  552. this.actionPopup.recallable = true;
  553. } else {
  554. this.actionPopup.recallable = false;
  555. }
  556. this.actionPopup.visible = true;
  557. },
  558. hideActionPopup () {
  559. this.actionPopup.visible = false;
  560. this.actionPopup.message = null;
  561. },
  562. deleteSingleMessage() {
  563. uni.showModal({
  564. content: '确认删除?',
  565. success: (res) => {
  566. this.actionPopup.visible = false;
  567. if (res.confirm) {
  568. this.deleteMessage();
  569. }
  570. },
  571. })
  572. },
  573. deleteMultipleMessages() {
  574. if (this.messageSelector.messages.length > 0) {
  575. uni.showModal({
  576. content: '确认删除?',
  577. success: (res) => {
  578. this.messageSelector.visible = false;
  579. if (res.confirm) {
  580. this.deleteMessage();
  581. }
  582. },
  583. })
  584. }
  585. },
  586. deleteMessage() {
  587. GoEasy.im.deleteMessage({
  588. messages: this.messageSelector.messages,
  589. onSuccess: (result) => {
  590. this.messageSelector.messages.forEach(message => {
  591. let index = this.history.messages.indexOf(message);
  592. if (index > -1) {
  593. this.history.messages.splice(index, 1);
  594. }
  595. });
  596. this.messageSelector.messages = [];
  597. },
  598. onFailed: (error) => {
  599. console.log('error:', error);
  600. }
  601. });
  602. },
  603. recallMessage() {
  604. this.actionPopup.visible = false;
  605. GoEasy.im.recallMessage({
  606. messages: this.messageSelector.messages,
  607. onSuccess: () => {
  608. console.log('撤回成功');
  609. },
  610. onFailed: (error) => {
  611. console.log('撤回失败,error:', error);
  612. }
  613. });
  614. },
  615. editRecalledMessage(text) {
  616. if (this.audio.visible) {
  617. this.audio.visible = false;
  618. }
  619. this.text = text;
  620. },
  621. showCheckBox() {
  622. this.messageSelector.messages = [];
  623. this.messageSelector.visible = true;
  624. this.actionPopup.visible = false;
  625. },
  626. selectMessages(e) {
  627. const selectedMessageIds = e.detail.value;
  628. let selectedMessages = [];
  629. this.history.messages.forEach(message => {
  630. if (selectedMessageIds.includes(message.messageId)) {
  631. selectedMessages.push(message);
  632. }
  633. })
  634. this.messageSelector.messages = selectedMessages;
  635. },
  636. loadHistoryMessage(scrollToBottom) {//历史消息
  637. this.history.loading = true;
  638. let lastMessageTimeStamp = null;
  639. let lastMessage = this.history.messages[0];
  640. if (lastMessage) {
  641. lastMessageTimeStamp = lastMessage.timestamp;
  642. }
  643. GoEasy.im.history({
  644. id: this.friend.id,
  645. type: GoEasy.IM_SCENE.PRIVATE,
  646. lastTimestamp: lastMessageTimeStamp,
  647. limit: 10,
  648. onSuccess: (result) => {
  649. uni.stopPullDownRefresh();
  650. this.history.loading = false;
  651. let messages = result.content;
  652. if (messages.length === 0) {
  653. this.history.allLoaded = true;
  654. } else {
  655. if (lastMessageTimeStamp) {
  656. this.history.messages = messages.concat(this.history.messages);
  657. } else {
  658. this.history.messages = messages;
  659. }
  660. if (messages.length < 10) {
  661. this.history.allLoaded = true;
  662. }
  663. if (scrollToBottom) {
  664. this.scrollToBottom();
  665. //收到的消息设置为已读
  666. this.markPrivateMessageAsRead();
  667. }
  668. }
  669. },
  670. onFailed: (error) => {
  671. //获取失败
  672. console.log('获取历史消息失败:', error);
  673. uni.stopPullDownRefresh();
  674. this.history.loading = false;
  675. }
  676. });
  677. },
  678. //语音录制按钮和键盘输入的切换
  679. switchAudioKeyboard() {
  680. if (!this.audio.visible) {
  681. recorderManager.authorize().then(() => {
  682. console.log('录音权限获取成功');
  683. this.audio.visible = true;
  684. }).catch((err) => {
  685. console.log('err:', err)
  686. uni.showModal({
  687. title: '获取录音权限失败',
  688. content: '请先打开麦克风权限'
  689. });
  690. });
  691. } else {
  692. this.audio.visible = false;
  693. }
  694. },
  695. onRecordStart() {
  696. recorderManager.start();
  697. },
  698. onRecordEnd() {
  699. recorderManager.stop();
  700. },
  701. showImageFullScreen(e) {
  702. let imagesUrl = [e.currentTarget.dataset.url];
  703. uni.previewImage({
  704. urls: imagesUrl
  705. });
  706. },
  707. playVideo(e) {
  708. //this.videoPlayer.visible = true;
  709. this.videoPlayer.url = e.currentTarget.dataset.url;
  710. this.videoShow=true
  711. /* this.$nextTick(() => {
  712. this.videoPlayer.context.requestFullScreen({
  713. direction: 0
  714. });
  715. this.videoPlayer.context.play();
  716. }); */
  717. },
  718. playAudio (audioMessage) {
  719. let playingMessage = this.audioPlayer.playingMessage;
  720. if (playingMessage) {
  721. this.audioPlayer.innerAudioContext.stop();
  722. // 如果点击的消息正在播放,就认为是停止播放操作
  723. if (playingMessage === audioMessage) {
  724. return;
  725. }
  726. }
  727. this.audioPlayer.playingMessage = audioMessage;
  728. this.audioPlayer.innerAudioContext.src = audioMessage.payload.url;
  729. this.audioPlayer.innerAudioContext.play();
  730. },
  731. onVideoFullScreenChange(e) {
  732. //当退出全屏播放时,隐藏播放器
  733. if (this.videoPlayer.visible && !e.detail.fullScreen) {
  734. this.videoPlayer.visible = false;
  735. this.videoPlayer.context.stop();
  736. }
  737. },
  738. messageInputFocusin() {
  739. this.otherTypesMessagePanelVisible = false;
  740. this.emoji.visible = false;
  741. },
  742. switchEmojiKeyboard() {
  743. this.emoji.visible = !this.emoji.visible;
  744. this.otherTypesMessagePanelVisible = false;
  745. },
  746. showOtherTypesMessagePanel() {
  747. this.otherTypesMessagePanelVisible = !this.otherTypesMessagePanelVisible;
  748. this.emoji.visible = false;
  749. },
  750. chooseEmoji(emojiKey) {
  751. this.text += emojiKey;
  752. },
  753. showOrderMessageList() {
  754. this.orderList.orders = restApi.getOrderList();
  755. this.orderList.visible = true;
  756. },
  757. hideOrderMessageList() {
  758. this.orderList.visible = false;
  759. },
  760. privateCall() {
  761. uni.showActionSheet({
  762. itemList: ['视频通话', '音频通话'],
  763. success: (res) => {
  764. const mediaType = res.tapIndex === 0 ? 1 : 0;
  765. const notificationBody = res.tapIndex === 0 ? '邀请你视频通话' : '邀请你语音通话';
  766. GRTC.call({
  767. calleeId: this.friend.id,
  768. mediaType: mediaType,
  769. notification: {
  770. title: this.currentUser.name,
  771. body: notificationBody,
  772. sound: 'ring',
  773. badge: '+1'
  774. },
  775. }).then(() => {
  776. uni.navigateTo({
  777. url: `./rtc/private/dial`,
  778. })
  779. }).catch((error)=>{
  780. console.log("呼叫失败:", error);
  781. uni.showToast({
  782. icon: "error",
  783. title: "呼叫失败:" + error,
  784. duration: 2000
  785. })
  786. })
  787. },
  788. fail: (res) => {
  789. console.log(res.errMsg);
  790. }
  791. });
  792. },
  793. scrollToBottom() {
  794. this.$nextTick(() => {
  795. uni.pageScrollTo({
  796. scrollTop: 2000000,
  797. duration: 0
  798. });
  799. });
  800. },
  801. markPrivateMessageAsRead() {
  802. GoEasy.im.markMessageAsRead({
  803. id: this.to.id,
  804. type: this.to.type,
  805. onSuccess: function () {
  806. console.log('标记私聊已读成功');
  807. },
  808. onFailed: function (error) {
  809. console.log("标记私聊已读失败", error);
  810. }
  811. });
  812. }
  813. }
  814. }
  815. </script>
  816. <style scoped>
  817. @import url('../static/style/chatInterface.css');
  818. .videoBox{
  819. position: fixed;
  820. top: 0;
  821. left: 0;
  822. background: rgba(0,0,0,0.5);
  823. width: 100%;
  824. height: 100%;
  825. display: flex;
  826. align-items: center;
  827. }
  828. #videoPlayer{
  829. width: 100%;
  830. }
  831. </style>