import dayjs from "dayjs"; const app = getApp(); interface IMessageItem { msgId: string; contentType: "1" | "2" | "3" | "4" | "5"; // 1:文本 2: 语音 3. 图片 4. 视频 5. 提示信息 content: any; fromDoctorId: string; doctorImg: string; msgVisitTime: string; createTime: ""; isSelf: 1 | 2; isRead: 1 | 2; } interface Item { questionId: string; question: string; } Page({ data: { id: "", show1: false, fold: true, scrollTop: 0, scrollIntoView: "", doctorDetail: {} as any, questionActive: 0, questionList: [] as any, questionActiveList: [] as Item[], roomDoctors: [], newRoomDoctors: [], case: {} as any, chatGroupInfo: {}, chatDoctor: {}, isLoad: false, isFinish: false, messageList: [ { msgId: "686", contentType: "2", msgContent: "重症肌无力患者复查的频率取决于多种因素", welcomeMsg: "", msgVisitTime: "", msgFromType: "2", msgCreateTime: "2024-10-10 09:37:40", }, { msgId: "686", contentType: "3", msgContent: "重症肌无力患者复查的频率取决于多种因素", welcomeMsg: "", msgVisitTime: "", msgFromType: "2", msgCreateTime: "2024-10-10 09:37:40", }, { msgId: "686", contentType: "2", msgContent: "重症肌无力患者复查的频率取决于多种因素", welcomeMsg: "", msgVisitTime: "", msgFromType: "1", msgCreateTime: "2024-10-10 09:37:40", }, { msgId: "686", contentType: "3", msgContent: "重症肌无力患者复查的频率取决于多种因素", welcomeMsg: "", msgVisitTime: "", msgFromType: "1", msgCreateTime: "2024-10-10 09:37:40", }, { msgId: "681", contentType: "1", msgContent: "重症肌无力是否有遗传性?", welcomeMsg: "", msgVisitTime: "", msgFromType: "1", msgCreateTime: "2024-10-10 09:37:17", }, { msgId: "681", contentType: "1", msgContent: "重症肌无力是否有遗传性?", welcomeMsg: "", msgVisitTime: "", msgFromType: "1", msgCreateTime: "2024-10-10 09:37:17", }, { msgId: "483", contentType: "4", msgContent: '[{"duration":8,"url":"https://circlehbsaas.oss-cn-beijing.aliyuncs.com/audio/20240907757_tmp_acf4f18c59553a8d2d9f74e173a16328.m4a.mp3"}]', welcomeMsg: "", msgVisitTime: "", msgFromType: "2", msgCreateTime: "2024-10-08 15:24:19", }, { msgId: "483", contentType: "4", msgContent: '[{"duration":8,"url":"https://circlehbsaas.oss-cn-beijing.aliyuncs.com/audio/20240907757_tmp_acf4f18c59553a8d2d9f74e173a16328.m4a.mp3"}]', welcomeMsg: "", msgVisitTime: "", msgFromType: "1", msgCreateTime: "2024-10-08 15:24:19", }, ], lastMsgId: "", unReadCount: 0, firstNotReadMsgId: 0, isVisitAdl: 2, adlMsgId: 0, week: ["周一", "周二", "周三", "周四", "周五", "周六", "周日"], timeDay: { 1: "上午", 2: "下午" }, userInfo: {}, }, innerAudioContext: null as WechatMiniprogram.InnerAudioContext | null, videoContext: null as WechatMiniprogram.VideoContext | null, onLoad(options) { this.setData({ id: options.id, }); this.videoContext = wx.createVideoContext("video"); app.waitLogin().then(() => { // this.getAgree(); this.getMessageList(); this.getDetail(); }); }, onUnload() { if (this.innerAudioContext) { this.innerAudioContext.stop(); } }, getAgree() { wx.ajax({ method: "GET", url: "?r=takeda/chat/get-agree", data: { roomId: this.data.id, }, }).then((res) => { this.setData({ show1: res === 2, }); }); }, onAgreeClose() { wx.navigateBack(); }, handleSubmitAgree() { wx.ajax({ method: "POST", url: "?r=takeda/chat/agree-room", data: { roomId: this.data.id, }, }).then(() => { this.setData({ show1: false, }); }); }, getDetail() { wx.ajax({ method: "GET", url: "?r=takeda/chat/get-room-detail", data: { roomId: this.data.id }, }).then((res) => { this.setData({ roomDoctors: res.roomDoctors, newRoomDoctors: res.roomDoctors.slice(0, 4), case: res.case, chatGroupInfo: res.chatGroupInfo, chatDoctor: res.chatDoctor, }); }); }, handleRoomDetail() { wx.navigateTo({ url: `/module1/pages/chatRoomInfo/index?id=${this.data.id}&cid=${this.data.case.caseId}`, }); }, handleView(index = -1) { this.setData({ scrollIntoView: index > -1 ? `view${index}` : `place`, }); }, getDoctorDetail() { wx.ajax({ method: "GET", url: "?r=zd/message-interact/get-bind-doctor-info", data: {}, }).then((res) => { this.setData({ doctorDetail: { ...res, }, }); }); }, getQuestionList() { wx.ajax({ method: "GET", url: "?r=zd/message-interact/get-question-list", data: {}, }).then((res) => { this.setData({ questionList: res, questionActiveList: res[0].questionList, }); }); }, getMessageList() { if (this.data.isLoad || this.data.isFinish) return; this.setData({ isLoad: true, }); const lastMsgId = this.data.lastMsgId; wx.ajax({ method: "GET", url: "?r=takeda/chat/get-message-list", data: { lastMsgId, roomId: this.data.id, }, }).then((res) => { res.map((item) => { //消息内容类型,1-文本,2-语音,3-图片,4-视频,5-提示信息 if (["2", "3", "4"].includes(item.contentType)) { item.msgContent = JSON.parse(item.msgContent); } return item; }); this.setData({ messageList: [...res.messageList.reverse(), ...this.data.messageList], lastMsgId: res.lastMsgId, unReadCount: res.unReadCount, firstNotReadMsgId: res.firstNotReadMsgId, isVisitAdl: res.isVisitAdl, adlMsgId: res.adlMsgId, isLoad: false, isFinish: lastMsgId === res.lastMsgId, }); this.filterCreateTime(); if (!lastMsgId) { this.handleView(); } }); }, scrolltoupper(e) { // if (e.detail.direction === 'top') { // this.getMessageList() // } }, handleHot(e) { const { index, mindex } = e.currentTarget.dataset; const { messageList } = this.data; const hotList = messageList[mindex].welcomeMsg?.hotQuestion; const hot = hotList?.[index]; this.setData({ messageList: [ ...messageList, { msgId: "", contentType: "1", msgContent: hot?.question, msgVisitTime: "", msgFromType: "1", msgCreateTime: dayjs().format("YYYY-MM-DD HH:mm:ss"), }, ], }); this.handleView(); this.sendQuestion(hot?.questionId); }, handleQuestion(e) { const { index } = e.currentTarget.dataset; const { questionActiveList, messageList } = this.data; const question = questionActiveList[index]; this.setData({ messageList: [ ...messageList, { msgId: "", contentType: "1", msgContent: question.question, msgVisitTime: "", msgFromType: "1", msgCreateTime: dayjs().format("YYYY-MM-DD HH:mm:ss"), }, ], }); this.sendQuestion(question.questionId); }, sendQuestion(questionId) { wx.ajax({ method: "POST", url: "?r=zd/message-interact/send-question-message", data: { questionId, }, }).then((res) => { this.setData({ messageList: [ ...this.data.messageList, { msgId: "", contentType: "1", msgContent: res, msgVisitTime: "", msgFromType: "2", msgCreateTime: dayjs().format("YYYY-MM-DD HH:mm:ss"), }, ], }); this.filterCreateTime(); this.handleView(); }); }, formatTime(date: string) { let msgCreateTimeName = ""; if (dayjs().format("YYYY-MM-DD") === dayjs(date).format("YYYY-MM-DD")) { msgCreateTimeName = dayjs(date).format("HH:mm"); } else if (dayjs().format("YYYY-MM-DD") === dayjs(date).add(1, "day").format("YYYY-MM-DD")) { msgCreateTimeName = `昨天 ${dayjs(date).format("HH:mm")}`; } else if (dayjs().diff(date, "day") < 7) { msgCreateTimeName = dayjs(date).format(`dddd HH:mm`); } else { msgCreateTimeName = dayjs(date).format("YYYY-MM-DD HH:mm"); } return msgCreateTimeName; }, filterCreateTime() { const gapTime = 5 * 60 * 1000; const { messageList } = this.data; let preTime = 0; messageList.forEach((item, index) => { if (index === 0) { item.showTime = true; preTime = dayjs(item.msgCreateTime).valueOf(); item.msgCreateTimeName = this.formatTime(item.msgCreateTime); } else { const curTime = dayjs(item.msgCreateTime).valueOf(); if (curTime - preTime > gapTime) { item.showTime = true; item.msgCreateTimeName = this.formatTime(item.msgCreateTime); preTime = dayjs(item.msgCreateTime).valueOf(); } } }); this.setData({ messageList, }); }, handleQuestionTab(e) { const { index } = e.currentTarget.dataset; this.setData({ questionActive: index, questionActiveList: this.data.questionList[index].questionList, }); }, handleDoctorDetail() { const { doctorId } = this.data.doctorDetail; wx.navigateTo({ url: `/pages/doctorDetail/index?id=${doctorId}`, }); }, handleRead() { const { firstNotReadMsgId, messageList } = this.data; const index = messageList.findIndex((item) => `${item.msgId}` === `${firstNotReadMsgId}`); this.handleView(index); this.setData({ unReadCount: 0, }); }, handleFooter() { this.setData({ fold: !this.data.fold, }); if (this.data.fold) { setTimeout(() => { this.handleView(); }, 300); } }, handleAdl() { wx.navigateTo({ url: "/pages/adl/index", }); }, handleAudio(e) { const { index } = e.currentTarget.dataset; const { messageList } = this.data; const messageItem = messageList[index]; const { url } = messageItem.msgContent[0]; if (this.innerAudioContext) { if (messageItem.isPlay) { this.innerAudioContext.stop(); messageItem.isPlay = false; this.setData({ messageList, }); return; } this.innerAudioContext.stop(); this.innerAudioContext.src = url; this.innerAudioContext.play(); messageItem.isPlay = true; this.innerAudioContext.onEnded(() => { messageItem.isPlay = false; this.setData({ messageList, }); }); this.setData({ messageList, }); } }, handlePreview(e) { const { url } = e.currentTarget.dataset; wx.previewImage({ urls: [url], }); }, handleVideo() { if (this.videoContext) { this.videoContext.play(); this.videoContext.requestFullScreen({}); } }, });