import { API } from './../../../utils/network/api' import { getTimeAgo } from './../../../utils/util' // import TIM from '../../../TUIKit/lib/tim-wx-sdk'; const api = new API() const app = getApp() Page({ data: { navbarData: { showCapsule: 0, //是否显示左上角图标 1表示显示 0表示不显示 title: '问诊消息', //导航栏 中间的标题 }, height: app.globalData.height, has_data: false, data_list_1: [], data_list_2: [], data_list_3: [], data_list_4: [], data_list_5: [], conversationList: [], message_inquiry_type: 99999, system_notice_unreadnnum: "", service_notice_unreadnnum: "", hasOnShow: false, show: false, show_dialog_from_account: "", show_dialog_order_inquiry_id: "", show_dialog_inquiry_type: "", unreadnnum_inter: 0 }, onLoad() { console.log("wenzhen onloadddd"); // if(wx.$TUIKit){ // wx.$TUIKit.on(wx.$TUIKitTIM.EVENT.MESSAGE_RECEIVED, this.$onMessageReceived, this); // }else{ // app.imInit().then(res => { // console.log("wenzhen onload imInit"); // wx.$TUIKit.on(wx.$TUIKitTIM.EVENT.MESSAGE_RECEIVED, this.$onMessageReceived, this); // }); // } }, initInterval(){ let usertype = wx.getStorageSync('usertype'); let userID = wx.getStorageSync('user_id_'+usertype); let unreadnnum_inter = setInterval(() => { let system_notice_unreadnnum = wx.getStorageSync(userID+"_system_notice_unreadnnum"); let service_notice_unreadnnum = wx.getStorageSync(userID+"_service_notice_unreadnnum"); this.setData({ system_notice_unreadnnum: system_notice_unreadnnum, service_notice_unreadnnum: service_notice_unreadnnum, }) }, 1000); this.setData({ unreadnnum_inter: unreadnnum_inter }) console.log("app.globalData.unreadnnum_inter: ", app.globalData.unreadnnum_inter); }, onShow: function () { console.log("onShow from wenzhen V2") if(this.data.hasOnShow){ return; } if (typeof this.getTabBar === 'function' && this.getTabBar()) { this.getTabBar().setData({ active: 1 //数字是当前页面在tabbar的索引,如我的查询页索引是2,因此这边为2,同理首页就为0,审批页面为1 }) } this.getConversationList(); if(this.data.unreadnnum_inter == 0){ this.initInterval(); } //避免onshow加载多次 this.setData({ hasOnShow: true }) if(wx.$TUIKit){ wx.$TUIKit.on(wx.$TUIKitTIM.EVENT.MESSAGE_RECEIVED, this.$onMessageReceived, this); }else{ app.imInit().then(res => { console.log("wenzhen onload imInit"); wx.$TUIKit.on(wx.$TUIKitTIM.EVENT.MESSAGE_RECEIVED, this.$onMessageReceived, this); }); } }, onHide(){ console.log("onHideonHide from wenzhen") clearInterval(this.data.unreadnnum_inter) this.setData({ hasOnShow: false, data_list_1: [], data_list_2: [], data_list_3: [], data_list_4: [], data_list_5: [], message_inquiry_type: 9999, unreadnnum_inter: 0 }) wx.$TUIKit.off(wx.$TUIKitTIM.EVENT.MESSAGE_RECEIVED, this.$onMessageReceived); }, onUnload(){ console.log("onUnload from wenzhen") this.setData({ hasOnShow: false, message_inquiry_type: 9999 }) }, getConversationList(){ console.log("getConversationListgetConversationListgetConversationList"); app.imInit().then(res => { console.log("wenzhen onload imInit: ", res); wx.$TUIKit.getConversationList().then((imResponse) => { console.log("imResponse.data.conversationList from wenzhen_v2: ", imResponse.data.conversationList); this.setData({ conversationList: imResponse.data.conversationList, }); }).then(() => { console.log("开始查询订单状态") this.formatStatus(); }).catch(error => { console.log(error) }); }); }, go(e){ let url = e.currentTarget.dataset.url; let name = e.currentTarget.dataset.name; wx.setStorageSync(name, 0); app.go(url); }, $onMessageReceived(value) { const message = value.data[0]; console.log("message from wenzhen: ",message) this.getConversationList(); // let cloudCustomData = JSON.parse(message.cloudCustomData); // let inquiry_type = cloudCustomData.inquiry_type; // let order_inquiry_id = cloudCustomData.order_inquiry_id; // let type = message.type; // let message_txt = "新消息"; // console.log(type) // switch (type) { // case TIM.TYPES.MSG_TEXT: // console.log("文本消息"); // message_txt = message.payload.text; // break; // case TIM.TYPES.MSG_IMAGE: // console.log("图片消息"); // message_txt = "[图片]"; // break; // case TIM.TYPES.MSG_AUDIO: // console.log("音频消息") // message_txt = "[语言]"; // break; // case TIM.TYPES.MSG_VIDEO: // console.log("视频消息"); // message_txt = "[视频]"; // break; // case TIM.TYPES.MSG_FILE: // console.log("文件消息") // message_txt = "[文件]"; // break; // case TIM.TYPES.MSG_CUSTOM: // console.log("自定义消息") // break; // case TIM.TYPES.MSG_MERGER: // console.log("合并消息") // break; // case TIM.TYPES.MSG_LOCATION: // console.log("位置消息") // message_txt = "[位置]"; // break; // case TIM.TYPES.MSG_FACE: // console.log("表情消息") // message_txt = "[表情]"; // break; // default: // break; // } // let list_name = "data_list_" + inquiry_type; // let _index = this.data[list_name].findIndex(item => { // return item.order_inquiry_id == order_inquiry_id; // }) // let new_list = []; // //没有当前会话 // if(_index > -1){ // let target = list_name + "[" + _index + "].last_message_content.Text"; // let target_dot = list_name + "[" + _index + "].message_dot"; // console.log("target: ",target); // this.setData({ // [target]: message_txt, // [target_dot]: true // }) // new_list.push(this.data[list_name][_index]);//先放入目标 // }else{ // let item = {}; // item.inquiry_status = message.inquiry_status; // let txt = {}; // txt.Text= message_txt; // item.last_message_content = txt; // item.message_send_time = message.message_send_time; // item.message_seq = message.message_seq; // item.message_type = message.message_type; // item.order_inquiry_id = message.order_inquiry_id; // item.patient_age = message.patient_age; // item.patient_name = message.patient_name; // item.patient_sex = message.patient_sex; // item.to_user_id = message.to_user_id; // item.message_dot = true; // new_list.push(item); // } // //置顶 // this.data[list_name].forEach((item, index) => { // if(index != _index){ // new_list.push(item) // } // }) // this.setData({ // [list_name]: new_list // }) }, formatStatus(){ let order_inquiry_id_list = []; order_inquiry_id_list.push("");//初始化一个,防止失败 this.data.conversationList.forEach(item => { let conversationID = item.conversationID; if(conversationID == "C2Cadministrator") return;//如果是管理员消息直接跳过 let lastMessage = item.lastMessage; if(!lastMessage) return; let cloudCustomData = lastMessage.cloudCustomData; if(!cloudCustomData) return; let cloudCustomDataJson; try { cloudCustomDataJson = JSON.parse(cloudCustomData); } catch (error) { console.log(error); return; } let order_inquiry_id = cloudCustomDataJson.order_inquiry_id; order_inquiry_id_list.push(order_inquiry_id);//准备去请求后端那订单状态 }) let conversationList = this.data.conversationList; //获取医生问诊消息用户属性 api.getDoctorInquiryMessageAttr({order_inquiry_ids: order_inquiry_id_list.join(",")}).then(response => { if(response.data.length > 0){ let new_conversationList = []; conversationList.forEach(item => { let conversationID = item.conversationID; if(conversationID == "C2Cadministrator") return;//如果是管理员消息直接跳过 let lastMessage = item.lastMessage; if(!lastMessage) return; let cloudCustomData = lastMessage.cloudCustomData; if(!cloudCustomData) return; let cloudCustomDataJson; try { cloudCustomDataJson = JSON.parse(item.lastMessage.cloudCustomData); } catch (error) { return; } let order_inquiry_id = cloudCustomDataJson.order_inquiry_id; let order = response.data.find(attr_item => { return attr_item.order_inquiry_id == order_inquiry_id; }) if(!order) return; let inquiry_status = order.inquiry_status; //inquiry_status 状态 if(inquiry_status !=3 && inquiry_status != 4){ return; } item.inquiry_status = inquiry_status; item.patient_age = order.patient_age; item.patient_name = order.patient_name; item.patient_sex = order.patient_sex; new_conversationList.push(item); }) this.setData({ conversationList: new_conversationList }) } }).then(() => { console.log("开始格式化formatConversationList") this.formatConversationList(); }).catch(errors => {console.error(errors);}) }, formatConversationList(){ let session_list = []; let session_data_list_1 = []; let session_data_list_2 = []; let session_data_list_3 = []; let session_data_list_4 = []; let session_data_list_5 = []; session_list.push(session_data_list_1); session_list.push(session_data_list_2); session_list.push(session_data_list_3); session_list.push(session_data_list_4); session_list.push(session_data_list_5); let usertype = wx.getStorageSync('usertype'); let userID = wx.getStorageSync('user_id_'+usertype); let totalUnreadCount = 0; let message_inquiry_type = this.data.message_inquiry_type; this.data.conversationList.forEach(item => { let conversationID = item.conversationID; if(conversationID == "C2Cadministrator") return;//如果是管理员消息直接跳过 let lastMessage = item.lastMessage; let type = lastMessage.type; let userProfile = item.userProfile; if(!lastMessage) return; let cloudCustomData = lastMessage.cloudCustomData; if(!cloudCustomData) return; let cloudCustomDataJson = JSON.parse(cloudCustomData); let order_inquiry_id = cloudCustomDataJson.order_inquiry_id; if(!order_inquiry_id) return; let inquiry_type = cloudCustomDataJson.inquiry_type; if(!inquiry_type) return; if(inquiry_type > 4) return; if(inquiry_type < message_inquiry_type){ message_inquiry_type = inquiry_type; } //去重order_inquiry_id let index = this.checkOrderInquiryId(session_list[inquiry_type - 1], order_inquiry_id); if(index > -1) return; let patient_family_data = cloudCustomDataJson.patient_family_data; if(patient_family_data == undefined) return; console.log("patient_family_data: ", patient_family_data); let session_item = {}; session_item.inquiry_status = item.inquiry_status;//订单状态暂无 let txt = {}; if(lastMessage.messageForShow == "[自定义消息]"){ let text = "自定义消息"; let payload = lastMessage.payload; if(payload) { console.log("payload: ",payload.data); let payloadJson = JSON.parse(payload.data); text = payloadJson.title.replace(/-/g, ""); } lastMessage.messageForShow = text; } txt.Text= lastMessage.messageForShow; session_item.last_message_content = txt; session_item.message_send_time = getTimeAgo(lastMessage.lastTime); session_item.message_type = type; session_item.order_inquiry_id = order_inquiry_id; session_item.inquiry_type = inquiry_type; session_item.patient_age = patient_family_data.patient_age; session_item.patient_name = patient_family_data.patient_name; session_item.patient_sex = patient_family_data.patient_sex; session_item.from_account = userProfile.userID; let unreadCount = item.unreadCount; if(unreadCount > 0){ totalUnreadCount = totalUnreadCount + unreadCount; session_item.message_dot = true; }else{ if(item.inquiry_status == 3){ totalUnreadCount = totalUnreadCount + 1; session_item.message_dot = true; }else{ session_item.message_dot = false; } } session_list[inquiry_type - 1].push(session_item); }) if(totalUnreadCount == 0){ app.formatUnReadNum(userID+"_wenzhen_info", totalUnreadCount); } this.setData({ data_list_1: session_data_list_1, data_list_2: session_data_list_2, data_list_3: session_data_list_3, data_list_4: session_data_list_4, message_inquiry_type: message_inquiry_type }) if(this.data.data_list_1.length == 0 && this.data.data_list_2.length == 0 && this.data.data_list_3.length == 0 && this.data.data_list_4.length == 0 && this.data.data_list_5.length == 0){ this.setData({has_data: false}) }else{ this.setData({has_data: true}) this.selectComponent('#tabs').resize(); } }, checkOrderInquiryId(list, order_inquiry_id){ let index = list.findIndex(item => { return item.order_inquiry_id == order_inquiry_id; }) return index; }, onTabsChange(e){ console.log(e); }, goChat(e){ this.setData({ show: false }) console.log("开始订阅消息"); wx.requestSubscribeMessage({ tmplIds: ['jhYUf91ULCTX_f69hazqAYwImdFf8ELasRAwB6X-MTM'], success (res) { console.log(res); let from_account = e.currentTarget.dataset.from_account; let order_inquiry_id = e.currentTarget.dataset.order_inquiry_id; let inquiry_type = e.currentTarget.dataset.inquiry_type; let url = e.currentTarget.dataset.url+"?from_account="+from_account+"&order_inquiry_id="+order_inquiry_id+"&inquiry_type="+inquiry_type; app.go(url); } }) }, postDoctorInquiry(e){ this.setData({ show: false }) console.log("order_inquiry_id: ", e.currentTarget.dataset.order_inquiry_id); let params = {}; params.order_inquiry_id = e.currentTarget.dataset.order_inquiry_id; console.log("params: ",params) api.postDoctorInquiry(params).then(response => { console.log("开始接诊"); console.log(response); }).then(() => { this.goChat(e); }).catch(errors => { console.error(errors); }) }, tabShow(e){ let show_dialog_from_account = e.currentTarget.dataset.show_dialog_from_account; let show_dialog_order_inquiry_id = e.currentTarget.dataset.show_dialog_order_inquiry_id; let show_dialog_inquiry_type = e.currentTarget.dataset.show_dialog_inquiry_type; console.log("tabShowtabShowtabShowtabShow"); this.setData({ show_dialog_from_account: show_dialog_from_account, show_dialog_order_inquiry_id: show_dialog_order_inquiry_id, show_dialog_inquiry_type: show_dialog_inquiry_type, show: true }) }, onClose(){ console.log("onCloseonCloseonCloseonClose") this.setData({ show: false }) } })