userPortrait.js 4.72 KB
// tabDate/pages/userPortrait/userPortrait.js
import CONSTANTDATA from './constant/data'
import api from '../../api'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    detail: {},
    formData: CONSTANTDATA.formData,
    detailForm: {
      additionalInformation: ''
    },
    customerSex: 1
  },
  click(e) {
    let { pidx, idx } = e.currentTarget.dataset
    let { formData } = this.data
    let _item = formData[pidx]
    let _active = formData[pidx].optionList[idx].active
    // debugger
    if (!_item.isMult) {
      this.setData({
        [`formData[${pidx}].optionList`]: JSON.parse(JSON.stringify(CONSTANTDATA[_item.optionsName]))
      })
    }

    // 赙赠产品 每类只能二选一
    if (_item.name == 'giftProductList') {
      const noActiveIdx = idx % 2 == 0 ? idx + 1 : idx - 1
      this.setData({
        [`formData[${pidx}].optionList[${noActiveIdx}].active`]: false
      })
    }

    this.setData({
      [`formData[${pidx}].optionList[${idx}].active`]: _item.name == 'customerSex' || _item.isMult ? !this.data.formData[pidx].optionList[idx].active : !_active
    })
  },

  handleInput(e) {
    let { attr } = e.currentTarget.dataset
    let { value } = e.detail
    this.setData({
      ['detailForm.' + attr]: value
    })
  },

  async recoverPortrait(id) {
    let { code, data: info } = await api.portraitQuery({ id })
    console.log(info);
    if (code != 0) return

    let { formData } = this.data
    formData.forEach(item => {
      if (info[item.name]) {
        if (item.isMult) {
          console.log(item.name, info[item.name], 'info[item.name]');
          // let _list = info[item.name].split(',')
          let _list = info[item.name]
          _list.forEach(n => {
            item.optionList.find(k => k.confId == n).active = true
          })
        } else {
          item.optionList.find(k => k.confId == info[item.name]).active = true
        }
        // }
      } else {
        if (item.name == 'customerSex') {
          let { customerSex } = this.data
          item.optionList.find(k => k.confId == customerSex).active = true
        }
      }
    })
    this.setData({
      formData
    })
  },

  reset() {
    let { formData } = this.data

    formData.forEach(item => {
      // console.log(item?.optionList,'item?.optionList');
      // if(item.isMult)  params[item.name]  = []
      if (item.name != 'customerSex') {
        item?.optionList?.forEach(subItem => {
          subItem.active = false
        })
      }

    })
    console.log(formData, 'formData');
    this.setData({
      formData
    })
  },

  dealParam() {
    let { formData } = this.data
    let params = {}
    formData.forEach(item => {
      // console.log(item?.optionList,'item?.optionList');
      if (item.isMult) params[item.name] = []
      item?.optionList?.forEach(subItem => {
        if (subItem.active) {
          item.isMult ? params[item.name].push(subItem.confId) : (params[item.name] = subItem.confId)
        }
      })
    })
    return params
  },

  confirm() {
    let { customerId } = this.data
    let params = {
      ...this.dealParam(),
      customerId
    }
    wx.$util.showLoading()
    api.updateProfile(params).then(res => {
      let { code, data, message } = res
      wx.hideLoading()
      if (code == '0') {
        wx.$util.showToast('编辑成功')
        // 仅刷新预约管理列表
        wx.$util.refreshPageData(true)
        wx.$router.back()
      } else {
        wx.$util.showToast(message)
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // wx.$router.getData(res => {
    //   res = {
    //     ...res.customerCoupon,
    //     ...res.customer,
    //     ...res
    //   }
    //   this.setData({
    //     detail: res
    //   })
    //   console.log(res, 'res')
    //   this.recoverPortrait(this.data.detail)
    // })
  },
  onRoute(options) {
    console.log(options);
    const { customerId, customer } = options
    // let detail = {
    // ...options.customerCoupon,
    // ...options.customer,
    // ...options
    // }
    this.setData({
      customerId,
      customerSex: customer?.customerSex || 1
    })
    console.log(customerId, 'customerId')
    this.recoverPortrait(customerId)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() { },

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

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

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

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

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

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