IndexMerchant.js 7.39 KB
// components/IndexMerchant/IndexMerchant.js
import {
  indexApi
} from '../../api/api'
import mapApi from '../../utils/mapapi'
import {
  componentsMixin
} from '../../mixins/index'
Component({
  behaviors: [componentsMixin],
  /**
   * 组件的属性列表
   */
  properties: {
    merchantSwitch: Boolean,
    key: {
      type: String,
      value: 'showMerchant'
    },
    init: {
      type: Boolean,
      observer(n) {
        if (n) {
          this.getLocationByIp()
          this.setData({
            getIndexListData: true
          })
        }
      }
    },
    storeCode: Number
  },
  observers: {
    'merchantSwitch': function (n) {
      let userInfo = wx.$util.getUserInfo()
      this.setData({
        show: n,
        group: userInfo && userInfo.group
      })
      if (userInfo && userInfo.group) wx.hideTabBar({
        success() {
          wx.$hideTab = true
        }
      })
      else wx.showTabBar({
        success() {
          wx.$hideTab = false
        }
      })
    }
  },
  /**
   * 组件的初始数据
   */
  data: {
    pageNum: 1,
    list: [],
    fromPosition: []
  },
  /**
   * 组件的方法列表
   */
  methods: {
    noScroll() {},
    close() {
      let thisPage = wx.$router.getPageInstance()
      let {
        key,
        group
      } = this.data
      if (group) return
      thisPage.setData({
        [key]: false
      })
    },
    getLocationByIp() {
      mapApi.getLocationByIp().then(({
        content: {
          address_detail
        }
      }) => {
        let defaultCity = address_detail.city.replace('市', '')
        this.setData({
          defaultCity,
          cityCode: defaultCity
        })
        this.getGHStoreList()
      })
    },
    getGHStoreList() {
      let thisPage = wx.$router.getPageInstance()
      let {
        key,
        show,
        list,
        pageNum,
        storeName,
        brandId,
        cityCode,
        nothing,
      } = this.data
      let userInfo = wx.$util.getUserInfo()
      if (!show) thisPage.setData({
        [key]: userInfo.group
      })
      let params = {
        pageNum
      }
      if (storeName) params.storeName = storeName
      if (brandId) params.brandId = brandId
      if (cityCode) params.cityCode = cityCode
      let storeCode = wx.$util.getStorageSync('srcm_shopmall_store_code'),
        addressInfo = wx.getStorageSync('address_info')
      wx.$util.showLoading('加载中...')
      indexApi.getGHStoreList(params).then(data => {
        wx.hideLoading()
        if (data.length == 0) nothing = true
        data.length && data.map(e => {
          if (e.storeCode == storeCode) e.current = true
        })
        this.setData({
          list: [...list, ...data],
          listFlag: true,
          nothing
        }, () => {
          if (addressInfo) this.getLocation()
        })
      })
    },
    setCalculateDistance() {
      let {
        list,
        fromPosition
      } = this.data
      if (!list.length || !fromPosition.length) return
      let toList = []
      let indexList = []
      let toGroup = []
      let indexGroup = []
      list.map((e, index) => {
        let {
          location
        } = e
        if (location && location.latitude) {
          if (!e.distance && location.latitude != '0.0') {
            let latitude = Number(location.latitude),
              longitude = Number(location.longitude)
            latitude = latitude > 90 ? Number(location.longitude) : Number(location.latitude)
            longitude = latitude > 90 ? Number(location.latitude) : Number(location.longitude)
            toList.push([latitude, longitude])
            indexList.push(index)
            if (toList.length >= 10) {
              toGroup.push(toList)
              indexGroup.push(indexList)
              toList = []
              indexList = []
            }
          }
        }
      })
      if (toList.length) {
        toGroup.push(toList)
        indexGroup.push(indexList)
      }
      if (toGroup.length) {
        let index = 0
        let interval = setInterval(() => {
          if (index == toGroup.length - 1) clearInterval(interval)
          mapApi.calculateDistanceMatrix(fromPosition, toGroup[index]).then(({
            rows: [data]
          }) => {
            let distanceList = []
            data.elements.map(e => {
              let distance = e.distance.value ? Number((e.distance.value / 1000)).toFixed(2) : 0
              distanceList.push(distance)
            })
            indexGroup[index].map((s, ind) => {
              if (!list[s].distance) {
                this.setData({
                  [`list[${s}].distance`]: distanceList[ind]
                })
              }
            })
            index += 1
          })
        }, 1 * 1000)
      }

    },
    phoneCall(e) {
      let {
        phone
      } = e.currentTarget.dataset
      if (!phone) return
      wx.$util.phoneCall(phone)
    },
    input(e) {
      let storeName = e.detail.value
      this.setData({
        storeName
      })
    },
    clear() {
      this.setData({
        storeName: ''
      })
    },
    search(e) {
      let storeName = e.detail.value
      this.setData({
        storeName
      })
      this.clearData()
    },
    openLocation(e) {
      let {
        location,
        storeName,
        address
      } = e.currentTarget.dataset.item
      mapApi.translate([
        [location.latitude, location.longitude]
      ], 5).then(res => {
        let {x,y} = res[0]
        wx.openLocation({
          latitude: y,
          longitude: x,
          name: storeName,
          address
        })
      })
    },
    // 下拉触底
    scrollTolower(e) {
      let {
        pageNum,
        nothing
      } = this.data
      if (nothing) return
      this.setData({
        pageNum: ++pageNum
      })
      this.getGHStoreList()
    },
    clearData() {
      this.setData({
        listFlag: false,
        list: [],
        nothing: false,
        pageNum: 1
      })
      this.getGHStoreList()
    },
    choose(e) {
      this.setData(e.detail)
      this.clearData()
    },
    searchAll() {
      this.setData({
        storeName: '',
        cityCode: '',
        brandName: '',
        brandId: ''
      })
      this.clearData()
    },
    showIndexList(e) {
      let {
        type
      } = e.currentTarget.dataset
      this.setData({
        [type == 0 ? 'showCity' : 'showBrand']: true
      })
    },
    chooseMerchant(e) {
      let {
        item
      } = e.currentTarget.dataset
      let {
        storeCode,
        key
      } = this.data
      if (item.storeCode == storeCode) return
      wx.$util.showLoading('切换中...')
      getApp().getGHMerchant(item.storeCode, ({
        data
      }) => {
        let thisPage = wx.$router.getPageInstance()
        thisPage.setData({
          userInfo: data,
          [key]: false
        })
        thisPage.getHomeData()
        this.setData({
          showMerchant: false
        })
        // 切换门店清空推荐人信息
        const staff = wx.getStorageSync('shareStaff')
        if (staff) {
          wx.removeStorageSync('shareStaff');
        }
      })

    },
    /**获取当前位置 */
    getLocation() {
      wx.$Track.getAddressInfo().then(({
        location
      }) => {
        this.setData({
          fromPosition: [location.lat, location.lng]
        })
        this.setCalculateDistance()
      }).catch(({
        errMsg
      }) => {
        this.setData({
          showAuth: true
        })
      })
    },
  }
})