| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310 |
- import dayjs from "dayjs";
- import { Post } from "../../../../lib/request/method";
- // module/chats/components/questionnaire/questionnaire.ts
- interface Message {
- id: string;
- type:
- | "system"
- | "analysis"
- | "select"
- | "text"
- | "report"
- | "again"
- | "follow"
- | "count";
- payload: AnyObject;
- }
- interface HandleEvent {
- target: { id: string };
- detail: AnyObject;
- type: "next";
- }
- interface MessageType {
- messageType: number;
- }
- Component({
- lifetimes: {
- attached: function () {
- let isAnalysis: number;
- isAnalysis = wx.getStorageSync("isAnalysis");
- console.log("attached-isAnalysis", isAnalysis);
- if (isAnalysis === 3) {
- // 对话管家
- this._start();
- } else if (isAnalysis === 2) {
- // 随访提醒
- this.setData({
- [`_next.classify`]: "",
- [`_next.dialogId`]: "",
- [`_next.questions`]: [],
- _timestamp: Date.now(),
- });
- this._next();
- }
- },
- },
- properties: {
- messageType: { type: Number, value: 0 },
- workId: { type: Number, value: 0 },
- },
- /**
- * 组件的初始数据
- */
- data: {
- inputBoxBottom: 0,
- messages: {} as Record<number, Message>,
- lastId: "",
- _next: {
- classify: "",
- dialogId: "",
- questions: [],
- } as AnyObject,
- _timestamp: Date.now(),
- },
- observers: {
- "messages.**"(messages) {
- const message = Object.values(messages).pop() as Message;
- this.setData({ lastId: message?.id });
- },
- },
- methods: {
- nextType(event: MessageType) {
- this.setData({ messageType: event.detail.MessageType });
- this._next();
- },
- boxBottom(event: boxBottom) {
- console.log(event, "监听页面高度event");
- this.setData({ inputBoxBottom: event.detail.inputBoxBottom });
- this.triggerEvent("boxBottom", {
- inputBoxBottom: this.data.inputBoxBottom,
- });
- console.log(this.data.inputBoxBottom, "监听页面高度boxbottom");
- },
- handle(event: HandleEvent) {
- const isAnalysis = wx.getStorageSync("isAnalysis");
- if (isAnalysis === 3) {
- console.log(this.data._next, "handle", isAnalysis);
- const index = event.target?.id.split(".").pop() ?? 0;
- const questions = this.data._next.questions;
- Object.assign(questions[index], event.detail);
- this.setData({ "_next.questions": questions });
- this._next();
- } else {
- this._createMessage({
- id: 2,
- type: "follow",
- payload: { title: "" },
- });
- }
- },
- async _start() {
- try {
- const count = await Post(
- `/patientInfoManage/rechargeUseDetail`,
- {},
- {
- transform({ data }: any) {
- console.log(data, "剩余次数");
- return data?.residuedCou;
- },
- }
- );
- this.triggerEvent("count", { count });
- console.log("count",count);
- if (count > 0) {
- // if (count < 0) {
- this.setData({
- [`_next.classify`]: "",
- [`_next.dialogId`]: "",
- [`_next.questions`]: [],
- _timestamp: Date.now(),
- });
- this._next();
- } else {
- // throw { errMsg: `您的健康分析次数已用完,请联系工作人员。` };
- this._createMessage({
- id: `count-${Date.now()}`,
- type: "count",
- payload: {
- date: Date.now(),
- title: `您的健康分析次数已用完,请联系工作人员。`,
- },
- });
- this._end();
- }
- } catch (error) {
- this._createMessage({
- id: `system-start`,
- type: "system",
- payload: {
- date: Date.now(),
- title: error.errMsg ?? `分析错误,请重试!`,
- },
- });
- this._end();
- }
- },
- _end() {
- this.setData({
- [`_next.classify`]: "",
- [`_next.dialogId`]: "",
- [`_next.questions`]: [],
- });
- this.triggerEvent("next", { component: "guide", scroll: true });
- },
- async _next() {
- let isAnalysis: number;
- isAnalysis = wx.getStorageSync("isAnalysis");
- console.log("isAnalysis",isAnalysis);
- if (isAnalysis === 3) {
- // 对话管家
- if (this.data._next.classify === "tongue") {
- this._createMessage({
- id: `tongue-loading.${Date.now()}`,
- type: "text",
- payload: { title: "分析中...", loading: true },
- });
- this.triggerEvent("to");
- }
- }
- try {
- if (this.data.messageType === 1) {
- this._createMessage({
- id: 1,
- type: "again",
- payload: { title: "" },
- });
- } else {
- const { data } = await Post(
- `/dialogueManage/dialog`,
- this.data._next
- );
- data.nextQuestions?.forEach((question: any, index: number) => {
- if (isAnalysis === 2) {
- // 随访
- if (question.css === "tongue") {
- console.log(question, "tonguetongue");
- this._createMessage({
- id: `${question.classify}.${question.id}.${index}`,
- type: "analysis",
- payload: { title: question.title },
- });
- }
- } else {
- // 对话管家
- if (question.classify === "tongue_result") {
- this._updateMessage({
- id: `${question.classify}.${question.id}.${index}`,
- type: "text",
- payload: { title: question.content },
- });
- } else {
- if (question.css === "tongue") {
- console.log(question, "tonguetongue");
- this._createMessage({
- id: `${question.classify}.${question.id}.${index}`,
- type: "analysis",
- payload: { title: question.title },
- });
- } else if (question.css === "text") {
- this._createMessage({
- id: `${question.classify}.${question.id}.${index}`,
- type: "text",
- payload: { title: question.content },
- });
- } else if (["select", "checkbox"].includes(question.css)) {
- this._createMessage({
- id: `${question.classify}.${question.id}.${index}`,
- type: "select",
- payload: {
- title: question.title,
- options: question.options.map((item: AnyObject) => {
- if (Array.isArray(item.options)) {
- item.options = item.options.map((item) => {
- return { ...item, hide: item.css === "hide" };
- });
- }
- return { ...item, hide: item.css === "hide" };
- }),
- multiple: question.css === "checkbox",
- required: question.required,
- },
- });
- } else if (question.over) {
- return this._end();
- }
- }
- }
- });
- // 对话管家
- if (isAnalysis === 3) {
- if (data.classify === "report") {
- const diff = dayjs().diff(this.data._timestamp, "m");
- this._createMessage({
- id: "report",
- type: "report",
- payload: {
- title: `本次问答已结束,历时${
- diff || 1
- }分钟,非常感谢您的配合!请查看您本次的健康评估情况。`,
- url: `/module/health/pages/report/report?id=${data.healthAnalysisReportId}`,
- },
- });
- }
- if (data.over) return this._end();
- this.setData({
- [`_next.classify`]: data.classify,
- [`_next.dialogId`]: data.dialogId,
- [`_next.questions`]: data.nextQuestions,
- });
- this.triggerEvent("to");
- }
- }
- } catch (error) {
- if (this.data._next.classify === "tongue") {
- this._updateMessage({
- id: `tongue-error-${Date.now()}`,
- type: "text",
- payload: { title: error.errMsg ?? `图像检测失败,请重新拍摄上传` },
- });
- this.triggerEvent("to");
- setTimeout(() => this._start(), 20);
- } else {
- const date = Date.now();
- this._createMessage({
- id: `system-${date}`,
- type: "system",
- payload: { date, title: error.errMsg ?? `分析错误,请重试!` },
- });
- this._end();
- }
- }
- },
- _createMessage(body: Message, data?: Record<string, any>) {
- const messages = this.data.messages;
- const index = Object.keys(messages).length;
- this.setData({
- [`messages.${index}`]: body,
- ...data,
- });
- },
- _updateMessage(body: Message) {
- const messages = this.data.messages;
- const index = Object.keys(messages).length;
- this.setData({
- [`messages.${index - 1}`]: body,
- });
- },
- },
- });
|