conversation.vue 8.3 KB

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