conversationWindow.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. <template>
  2. <view class="main">
  3. <view class="padding text-center text-white" style="background-color: #dddcdc;margin-left: 60rpx;margin-right: 60rpx;margin-top: 25rpx;" v-if="sures">
  4. 本次咨询:24小时内回复有效。超时平台将自动退单,请及时回复患者。
  5. </view>
  6. <view class="cu-chat" style="position: relative;" v-for="(item,index) in messages" :key="index">
  7. <view class="cu-item self" v-if="item.senderId==currentUser.uuid">
  8. <view class="time-lag" style="position: absolute;top: 36rpx; right: 43%;">
  9. {{renderMessageDate(item, index)}}
  10. </view>
  11. <view class="main" style="width: 500rpx;word-break:break-all;display: flex;justify-content: flex-end;" v-if="item.type=='text'">
  12. <view class="content bg-green shadow">
  13. <text>{{item.payload.text}}</text>
  14. </view>
  15. </view>
  16. <view class="main" v-else-if="item.type =='image'">
  17. <image :src="item.payload.url" @click="yulan(item.payload.url)" :style="'width:'+item.payload.width+'rpx;height'+item.payload.height+'rpx;'"
  18. class="radius" mode="widthFix"></image>
  19. </view>
  20. <view class="main" v-else="item.type =='audio'">
  21. <GoEasyAudioPlayer :src="item.payload.url" :duration="item.payload.duration" />
  22. </view>
  23. <view class="cu-avatar radius" :style="'background-image:url('+currentUser.avatar+');'"></view>
  24. </view>
  25. <view class="cu-item" v-else>
  26. <view class="cu-avatar radius" :style="'background-image:url('+doctorInfo.user_avatar+');'"></view>
  27. <view class="main" style="width: 500rpx;word-break:break-all;" v-if="item.type=='text'">
  28. <view class="content shadow">
  29. <text>{{item.payload.text}}</text>
  30. </view>
  31. </view>
  32. <view class="main" v-else-if="item.type =='image'">
  33. <image :src="item.payload.url" @click="yulan(item.payload.url)" :style="'width:'+item.payload.width+'rpx;height'+item.payload.height+'rpx;'"
  34. class="radius" mode="widthFix"></image>
  35. </view>
  36. <view class="main" v-else="item.type =='audio'">
  37. <GoEasyAudioPlayer :src="item.payload.url" :duration="item.payload.duration" />
  38. </view>
  39. </view>
  40. </view>
  41. <view class="padding text-center text-white" style="background-color: #dddcdc;margin-left: 60rpx;margin-right: 60rpx;" v-if="!sures">
  42. 本次咨询已结束,如还有问题可向患者发起电话回访。
  43. </view>
  44. <view class="cu-tabbar-height"></view>
  45. <!-- <view class="cu-tabbar-height"></view> -->
  46. <view :class="InputBottom!=0?'cu-bar foot tab input cur':'cu-bar foot input'" style="padding-bottom: 45rpx;padding-top: 45rpx;"
  47. :style="'bottom:'+InputBottom+'px'" v-show="sures">
  48. <view class="action" @click="sendvoice">
  49. <text class="cuIcon-sound text-grey"></text>
  50. </view>
  51. <input v-if="!isvoice" v-model="keyword" class="solid-bottom" @focus="InputFocus" @blur="InputBlur" :disabled="isinput"
  52. :adjust-position="false" :focus="false" maxlength="300" :placeholder="textinput" cursor-spacing="10"></input>
  53. <button class="flex-sub" style="font-size: 30rpx; height: 64rpx;padding: 0;" @longpress="onRecordStart"
  54. @touchend.stop="onRecordEnd" v-else>{{audio.recording ? '松开发送':'按住发送语音'}}</button>
  55. <view class="action" style="margin-right: 20rpx;" @click="sendImage">
  56. <text class="cuIcon-picfill text-grey"></text>
  57. </view>
  58. <button class="cu-btn bg-green shadow" @click="sendMessage">发送</button>
  59. </view>
  60. <view class="record-loading" v-if="audio.recording"></view>
  61. <view class="top_details" @click="detailsShow = true" v-if="orderDetailsList.length!=0">
  62. <u-icon name="arrow-down" size="40"></u-icon>
  63. </view>
  64. <u-popup v-model="detailsShow" mode="top">
  65. <view class="details">
  66. <view>患者姓名:<text>{{orderDetailsList.name}}</text></view>
  67. <view>患者年龄:<text>{{orderDetailsList.numbirthday}}</text></view>
  68. <view>患者性别:<text>{{orderDetailsList.sex==2?'女':'男'}}</text></view>
  69. <view>症状描述:{{orderDetailsList.symptoms}}</view>
  70. <view>图片:</view>
  71. <scroll-view :scroll-x="true">
  72. <view style="display: flex;">
  73. <view v-for="item,index in orderDetailsList.medical_imgs" :key='index' style="margin: 0 10rpx 10rpx 0 ;">
  74. <image mode="aspectFit" :src="item" @click="TanPreviewImage(item)" style="height: 200rpx;width: 200rpx;"></image>
  75. </view>
  76. </view>
  77. </scroll-view>
  78. </view>
  79. <view class="bot_details" @click="detailsShow = false">
  80. <u-icon name="arrow-up" size="40"></u-icon>
  81. </view>
  82. </u-popup>
  83. </view>
  84. </template>
  85. <script>
  86. import {
  87. sendMessages
  88. } from "../../common/goeasyimutil.js"
  89. import store from '@/store'
  90. import GoEasyAudioPlayer from "../../components/GoEasyAudioPlayer/GoEasyAudioPlayer.vue";
  91. import IMService from "../../common/goeasyimutil.js"
  92. const recorderManager = uni.getRecorderManager()
  93. export default {
  94. components: {
  95. GoEasyAudioPlayer
  96. },
  97. onLoad: function(op) {
  98. // this.messages = []
  99. // this.self_messages = []
  100. console.log('聊天状态', op)
  101. if (op.type == 2) {
  102. this.sures = false;
  103. }
  104. this.imService = getApp().globalData.imService;
  105. this.doctorInfo = JSON.parse(op.patient)
  106. console.log(this.doctorInfo)
  107. // this.getMessage("member_" + this.doctorInfo.user_id)
  108. //对话数据
  109. this.friend = {
  110. uuid: "member_" + this.doctorInfo.user_id,
  111. name: this.doctorInfo.user_name,
  112. avatar: this.doctorInfo.user_avatar
  113. };
  114. this.currentUser = this.imService.currentUser;
  115. let privateMessages = this.imService.getPrivateMessages(this.friend.uuid);
  116. this.messages = privateMessages.sentMessages;
  117. this.pendingMessages = privateMessages.pendingMessages;
  118. uni.setNavigationBarTitle({
  119. title: this.friend.name
  120. });
  121. this.initialListeners();
  122. //每次进入聊天页面,总是滚动到底部
  123. //收到的消息设置为已读
  124. if (this.messages.length != 0) {
  125. this.imService.markPrivateMessageAsRead(this.friend.uuid);
  126. }
  127. this.loadMoreHistoryMessage();
  128. // 获取订单详情
  129. this.getOrderDetails();
  130. },
  131. mounted() {
  132. },
  133. data() {
  134. return {
  135. //控制患者详情
  136. detailsShow: false,
  137. sures: true,
  138. keyword: "",
  139. InputBottom: 0,
  140. isvoice: false,
  141. textinput: "请输入",
  142. isinput: false,
  143. messages: [],
  144. self_messages: [],
  145. friend: null,
  146. currentUser: null,
  147. imService: null,
  148. audio: {
  149. //语音录音中
  150. recording: false,
  151. },
  152. doctorInfo: {},
  153. orderDetailsList: {}
  154. }
  155. },
  156. onPullDownRefresh: function(e) {
  157. this.loadMoreHistoryMessage();
  158. },
  159. onUnload() {
  160. console.log("页面卸载")
  161. this.messages.splice(0, this.messages.length)
  162. //退出聊天页面之前,清空页面传入的监听器
  163. if (this.imService) {
  164. this.imService.onNewPrivateMessageReceive = (friendId, message) => {};
  165. }
  166. },
  167. methods: {
  168. //图片预览
  169. TanPreviewImage(imageUrl) {
  170. console.log(imageUrl) // http://192.168.100.251:8970/6_1597822634094.png
  171. var images = [];
  172. images.push(imageUrl);
  173. console.log(images) // ["http://192.168.100.251:8970/6_1597822634094.png"]
  174. uni.previewImage({ // 预览图片 图片路径必须是一个数组 => ["http://192.168.100.251:8970/6_1597822634094.png"]
  175. current: 0,
  176. urls: images,
  177. longPressActions: { //长按保存图片到相册
  178. itemList: ['保存图片'],
  179. success: (data) => {
  180. console.log(data);
  181. uni.saveImageToPhotosAlbum({ //保存图片到相册
  182. filePath: payUrl,
  183. success: function() {
  184. uni.showToast({
  185. icon: 'success',
  186. title: '保存成功'
  187. })
  188. },
  189. fail: (err) => {
  190. uni.showToast({
  191. icon: 'none',
  192. title: '保存失败,请重新尝试'
  193. })
  194. }
  195. });
  196. },
  197. fail: (err) => {
  198. console.log(err.errMsg);
  199. }
  200. }
  201. });
  202. },
  203. //
  204. getOrderDetails: async function() {
  205. let res = await this.$request.post("patient/CaseAcquisition", {
  206. 'user_id': this.doctorInfo.user_id
  207. });
  208. if (res.status == 0) {
  209. console.log("订单详情", res)
  210. this.orderDetailsList = res.data
  211. this.orderDetailsList.medical_imgs = this.orderDetailsList.medical_imgs.split(",")
  212. // this.orderDetailsList.medical_imgs = this.orderDetailsList.medical_imgs.replace(/"/g,"")
  213. // this.orderDetailsList.medical_imgs = this.orderDetailsList.medical_imgs.replace("]"/g,"")
  214. // this.orderDetailsList.medical_imgs = this.orderDetailsList.medical_imgs.replace("["/g,"")
  215. // this.orderDetailsList.medical_imgs.forEach(function(element) {
  216. // element = element.replace(/\"/g, "");
  217. // element = element.replace("[","")
  218. // element = element.replace("]","")
  219. // });
  220. for (let a = 0; a < this.orderDetailsList.medical_imgs.length; a++) {
  221. this.orderDetailsList.medical_imgs[a] = this.orderDetailsList.medical_imgs[a].replace(/\"/g, "");
  222. this.orderDetailsList.medical_imgs[a] = this.orderDetailsList.medical_imgs[a].replace("[", "");
  223. this.orderDetailsList.medical_imgs[a] = this.orderDetailsList.medical_imgs[a].replace("]", "");
  224. }
  225. console.log("图片", this.orderDetailsList.medical_imgs)
  226. }
  227. },
  228. // ["https://wechat.fresherbaby.com/uploads/20210119/4IPuheVvP1s7hX5LvHoHcexiswK0DY4XGCNlplba.jpeg","https://wechat.fresherbaby.com/uploads/20210119/NECAiCuWs3mHwH7cb5YiqVhGiwTakxzawfYsK1Ii.jpeg","https://wechat.fresherbaby.com/uploads/20210119/FJkLJVfEBn3sjsltkhvCVh0vdnGfokcWoBVZi8sr.jpeg","https://wechat.fresherbaby.com/uploads/20210119/Ef0E7fgB0fiBf5HQaX6vlSqP2HGP3LnZ0D5i8pfd.jpeg"]
  229. // getMessage: async function(flag) {
  230. // let res = await this.$request.post("doctor/getImList", {
  231. // 'flag': flag
  232. // });
  233. // if (res.status == 0) {
  234. // if (res.data.length != 0) {
  235. // let newList = [];
  236. // for (let i = 0; i < res.data.length; i++) {
  237. // this.messages.push(res.data[i]);
  238. // // newList.push(res.data[i]);
  239. // }
  240. // }
  241. // this.scrollToBottom()
  242. // }
  243. // },
  244. renderMessageDate(message, index) {
  245. if (index === 0) {
  246. return this.formatDate(message.timestamp)
  247. } else {
  248. if (message.timestamp - this.messages[index - 1].timestamp > 5 * 60 * 1000) {
  249. return this.formatDate(message.timestamp)
  250. }
  251. }
  252. return ''
  253. },
  254. InputFocus(e) {
  255. this.InputBottom = e.detail.height
  256. },
  257. InputBlur(e) {
  258. this.InputBottom = 0
  259. },
  260. sendvoice() {
  261. this.isvoice = !this.isvoice
  262. },
  263. subscribeMessage() { //订阅消息
  264. },
  265. yulan(url) {
  266. uni.previewImage({
  267. urls: [url],
  268. current: url
  269. })
  270. },
  271. initialListeners() {
  272. //传入监听器,收到一条私聊消息总是滚到到页面底部
  273. this.imService.onNewPrivateMessageReceive = (friendId, message) => {
  274. if (friendId == this.friend.uuid) {
  275. this.imService.markPrivateMessageAsRead(friendId);
  276. //收到新消息,是滚动到最底部
  277. this.scrollToBottom()
  278. }
  279. };
  280. // 录音监听器
  281. this.initRecorderListeners();
  282. },
  283. initRecorderListeners() {
  284. let self = this;
  285. // 监听录音开始
  286. recorderManager.onStart(function() {
  287. self.audio.recording = true;
  288. });
  289. //录音结束后,发送
  290. recorderManager.onStop(function(res) {
  291. console.log(res)
  292. self.audio.recording = false;
  293. self.imService.sendPrivateAudioMessage(self.friend.uuid, res)
  294. });
  295. // 监听录音报错
  296. recorderManager.onError(function(res) {
  297. console.log("录音报错:", res);
  298. })
  299. },
  300. onRecordStart(event) {
  301. try {
  302. recorderManager.start();
  303. } catch (e) {
  304. uni.showModal({
  305. title: '发送语音错误',
  306. content: '请联系客服'
  307. });
  308. }
  309. event.preventDefault();
  310. },
  311. onRecordEnd() {
  312. try {
  313. recorderManager.stop();
  314. } catch (e) {
  315. uni.showModal({
  316. title: '发送语音错误',
  317. content: '请联系客服'
  318. });
  319. }
  320. },
  321. sendMessage() { //发送消息
  322. if (this.keyword.trim() != '') {
  323. console.log(this.friend.uuid)
  324. this.imService.sendPrivateTextMessage(this.friend.uuid, this.keyword);
  325. }
  326. this.keyword = "";
  327. },
  328. scrollToBottom() {
  329. this.$nextTick(function() {
  330. uni.pageScrollTo({
  331. scrollTop: 2000000,
  332. duration: 10
  333. })
  334. })
  335. console.log("滚动")
  336. },
  337. sendImage() {
  338. uni.chooseImage({
  339. count: 1,
  340. success: (res) => {
  341. this.imService.sendPrivateImageMessage(this.friend.uuid, res);
  342. }
  343. })
  344. },
  345. loadMoreHistoryMessage() { //历史消息
  346. let lastMessageTimeStamp = Date.now();
  347. let lastMessage = this.messages[0];
  348. if (lastMessage) {
  349. lastMessageTimeStamp = lastMessage.timestamp;
  350. }
  351. var currentLength = this.messages.length;
  352. let promise = this.imService.loadPrivateHistoryMessage(this.friend.uuid, lastMessageTimeStamp);
  353. promise.then(messages => {
  354. if (messages.length == currentLength) {
  355. this.allHistoryLoaded = true
  356. }
  357. this.messages = messages;
  358. uni.stopPullDownRefresh();
  359. }).catch(e => {
  360. console.log(e)
  361. uni.stopPullDownRefresh();
  362. })
  363. console.log(this.messages)
  364. },
  365. }
  366. };
  367. </script>
  368. <style lang="scss">
  369. page {
  370. padding-bottom: 100rpx;
  371. }
  372. .time-lag {
  373. font-size: 20rpx;
  374. text-align: center;
  375. }
  376. .record-loading {
  377. position: fixed;
  378. top: 50%;
  379. left: 50%;
  380. width: 300rpx;
  381. height: 300rpx;
  382. margin: -150rpx -150rpx;
  383. background: #262628;
  384. background: url("https://zhengda.oss-cn-chengdu.aliyuncs.com/baoma/static/recording-loading.gif") no-repeat center;
  385. background-size: 100%;
  386. border-radius: 40rpx;
  387. }
  388. // 顶部患者详情小图标
  389. .top_details {
  390. height: 50rpx;
  391. width: 100rpx;
  392. margin: 0 -50rpx;
  393. position: fixed;
  394. left: 50%;
  395. top: -20rpx;
  396. border: 1rpx solid #E4E4E4;
  397. border-radius: 20rpx;
  398. box-shadow: 0px 0px 0.5px #bfbfbf;
  399. display: flex;
  400. justify-content: center;
  401. align-items: flex-end;
  402. }
  403. .bot_details {
  404. height: 50rpx;
  405. width: 100rpx;
  406. margin: 0 -50rpx;
  407. position: relative;
  408. left: 50%;
  409. border-radius: 20rpx;
  410. display: flex;
  411. justify-content: center;
  412. align-items: flex-end;
  413. z-index: 99;
  414. }
  415. .details {
  416. margin: 20rpx 28rpx;
  417. font-size: 30rpx;
  418. view {
  419. margin-bottom: 20rpx;
  420. text {
  421. margin-left: 20rpx;
  422. font-weight: bold;
  423. }
  424. }
  425. }
  426. </style>