conversationWindow.vue 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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" style="width: 500rpx;word-break:break-all;display: flex;justify-content: flex-end;" 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('+doctorInfo.user_avatar+');'"></view>
  24. <view class="main" style="width: 500rpx;word-break:break-all;" 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. v-show="sures">
  40. <view class="action" @click="sendvoice">
  41. <text class="cuIcon-sound text-grey"></text>
  42. </view>
  43. <input v-if="!isvoice" v-model="keyword" class="solid-bottom" @focus="InputFocus" @blur="InputBlur" :disabled="isinput"
  44. :adjust-position="false" :focus="false" maxlength="300" :placeholder="textinput" cursor-spacing="10"></input>
  45. <button class="flex-sub" style="font-size: 30rpx; height: 64rpx;padding: 0;" @touchstart.stop="onRecordStart"
  46. @touchend.stop="onRecordEnd" v-else>{{audio.recording ? '松开发送':'按住发送语音'}}</button>
  47. <view class="action" style="margin-right: 20rpx;" @click="sendImage">
  48. <text class="cuIcon-picfill text-grey"></text>
  49. </view>
  50. <button class="cu-btn bg-green shadow" @click="sendMessage">发送</button>
  51. </view>
  52. </view>
  53. </template>
  54. <script>
  55. import {
  56. sendMessages
  57. } from "../../common/goeasyimutil.js"
  58. import store from '@/store'
  59. import GoEasyAudioPlayer from "../../components/GoEasyAudioPlayer/GoEasyAudioPlayer.vue";
  60. import IMService from "../../common/goeasyimutil.js"
  61. const recorderManager = uni.getRecorderManager()
  62. export default {
  63. components: {
  64. GoEasyAudioPlayer
  65. },
  66. onLoad: function(op) {
  67. this.messages = []
  68. this.self_messages = []
  69. console.log('聊天状态',op)
  70. if (op.type == 2) {
  71. this.sures = false;
  72. }
  73. this.imService = getApp().globalData.imService;
  74. this.doctorInfo = JSON.parse(op.patient)
  75. this.getMessage("member_" + this.doctorInfo.user_id)
  76. //对话数据
  77. this.friend = {
  78. uuid: "member_" + this.doctorInfo.user_id,
  79. name: this.doctorInfo.user_name,
  80. avatar: this.doctorInfo.user_avatar
  81. };
  82. this.currentUser = this.imService.currentUser;
  83. let privateMessages = this.imService.getPrivateMessages(this.friend.uuid);
  84. this.messages = privateMessages.sentMessages;
  85. this.pendingMessages = privateMessages.pendingMessages;
  86. uni.setNavigationBarTitle({
  87. title: this.friend.name
  88. });
  89. console.log('打印数据:', this.messages)
  90. console.log('打印数据:', this.pendingMessages)
  91. this.initialListeners();
  92. //每次进入聊天页面,总是滚动到底部
  93. //收到的消息设置为已读
  94. if (this.messages.length != 0) {
  95. this.imService.markPrivateMessageAsRead(this.friend.uuid);
  96. }
  97. this.loadMoreHistoryMessage();
  98. },
  99. mounted() {
  100. },
  101. data() {
  102. return {
  103. sures: true,
  104. keyword: "",
  105. InputBottom: 0,
  106. isvoice: false,
  107. textinput: "请输入",
  108. isinput: false,
  109. messages: [],
  110. self_messages: [],
  111. friend: null,
  112. currentUser: null,
  113. imService: null,
  114. audio: {
  115. //语音录音中
  116. recording: false,
  117. },
  118. doctorInfo: {}
  119. }
  120. },
  121. onPullDownRefresh: function(e) {
  122. this.loadMoreHistoryMessage();
  123. },
  124. onUnload() {
  125. console.log("页面卸载")
  126. this.messages.splice(0,this.messages.length)
  127. //退出聊天页面之前,清空页面传入的监听器
  128. if (this.imService) {
  129. this.imService.onNewPrivateMessageReceive = (friendId, message) => {};
  130. }
  131. },
  132. methods: {
  133. getMessage: async function(flag) {
  134. let res = await this.$request.post("doctor/getImList", {
  135. 'flag': flag
  136. });
  137. if (res.status == 0) {
  138. if (res.data.length != 0) {
  139. let newList = [];
  140. for (let i = 0; i < res.data.length; i++) {
  141. this.messages.push(res.data[i]);
  142. // newList.push(res.data[i]);
  143. }
  144. }
  145. this.scrollToBottom()
  146. }
  147. },
  148. renderMessageDate(message, index) {
  149. if (index === 0) {
  150. return this.formatDate(message.timestamp)
  151. } else {
  152. if (message.timestamp - this.messages[index - 1].timestamp > 5 * 60 * 1000) {
  153. return this.formatDate(message.timestamp)
  154. }
  155. }
  156. return ''
  157. },
  158. InputFocus(e) {
  159. this.InputBottom = e.detail.height
  160. },
  161. InputBlur(e) {
  162. this.InputBottom = 0
  163. },
  164. sendvoice() {
  165. this.isvoice = !this.isvoice
  166. },
  167. subscribeMessage() { //订阅消息
  168. },
  169. yulan(url) {
  170. uni.previewImage({
  171. urls: [url],
  172. current: url
  173. })
  174. },
  175. initialListeners() {
  176. //传入监听器,收到一条私聊消息总是滚到到页面底部
  177. this.imService.onNewPrivateMessageReceive = (friendId, message) => {
  178. if (friendId == this.friend.uuid) {
  179. this.imService.markPrivateMessageAsRead(friendId);
  180. //收到新消息,是滚动到最底部
  181. this.scrollToBottom()
  182. }
  183. };
  184. // 录音监听器
  185. this.initRecorderListeners();
  186. },
  187. initRecorderListeners() {
  188. let self = this;
  189. // 监听录音开始
  190. recorderManager.onStart(function() {
  191. self.audio.recording = true;
  192. });
  193. //录音结束后,发送
  194. recorderManager.onStop(function(res) {
  195. console.log(res)
  196. self.audio.recording = false;
  197. self.imService.sendPrivateAudioMessage(self.friend.uuid, res)
  198. });
  199. // 监听录音报错
  200. recorderManager.onError(function(res) {
  201. console.log("录音报错:", res);
  202. })
  203. },
  204. onRecordStart(event) {
  205. try {
  206. recorderManager.start();
  207. } catch (e) {
  208. uni.showModal({
  209. title: '发送语音错误',
  210. content: '请联系客服'
  211. });
  212. }
  213. event.preventDefault();
  214. },
  215. onRecordEnd() {
  216. try {
  217. recorderManager.stop();
  218. } catch (e) {
  219. uni.showModal({
  220. title: '发送语音错误',
  221. content: '请联系客服'
  222. });
  223. }
  224. },
  225. sendMessage() { //发送消息
  226. if (this.keyword.trim() != '') {
  227. console.log(this.friend.uuid)
  228. this.imService.sendPrivateTextMessage(this.friend.uuid, this.keyword);
  229. }
  230. this.keyword = "";
  231. },
  232. scrollToBottom() {
  233. this.$nextTick(function() {
  234. uni.pageScrollTo({
  235. scrollTop: 2000000,
  236. duration: 10
  237. })
  238. })
  239. console.log("滚动")
  240. },
  241. sendImage() {
  242. uni.chooseImage({
  243. count: 1,
  244. success: (res) => {
  245. this.imService.sendPrivateImageMessage(this.friend.uuid, res);
  246. }
  247. })
  248. },
  249. loadMoreHistoryMessage() { //历史消息
  250. // let lastMessageTimeStamp = Date.now();
  251. // let lastMessage = this.messages[0];
  252. // if (lastMessage) {
  253. // lastMessageTimeStamp = lastMessage.timestamp;
  254. // }
  255. // var currentLength = this.messages.length;
  256. // let promise = this.imService.loadPrivateHistoryMessage(this.friend.uuid, lastMessageTimeStamp);
  257. // promise.then(messages => {
  258. // if (messages.length == currentLength) {
  259. // this.allHistoryLoaded = true
  260. // }
  261. // this.messages = messages;
  262. // uni.stopPullDownRefresh();
  263. // }).catch(e => {
  264. // console.log(e)
  265. // uni.stopPullDownRefresh();
  266. // })
  267. },
  268. }
  269. };
  270. </script>
  271. <style lang="scss">
  272. page {
  273. padding-bottom: 100rpx;
  274. }
  275. .time-lag {
  276. font-size: 20rpx;
  277. text-align: center;
  278. }
  279. </style>