privateChat.vue 30 KB

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