// module/chats/components/message-select/message-select.ts import { Message } from "tdesign-miniprogram"; import { getTickleContext } from "../../../../core/behavior/tickle.behavior"; import { Get, Post } from "../../../../lib/request/method"; import Input from "../../../../miniprogram_npm/tdesign-miniprogram/input/input"; interface Option { id: string; name: string; checked?: boolean; options: Option[] | null; } interface HandleEvent { mark: { type: "sub" | "options"; index: number; item: Option; }; } const app = getApp(); Component({ properties: { payload: { type: Object, value: { title: "", multiple: false, options: [] }, result: "", }, active: { type: Boolean, value: false }, workId: { type: Number, value: 0 }, }, data: { // 键盘逻辑 inputBoxBottom: 0, inputText: "", messages: [], scrollTop: 0, // 滚动位置 showrePlenish: false, inputFocus: false, // end options: [], subOptions: [] as Option[], subMultiple: false, itemHeight: 48, result: "", hasSelected: false, required: false, multiple: true, value1: null, radioList: [ { id: "Y", label: "有", value: 0, }, { id: "N", label: "没有", value: 1, }, ], symptomResult: "", btnDisabled: false, followObj: {}, labelList: [], symptomsList: [], isHaveNewSyndrome: "", isAnalysis: 0, shows: false, }, attached(options: any) { this.setData({ isAnalysis: wx.getStorageSync("isAnalysis"), }); if (wx.getSystemInfoSync().platform === "ios") { this.setData({ inputBoxBottom: 10, }); } else { this.setData({ inputBoxBottom: 0, }); } // 键盘=========================== wx.onKeyboardHeightChange((res) => { console.log(res, "res1111111111111"); const height = res.height; if (height > 0) { this.setData({ // 某些机型需要在此处也设置键盘高度 inputBoxBottom: height, }); } else { this.setData({ // 某些机型需要在此处也设置键盘高度 inputBoxBottom: 0, }); } }); console.log(this.data.inputBoxBottom, "刚进页面的高度"); // end====================== this.setData({ hasSelected: this.data.options .filter((option) => !option.hide) .some((option) => option.checked), }); this.getContent(); this.getStatus(); }, observers: { options(options: AnyArray) { this.setData({ hasSelected: options .filter((option) => !option.hide) .some((option) => option.checked), }); }, // 'messages':function(value: any) { // console.log(value, "messages11111"); // if(value.length>0){ // } // }, }, methods: { // 滚动到底部 // scrollToBottom() { // // 创建一个选择器获取消息容器高度 // const query = wx.createSelectorQuery(); // query.select(".messages").boundingClientRect(); // query.exec((res) => { // if (res && res[0]) { // this.setData({ // scrollTop: res[0].height, // }); // } // }); // }, handleInput(e: any) { // console.log(e, "输入框输入内容"); this.setData({ inputText: e.detail.value }); }, // 发送信息 send() { const text = this.data.inputText.trim(); if (!text) { wx.showToast({ title: "发送的信息不能为空", icon: "none" }); return; } const userMessage = { id: Date.now().toString(), content: text, sender: "user", timestamp: Date.now(), status: "sending", }; this.setData({ inputBoxBottom: 0, messages: [...this.data.messages, userMessage], inputText: "", }); this.triggerEvent("boxBottom", { inputBoxBottom: this.data.inputBoxBottom, }); // 保持键盘打开状态 this.setData({ inputFocus: true }); if (this.data.messages.length === 2 && this.data.symptomResult === "有") { this.setData({ showrePlenish: false, }); this.triggerEvent("nextType", { MessageType: 2 }); this.triggerEvent("boxBottom", { inputBoxBottom: this.data.inputBoxBottom - 50, }); } else if ( this.data.messages.length === 1 && this.data.symptomResult === "没有" ) { this.setData({ showrePlenish: false, }); this.triggerEvent("nextType", { MessageType: 2 }); } // 滚动到底部 // this.scrollToBottom(); }, // 键盘聚焦 onInputFocus(e) { this.setData({ inputFocus: false, // 某些机型需要在此处也设置键盘高度 inputBoxBottom: e.detail.height || this.data.inputBoxBottom, }); this.triggerEvent("boxBottom", { inputBoxBottom: this.data.inputBoxBottom, }); }, // 键盘失焦 onInputBlur() { this.setData({ inputFocus: false, inputBoxBottom: 0, }); this.triggerEvent("boxBottom", { inputBoxBottom: this.data.inputBoxBottom, }); }, // end ============================ // 在字典中取症状的状态 async getStatus() { const dict = await Get("/dict/getDicts", { transform({ data }: any) { return data; }, }); // 查找出是随访病症的状态 let statusArr = dict.filter( (item: any) => item.dictType === "followup_syndrome_change" ); this.setData({ labelList: statusArr[0].items, }); }, // 获取随访任务详情 async getContent() { // 先从字典中获取状态 await this.getStatus(); let followArr: any = []; try { const res = await Post( `/followupTaskManage/getFollowupTaskDetail/${this.data?.workId}`, {}, { transform({ data }: any) { return data; }, } ); this.setData({ followObj: res }); // 处理数据 if (res.syndromeList.length > 0) { res.syndromeList.forEach((syndrome: any, index: number) => { followArr.push({ name: syndrome, id: index + 1, checked: false, apiChecked: false, options: [], }); if (this.data.labelList.length > 0) { this.data.labelList.forEach((label: any, lebelIdx: number) => { followArr[index].options.push({ name: label.dictLabel, id: lebelIdx + index + 1 + index, checked: false, apiChecked: false, }); }); } }); } this.setData({ options: followArr }); } catch (error: any) { wx.showToast({ title: error.errMsg, icon: "error" }); } }, onChangeValue(e) { this.setData({ value1: e.detail.value }); this.triggerEvent("boxBottom", { inputBoxBottom: this.data.inputBoxBottom, }); this.setData({ symptomResult: this.data.radioList.find( (item) => item.value === e.detail.value )?.label, isHaveNewSyndrome: this.data.radioList.find( (item) => item.value === e.detail.value )?.id, }); let followObj: any = { isHaveNewSyndrome: this.data.isHaveNewSyndrome, symptomList: this.data.symptomsList, }; // console.log(this.data.isHaveNewSyndrome, "this.data.isHaveNewSyndrome"); this.setData({ btnDisabled: true }); wx.setStorageSync("followObj", followObj); this.setData({ showrePlenish: true, }); }, handleTop(event: HandleEvent) { if (this.data.result) return; const { mark: { item }, } = event; if (!item) return; const index = this.data.options.findIndex( (option) => option.id === item.id ); const multiple = this.data.multiple; const checked = !item.checked; const options = this._handle(this.data.options, item, index, multiple); this.setData({ options }); if (checked && !multiple) { this.onSubmit(); } }, handleSub(event: HandleEvent) { const { mark: { item }, } = event; if (!item) return; const index = this.data.subOptions.findIndex( (option) => option.id === item.id ); const multiple = this.data.subMultiple; let checked; this.data.subOptions.forEach((option: any, index: number) => { if (option.id === item.id) { checked = option.checked = true; } else { option.checked = false; } }); const subOptions = this._handle( this.data.subOptions, item, index, multiple ); this.setData({ subOptions }); if (checked && !multiple) { this.onConfirm(); } }, _handle( options: Option[], item: Option, index: number, multiple?: boolean ) { const checked = !item.checked; console.log(checked, multiple, "_handle"); if (checked) { const fn = () => { if (multiple) { options[index].checked = checked; } else { options.forEach((option) => { option.checked = option.id === item.id; }); } return options; }; if (item.options?.filter((option) => !(option)?.hide).length) { this.setData({ subTitle: item.name, subOptions: item.options, subMultiple: true, }); // console.log(this.data.subMultiple, "subOptions", item.css); this.onCancel = () => { this.setData({ subOptions: [] }); }; this.onConfirm = () => { // console.log(this.data.subOptions, "subOptions", item.css); if (!this.data.subOptions.some((option) => option.checked)) { wx.showToast({ title: "请至少选择一项", icon: "error" }); } else { const options = fn(); options[index].options = this.data.subOptions; this.setData({ subOptions: [], options }); } }; } else { return fn(); } } else { options[index].checked = !item.checked; if (item.options) options[index].options = item.options.map((option) => { if (!(option)?.hide) option.checked = !item.checked; return option; }); } return options; }, onSubmit() { if (this.data.result) return; if (!this.data.hasSelected) { wx.showToast({ title: "请至少选择一项", icon: "error" }); } else { const result = this.data.options .filter((item: any) => item?.checked && !item?.hide) .map((option: any) => { const sub = option.options ?.filter((item: any) => item?.checked && !item?.hide) .map((item: any) => item.name) .join(", "); return [option.name, sub].filter(Boolean).join(": "); }) .join("; "); this.setData({ result }); let symptomsList: any = []; this.data.options.forEach((option: any) => { const isCheck = option.options.every((item: any) => { return !item.checked; }); if (isCheck) { symptomsList.push({ name: option.name, type: "" }); } else { symptomsList.push({ name: option.name, type: option.options.find((item: any) => item.checked)?.name || "", }); } }); this.setData({ symptomsList }); this.triggerEvent("next", { options: this.data.options }); } }, onSkip() { if (this.data.result || this.data.hasSelected) return; if (!this.data.payload.required) { this.setData({ result: "无变化" }); this.triggerEvent("next", { options: this.data.options }); } }, }, });