message-again.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. // module/chats/components/message-select/message-select.ts
  2. import { Message } from "tdesign-miniprogram";
  3. import { getTickleContext } from "../../../../core/behavior/tickle.behavior";
  4. import { Get, Post } from "../../../../lib/request/method";
  5. import Input from "../../../../miniprogram_npm/tdesign-miniprogram/input/input";
  6. interface Option {
  7. id: string;
  8. name: string;
  9. checked?: boolean;
  10. options: Option[] | null;
  11. }
  12. interface HandleEvent {
  13. mark: {
  14. type: "sub" | "options";
  15. index: number;
  16. item: Option;
  17. };
  18. }
  19. const app = getApp();
  20. Component({
  21. properties: {
  22. payload: {
  23. type: Object,
  24. value: { title: "", multiple: false, options: [] },
  25. result: "",
  26. },
  27. active: { type: Boolean, value: false },
  28. workId: { type: Number, value: 0 },
  29. },
  30. data: {
  31. // 键盘逻辑
  32. inputBoxBottom: 0,
  33. inputText: "",
  34. messages: [],
  35. scrollTop: 0, // 滚动位置
  36. showrePlenish: false,
  37. inputFocus: false,
  38. // end
  39. options: [],
  40. subOptions: [] as Option[],
  41. subMultiple: false,
  42. itemHeight: 48,
  43. result: "",
  44. hasSelected: false,
  45. required: false,
  46. multiple: true,
  47. value1: null,
  48. radioList: [
  49. {
  50. id: "Y",
  51. label: "有",
  52. value: 0,
  53. },
  54. {
  55. id: "N",
  56. label: "没有",
  57. value: 1,
  58. },
  59. ],
  60. symptomResult: "",
  61. btnDisabled: false,
  62. followObj: {},
  63. labelList: [],
  64. symptomsList: [],
  65. isHaveNewSyndrome: "",
  66. isAnalysis: 0,
  67. shows: false,
  68. },
  69. attached(options: any) {
  70. this.setData({
  71. isAnalysis: wx.getStorageSync("isAnalysis"),
  72. });
  73. if (wx.getSystemInfoSync().platform === "ios") {
  74. this.setData({
  75. inputBoxBottom: 10,
  76. });
  77. } else {
  78. this.setData({
  79. inputBoxBottom: 0,
  80. });
  81. }
  82. // 键盘===========================
  83. wx.onKeyboardHeightChange((res) => {
  84. console.log(res, "res1111111111111");
  85. const height = res.height;
  86. if (height > 0) {
  87. this.setData({
  88. // 某些机型需要在此处也设置键盘高度
  89. inputBoxBottom: height,
  90. });
  91. } else {
  92. this.setData({
  93. // 某些机型需要在此处也设置键盘高度
  94. inputBoxBottom: 0,
  95. });
  96. }
  97. });
  98. console.log(this.data.inputBoxBottom, "刚进页面的高度");
  99. // end======================
  100. this.setData({
  101. hasSelected: this.data.options
  102. .filter((option) => !option.hide)
  103. .some((option) => option.checked),
  104. });
  105. this.getContent();
  106. this.getStatus();
  107. },
  108. observers: {
  109. options(options: AnyArray) {
  110. this.setData({
  111. hasSelected: options
  112. .filter((option) => !option.hide)
  113. .some((option) => option.checked),
  114. });
  115. },
  116. // 'messages':function(value: any) {
  117. // console.log(value, "messages11111");
  118. // if(value.length>0){
  119. // }
  120. // },
  121. },
  122. methods: {
  123. // 滚动到底部
  124. // scrollToBottom() {
  125. // // 创建一个选择器获取消息容器高度
  126. // const query = wx.createSelectorQuery();
  127. // query.select(".messages").boundingClientRect();
  128. // query.exec((res) => {
  129. // if (res && res[0]) {
  130. // this.setData({
  131. // scrollTop: res[0].height,
  132. // });
  133. // }
  134. // });
  135. // },
  136. handleInput(e: any) {
  137. // console.log(e, "输入框输入内容");
  138. this.setData({ inputText: e.detail.value });
  139. },
  140. // 发送信息
  141. send() {
  142. const text = this.data.inputText.trim();
  143. if (!text) {
  144. wx.showToast({ title: "发送的信息不能为空", icon: "none" });
  145. return;
  146. }
  147. const userMessage = {
  148. id: Date.now().toString(),
  149. content: text,
  150. sender: "user",
  151. timestamp: Date.now(),
  152. status: "sending",
  153. };
  154. this.setData({
  155. inputBoxBottom: 0,
  156. messages: [...this.data.messages, userMessage],
  157. inputText: "",
  158. });
  159. this.triggerEvent("boxBottom", {
  160. inputBoxBottom: this.data.inputBoxBottom,
  161. });
  162. // 保持键盘打开状态
  163. this.setData({ inputFocus: true });
  164. if (this.data.messages.length === 2 && this.data.symptomResult === "有") {
  165. this.setData({
  166. showrePlenish: false,
  167. });
  168. this.triggerEvent("nextType", { MessageType: 2 });
  169. this.triggerEvent("boxBottom", {
  170. inputBoxBottom: this.data.inputBoxBottom - 50,
  171. });
  172. } else if (
  173. this.data.messages.length === 1 &&
  174. this.data.symptomResult === "没有"
  175. ) {
  176. this.setData({
  177. showrePlenish: false,
  178. });
  179. this.triggerEvent("nextType", { MessageType: 2 });
  180. }
  181. // 滚动到底部
  182. // this.scrollToBottom();
  183. },
  184. // 键盘聚焦
  185. onInputFocus(e) {
  186. this.setData({
  187. inputFocus: false,
  188. // 某些机型需要在此处也设置键盘高度
  189. inputBoxBottom: e.detail.height || this.data.inputBoxBottom,
  190. });
  191. this.triggerEvent("boxBottom", {
  192. inputBoxBottom: this.data.inputBoxBottom,
  193. });
  194. },
  195. // 键盘失焦
  196. onInputBlur() {
  197. this.setData({
  198. inputFocus: false,
  199. inputBoxBottom: 0,
  200. });
  201. this.triggerEvent("boxBottom", {
  202. inputBoxBottom: this.data.inputBoxBottom,
  203. });
  204. },
  205. // end ============================
  206. // 在字典中取症状的状态
  207. async getStatus() {
  208. const dict = await Get("/dict/getDicts", {
  209. transform({ data }: any) {
  210. return data;
  211. },
  212. });
  213. // 查找出是随访病症的状态
  214. let statusArr = dict.filter(
  215. (item: any) => item.dictType === "followup_syndrome_change"
  216. );
  217. this.setData({
  218. labelList: statusArr[0].items,
  219. });
  220. },
  221. // 获取随访任务详情
  222. async getContent() {
  223. // 先从字典中获取状态
  224. await this.getStatus();
  225. let followArr: any = [];
  226. try {
  227. const res = await Post(
  228. `/followupTaskManage/getFollowupTaskDetail/${this.data?.workId}`,
  229. {},
  230. {
  231. transform({ data }: any) {
  232. return data;
  233. },
  234. }
  235. );
  236. this.setData({ followObj: res });
  237. // 处理数据
  238. if (res.syndromeList.length > 0) {
  239. res.syndromeList.forEach((syndrome: any, index: number) => {
  240. followArr.push({
  241. name: syndrome,
  242. id: index + 1,
  243. checked: false,
  244. apiChecked: false,
  245. options: [],
  246. });
  247. if (this.data.labelList.length > 0) {
  248. this.data.labelList.forEach((label: any, lebelIdx: number) => {
  249. followArr[index].options.push({
  250. name: label.dictLabel,
  251. id: lebelIdx + index + 1 + index,
  252. checked: false,
  253. apiChecked: false,
  254. });
  255. });
  256. }
  257. });
  258. }
  259. this.setData({ options: followArr });
  260. } catch (error: any) {
  261. wx.showToast({ title: error.errMsg, icon: "error" });
  262. }
  263. },
  264. onChangeValue(e) {
  265. this.setData({ value1: e.detail.value });
  266. this.triggerEvent("boxBottom", {
  267. inputBoxBottom: this.data.inputBoxBottom,
  268. });
  269. this.setData({
  270. symptomResult: this.data.radioList.find(
  271. (item) => item.value === e.detail.value
  272. )?.label,
  273. isHaveNewSyndrome: this.data.radioList.find(
  274. (item) => item.value === e.detail.value
  275. )?.id,
  276. });
  277. let followObj: any = {
  278. isHaveNewSyndrome: this.data.isHaveNewSyndrome,
  279. symptomList: this.data.symptomsList,
  280. };
  281. // console.log(this.data.isHaveNewSyndrome, "this.data.isHaveNewSyndrome");
  282. this.setData({ btnDisabled: true });
  283. wx.setStorageSync("followObj", followObj);
  284. this.setData({
  285. showrePlenish: true,
  286. });
  287. },
  288. handleTop(event: HandleEvent) {
  289. if (this.data.result) return;
  290. const {
  291. mark: { item },
  292. } = event;
  293. if (!item) return;
  294. const index = this.data.options.findIndex(
  295. (option) => option.id === item.id
  296. );
  297. const multiple = this.data.multiple;
  298. const checked = !item.checked;
  299. const options = this._handle(this.data.options, item, index, multiple);
  300. this.setData({ options });
  301. if (checked && !multiple) {
  302. this.onSubmit();
  303. }
  304. },
  305. handleSub(event: HandleEvent) {
  306. const {
  307. mark: { item },
  308. } = event;
  309. if (!item) return;
  310. const index = this.data.subOptions.findIndex(
  311. (option) => option.id === item.id
  312. );
  313. const multiple = this.data.subMultiple;
  314. let checked;
  315. this.data.subOptions.forEach((option: any, index: number) => {
  316. if (option.id === item.id) {
  317. checked = option.checked = true;
  318. } else {
  319. option.checked = false;
  320. }
  321. });
  322. const subOptions = this._handle(
  323. this.data.subOptions,
  324. item,
  325. index,
  326. multiple
  327. );
  328. this.setData({ subOptions });
  329. if (checked && !multiple) {
  330. this.onConfirm();
  331. }
  332. },
  333. _handle(
  334. options: Option[],
  335. item: Option,
  336. index: number,
  337. multiple?: boolean
  338. ) {
  339. const checked = !item.checked;
  340. console.log(checked, multiple, "_handle");
  341. if (checked) {
  342. const fn = () => {
  343. if (multiple) {
  344. options[index].checked = checked;
  345. } else {
  346. options.forEach((option) => {
  347. option.checked = option.id === item.id;
  348. });
  349. }
  350. return options;
  351. };
  352. if (item.options?.filter((option) => !(<any>option)?.hide).length) {
  353. this.setData({
  354. subTitle: item.name,
  355. subOptions: item.options,
  356. subMultiple: true,
  357. });
  358. // console.log(this.data.subMultiple, "subOptions", item.css);
  359. this.onCancel = () => {
  360. this.setData({ subOptions: [] });
  361. };
  362. this.onConfirm = () => {
  363. // console.log(this.data.subOptions, "subOptions", item.css);
  364. if (!this.data.subOptions.some((option) => option.checked)) {
  365. wx.showToast({ title: "请至少选择一项", icon: "error" });
  366. } else {
  367. const options = fn();
  368. options[index].options = this.data.subOptions;
  369. this.setData({ subOptions: [], options });
  370. }
  371. };
  372. } else {
  373. return fn();
  374. }
  375. } else {
  376. options[index].checked = !item.checked;
  377. if (item.options)
  378. options[index].options = item.options.map((option) => {
  379. if (!(<any>option)?.hide) option.checked = !item.checked;
  380. return option;
  381. });
  382. }
  383. return options;
  384. },
  385. onSubmit() {
  386. if (this.data.result) return;
  387. if (!this.data.hasSelected) {
  388. wx.showToast({ title: "请至少选择一项", icon: "error" });
  389. } else {
  390. const result = this.data.options
  391. .filter((item: any) => item?.checked && !item?.hide)
  392. .map((option: any) => {
  393. const sub = option.options
  394. ?.filter((item: any) => item?.checked && !item?.hide)
  395. .map((item: any) => item.name)
  396. .join(", ");
  397. return [option.name, sub].filter(Boolean).join(": ");
  398. })
  399. .join("; ");
  400. this.setData({ result });
  401. let symptomsList: any = [];
  402. this.data.options.forEach((option: any) => {
  403. const isCheck = option.options.every((item: any) => {
  404. return !item.checked;
  405. });
  406. if (isCheck) {
  407. symptomsList.push({ name: option.name, type: "" });
  408. } else {
  409. symptomsList.push({
  410. name: option.name,
  411. type:
  412. option.options.find((item: any) => item.checked)?.name || "",
  413. });
  414. }
  415. });
  416. this.setData({ symptomsList });
  417. this.triggerEvent("next", { options: this.data.options });
  418. }
  419. },
  420. onSkip() {
  421. if (this.data.result || this.data.hasSelected) return;
  422. if (!this.data.payload.required) {
  423. this.setData({ result: "无变化" });
  424. this.triggerEvent("next", { options: this.data.options });
  425. }
  426. },
  427. },
  428. });