| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432 |
- // 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) => !(<any>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 (!(<any>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 });
- }
- },
- },
- });
|