index.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. <template>
  2. <!-- 禁止滚动穿透 -->
  3. <page-meta :page-style="'overflow:'+(show?'hidden':'visible')"></page-meta>
  4. <view class="main">
  5. <view class="content">
  6. <scroll-view scroll-y="true" class="scroll-view" :scroll-into-view="poaMessgae" scroll-with-animation="true"
  7. refresher-enabled="true" :refresher-triggered="triggered" @refresherrefresh="getFresh">
  8. <view class="list-scroll-view">
  9. <view class="block" v-for="(item,index) in list" :key="index">
  10. <!-- 显示时间 -->
  11. <view class="time" v-if="index == 0 || item.time - list[index-1].time >= 300000">
  12. <text>{{formatDate(item.time)}}</text>
  13. </view>
  14. <!-- 判断是再左边显示还是右边显示 -->
  15. <view class="list" :id="item.speaker._id != user._id ? 'left' : 'right'">
  16. <!-- 用户身份并且数据发送类型是护士的显示医生信息 -->
  17. <image class="avatar" v-if="user.role=='Patient'&&item.speaker_type=='Nurse'"
  18. @tap="toInfo(doctorInfo,'0')"
  19. :src="doctorInfo.icon&&doctorInfo.icon.length>0?doctorInfo.icon[0].url:config.user_url[0].url"
  20. mode=""></image>
  21. <!-- 否则正常显示 -->
  22. <image class="avatar" v-else @tap="toInfo(item,'1')"
  23. :src="item.speaker&&item.speaker.icon.length>0?item.speaker.icon[0].url:config.user_url[0].url"
  24. mode=""></image>
  25. <!-- 信息类型是文字 -->
  26. <text class="record" v-if="item.type == 'text'">{{item.content}}</text>
  27. <!-- 信息类型是图片 -->
  28. <image :src="item.content" v-if="item.type == 'image'" @tap="onPreview(item.content)"
  29. mode="widthFix">
  30. </image>
  31. <!-- 信息类型是语音 -->
  32. <view class="record" v-if="item.type == 'record'" @tap="onPlay(item.content,index)">
  33. <uni-icons v-if="item.speaker._id != user._id" custom-prefix="iconfont"
  34. type="icon-zuobofang" size="15" color="#333333">
  35. </uni-icons>
  36. <text class="voice">{{item.voice}}</text>"
  37. <uni-icons v-if="item.speaker._id == user._id" custom-prefix="iconfont"
  38. type="icon-youbofang" size="15" color="#ffffff">
  39. </uni-icons>
  40. </view>
  41. </view>
  42. </view>
  43. <view id="poaMessgae"></view> <!-- 仅用于定位到消息最后一条 -->
  44. </view>
  45. </scroll-view>
  46. </view>
  47. <!-- 底部 -->
  48. <view class="bottom">
  49. <submit_1 :record="record" :inrecord="inrecord" :isRecorder="isRecorder" @toSend="toSend" @heights="heights"
  50. @onRecorderchange="onRecorderchange" @onStart="onStart" @onEnd="onEnd"></submit_1>
  51. </view>
  52. <!-- 语音 -->
  53. <uni-popup ref="voice" background-color="#fff">
  54. <view class="image">
  55. <image class="avatar" :src="config.audio_url[0].url"></image>
  56. </view>
  57. </uni-popup>
  58. <!-- 说话用户详情 -->
  59. <uni-popup ref="speaker" background-color="#fff" :is-mask-click="false" @change="change">
  60. <view class="popup">
  61. <view class="close">
  62. <text>用户详情</text>
  63. <uni-icons @tap="toClose" type="close" size="20"></uni-icons>
  64. </view>
  65. <view class="info_1">
  66. <speaker_1 class="speaker_1" :speakerInfo="speakerInfo" :type="type" @onPreview="onPreview">
  67. </speaker_1>
  68. </view>
  69. </view>
  70. </uni-popup>
  71. </view>
  72. </template>
  73. <script>
  74. import WebSocket from "@/common/websocket.js"
  75. import submit_1 from './common/submit_1.vue';
  76. import speaker_1 from './common/speaker_1.vue';
  77. export default {
  78. components: {
  79. submit_1,
  80. speaker_1
  81. },
  82. data() {
  83. return {
  84. id: '',
  85. // 群
  86. groupId: '',
  87. // 病人
  88. patientId: '',
  89. // 医生
  90. doctorId: '',
  91. // 用户id
  92. userId: '',
  93. user: {},
  94. config: {},
  95. // 医生信息
  96. doctorInfo: {},
  97. // 说话人信息
  98. speakerInfo: {},
  99. // 说话人类型
  100. type: '',
  101. // 聊天列表数据
  102. list: [],
  103. total: 0,
  104. skip: 0,
  105. limit: 6,
  106. page: 0,
  107. record: '', // 全局唯一录音管理区
  108. inrecord: false, // 是否处于录音状态
  109. isRecorder: false, // 是否处于语音状态
  110. bgAudioManager: '', // 全局音频播放
  111. poaMessgae: 'poaMessgae', //滚动到最底部
  112. // 判断是否跳到最后一条
  113. is_bottom: true,
  114. // 判断是否下拉刷新复位
  115. triggered: false,
  116. // 是否发送消息
  117. is_send: true,
  118. //实时通信
  119. client: null,
  120. subscription: null,
  121. // 禁止滚动穿透
  122. show: false
  123. }
  124. },
  125. onLoad: async function(e) {
  126. const that = this;
  127. that.$set(that, `groupId`, e && e.groupId || '');
  128. that.$set(that, `patientId`, e && e.patientId || '');
  129. that.$set(that, `doctorId`, e && e.doctorId || '');
  130. uni.setNavigationBarTitle({
  131. title: e && e.title || '聊天室'
  132. });
  133. await that.searchToken();
  134. await that.searchConfig();
  135. await that.searchOther();
  136. await that.search();
  137. },
  138. onShow: async function() {
  139. const that = this;
  140. // 全局唯一录音管理器
  141. that.record = uni.getRecorderManager();
  142. // 滚动到最底部
  143. that.poalast()
  144. // stomp协议请求 实时链接
  145. that.initWS()
  146. },
  147. // 离开页面是关闭连接
  148. // 我的业务是仿微信这种,每次连接人不同,频繁建立新连接,根据自己需要决定什么时候关闭连接
  149. onUnload: function() {
  150. const that = this;
  151. that.client && that.client.close()
  152. },
  153. methods: {
  154. // 禁止滚动穿透
  155. change(e) {
  156. const that = this;
  157. that.show = e.show
  158. },
  159. // 用户信息
  160. searchToken() {
  161. const that = this;
  162. try {
  163. const res = uni.getStorageSync('token');
  164. if (res) {
  165. const user = that.$jwt(res);
  166. that.$set(that, `user`, user);
  167. }
  168. } catch (e) {}
  169. },
  170. // 基本信息
  171. searchConfig() {
  172. const that = this;
  173. try {
  174. const res = uni.getStorageSync('config');
  175. if (res) that.$set(that, `config`, res);
  176. } catch (e) {}
  177. },
  178. // 查询聊天记录
  179. async search() {
  180. const that = this;
  181. // 聊天记录
  182. let info = {
  183. skip: that.skip,
  184. limit: that.limit,
  185. patient: that.patientId,
  186. group: that.groupId,
  187. doctor: that.doctorId
  188. }
  189. const res = await that.$api(`/chat`, 'GET', {
  190. ...info
  191. })
  192. if (res.errcode == '0') {
  193. let list;
  194. if (that.is_send) list = [...res.data.reverse(), ...that.list];
  195. else list = [...res.data.reverse()];
  196. that.$set(that, `list`, list)
  197. that.$set(that, `total`, res.total)
  198. that.$set(that, `is_send`, true);
  199. } else {
  200. uni.showToast({
  201. title: res.errmsg,
  202. icon: 'none'
  203. });
  204. }
  205. },
  206. // 处理时间
  207. formatDate(value) {
  208. if (typeof(value) == 'undefined') {
  209. return ''
  210. } else {
  211. let date = new Date(value)
  212. let now = new Date()
  213. let y = date.getFullYear()
  214. let MM = date.getMonth() + 1
  215. MM = MM < 10 ? ('0' + MM) : MM
  216. let d = date.getDate()
  217. d = d < 10 ? ('0' + d) : d
  218. let h = date.getHours()
  219. h = h < 10 ? ('0' + h) : h
  220. let m = date.getMinutes()
  221. m = m < 10 ? ('0' + m) : m
  222. let s = date.getSeconds()
  223. s = s < 10 ? ('0' + s) : s
  224. if (now.getDate() - d == 1 && now - date < 172800000) {
  225. return '昨天' + h + ':' + m
  226. } else if (now - date < 86400000) {
  227. return h + ':' + m
  228. } else if (now - date >= 86400000 && now - date < 31536000000) {
  229. return MM + '-' + d + ' ' + h + ':' + m
  230. } else if (now - date >= 31536000000) {
  231. return y + '-' + MM + '-' + d + ' ' + h + ':' + m
  232. }
  233. }
  234. },
  235. //输入框高度
  236. heights(e) {
  237. const that = this;
  238. that.poalast()
  239. },
  240. // 切换语音 or 键盘
  241. onRecorderchange() {
  242. const that = this
  243. if (that.isRecorder) {
  244. that.isRecorder = false
  245. } else {
  246. uni.authorize({
  247. scope: 'scope.record',
  248. success: res => {
  249. that.isRecorder = true
  250. }
  251. })
  252. }
  253. },
  254. // 长按开始录入语音
  255. onStart() {
  256. const that = this
  257. that.$set(that, `inrecord`, true);
  258. that.$refs.voice.open('center')
  259. that.record.start({
  260. format: "mp3"
  261. })
  262. },
  263. // 长按结束录入语音
  264. onEnd() {
  265. const that = this
  266. const fileUrl = that.$config.serverUrl;
  267. that.$set(that, `inrecord`, false);
  268. that.$refs.voice.close('center')
  269. that.record.stop()
  270. // 监听录音暂停的回调参数
  271. that.record.onStop(res => {
  272. if (res.duration < 1000) {
  273. return that.global.toast('请说话久一点')
  274. }
  275. // 上传音频
  276. uni.uploadFile({
  277. url: `${fileUrl}/visit/api/files/chat_voice/${that.user._id}/upload`,
  278. filePath: res.tempFilePath,
  279. name: `file`,
  280. formData: {},
  281. success: (arr) => {
  282. const file = JSON.parse(arr.data)
  283. let mess = {
  284. type: 'record',
  285. voice: Math.round(res.duration / 1000), // 时长
  286. content: fileUrl + file.data.uri,
  287. time: new Date() - 0
  288. }
  289. // 写在上传回调内
  290. that.toSend(mess)
  291. },
  292. fail: (err) => {
  293. console.log('uploadImage fail', err);
  294. },
  295. });
  296. })
  297. that.record.onError(err => {
  298. that.global.toast('获取录音失败')
  299. })
  300. },
  301. // 播放音频
  302. onPlay(e, index) {
  303. const that = this
  304. that.bgAudioManager = uni.createInnerAudioContext()
  305. that.bgAudioManager.src = e
  306. that.bgAudioManager.play()
  307. that.bgAudioManager.onEnded(() => {
  308. that.bgAudioManager.offPlay()
  309. that.bgAudioManager.offEnded()
  310. that.bgAudioManager = null
  311. })
  312. },
  313. // 预览图片
  314. onPreview(e) {
  315. uni.previewImage({
  316. urls: [e]
  317. })
  318. },
  319. // 点击详情
  320. toInfo(item, type) {
  321. const that = this
  322. if (type == '0') {
  323. that.$set(that, `speakerInfo`, item);
  324. that.$set(that, `type`, 'Doctor');
  325. } else {
  326. that.$set(that, `speakerInfo`, item.speaker);
  327. that.$set(that, `type`, item.speaker_type);
  328. }
  329. that.$refs.speaker.open()
  330. },
  331. // 关闭弹框
  332. toClose() {
  333. const that = this;
  334. that.$refs.speaker.close();
  335. that.$set(that, `speakerInfo`, {})
  336. that.$set(that, `type`, '')
  337. },
  338. // 消息链接
  339. initWS() {
  340. const that = this
  341. const brokerURL = that.$config.brokerURL;
  342. WebSocket.init(
  343. `${brokerURL}`,
  344. // 传参
  345. {
  346. host: 'visit',
  347. login: 'visit',
  348. passcode: 'visit'
  349. },
  350. // ws断开回调
  351. () => {
  352. that.initWS()
  353. }
  354. ).then((client) => {
  355. that.client = client
  356. // 订阅
  357. that.subscription = client.subscribe(
  358. // 路径
  359. `/exchange/chatEx/${that.groupId}_${that.patientId}`,
  360. // 接收到的数据
  361. (res) => {
  362. console.log(res);
  363. that.list.push(JSON.parse(res.body))
  364. // 滚动到最底部
  365. that.poalast()
  366. },
  367. )
  368. })
  369. },
  370. // 发送消息
  371. async toSend(e) {
  372. const that = this;
  373. const mess = {
  374. speaker: that.user._id,
  375. speaker_type: that.user.role,
  376. group: that.groupId,
  377. patient: that.patientId,
  378. doctor: that.doctorId,
  379. }
  380. const data = {
  381. ...e,
  382. ...mess
  383. }
  384. let res = await that.$api(`/chat`, 'POST', data)
  385. if (res.errcode == '0') {
  386. that.$set(that, `is_send`, false);
  387. that.search()
  388. that.$set(that, `isRecorder`, false);
  389. that.poalast() // 定位消息最后一行
  390. } else {
  391. uni.showToast({
  392. title: res.errmsg,
  393. icon: 'none'
  394. });
  395. }
  396. },
  397. // 下拉刷新分页
  398. getFresh(e) {
  399. const that = this;
  400. that.$set(that, `triggered`, true)
  401. let list = that.list;
  402. let limit = that.limit;
  403. setTimeout(() => {
  404. if (that.total > list.length) {
  405. uni.showLoading({
  406. title: '加载中',
  407. mask: true
  408. })
  409. let page = that.page + 1;
  410. that.$set(that, `page`, page)
  411. let skip = page * limit;
  412. that.$set(that, `skip`, skip)
  413. that.$set(that, `is_bottom`, false)
  414. that.search();
  415. uni.hideLoading();
  416. } else {
  417. uni.showToast({
  418. title: `没有更多聊天记录了`,
  419. icon: 'none'
  420. })
  421. }
  422. that.triggered = false;
  423. }, 1000)
  424. },
  425. // 定位到消息最后一行
  426. poalast() {
  427. const that = this
  428. that.$nextTick(() => {
  429. that.$set(that, `poaMessgae`, '');
  430. setTimeout(() => {
  431. that.$set(that, `poaMessgae`, 'poaMessgae');
  432. }, 50)
  433. })
  434. },
  435. // 其他查询
  436. async searchOther() {
  437. const that = this
  438. let res;
  439. // 医生信息
  440. res = await that.$api(`/doctor/${that.doctorId}`, 'GET', {})
  441. if (res.errcode == '0') {
  442. that.$set(that, `doctorInfo`, res.data)
  443. } else {
  444. uni.showToast({
  445. title: res.errmsg,
  446. icon: 'none'
  447. });
  448. }
  449. // 处理已读未读
  450. },
  451. }
  452. }
  453. </script>
  454. <style scoped lang="scss">
  455. .main {
  456. display: flex;
  457. flex-direction: column;
  458. box-sizing: border-box;
  459. width: 100vw;
  460. height: 100vh;
  461. .content {
  462. position: relative;
  463. flex-grow: 1;
  464. .time {
  465. display: flex;
  466. align-items: center;
  467. flex-direction: column;
  468. margin: 2vw 0;
  469. text {
  470. background: rgba(0, 0, 0, 0.6);
  471. color: var(--f6Color);
  472. padding: 1vw 2vw;
  473. border-radius: 5px;
  474. font-size: var(--font12Size);
  475. }
  476. }
  477. .list {
  478. display: flex;
  479. align-items: flex-start;
  480. margin-bottom: 4vw;
  481. .avatar {
  482. border-radius: 50%;
  483. width: 10vw;
  484. height: 10vw;
  485. }
  486. image {
  487. width: 20vw;
  488. }
  489. text {
  490. font-size: var(--font14Size);
  491. font-family: PingFang SC-Regular, PingFang SC;
  492. font-weight: 400;
  493. max-width: 70vw;
  494. }
  495. .record {
  496. display: flex;
  497. align-items: center;
  498. word-break: break-all;
  499. .voice {
  500. margin: 0 1vw;
  501. }
  502. }
  503. }
  504. #left {
  505. .avatar {
  506. margin-right: 2vw;
  507. }
  508. .record {
  509. background: #F4F8FB;
  510. border-radius: 0rpx 2vw 2vw 2vw;
  511. padding: 2vw 3vw;
  512. color: var(--f33Color);
  513. }
  514. }
  515. #right {
  516. flex-direction: row-reverse;
  517. .avatar {
  518. margin-left: 2vw;
  519. }
  520. .record {
  521. background: #6B72F6;
  522. border-radius: 2vw 0 2vw 2vw;
  523. padding: 2vw 3vw;
  524. color: var(--mainColor);
  525. }
  526. }
  527. }
  528. .bottom {
  529. background: var(--mainColor);
  530. }
  531. }
  532. .image {
  533. display: flex;
  534. align-items: center;
  535. .avatar {
  536. width: 50vw;
  537. height: 50vw;
  538. }
  539. }
  540. .uni-popup {
  541. z-index: 9999 !important;
  542. }
  543. .popup {
  544. display: flex;
  545. flex-direction: column;
  546. width: 90vw;
  547. height: 45vh;
  548. .close {
  549. display: flex;
  550. justify-content: space-between;
  551. padding: 2vw;
  552. text:first-child {
  553. font-size: var(--font16Size);
  554. font-weight: bold;
  555. }
  556. }
  557. .info_1 {
  558. position: relative;
  559. display: flex;
  560. flex-direction: column;
  561. max-height: 40vh;
  562. padding: 2vw;
  563. align-items: center;
  564. .speaker_1 {
  565. width: 100%;
  566. }
  567. }
  568. }
  569. .scroll-view {
  570. position: absolute;
  571. top: 0;
  572. left: 0;
  573. right: 0;
  574. bottom: 0;
  575. .list-scroll-view {
  576. display: flex;
  577. flex-direction: column;
  578. margin: 0 2vw;
  579. }
  580. }
  581. </style>