conversation.vue 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. <template>
  2. <view class="main">
  3. <view class="cu-chat" v-for="(item,index) in messages" :key="index">
  4. <view class="cu-item self" v-if="item.receiverId!=currentUser.uuid">
  5. <view class="time-lag">
  6. {{renderMessageDate(message, index)}}
  7. </view>
  8. <view class="main" v-if="item.type=='text'">
  9. <view class="content bg-green shadow">
  10. <text>{{item.payload.text}}</text>
  11. </view>
  12. </view>
  13. <view class="main" v-else-if="item.type =='image'">
  14. <image :src="item.payload.url" @click="yulan(item.payload.url)" :style="'width:'+item.payload.width+'rpx;height'+item.payload.height+'rpx;'"
  15. class="radius" mode="widthFix"></image>
  16. </view>
  17. <view class="main" v-else="item.type =='audio'">
  18. <GoEasyAudioPlayer :src="item.payload.url" :duration="item.payload.duration" />
  19. </view>
  20. <view class="cu-avatar radius" :style="'background-image:url('+currentUser.avatar+');'"></view>
  21. </view>
  22. <view class="cu-item" v-else>
  23. <view class="cu-avatar radius" style="background-image:url(https://ossweb-img.qq.com/images/lol/web201310/skin/big143004.jpg);"></view>
  24. <view class="main" v-if="item.type=='text'">
  25. <view class="content shadow">
  26. <text>{{item.payload.text}}</text>
  27. </view>
  28. </view>
  29. <view class="main" v-else-if="item.type =='image'">
  30. <image :src="item.payload.url" @click="yulan(item.payload.url)" :style="'width:'+item.payload.width+'rpx;height'+item.payload.height+'rpx;'"
  31. class="radius" mode="widthFix"></image>
  32. </view>
  33. <view class="main" v-else="item.type =='audio'">
  34. <GoEasyAudioPlayer :src="item.payload.url" :duration="item.payload.duration" />
  35. </view>
  36. </view>
  37. </view>
  38. <view :class="InputBottom!=0?'cu-bar foot tab input cur':'cu-bar foot input'" :style="'bottom:'+InputBottom+'px'">
  39. <view class="action" @click="sendvoice">
  40. <text class="cuIcon-sound text-grey"></text>
  41. </view>
  42. <input v-if="!isvoice" v-model="keyword" class="solid-bottom" @focus="InputFocus" @blur="InputBlur" :disabled="isinput"
  43. :adjust-position="false" :focus="false" maxlength="300" :placeholder="textinput" cursor-spacing="10"></input>
  44. <button class="flex-sub" style="font-size: 30rpx; height: 64rpx;padding: 0;" @touchstart.stop="onRecordStart"
  45. @touchend.stop="onRecordEnd" v-else>{{audio.recording ? '松开发送':'按住发送语音'}}</button>
  46. <view class="action" style="margin-right: 20rpx;" @click="sendImage">
  47. <text class="cuIcon-picfill text-grey"></text>
  48. </view>
  49. <button class="cu-btn bg-green shadow" @click="sendMessage">发送</button>
  50. </view>
  51. </view>
  52. </template>
  53. <script>
  54. import {
  55. sendMessages
  56. } from "../../../common/goeasyimutil.js"
  57. import GoEasyAudioPlayer from "../../../components/GoEasyAudioPlayer/GoEasyAudioPlayer";
  58. const recorderManager = uni.getRecorderManager()
  59. export default {
  60. components: {
  61. GoEasyAudioPlayer
  62. },
  63. onLoad() {
  64. this.imService = getApp().globalData.imService;
  65. //对话数据
  66. this.friend = {
  67. uuid: "8",
  68. name: "勇敢的心",
  69. avatar: "xxxxxxxx"
  70. };
  71. this.currentUser = this.imService.currentUser;
  72. let privateMessages = this.imService.getPrivateMessages(this.friend.uuid);
  73. this.messages = privateMessages.sentMessages;
  74. this.pendingMessages = privateMessages.pendingMessages;
  75. uni.setNavigationBarTitle({
  76. title: this.friend.name
  77. });
  78. this.initialListeners();
  79. //每次进入聊天页面,总是滚动到底部
  80. this.scrollToBottom()
  81. //收到的消息设置为已读
  82. if (this.messages.length != 0) {
  83. this.imService.markPrivateMessageAsRead(this.friend.uuid);
  84. }
  85. },
  86. mounted() {
  87. },
  88. data() {
  89. return {
  90. keyword: "",
  91. InputBottom: 0,
  92. isvoice: false,
  93. textinput: "请输入",
  94. isinput: false,
  95. messages: [],
  96. self_messages: [],
  97. friend: null,
  98. currentUser: null,
  99. imService: null,
  100. audio: {
  101. //语音录音中
  102. recording: false,
  103. },
  104. }
  105. },
  106. onPullDownRefresh: function(e) {
  107. this.loadMoreHistoryMessage();
  108. },
  109. onUnload() {
  110. //退出聊天页面之前,清空页面传入的监听器
  111. if (this.imService) {
  112. this.imService.onNewPrivateMessageReceive = (friendId, message) => {};
  113. }
  114. },
  115. methods: {
  116. renderMessageDate(message, index) {
  117. if (index === 0) {
  118. return this.formatDate(message.timestamp)
  119. } else {
  120. if (message.timestamp - this.messages[index - 1].timestamp > 5 * 60 * 1000) {
  121. return this.formatDate(message.timestamp)
  122. }
  123. }
  124. return ''
  125. },
  126. InputFocus(e) {
  127. this.InputBottom = e.detail.height
  128. },
  129. InputBlur(e) {
  130. this.InputBottom = 0
  131. },
  132. sendvoice() {
  133. this.isvoice = !this.isvoice
  134. },
  135. subscribeMessage() { //订阅消息
  136. },
  137. yulan(url) {
  138. uni.previewImage({
  139. urls: [url],
  140. current: url
  141. })
  142. },
  143. initialListeners() {
  144. //传入监听器,收到一条私聊消息总是滚到到页面底部
  145. this.imService.onNewPrivateMessageReceive = (friendId, message) => {
  146. if (friendId == this.friend.uuid) {
  147. this.imService.markPrivateMessageAsRead(friendId);
  148. //收到新消息,是滚动到最底部
  149. this.scrollToBottom()
  150. }
  151. };
  152. // 录音监听器
  153. this.initRecorderListeners();
  154. },
  155. initRecorderListeners() {
  156. let self = this;
  157. // 监听录音开始
  158. recorderManager.onStart(function() {
  159. self.audio.recording = true;
  160. });
  161. //录音结束后,发送
  162. recorderManager.onStop(function(res) {
  163. console.log(res)
  164. self.audio.recording = false;
  165. self.imService.sendPrivateAudioMessage(self.friend.uuid, res)
  166. });
  167. // 监听录音报错
  168. recorderManager.onError(function(res) {
  169. console.log("录音报错:", res);
  170. })
  171. },
  172. onRecordStart(event) {
  173. try {
  174. recorderManager.start();
  175. } catch (e) {
  176. uni.showModal({
  177. title: '发送语音错误',
  178. content: '请联系客服'
  179. });
  180. }
  181. event.preventDefault();
  182. },
  183. onRecordEnd() {
  184. try {
  185. recorderManager.stop();
  186. } catch (e) {
  187. uni.showModal({
  188. title: '发送语音错误',
  189. content: '请联系客服'
  190. });
  191. }
  192. },
  193. sendMessage() { //发送消息
  194. if (this.keyword.trim() != '') {
  195. console.log(this.friend.uuid)
  196. this.imService.sendPrivateTextMessage(this.friend.uuid, this.keyword);
  197. }
  198. this.keyword = "";
  199. },
  200. scrollToBottom() {
  201. this.$nextTick(function() {
  202. uni.pageScrollTo({
  203. scrollTop: 2000000,
  204. duration: 10
  205. })
  206. })
  207. },
  208. sendImage() {
  209. uni.chooseImage({
  210. count: 1,
  211. success: (res) => {
  212. this.imService.sendPrivateImageMessage(this.friend.uuid, res);
  213. }
  214. })
  215. },
  216. loadMoreHistoryMessage() { //历史消息
  217. let lastMessageTimeStamp = Date.now();
  218. let lastMessage = this.messages[0];
  219. if (lastMessage) {
  220. lastMessageTimeStamp = lastMessage.timestamp;
  221. }
  222. var currentLength = this.messages.length;
  223. let promise = this.imService.loadPrivateHistoryMessage(this.friend.uuid, lastMessageTimeStamp);
  224. promise.then(messages => {
  225. if (messages.length == currentLength) {
  226. this.allHistoryLoaded = true
  227. }
  228. this.messages = messages;
  229. uni.stopPullDownRefresh();
  230. }).catch(e => {
  231. console.log(e)
  232. uni.stopPullDownRefresh();
  233. })
  234. },
  235. }
  236. };
  237. </script>
  238. <style lang="scss">
  239. page {
  240. padding-bottom: 100rpx;
  241. }
  242. .time-lag {
  243. font-size: 20rpx;
  244. text-align: center;
  245. }
  246. </style>