consultation.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  1. <template>
  2. <view class="container">
  3. <view class="head-box">
  4. <view class="chose-patient">
  5. <view class="title-box" @click="addPatient()" v-if="patient==null">
  6. <text class="title">选择就诊人</text>
  7. <view class="right">
  8. <text class="value">请点击添加</text>
  9. <image src="/static/images/arrow_gray.png" mode=""></image>
  10. </view>
  11. </view>
  12. <view class="patient" @click="addPatient()" v-if="patient!=null">
  13. <view class="left">
  14. <view class="name">{{patient.patientName}}</view>
  15. <view class="info">
  16. <text class="text" v-if="patient.sex==1">男</text>
  17. <text class="text" v-if="patient.sex==2">女</text>
  18. <text class="text">{{$getAge(patient.birthday)}}岁</text>
  19. <text class="text">{{$parseIdCard(patient.idCard)}}</text>
  20. </view>
  21. </view>
  22. <view class="right">
  23. <image src="/static/images/arrow_gray.png" mode=""></image>
  24. </view>
  25. </view>
  26. </view>
  27. <view class="aitips">欢迎使用我的智能体。但我的智能体诊断和建议不能全权替代我本人诊断结论,仅供参考</view>
  28. </view>
  29. <scroll-view class="msg-scroll" :style="{height: height}" :scroll-top="scrollTop" scroll-y="true"
  30. :scroll-with-animation="true">
  31. <view class="msgs">
  32. <view class="msg-item" v-for="(item,index) in msgs">
  33. <view class="left" v-if="item.type==1">
  34. <image class="img" src="@/static/images/AI_Doctor_image_font_icon48.png"></image>
  35. <view class="msg-box">
  36. <view class="msg-content">
  37. <view class="">
  38. {{item.content}}
  39. </view>
  40. <view class="es-fs-24 es-mt-30 es-c-99">
  41. 内容由AI生成,非诊疗意见,不适请及时就医
  42. </view>
  43. </view>
  44. <!-- <view class="msg-btn-box">
  45. <view class="btn" @click="editMsgStatus(item)"
  46. v-if="item.msg!=null&&item.msg.status==0">回答不满意</view>
  47. <view class="btn" v-if="item.msg!=null" @click="copyMsg(item.content)">复制内容</view>
  48. </view> -->
  49. </view>
  50. </view>
  51. <view class="right" v-if="item.type==2">
  52. <view class="msg-content">{{item.content}}</view>
  53. <image class="img" :src="avatar"></image>
  54. </view>
  55. </view>
  56. <!-- <view class="msgEnd" v-show="msgEnd"><u-divider text="会话结束" :hairline="true"></u-divider></view> -->
  57. </view>
  58. </scroll-view>
  59. <view class="footer x-bc">
  60. <u-input placeholder="请输入想要咨询的问题..." border="surround" v-model="inputText" @confirm="handleInput"></u-input>
  61. <view class="send-box">
  62. <view class="send-btn x-ac" @click="handleInput()">发送</view>
  63. </view>
  64. </view>
  65. </view>
  66. </template>
  67. <script>
  68. import { aiChatListByUser } from "@/api/index.js"
  69. export default {
  70. data() {
  71. return {
  72. avatar: '',
  73. patient: null,
  74. statusBarHeight: uni.getSystemInfoSync().statusBarHeight,
  75. windowHeight: uni.getSystemInfoSync().screenHeight,
  76. height: '50vh',
  77. scrollTop: 0,
  78. msgs: [],
  79. inputText:"",
  80. roleId:null,
  81. msgTimes:null,
  82. isSocketOpen:false,
  83. socket:null,
  84. isSend:true,
  85. userInfo: {},
  86. doctorId: '',
  87. sessionId: undefined,
  88. msgEnd: false,
  89. doctorName: ""
  90. }
  91. },
  92. onLoad(option) {
  93. this.doctorName = option.doctorName || ''
  94. uni.setNavigationBarTitle({
  95. title: this.doctorName + "(智能体)"
  96. })
  97. this.doctorId = option.doctorId || ''
  98. this.height =
  99. `calc(${this.windowHeight}px - ${this.statusBarHeight}px - 196px)`
  100. this.roleId=option.roleId;
  101. this.roleName=option.roleName;
  102. this.userInfo = uni.getStorageSync("userInfo") ? JSON.parse(uni.getStorageSync("userInfo")) : {}
  103. this.avatar= this.userInfo.avatar || '';
  104. this.getMsgList();
  105. this.initSocket();
  106. uni.$on('refreshOrderPatient', (res) => {
  107. if(this.patient&&this.patient.patientId == res.patientId) {
  108. return
  109. }
  110. this.msgEnd = false
  111. this.patient = res
  112. this.getMsgList();
  113. })
  114. },
  115. onShow() {
  116. this.userInfo = uni.getStorageSync("userInfo") ? JSON.parse(uni.getStorageSync("userInfo")) : {}
  117. },
  118. methods: {
  119. // getTips() {
  120. // const that = this
  121. // uni.showModal({
  122. // title: '风险提示',
  123. // content: '内容由AI生成,仅供您参考使用,不作为疾病诊断治疗依据',
  124. // showCancel: false,
  125. // success: function (res) {
  126. // if (res.confirm) {
  127. // that.getMsgList();
  128. // that.initSocket();
  129. // }
  130. // }
  131. // });
  132. // },
  133. getHeight() {
  134. const query = uni.createSelectorQuery().in(this);
  135. query
  136. .select(".head-box")
  137. .boundingClientRect((data) => {
  138. this.height =
  139. `calc(${this.windowHeight}px - ${this.statusBarHeight}px - 94px - ${data.height}px)`
  140. })
  141. .exec();
  142. },
  143. addPatient() {
  144. uni.navigateTo({
  145. url: '/pages/user/patient'
  146. })
  147. },
  148. handleInput() {
  149. if(this.patient && this.patient.patientId) {
  150. if(this.msgEnd || !this.isSocketOpen) {
  151. // 重新发起会话
  152. this.initSocket()
  153. } else {
  154. this.sendMsg();
  155. }
  156. } else {
  157. uni.showToast({
  158. title: '请选择就诊人',
  159. icon: "none"
  160. })
  161. }
  162. },
  163. getMsgList() {
  164. if(this.patient && this.patient.patientId) {
  165. const that = this;
  166. const userId = this.userInfo.userId;
  167. const param = {
  168. doctorId: this.doctorId,
  169. userId: this.userInfo.userId,
  170. patientId: this.patient.patientId,
  171. }
  172. aiChatListByUser(param).then(res=>{
  173. setTimeout(()=>{
  174. this.getHeight()
  175. },200)
  176. let list = [{
  177. msgType: 2,
  178. content: '您好,我是'+this.doctorName+'医生智能体。我学习了海量的医学书籍、药品知识以及临床病例,我可以为您提供健康咨询、营养指导及医疗信息参考,如果您有健康方面的问题,可以随时向我咨询。'
  179. }]
  180. if(res.data&&res.data.length > 0) {
  181. list = list.concat(res.data)
  182. that.sessionId = res.data&&res.data.length > 0 ? res.data[0].sessionId : undefined
  183. }
  184. list.forEach(function(value, index, array) {
  185. that.addMsg(value.msgType == 1 ? 2 : 1, value.content, value, 1);
  186. });
  187. }).catch(()=>{
  188. setTimeout(()=>{
  189. this.getHeight()
  190. },200)
  191. })
  192. }
  193. },
  194. initSocket() {
  195. //创建一个socket连接
  196. var userId = this.userInfo.userId;
  197. var that = this;
  198. if (this.socket) {
  199. this.socket.close()
  200. this.socket = null;
  201. }
  202. this.socket = uni.connectSocket({
  203. url: getApp().globalData.wsUrl + "/app/webSocket/" + userId,
  204. multiple: true,
  205. success: res => {
  206. console.log('WebSocket连接已打开1!');
  207. that.isSocketOpen = true
  208. },
  209. error: res => {
  210. console.log(res)
  211. },
  212. })
  213. this.socket.onMessage((res) => {
  214. console.log("收到消息",res)
  215. that.isSend = true;
  216. that.addMsg(1, res.data, null, 2);
  217. })
  218. //监听socket打开
  219. this.socket.onOpen(() => {
  220. console.log('WebSocket连接已打开2!',that.msgEnd);
  221. that.isSocketOpen = true
  222. if(that.msgEnd) {
  223. // 重新发起会话
  224. that.isSend = true;
  225. that.sendMsg();
  226. }
  227. })
  228. //监听socket关闭
  229. this.socket.onClose(() => {
  230. that.isSocketOpen = false
  231. that.socket = null
  232. console.log('WebSocket连接已关闭!');
  233. that.msgEnd = true
  234. setTimeout(()=> {
  235. const query = uni.createSelectorQuery().in(this);
  236. query
  237. .select(".msgs")
  238. .boundingClientRect((res) => {
  239. if(res&&res.height) {
  240. const scrollH = res.height;
  241. that.scrollTop = scrollH;
  242. }
  243. })
  244. .exec();
  245. }, 500)
  246. if(that.msgTimes) {
  247. clearInterval(that.msgTimes)
  248. that.msgTimes = null
  249. }
  250. })
  251. //监听socket错误
  252. this.socket.onError(() => {
  253. that.isSocketOpen = false
  254. that.socket = null
  255. that.msgEnd = true
  256. console.log('WebSocket连接打开失败');
  257. if(that.msgTimes) {
  258. clearInterval(that.msgTimes)
  259. that.msgTimes = null
  260. }
  261. })
  262. },
  263. sendMsg() {
  264. if (this.inputText == "") {
  265. return;
  266. }
  267. if (!this.isSend) {
  268. return;
  269. }
  270. if (this.isSocketOpen) {
  271. var userId = this.userInfo.userId;
  272. var data = {
  273. userId: this.userInfo.userId,
  274. doctorId: this.doctorId,
  275. message: this.inputText,
  276. sessionId: this.sessionId,
  277. patientId: this.patient.patientId
  278. };
  279. this.socket.send({
  280. data: JSON.stringify(data),
  281. success: () => {
  282. console.log("发送成功")
  283. this.addMsg(2, this.inputText, null, 1);
  284. this.addMsg(1, "正在思考中...", null, 1);
  285. this.isSend = false;
  286. },
  287. fail: () => {
  288. console.log("发送失败")
  289. }
  290. });
  291. }
  292. },
  293. addMsg(type, content, msg, inputType) {
  294. this.msgEnd = false
  295. var obj = {
  296. type: type,
  297. content: content,
  298. msg: msg
  299. }
  300. if (inputType == 2) {
  301. this.msgs.splice(-1);
  302. this.msgs.push(obj);
  303. } else if (inputType == 1) {
  304. this.msgs.push(obj)
  305. }
  306. this.inputText = ""
  307. var that = this;
  308. setTimeout(()=> {
  309. const query = uni.createSelectorQuery().in(this);
  310. query
  311. .select(".msgs")
  312. .boundingClientRect((res) => {
  313. if(res&&res.height) {
  314. const scrollH = res.height;
  315. that.scrollTop = scrollH;
  316. }
  317. })
  318. .exec();
  319. }, 500)
  320. //先确保清除了之前的消息定时器
  321. if(that.msgTimes) {
  322. clearInterval(that.msgTimes)
  323. that.msgTimes = null
  324. }
  325. // 5分钟无消息自动结束
  326. that.msgTimes=setInterval(()=>{
  327. console.log("5分钟无消息自动结束")
  328. clearInterval(that.msgTimes)
  329. if(this.socket!=null){
  330. this.socket.close()
  331. }
  332. that.msgTimes = null
  333. },300000)
  334. },
  335. },
  336. onUnload() {
  337. this.msgEnd = true
  338. if(this.socket!=null){
  339. this.socket.close()
  340. }
  341. if(this.msgTimes) {
  342. clearInterval(this.msgTimes)
  343. this.msgTimes = null
  344. }
  345. uni.$off('refreshOrderPatient')
  346. }
  347. }
  348. </script>
  349. <style lang="scss" scoped>
  350. .head-box {
  351. padding: 15rpx 15rpx 0 15rpx;
  352. }
  353. .chose-patient {
  354. // margin: 15rpx 15rpx;
  355. margin-bottom: 15rpx;
  356. padding: 30rpx 40rpx;
  357. box-shadow: 0px 0px 5px 2px rgba(0, 0, 0, 0.05);
  358. background-color: #fff;
  359. border-radius: 15rpx;
  360. .title-box {
  361. display: flex;
  362. align-items: center;
  363. justify-content: space-between;
  364. .title {
  365. font-size: 32upx;
  366. font-family: PingFang SC;
  367. font-weight: bold;
  368. color: #111111;
  369. }
  370. .right {
  371. height: 100%;
  372. display: flex;
  373. align-items: center;
  374. justify-content: center;
  375. .value {
  376. font-size: 28upx;
  377. font-family: PingFang SC;
  378. color: #999;
  379. margin-right: 10rpx;
  380. }
  381. image {
  382. width: 15upx;
  383. height: 30upx;
  384. }
  385. }
  386. }
  387. .patient {
  388. display: flex;
  389. align-items: center;
  390. justify-content: space-between;
  391. height: 110upx;
  392. .left {
  393. .name {
  394. font-size: 30upx;
  395. line-height: 1;
  396. font-family: PingFang SC;
  397. font-weight: bold;
  398. color: #111111;
  399. }
  400. .info {
  401. margin-top: 30rpx;
  402. display: flex;
  403. align-items: center;
  404. .text {
  405. font-size: 26upx;
  406. font-family: PingFang SC;
  407. line-height: 1;
  408. font-weight: 500;
  409. color: #999;
  410. margin-right: 19upx;
  411. }
  412. }
  413. }
  414. .right {
  415. display: flex;
  416. align-items: center;
  417. image {
  418. width: 15upx;
  419. height: 30upx;
  420. }
  421. }
  422. }
  423. }
  424. .aitips {
  425. font-size: 24rpx;
  426. font-family: PingFang SC;
  427. color: #999;
  428. text-align: center;
  429. padding: 20rpx;
  430. box-sizing: border-box;
  431. }
  432. .msgEnd {
  433. padding: 20rpx 100rpx;
  434. }
  435. .footer {
  436. padding: 10rpx;
  437. width: 100%;
  438. background-color: #fff;
  439. padding-bottom: calc(var(--window-bottom) + 10rpx);
  440. position: fixed;
  441. bottom: 0;
  442. left: 0;
  443. .send-btn {
  444. margin-left: 20rpx;
  445. padding: 20rpx 40rpx;
  446. min-height: 76rpx;
  447. border-radius: 10rpx;
  448. box-sizing: border-box;
  449. font-family: PingFang SC, PingFang SC;
  450. font-weight: 500;
  451. font-size: 30rpx;
  452. color: #FFFFFF;
  453. background-color: #FF5030;
  454. }
  455. }
  456. .msg-scroll {
  457. height: calc(100vh - 120px);
  458. .msgs {
  459. width: 100%;
  460. .msg-item {
  461. padding: 15rpx;
  462. box-sizing: border-box;
  463. display: flex;
  464. flex-direction: column;
  465. justify-content: center;
  466. align-items: flex-start;
  467. width: 100%;
  468. .left {
  469. width: 100%;
  470. display: flex;
  471. justify-content: flex-start;
  472. align-items: flex-start;
  473. .img {
  474. min-width: 50px;
  475. width: 50px;
  476. height: 50px;
  477. border-radius: 50%;
  478. image {
  479. width: 100%;
  480. height: 100%;
  481. }
  482. }
  483. .msg-box {
  484. max-width: 80%;
  485. // margin-top: 10px;
  486. .msg-content {
  487. max-width: 100%;
  488. border-radius: 0 10px 10px 10px;
  489. padding: 10px;
  490. margin-top: 15px;
  491. margin-left: 10px;
  492. background-color: #fff;
  493. color: #111;
  494. font-size: 16px;
  495. font-family: PingFang SC;
  496. word-wrap: break-word;
  497. /* 旧版浏览器支持 */
  498. overflow-wrap: break-word;
  499. /* 现代浏览器支持 */
  500. white-space: normal;
  501. /* 确保连续文本不会超出容器 */
  502. }
  503. .msg-btn-box {
  504. max-width: 100%;
  505. margin-top: 10px;
  506. display: flex;
  507. justify-content: flex-start;
  508. align-items: flex-start;
  509. .btn {
  510. margin-right: 15px;
  511. display: flex;
  512. justify-content: center;
  513. align-items: center;
  514. border-radius: 10px;
  515. padding: 10px 20px;
  516. background-color: #fff;
  517. color: #FF5030;
  518. font-size: 14px;
  519. }
  520. }
  521. }
  522. }
  523. .right {
  524. width: 100%;
  525. display: flex;
  526. justify-content: flex-end;
  527. align-items: flex-start;
  528. .msg-content {
  529. max-width: 80%;
  530. margin-top: 15px;
  531. margin-right: 10px;
  532. border-radius: 10px 0 10px 10px;
  533. padding: 10px;
  534. background-color: #FF5030;
  535. color: #fff;
  536. font-size: 16px;
  537. font-family: PingFang SC;
  538. word-wrap: break-word;
  539. /* 旧版浏览器支持 */
  540. overflow-wrap: break-word;
  541. /* 现代浏览器支持 */
  542. white-space: normal;
  543. /* 确保连续文本不会超出容器 */
  544. }
  545. .img {
  546. min-width: 50px;
  547. width: 50px;
  548. height: 50px;
  549. border-radius: 50%;
  550. image {
  551. width: 100%;
  552. height: 100%;
  553. }
  554. }
  555. }
  556. }
  557. }
  558. }
  559. </style>