conversationWindow.vue 7.5 KB

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