index.js 2.24 KB
import mineApi from '~/api/module/mine.js'
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    defaultAddress: Object,
    address:Array,
    show: {
      type: Boolean,
      observer: function (val, o) {
        if (val && this.properties.address.length) {
          console.log(this.properties.defaultAddress?.id)
          this.properties.address.forEach((item, index) => {
            if (item.id == this.properties.defaultAddress?.id) {
              item.active = true
            }
          })
          this.setData({
            address: this.properties.address
          })
        }
      }
    },
    staff:Boolean
  },

  /**
   * 组件的初始数据
   */
  data: {
    addressList: []
  },

  /**
   * 组件的方法列表
   */
  methods: {
    back() {
      this.cancel()
    },
    addNew (e) {
      let { url } = e.currentTarget.dataset
      wx.p.router.go(url,{
        platform:true,
        staff:this.data.staff,
        notIntercept:this.data.staff
      })
    },
    // 确认
    confirm(e) {
      let { address } = this.data
      const item = address?.find(val => val.active == true)
      if (!item || !Object.keys(item)?.length) return wx.p.wxApi.showToast('请选择收货地址')
      const contactAddress = item.provinceName+item.cityName+item.countyName+item.address
      this.triggerEvent('getAddress', {defaultAddress:item,contactAddress})
      this.cancel()
    },
    cancel() {
      this.setData({
        show: false
      })
      this.triggerEvent('cancel')
    },
    toSelect(e) {
      const { item } = e.currentTarget.dataset
      let { address } = this.data
      address?.forEach(val => {
        val.active = false
        if (item.id == val.id) val.active = true
      })
      this.setData({address})
    },
    addressList() {
      mineApi.getCustomerAddressList().then(data => {
        // 新增地址后默认将新增的选中
        data?.forEach((val, index) => {
          val.active = false
          if (index == 0) val.active = true
        })
        this.setData({
          address: data
        })
        wx.hideLoading()
      })
    }
  },
  lifetimes: {
    attached: function () {
      wx.p.eventbus.on('REFRESH_PLATFORM_ADDRESS',()=>{
        this.addressList()
      })
    },
  },
})