jiangping
2024-07-29 d9a89d83c2048a964f16ca179feb2b7f21b9d33a
wechat_jiaxuan/pages/calculator/index.js
@@ -1,7 +1,13 @@
// pages/calculator/index.js
// import { imageUrl } from '../../utils/config'
// import { calculatorOption, calculatorSave, decodePhone, bindPhone } from '../../api/index'
import { getBjParamConfig, saveRenovationCalculator } from '../../api/index'
import {
  getBjParamConfig,
  saveRenovationCalculator,
  getDictData,
  getWxMiniPhone,
  binDingPhone
} from '../../api/index'
// import Ls from '../../utils/storage'
// let ls = new Ls
Page({
@@ -10,7 +16,7 @@
   * 页面的初始数据
   */
  data: {
    // background: imageUrl + 'calculator/background.jpg',
    background: '',
    userPhone: '',
    type: '',
    info: '',
@@ -21,7 +27,8 @@
      '一室一厅', '两室一厅', '两室两厅', '三室两厅',
      '四室两厅', '五室两厅及以上'
    ],
    calculatorOptions: [],
    calculatorOptions: {},
    activeList: [],
    showPopup: false,
    activePicker: '',
    pickerIndex: 0,
@@ -35,32 +42,41 @@
    this.getCalculatorOption()
    const userInfo = wx.getStorageSync('member')
    if (userInfo.phone) {
      that.setData({ userPhone: userInfo.phone })
      that.setData({
        userPhone: userInfo.phone
      })
    }
  },
  // 返回上一页
  navigateBack () {
  navigateBack() {
    wx.navigateBack({
      delta: 1,
    })
  },
  // picker 选择
  onChange (e) {
    this.setData({ pickerIndex: e.detail.index })
  onChange(e) {
    this.setData({
      pickerIndex: e.detail.index
    })
  },
  // input 输入
  inputTyping (e) {
    this.setData({ [e.currentTarget.dataset.index]: e.detail.value })
  inputTyping(e) {
    this.setData({
      [e.currentTarget.dataset.index]: e.detail.value
    })
  },
  // 选择房屋类型、户型信息
  toSelectType (e) {
  toSelectType(e) {
    let index = e.currentTarget.dataset.index
    let { activePicker } = this.data
    if ( activePicker === index) {
      this.setData({ showPopup: true })
    let {
      activePicker
    } = this.data
    if (activePicker === index) {
      this.setData({
        showPopup: true
      })
    } else {
      this.setData({
        pickerIndex: 0,
@@ -71,46 +87,71 @@
  },
  // 取消选择
  cancelPicker () {
    this.setData({ showPopup: false })
  cancelPicker() {
    this.setData({
      showPopup: false
    })
  },
  // 确认选择
  confirmPicker () {
    let { activePicker, pickerIndex } = this.data
    this.setData({ [activePicker]: pickerIndex, showPopup: false })
  confirmPicker() {
    let {
      activePicker,
      pickerIndex
    } = this.data
    this.setData({
      [activePicker]: pickerIndex,
      showPopup: false
    })
  },
  // 获取用户手机号
  getPhoneNumber (e) {
    // console.log(e)
    let that = this
    decodePhone({ code: e.detail.code })
      .then(res => {
        if (res.phone === undefined || res.phone.length === null || !res.phone) {
          wx.showToast({
            title: res.msg,
            icon: 'none'
  getPhoneNumber(e) {
    const data = {
      ...e.detail
    }
    console.log('e', e);
    if (data.encryptedData && data.iv) {
      getWxMiniPhone({
        encryptedData: data.encryptedData,
        iv: data.iv,
        sessionKey: wx.getStorageSync('sessionKey') || data.iv,
      }).then(res => {
        binDingPhone({
          phone: res.data
        }).then((res) => {
          this.setData({
            userPhone: res.data.phone
          })
        } else {
          bindPhone({ phone: res.phone })
            .then(newInfo => {
              ls.set('userInfo', newInfo.member)
              that.setData({ userPhone: res.phone })
              that.submit()
            })
        }
          console.log('userPhone', this.data.userPhone);
          if (res.code == 200) {
            this.setData()
            wx.setStorageSync('member', res.data)
            this.submit()
          }
        })
      })
    }
  },
  // 立即计算
  submit () {
  submit() {
    // console.log('立即计算')
    let { userPhone } = this.data
    let {
      userPhone
    } = this.data
    const res = wx.getStorageSync('member')
    if (userPhone) {
      let { type, info, area, money, typeArray, infoArray, calculatorOptions: options } = this.data
      let {
        type,
        info,
        area,
        money,
        typeArray,
        infoArray,
        calculatorOptions
      } = this.data
      let submitData = {}
      submitData.name = res.name
      submitData.phone = res.phone
@@ -121,21 +162,21 @@
          icon: 'none'
        })
        return false
      }
      }
      if (info === '') {
        wx.showToast({
          title: '请选择户型信息',
          icon: 'none'
        })
        return false
      }
      }
      if (area === '') {
        wx.showToast({
          title: '请输入房屋面积',
          icon: 'none'
        })
        return false
      }
      }
      if (money === '') {
        wx.showToast({
          title: '请输入装修预算',
@@ -144,22 +185,46 @@
        return false
      }
      submitData = ['房屋类型:' + typeArray[type], '户型信息:' + infoArray[info], '房屋面积:' + area + '㎡', '装修预算:' + money + '万元'].join('-')
      for (let i in options) {
        options[i].money = (money * options[i].rate / 100).toFixed(2)
        let childList = options[i].childList
        for(let j in childList) {
          childList[j].money = (money * childList[j].rate / 100).toFixed(2)
      // submitData = ['房屋类型:' + typeArray[type], '户型信息:' + infoArray[info], '房屋面积:' + area + '㎡', '装修预算:' + money + '万元'].join('-')
      let activeList = type == '0' ? [...calculatorOptions.exquisiteList] : [...calculatorOptions.roughList]
      activeList.forEach(item => {
        item.money = (money * item.rate / 100).toFixed(2)
        if (item.children && item.children.length > 0) {
          item.children.forEach(child => {
            child.money = (money * child.rate / 100).toFixed(2)
          })
        }
      }
      this.setData({ calculatorOptions: options, showResultMoney: true, containerContentStyle: 'auto' })
      this.submitCalculatorResult(submitData)
      })
      console.log('activeList', activeList);
      saveRenovationCalculator({
        area,
        budget: money,
        houseStatus: type,
        houseType: info
      }).then(res => {
        // console.log(res)
        if (res.success) {
          wx.showToast({
            title: '计算成功',
            icon: 'none'
          })
        } else {
          console.log(res.msg)
        }
      })
      this.setData({
        activeList,
        showResultMoney: true,
        containerContentStyle: 'auto'
      })
    }
  },
  // 提交计算结果
  submitCalculatorResult (data) {
    saveRenovationCalculator({ info: data }).then(res => {
  submitCalculatorResult(data) {
    saveRenovationCalculator({
      info: data
    }).then(res => {
      // console.log(res)
      if (res.success) {
        wx.showToast({
@@ -173,9 +238,19 @@
  },
  // 获取计算器配置
  getCalculatorOption () {
  getCalculatorOption() {
    getBjParamConfig().then(res => {
      this.setData({ calculatorOptions: res.data })
      this.setData({
        calculatorOptions: res.data
      })
    })
    getDictData({
      code: 'WEIXIN_DEFAULT_IMGS',
      label: 'JISUAN_BG'
    }).then(res => {
      this.setData({
        background: res.data.code
      })
    })
  },
  onReady: function () {