conversation.vue 8.3 KB

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