accountSwitchingPop.js 2.6 KB
// pages/tab/output/components/accountSwitchingPop/accountSwitchingPop.js
import { IMG_URL } from '~/utils/constantData.js'
import USERAPI from '~/api/usersApi/index'
import { setUserInfo } from '~/utils/cache/storageApi'
Component({
  options: {
    addGlobalClass: true
  },
  /**
   * 组件的属性列表
   */
  properties: {
    popShow: {
      type: Boolean,
      value: false
    },
    merchantRange: Object
  },

  /**
   * 组件的初始数据
   */
  data: {
    IMG_URL,
    merchantList: []
  },
  observers: {
    merchantRange: function (e) {
      // console.log(e)
      this.setData({
        merchantList: e
      })
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    hidePopup() {
      console.log(222)
      this.triggerEvent('hidePopup')
    },
    confirm() {
      console.log(111)
      let { merchantRange, subIndex } = this.data
      let actObj = merchantRange.find((item, index) => {
        if (item.act) {
          subIndex = index
        }
        return item.act
      })
      // console.log(actObj)
      // this.setData({
      //   subIndex
      // })

      if (!actObj) {
        return wx.$api.showToast({
          title: '请选择服务商',
          icon: 'error'
        })
      } else {
        this.changePicker(actObj)
      }
    },
    changePicker(actObj) {
      // 修改对应下标显示
      console.log('changePicker')
      // let { value } = e.detail
      // app.globalData.yqdInfo.subIndex = value
      // let { merchantId } = this.data.merchantRange[value]
      // let obj = {}
      // if (merchantId) obj.merchantId = actObj
      let { merchantId } = actObj
      wx.$api.showLoading('加载中...')
      USERAPI.switchSubLoginInfo({ merchantId }).then(res => {
        console.log(res)
        setUserInfo(res)
        this.hidePopup()
        this.triggerEvent('changeAccount', 'refresh')
        // }
        wx.$api.hideLoading()
      })
      // this.tapPicker()
    },
    select(e) {
      // console.log(e)
      let { index } = e.currentTarget.dataset
      console.log(index)
      let { merchantRange } = this.data
      merchantRange.forEach((item, ind) => {
        if (index == ind) {
          item.act = true
        } else {
          item.act = false
        }
        this.setData({
          merchantRange
        })
      })
    },
    input(e) {
      console.log(e)
      let { merchantRange } = this.data
      let { value } = e.detail
      merchantRange.forEach(item => {
        item.act = false
        item.show = item.merchantName.includes(value)
        //  = show
      })
      this.setData({
        merchantRange
      })
    }
  }
})