SecondStep.js 9.52 KB
import constantData from "../constant/firstData";
const IMG_URL =
  "https://img02.glsx.com.cn/weapp/resource/dj-car-boutique-work-wechat/";
import fn from "../constant/ruleFn";
const defaultLabelWidth = "var(--fontSize140)";
import api from "~/api/api";
import newApi from "~/packageUsedCar/api";
import { validateVin, formatVinInput } from "~/utils/vinVaildate";
import Dialog from "@vant/weapp/dialog/dialog";

Component({
  options: {
    styleIsolation: "shared", // 自定义覆盖vant样式
  },
  /**
   * 组件的属性列表
   */
  properties: {
    serviceInfo: {
      type: Object,
      value: () => {},
      observer: function (n) {
        // this.dealFormRule();
      },
    },
    // 页面存放表单对象的对象名
    dataAttrName: {
      type: String,
      value: null,
    },
    evalOrderBasicReqDTO: {
      type: Object,
      value: () => ({}),
    },
  },
  lifetimes: {},

  /**
   * 组件的初始数据
   */
  data: {
    IMG_URL,
    imgShow: false,
    imgType: "rule",
    defaultLabelWidth,
    vinValid: null,
    secondObj: constantData.licenseObj,
    formRule: constantData.formRule,
    isShowObj: {
      car: true,
      customer: true,
      product: true,
      info: true,
      store: true,
      retrofit: true,
    },
    codeToAttr: {
      YB0001: "invoice",
      YB0003: "driveFront",
      YB0004: "driveBack",
      YB0002: "contract",
      YB0005: "other",
      QS0001: "retrofitInvoice",
      QS0002: "retrofit",
    },
    imgToInfo: {
      invoice: {
        code: "YB0001",
        label: "购车发票",
      },
      driveFront: {
        code: "YB0003",
        label: "行驶证主页",
      },
      driveBack: {
        code: "YB0004",
        label: "行驶证副页",
      },
      contract: {
        code: "YB0002",
        label: "合同购买时里程数",
      },
      other: {
        code: "YB0005",
      },
      retrofitInvoice: {
        code: "QS0001",
        label: "发票或收据",
      },
      retrofit: {
        code: "QS0002",
        label: "加改装部位",
      },
    },
    imgObj: {
      invoice: [],
      driveFront: [],
      driveBack: [],
      contract: [],
      other: [],
      retrofitInvoice: [],
      retrofit: [],
    },
    popObj: {
      popValue: "",
    },
    tireObjList: constantData.tireObjList,
    retrofitInfo: {
      inclueType: 1,
    },
    referrerPicker: false,
    deliveryPicker: false,
    customerInfoBox: false,
    employeeList: [],
    orderObj: {},
    pointsMultiRole: "N",
  },

  /**
   * 组件的方法列表
   */
  methods: {
    changeInclueType(e) {
      const { types } = e.target.dataset;
      let formData = this.getPageData();
      formData.addOnsFlag = types;
      this.setData({
        "retrofitInfo.inclueType": types,
        formData,
      });
    },
    // 切换排量/功率
    changeType(e) {
      let { type, index } = e.currentTarget.dataset;
      let idName = "extendService";
      let childCom = this.selectComponent("#" + idName + index);
      childCom.parentSetData("displacementPowerFlag", type);
      this.setData({
        [`serviceInfo.goodInfoList[${index}].displacementPowerFlag`]: type,
      });
    },
    // 获取推荐人列表
    getEmployee() {
      let userInfo = wx.$util.getStorageSync("dj_boutique_user_info");
      this.setData({
        userInfo,
      });
      let params = {
        merchantId: userInfo.default4SStoreInfo.merchantId,
      };
      api.getEmployeeList(params).then((res) => {
        if (res.code == 0) {
          this.setData({
            employeeList: res.data,
          });
        } else {
          wx.$util.showToast(res.message || "接口异常");
        }
      });
    },
    handleReferrerPicker() {
      this.setData({
        referrerPicker: true,
      });
    },
    handleDeliveryPicker() {
      this.setData({
        deliveryPicker: true,
      });
    },
    onBuyNewCarChange(e) {
      let thisPage = wx.$router.getPageInstance();
      thisPage.setData({
        ["formData.carBaseReqDTO.buyNewCar"]: e.detail,
      });
    },
    onHasAccidentChange(e) {
      let thisPage = wx.$router.getPageInstance();
      thisPage.setData({
        ["formData.carBaseReqDTO.hasAccident"]: e.detail,
      });
    },
    handleInput(e) {
      let thisPage = wx.$router.getPageInstance();
      let value = e.detail.value || e.detail;
      thisPage.setData({
        ["formData.carBaseReqDTO.conditionDescription"]: value,
      });
    },

    // 处理原车主住址输入
    handleAddressInput(e) {
      let thisPage = wx.$router.getPageInstance();
      // 直接使用 e.detail.value,避免空字符串回退到 e.detail 对象
      let value = e.detail.value ?? "";
      // 限制最大长度为52字
      const maxLength = 52;
      if (value.length > maxLength) {
        value = value.substring(0, maxLength);
      }
      const { dataAttrName } = this.data;
      thisPage.setData({
        [dataAttrName + ".previousOwnerAddress"]: value,
      });

      // 同步更新组件的 serviceInfo
      this.setData({
        "serviceInfo.previousOwnerAddress": value,
      });
    },
    handleSalesChange(e) {
      let idx = e.detail.value || 0;
      let { employeeList } = this.data;
      let thisPage = wx.$router.getPageInstance();
      thisPage.setData({
        ["formData.evalOrderBasicReqDTO.sellerUserId"]: employeeList[idx].uuid,
        ["formData.evalOrderBasicReqDTO.sellerName"]:
          employeeList[idx].staffName,
      });
      this.setData({
        referrerPicker: false,
      });
    },
    handleDeliveryChange(e) {
      let idx = e.detail.value || 0;
      let { employeeList } = this.data;
      let thisPage = wx.$router.getPageInstance();
      thisPage.setData({
        ["formData.evalOrderBasicReqDTO.deliveryId"]: employeeList[idx].uuid,
        ["formData.evalOrderBasicReqDTO.deliveryName"]:
          employeeList[idx].staffName,
      });
      this.setData({
        deliveryPicker: false,
      });
    },
    chooseCustomerInfo(e) {
      console.log("选择客户信息", e);
      this.setData({
        customerInfoBox: false,
      });
    },
    getPageData() {
      let thisPage = wx.$router.getPageInstance();
      return thisPage?.data[this.data.dataAttrName];
    },
    // 表单验证方法
    validate(rules) {
      const glFormComponent = this.selectComponent("#customer");
      if (this.data.vinValid === false) {
        return {
          message: "车架号校验未通过,请重新输入",
        };
      }
      if (!glFormComponent) {
        return {
          message: "表单组件未找到",
        };
      }
      return glFormComponent.validate(rules);
    },
    // 表单输入事件处理
    inputEvent(e) {
      let { attr } = e.detail;
      // 车架号输入处理:实时转大写 + 实时校验
      if (attr == "vin") {
        const thisPage = wx.$router.getPageInstance();
        const { dataAttrName, serviceInfo } = this.data;
        let value = serviceInfo?.vin || "";
        const upperVin = formatVinInput(value);
        // 更新为大写(同时更新页面数据和组件数据)
        thisPage.setData({
          [dataAttrName + ".vin"]: upperVin,
        });
        // 同步更新组件的 serviceInfo
        this.setData({
          "serviceInfo.vin": upperVin,
        });
        // 当输入满17位时,进行实时校验(防抖500ms)
        if (upperVin.length === 17) {
          validateVin(upperVin)
            .then((res) => {
              let { valid } = res.data.data;
              // 保存 valid 值到组件 data 中
              this.setData({
                vinValid: valid,
              });
            })
            .catch(() => {
              // 校验失败时,设置为 false
              this.setData({
                vinValid: false,
              });
            });
        } else {
          // 长度不足17位时,重置校验状态
          this.setData({
            vinValid: null,
          });
        }
      }
    },
    // 表单失去焦点事件处理
    inputBlur(e) {
      let { attr } = e.detail;
      if (attr == "licensePlate") {
        const { serviceInfo } = this.data;
        console.log(serviceInfo, "serviceInfo");
        let value = serviceInfo?.licensePlate || "";
        console.log(value, "value");
        newApi
          .checkRepeat({
            licensePlate: value,
          })
          .then((res) => {
            console.log(res, "res");
            if (res.data.code == 0) {
              if (res.data.data.result == 1) {
                Dialog.confirm({
                  context: this, // 必须指定 context 为组件实例
                  title: "提示",
                  message: "已存在相同车牌号的评估单,是否进入编辑流程?",
                })
                  .then(() => {
                    // on confirm
                  })
                  .catch(() => {
                    // on cancel
                  });
              } else if (res.data.data.result == 2) {
                Dialog.confirm({
                  context: this, // 必须指定 context 为组件实例
                  title: "提示",
                  message: "已存在一个30天内有效评估,是否进入详情查看?",
                })
                  .then(() => {
                    // on confirm
                  })
                  .catch(() => {
                    // on cancel
                  });
              }
            }
            // if (res.code == 0) {
            //   this.setData({
            //     licensePlateValid: true,
            //   });
            // }
          });
      }
    },
  },
});