privateChatG.vue 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839
  1. <template>
  2. <view class="chatInterface" @contextmenu.prevent="">
  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="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="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="showOtherTypesMessagePanel()" class="more" src="/static/images/more.png"/>
  115. </view>
  116. <view v-if="text" class="send-btn-box">
  117. <text class="btn" @click="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="chooseEmoji(emojiKey)"></image>
  124. </view>
  125. <!--其他类型消息面板-->
  126. <view v-if="otherTypesMessagePanelVisible" class="action-bottom">
  127. <view class="more-icon">
  128. <image @click="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="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. ></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. //渲染文本消息,如果包含表情,替换为图片
  309. //todo:本不需要该方法,可以在标签里完成,但小程序有兼容性问题,被迫这样实现
  310. renderTextMessage(message) {
  311. return '<span>' + this.emoji.decoder.decode(message.payload.text) + '</span>'
  312. },
  313. //像微信那样显示时间,如果有几分钟没发消息了,才显示时间
  314. //todo:本不需要该方法,可以在标签里完成,但小程序有兼容性问题,被迫这样实现
  315. renderMessageDate(message, index) {
  316. if (index === 0) {
  317. return formatDate(message.timestamp)
  318. } else {
  319. if (message.timestamp - this.history.messages[index - 1].timestamp > 5 * 60 * 1000) {
  320. return formatDate(message.timestamp)
  321. }
  322. }
  323. return '';
  324. },
  325. initGoEasyListeners() {
  326. // 监听私聊消息
  327. GoEasy.im.on(GoEasy.IM_EVENT.PRIVATE_MESSAGE_RECEIVED, this.onMessageReceived);
  328. //监听消息删除
  329. GoEasy.im.on(GoEasy.IM_EVENT.MESSAGE_DELETED, this.onMessageDeleted);
  330. // 监听断网重连
  331. GoEasy.im.on(GoEasy.IM_EVENT.HISTORY_EXPIRED, this.onHistoryExpired);
  332. },
  333. onMessageReceived (message) {
  334. let senderId = message.senderId;
  335. let receiverId = message.receiverId;
  336. let friendId = this.currentUser.userId === senderId ? receiverId : senderId;
  337. if (friendId === this.friend.id) {
  338. this.history.messages.push(message);
  339. //聊天时,收到消息标记为已读
  340. this.markPrivateMessageAsRead();
  341. //收到新消息,是滚动到最底部
  342. this.scrollToBottom();
  343. }
  344. },
  345. onMessageDeleted (deletedMessages) {
  346. deletedMessages.forEach(message => {
  347. let senderId = message.senderId;
  348. let receiverId = message.receiverId;
  349. let friendId = this.currentUser.userId === senderId ? receiverId : senderId;
  350. if (friendId === this.friend.id) {
  351. let index = this.history.messages.indexOf(message);
  352. if (index > -1) {
  353. this.history.messages.splice(index, 1);
  354. }
  355. }
  356. });
  357. },
  358. onHistoryExpired() {
  359. this.history.messages = [];
  360. this.loadHistoryMessage(true);
  361. },
  362. initialAudioPlayer () {
  363. this.audioPlayer.innerAudioContext = uni.createInnerAudioContext();
  364. this.audioPlayer.innerAudioContext.onEnded(() => {
  365. this.audioPlayer.playingMessage = null;
  366. });
  367. this.audioPlayer.innerAudioContext.onStop(() => {
  368. this.audioPlayer.playingMessage = null;
  369. });
  370. },
  371. initRecorderListeners() {
  372. recorderManager.onRecordComplete((file, duration) => {
  373. if (duration < 1000) {
  374. uni.showToast({
  375. icon: 'none',
  376. title: '录音时间太短',
  377. duration: 500
  378. });
  379. return;
  380. }
  381. GoEasy.im.createAudioMessage({
  382. to: this.to,
  383. file: file,
  384. notification: {
  385. title: this.currentUser.supplierName + '发来一段语音',
  386. body: '[语音消息]', // 字段最长 50 字符
  387. sound: 'message',
  388. badge: '+1'
  389. },
  390. onProgress: function (progress) {
  391. console.log(progress)
  392. },
  393. onSuccess: (message) => {
  394. this.sendMessage(message);
  395. },
  396. onFailed: (e) => {
  397. console.log('error :', e);
  398. }
  399. });
  400. });
  401. },
  402. /**
  403. * 核心就是设置高度,产生明确占位
  404. *
  405. * 小 (宽度和高度都小于预设尺寸)
  406. * 设高=原始高度
  407. * 宽 (宽度>高度)
  408. * 高度= 根据宽度等比缩放
  409. * 窄 (宽度<高度)或方(宽度=高度)
  410. * 设高=MAX height
  411. *
  412. * @param width,height
  413. * @returns number
  414. */
  415. getImageHeight(width, height) {
  416. if (width < IMAGE_MAX_WIDTH && height < IMAGE_MAX_HEIGHT) {
  417. return height * 2;
  418. } else if (width > height) {
  419. return (IMAGE_MAX_WIDTH / width * height) * 2;
  420. } else if (width === height || width < height) {
  421. return IMAGE_MAX_HEIGHT * 2;
  422. }
  423. },
  424. sendMessage(message) {
  425. this.history.messages.push(message);
  426. this.scrollToBottom();
  427. GoEasy.im.sendMessage({
  428. message: message,
  429. onSuccess: function () {
  430. console.log('发送成功.', message);
  431. },
  432. onFailed: function (error) {
  433. if (error.code === 507) {
  434. console.log('发送语音/图片/视频/文件失败,没有配置OSS存储,详情参考:https://docs.goeasy.io/2.x/im/message/media/alioss');
  435. } else {
  436. console.log('发送失败:', error);
  437. }
  438. }
  439. });
  440. },
  441. sendTextMessage() {
  442. if (this.text.trim() !== '') {
  443. let body = this.text;
  444. if (this.text.length >= 50) {
  445. body = this.text.substring(0, 30) + '...';
  446. }
  447. GoEasy.im.createTextMessage({
  448. text: this.text,
  449. to: this.to,
  450. notification: {
  451. title: this.currentUser.supplierName + '发来一段文字',
  452. body: body,
  453. sound: 'message',
  454. badge: '+1'
  455. },
  456. onSuccess: (message) => {
  457. this.sendMessage(message);
  458. },
  459. onFailed: (e) => {
  460. console.log('error :', e);
  461. }
  462. });
  463. }
  464. this.text = '';
  465. },
  466. sendVideoMessage() {
  467. uni.chooseVideo({
  468. success: (res) => {
  469. GoEasy.im.createVideoMessage({
  470. to: this.to,
  471. file: res,
  472. notification: {
  473. title: this.currentUser.supplierName + '发来一个视频',
  474. body: '[视频消息]', // 字段最长 50 字符
  475. sound: 'message',
  476. badge: '+1'
  477. },
  478. onProgress: function (progress) {
  479. console.log(progress)
  480. },
  481. onSuccess: (message) => {
  482. this.otherTypesMessagePanelVisible = false;
  483. this.sendMessage(message);
  484. },
  485. onFailed: (e) => {
  486. console.log('error :', e);
  487. }
  488. });
  489. }
  490. })
  491. },
  492. sendImageMessage() {
  493. uni.chooseImage({
  494. count: 9,
  495. success: (res) => {
  496. res.tempFiles.forEach(file => {
  497. GoEasy.im.createImageMessage({
  498. to: this.to,
  499. file: file,
  500. notification: {
  501. title: this.currentUser.supplierName + '发来一张图片',
  502. body: '[图片消息]', // 字段最长 50 字符
  503. sound: 'message',
  504. badge: '+1'
  505. },
  506. onProgress: function (progress) {
  507. console.log(progress)
  508. },
  509. onSuccess: (message) => {
  510. this.otherTypesMessagePanelVisible = false;
  511. this.sendMessage(message);
  512. },
  513. onFailed: (e) => {
  514. console.log('error :', e);
  515. }
  516. });
  517. })
  518. }
  519. });
  520. },
  521. sendOrderMessage(order) {
  522. //GoEasyIM自定义消息,实现订单发送
  523. GoEasy.im.createCustomMessage({
  524. type: 'order',
  525. payload: order,
  526. to: this.to,
  527. notification: {
  528. title: this.currentUser.supplierName + '发来一个订单',
  529. body: '[订单消息]',
  530. sound: 'message',
  531. badge: '+1'
  532. },
  533. onSuccess: (message) => {
  534. this.otherTypesMessagePanelVisible = false;
  535. this.sendMessage(message);
  536. },
  537. onFailed: (e) => {
  538. console.log('error :', e);
  539. }
  540. });
  541. this.orderList.visible = false;
  542. },
  543. showActionPopup(message) {
  544. const MAX_RECALLABLE_TIME = 3 * 60 * 1000; //3分钟以内的消息才可以撤回
  545. this.messageSelector.messages = [message];
  546. if ((Date.now() - message.timestamp) < MAX_RECALLABLE_TIME && message.senderId === this.currentUser.userId && message.status === 'success') {
  547. this.actionPopup.recallable = true;
  548. } else {
  549. this.actionPopup.recallable = false;
  550. }
  551. this.actionPopup.visible = true;
  552. },
  553. hideActionPopup () {
  554. this.actionPopup.visible = false;
  555. this.actionPopup.message = null;
  556. },
  557. deleteSingleMessage() {
  558. uni.showModal({
  559. content: '确认删除?',
  560. success: (res) => {
  561. this.actionPopup.visible = false;
  562. if (res.confirm) {
  563. this.deleteMessage();
  564. }
  565. },
  566. })
  567. },
  568. deleteMultipleMessages() {
  569. if (this.messageSelector.messages.length > 0) {
  570. uni.showModal({
  571. content: '确认删除?',
  572. success: (res) => {
  573. this.messageSelector.visible = false;
  574. if (res.confirm) {
  575. this.deleteMessage();
  576. }
  577. },
  578. })
  579. }
  580. },
  581. deleteMessage() {
  582. GoEasy.im.deleteMessage({
  583. messages: this.messageSelector.messages,
  584. onSuccess: (result) => {
  585. this.messageSelector.messages.forEach(message => {
  586. let index = this.history.messages.indexOf(message);
  587. if (index > -1) {
  588. this.history.messages.splice(index, 1);
  589. }
  590. });
  591. this.messageSelector.messages = [];
  592. },
  593. onFailed: (error) => {
  594. console.log('error:', error);
  595. }
  596. });
  597. },
  598. recallMessage() {
  599. this.actionPopup.visible = false;
  600. GoEasy.im.recallMessage({
  601. messages: this.messageSelector.messages,
  602. onSuccess: () => {
  603. console.log('撤回成功');
  604. },
  605. onFailed: (error) => {
  606. console.log('撤回失败,error:', error);
  607. }
  608. });
  609. },
  610. editRecalledMessage(text) {
  611. if (this.audio.visible) {
  612. this.audio.visible = false;
  613. }
  614. this.text = text;
  615. },
  616. showCheckBox() {
  617. this.messageSelector.messages = [];
  618. this.messageSelector.visible = true;
  619. this.actionPopup.visible = false;
  620. },
  621. selectMessages(e) {
  622. const selectedMessageIds = e.detail.value;
  623. let selectedMessages = [];
  624. this.history.messages.forEach(message => {
  625. if (selectedMessageIds.includes(message.messageId)) {
  626. selectedMessages.push(message);
  627. }
  628. })
  629. this.messageSelector.messages = selectedMessages;
  630. },
  631. loadHistoryMessage(scrollToBottom) {//历史消息
  632. this.history.loading = true;
  633. let lastMessageTimeStamp = null;
  634. let lastMessage = this.history.messages[0];
  635. if (lastMessage) {
  636. lastMessageTimeStamp = lastMessage.timestamp;
  637. }
  638. GoEasy.im.history({
  639. id: this.friend.id,
  640. type: GoEasy.IM_SCENE.PRIVATE,
  641. lastTimestamp: lastMessageTimeStamp,
  642. limit: 10,
  643. onSuccess: (result) => {
  644. uni.stopPullDownRefresh();
  645. this.history.loading = false;
  646. let messages = result.content;
  647. if (messages.length === 0) {
  648. this.history.allLoaded = true;
  649. } else {
  650. if (lastMessageTimeStamp) {
  651. this.history.messages = messages.concat(this.history.messages);
  652. } else {
  653. this.history.messages = messages;
  654. }
  655. if (messages.length < 10) {
  656. this.history.allLoaded = true;
  657. }
  658. if (scrollToBottom) {
  659. this.scrollToBottom();
  660. //收到的消息设置为已读
  661. this.markPrivateMessageAsRead();
  662. }
  663. }
  664. },
  665. onFailed: (error) => {
  666. //获取失败
  667. console.log('获取历史消息失败:', error);
  668. uni.stopPullDownRefresh();
  669. this.history.loading = false;
  670. }
  671. });
  672. },
  673. //语音录制按钮和键盘输入的切换
  674. switchAudioKeyboard() {
  675. if (!this.audio.visible) {
  676. recorderManager.authorize().then(() => {
  677. console.log('录音权限获取成功');
  678. this.audio.visible = true;
  679. }).catch((err) => {
  680. console.log('err:', err)
  681. uni.showModal({
  682. title: '获取录音权限失败',
  683. content: '请先打开麦克风权限'
  684. });
  685. });
  686. } else {
  687. this.audio.visible = false;
  688. }
  689. },
  690. onRecordStart() {
  691. recorderManager.start();
  692. },
  693. onRecordEnd() {
  694. recorderManager.stop();
  695. },
  696. showImageFullScreen(e) {
  697. let imagesUrl = [e.currentTarget.dataset.url];
  698. uni.previewImage({
  699. urls: imagesUrl
  700. });
  701. },
  702. playVideo(e) {
  703. //this.videoPlayer.visible = true;
  704. this.videoPlayer.url = e.currentTarget.dataset.url;
  705. this.videoShow=true
  706. /* this.$nextTick(() => {
  707. this.videoPlayer.context.requestFullScreen({
  708. direction: 0
  709. });
  710. this.videoPlayer.context.play();
  711. }); */
  712. },
  713. playAudio (audioMessage) {
  714. let playingMessage = this.audioPlayer.playingMessage;
  715. if (playingMessage) {
  716. this.audioPlayer.innerAudioContext.stop();
  717. // 如果点击的消息正在播放,就认为是停止播放操作
  718. if (playingMessage === audioMessage) {
  719. return;
  720. }
  721. }
  722. this.audioPlayer.playingMessage = audioMessage;
  723. this.audioPlayer.innerAudioContext.src = audioMessage.payload.url;
  724. this.audioPlayer.innerAudioContext.play();
  725. },
  726. onVideoFullScreenChange(e) {
  727. //当退出全屏播放时,隐藏播放器
  728. if (this.videoPlayer.visible && !e.detail.fullScreen) {
  729. this.videoPlayer.visible = false;
  730. this.videoPlayer.context.stop();
  731. }
  732. },
  733. messageInputFocusin() {
  734. this.otherTypesMessagePanelVisible = false;
  735. this.emoji.visible = false;
  736. },
  737. switchEmojiKeyboard() {
  738. this.emoji.visible = !this.emoji.visible;
  739. this.otherTypesMessagePanelVisible = false;
  740. },
  741. showOtherTypesMessagePanel() {
  742. this.otherTypesMessagePanelVisible = !this.otherTypesMessagePanelVisible;
  743. this.emoji.visible = false;
  744. },
  745. chooseEmoji(emojiKey) {
  746. this.text += emojiKey;
  747. },
  748. showOrderMessageList() {
  749. this.orderList.orders = restApi.getOrderList();
  750. this.orderList.visible = true;
  751. },
  752. hideOrderMessageList() {
  753. this.orderList.visible = false;
  754. },
  755. privateCall() {
  756. uni.showActionSheet({
  757. itemList: ['视频通话', '音频通话'],
  758. success: (res) => {
  759. const mediaType = res.tapIndex === 0 ? 1 : 0;
  760. const notificationBody = res.tapIndex === 0 ? '邀请你视频通话' : '邀请你语音通话';
  761. GRTC.call({
  762. calleeId: this.friend.id,
  763. mediaType: mediaType,
  764. notification: {
  765. title: this.currentUser.name,
  766. body: notificationBody,
  767. sound: 'ring',
  768. badge: '+1'
  769. },
  770. }).then(() => {
  771. uni.navigateTo({
  772. url: `./rtc/private/dial`,
  773. })
  774. }).catch((error)=>{
  775. console.log("呼叫失败:", error);
  776. uni.showToast({
  777. icon: "error",
  778. title: "呼叫失败:" + error,
  779. duration: 2000
  780. })
  781. })
  782. },
  783. fail: (res) => {
  784. console.log(res.errMsg);
  785. }
  786. });
  787. },
  788. scrollToBottom() {
  789. this.$nextTick(() => {
  790. uni.pageScrollTo({
  791. scrollTop: 2000000,
  792. duration: 0
  793. });
  794. });
  795. },
  796. markPrivateMessageAsRead() {
  797. GoEasy.im.markMessageAsRead({
  798. id: this.to.id,
  799. type: this.to.type,
  800. onSuccess: function () {
  801. console.log('标记私聊已读成功');
  802. },
  803. onFailed: function (error) {
  804. console.log("标记私聊已读失败", error);
  805. }
  806. });
  807. }
  808. }
  809. }
  810. </script>
  811. <style scoped>
  812. @import url('../static/style/chatInterface.css');
  813. .videoBox{
  814. position: fixed;
  815. top: 0;
  816. left: 0;
  817. background: rgba(0,0,0,0.5);
  818. width: 100%;
  819. height: 100%;
  820. display: flex;
  821. align-items: center;
  822. }
  823. #videoPlayer{
  824. width: 100%;
  825. }
  826. </style>