conversation.vue 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. const recorderManager = uni.getRecorderManager()
  56. export default {
  57. components: {
  58. GoEasyAudioPlayer
  59. },
  60. onLoad() {
  61. this.imService = getApp().globalData.imService;
  62. //对话数据
  63. this.friend = {
  64. uuid: "8",
  65. name: "勇敢的心",
  66. avatar: "xxxxxxxx"
  67. };
  68. this.currentUser = this.imService.currentUser;
  69. let privateMessages = this.imService.getPrivateMessages(this.friend.uuid);
  70. this.messages = privateMessages.sentMessages;
  71. this.pendingMessages = privateMessages.pendingMessages;
  72. uni.setNavigationBarTitle({
  73. title: this.friend.name
  74. });
  75. this.initialListeners();
  76. //每次进入聊天页面,总是滚动到底部
  77. this.scrollToBottom()
  78. //收到的消息设置为已读
  79. if (this.messages.length != 0) {
  80. this.imService.markPrivateMessageAsRead(this.friend.uuid);
  81. }
  82. },
  83. mounted() {
  84. },
  85. data() {
  86. return {
  87. keyword: "",
  88. InputBottom: 0,
  89. isvoice: false,
  90. textinput: "请输入",
  91. isinput: false,
  92. messages: [],
  93. self_messages: [],
  94. friend: null,
  95. currentUser: null,
  96. imService: null,
  97. audio: {
  98. //语音录音中
  99. recording: false,
  100. },
  101. }
  102. },
  103. onPullDownRefresh: function(e) {
  104. this.loadMoreHistoryMessage();
  105. },
  106. onUnload() {
  107. //退出聊天页面之前,清空页面传入的监听器
  108. if (this.imService) {
  109. this.imService.onNewPrivateMessageReceive = (friendId, message) => {};
  110. }
  111. },
  112. methods: {
  113. InputFocus(e) {
  114. this.InputBottom = e.detail.height
  115. },
  116. InputBlur(e) {
  117. this.InputBottom = 0
  118. },
  119. sendvoice() {
  120. this.isvoice = !this.isvoice
  121. },
  122. subscribeMessage() { //订阅消息
  123. },
  124. yulan(url) {
  125. uni.previewImage({
  126. urls: [url],
  127. current: url
  128. })
  129. },
  130. initialListeners() {
  131. //传入监听器,收到一条私聊消息总是滚到到页面底部
  132. this.imService.onNewPrivateMessageReceive = (friendId, message) => {
  133. if (friendId == this.friend.uuid) {
  134. this.imService.markPrivateMessageAsRead(friendId);
  135. //收到新消息,是滚动到最底部
  136. this.scrollToBottom()
  137. }
  138. };
  139. // 录音监听器
  140. this.initRecorderListeners();
  141. },
  142. initRecorderListeners() {
  143. let self = this;
  144. // 监听录音开始
  145. recorderManager.onStart(function() {
  146. self.audio.recording = true;
  147. });
  148. //录音结束后,发送
  149. recorderManager.onStop(function(res) {
  150. console.log(res)
  151. self.audio.recording = false;
  152. self.imService.sendPrivateAudioMessage(self.friend.uuid, res)
  153. });
  154. // 监听录音报错
  155. recorderManager.onError(function(res) {
  156. console.log("录音报错:", res);
  157. })
  158. },
  159. onRecordStart(event) {
  160. try {
  161. recorderManager.start();
  162. } catch (e) {
  163. uni.showModal({
  164. title: '发送语音错误',
  165. content: '请联系客服'
  166. });
  167. }
  168. event.preventDefault();
  169. },
  170. onRecordEnd() {
  171. try {
  172. recorderManager.stop();
  173. } catch (e) {
  174. uni.showModal({
  175. title: '发送语音错误',
  176. content: '请联系客服'
  177. });
  178. }
  179. },
  180. sendMessage() { //发送消息
  181. if (this.keyword.trim() != '') {
  182. console.log(this.friend.uuid)
  183. this.imService.sendPrivateTextMessage(this.friend.uuid, this.keyword);
  184. }
  185. this.keyword = "";
  186. },
  187. scrollToBottom() {
  188. this.$nextTick(function() {
  189. uni.pageScrollTo({
  190. scrollTop: 2000000,
  191. duration: 10
  192. })
  193. })
  194. },
  195. sendImage() {
  196. uni.chooseImage({
  197. count: 1,
  198. success: (res) => {
  199. this.imService.sendPrivateImageMessage(this.friend.uuid, res);
  200. }
  201. })
  202. },
  203. loadMoreHistoryMessage() { //历史消息
  204. let lastMessageTimeStamp = Date.now();
  205. let lastMessage = this.messages[0];
  206. if (lastMessage) {
  207. lastMessageTimeStamp = lastMessage.timestamp;
  208. }
  209. var currentLength = this.messages.length;
  210. let promise = this.imService.loadPrivateHistoryMessage(this.friend.uuid, lastMessageTimeStamp);
  211. promise.then(messages => {
  212. if (messages.length == currentLength) {
  213. this.allHistoryLoaded = true
  214. }
  215. this.messages = messages;
  216. uni.stopPullDownRefresh();
  217. }).catch(e => {
  218. console.log(e)
  219. uni.stopPullDownRefresh();
  220. })
  221. },
  222. }
  223. };
  224. </script>
  225. <style lang="scss">
  226. page {
  227. padding-bottom: 100rpx;
  228. }
  229. </style>