conversationWindow.vue 13 KB

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