conversation.vue 7.1 KB

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