addValue.js 5.79 KB
// pages/addValue/addValue/addValue.js
import Util from "../../../utils/util";
import pageData from "../constant/data";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    visible: false,
    tabList: pageData.tabList,
    tabActive: 0,
    typeObj: {
      service: {
        name: "服务类",
        class: "service-color",
      },
      membrane: {
        name: "膜类",
        class: "membrane-color",
      },
      beauty: {
        name: "美容类",
        class: "beauty-color",
      },
      light: {
        name: "轻改类",
        class: "light-color",
      },
    },
    formData: {
      sales: 100,
      flow: 10000,
    },
    productList: [],
    isReset: false,
    hasPhoneNumber: false,
    lastFormData: {
      sales: 100,
      flow: 10000,
    },
    isFirst: true
  },

  // tab切换
  tabChange(e) {
    console.log(e);
    const index = e.currentTarget.dataset.index;
    this.setData({
      tabActive: index,
      showPlay: false
    })
    this.showCase()
  },
  // 提交测算
  submit(e) {
    console.log(e);
    let sales = e ? +e.detail.value.sales : this.data.formData.sales
    let flow = e ? +e.detail.value.flow : this.data.formData.flow
    const reg = /^[1-9]\d*$/
    console.log(reg.test(sales), flow, '---')
    if (!reg.test(sales)) {
      wx.showToast({
        title: '门店月平均新车销量应为正整数',
        icon: 'none',
        duration: 2000
      })
      return
    }
    if (!reg.test(flow)) {
      wx.showToast({
        title: '3年内售后存量用户应为正整数',
        icon: 'none',
        duration: 2000
      })
      return
    }
    this.setData({
      'lastFormData.sales': sales,
      'lastFormData.flow': flow
    })
    const _this = this;
    this.data.tabList.forEach((item, index) => {
      item.productList.forEach((i, lastIndex) => {
        if (i.profit) {
          let name = `tabList[${index}].productList[${lastIndex}].profit`;
          let profit = _this.formula(i.productType, sales, flow);
          console.log(name, profit)
          this.setData({
            [name]: profit,
          });
        }
      });
    });
    this.setData({
      visible: false
    });
    this.showCase()
    if (!this.data.isFirst) {
      wx.showToast({
        title: '测算结果已在页面内同步展示',
        icon: 'none',
        duration: 2000
      })
    } else {
      this.setData({
        isFirst: false
      })
    }
  },
  // 计算公式,参数:productType
  formula(productType, sales, flow) {
    let profit = 0;
    switch (productType) {
      case "benefitCard":
        // 数字无忧权益卡
        profit = (sales * 0.5 * 2000 * 12).toFixed(0);
        break;
      case "sunMembrane":
        // 汽车太阳膜
        profit = (sales * 0.8 * 0.7 * 1500 * 12).toFixed(0);
        break;
      case "protectMembrane":
        // 漆面保护膜
        profit = (flow * 0.15 * 12000 * 12).toFixed(0);
        break;
      case "colorMembrane":
        // 改色膜
        profit = (flow * 0.05 * 5000 * 12).toFixed(0);
        break;
      case "digitBeauty":
        // 数字化美容
        profit = (sales * 0.2 * 300 * 12).toFixed(0);
        break;
      case "chair":
        // 座椅通风
        profit = (flow * 2580 * 0.9 * 0.01).toFixed(0);
        break;
      case "ar":
        // AR导航
        profit = (flow * 1680 * 0.9 * 0.015).toFixed(0);
        break;
      case "lamp":
        // 氛围灯
        profit = (flow * 2680 * 0.9 * 0.015).toFixed(0);
        break;
      case "navigation":
        // 360全景导航
        profit = (flow * 2880 * 0.9 * 0.015).toFixed(0);
        break;
    }
    console.log(profit.length);
    console.log(productType, sales, flow, profit);
    if (profit.length > 8) {
      return (profit / 10000).toFixed(0) + "万";
    } else {
      return profit;
    }
  },
  reset() {
    this.setData({
      isReset: false,
      formData: {
        sales: 100,
        flow: 10000,
      },
    });
  },
  focus() {
    this.setData({
      isReset: true,
    });
    return false;
  },
  // 显示测算弹窗
  showCalculate() {
    this.setData({
      visible: !this.data.visible,
      isReset: false
    });
  },
  clickDialog(e) {
    console.log(e.changedTouches[0].clientY);
    if (e.changedTouches[0].clientY < 352) {
      this.setData({
        visible: false,
        isReset: false,
        formData: this.data.lastFormData
      });
    }
  },

  // 跳转产品详情
  goDetail(e) {
    console.log(e.currentTarget.dataset.item, "===");
    wx.$router.go(e.currentTarget.dataset.item);
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.submit()
    this.showCase()
  },
  showCase() {
    console.log(Util.getUserInfo())
    if (Util.getUserInfo() && Util.getUserInfo().phone) {
      this.setData({
        hasPhoneNumber: true,
        productList: this.data.tabList[this.data.tabActive].productList,
      });
    } else {
      this.setData({
        productList: this.data.tabList[this.data.tabActive].productList.slice(
          0,
          2
        ),
      });
    }
  },
  // 更新手机号
  updatePhoneNumber(e) {
    if (e.detail) {
      this.setData({
        hasPhoneNumber: true,
        productList: this.data.tabList[this.data.tabActive].productList,
      });
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});