index.js 8.33 KB
// packageStoreAssistant/components/BoutiqueCoupon/index.js
import boutiquecouponApi from '~/api/boutiquecouponApi'
import USERAPI from '~/api/usersApi/index'
import environment from '~/utils/request/environment'

Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },
  lifetimes: {
    ready() {
      const {
        boutiqueCoupon_myself_status
      } = wx.$storage.get('userInfo')
      this.setData({
        myself: boutiqueCoupon_myself_status
      })
      wx.$eventbus.on('REFRESHBOUTIQUECOUPON', () => {
        this.refresherrefresh(false)
      })
      const {
        query
      } = wx.getEnterOptionsSync()
      if (query.usedFlag) {
        this.setData({
          usedFlag: query.usedFlag
        })
      }
      this.refresherrefresh(false)
    }
  },
  /**
   * 组件的初始数据
   */
  data: {
    couponIndex: 0,
    usedFlag: 'N',
    useCouponList: [],
    pageNum: 1,
    total: 0,
    totalY: 0,
    totalN: 0,
    orderBoardData: [{
      title: '本月产值',
      icon: 'order_icon_5',
      count: 0,
      unit: '¥',
      prop: 'monthlyOrderMoneyTotal'
    }, {
      title: '累计产值',
      icon: 'order_icon_8',
      count: 0,
      unit: '¥',
      prop: 'orderMoneyTotal'
    }, {
      title: '本月客单价',
      icon: 'order_icon_4',
      count: 0,
      unit: '¥',
      prop: 'monthlyAveragePrice'
    }, {
      title: '累计客单价',
      icon: 'order_icon_7',
      count: 0,
      unit: '¥',
      prop: 'averagePrice'
    }, {
      title: '本月订单',
      icon: 'order_icon_2',
      count: 0,
      unit: '',
      prop: 'monthlyOrderNum'
    }, {
      title: '累计订单',
      icon: 'order_icon_6',
      count: 0,
      unit: '',
      prop: 'orderNum'
    }]
  },

  /**
   * 组件的方法列表
   */
  methods: {
    changeMyself() {
      const userInfo = wx.$storage.get('userInfo')
      let {
        myself
      } = this.data
      myself = !myself
      this.setData({
        myself
      })
      userInfo.boutiqueCoupon_myself_status = myself
      wx.$storage.set('userInfo', userInfo)
      let {
        usedFlag
      } = this.data
      this.getTotal()
      this.clearData(usedFlag)
    },
    async getMerchantCouponList() {
      let couponList = []
      try {
        const {
          merchantId
        } = wx.$storage.get('userInfo'), {
          code,
          data,
          message
        } = await boutiquecouponApi.getMerchantCouponList({
          merchantId
          // merchantId: '44209390'
        })
        if (code == 0) {
          console.log(data)
          data.map(e => {
            if (e.enabledFlag == 'Y') {
              e.num = e.amount - e.getAmount
              if (e.num < 0) e.num = 0
              e.couponName = e.couponInfo.couponName
              couponList.push(e)
            }
          })
          this.setData({
            couponList
          })
        } else throw message
      } catch (error) {
        wx.$api.showToast(error)
      }
      this.setData({
        couponFlag: true
      })
    },
    async getMerchantCustomerCouponList(flag) {
      wx.$api.showLoading()
      let {
        usedFlag,
        pageNum,
        myself
      } = this.data
      try {
        const {
          merchantId,
          uuid
        } = wx.$storage.get('userInfo')
        let params = {
          merchantId,
          // merchantId: '44209390',
          pageNum,
          usedFlag: flag ? flag : usedFlag
        }
        if (myself) params.userId = uuid
        const {
          code,
          data,
          message
        } = await boutiquecouponApi.getMerchantCustomerCouponList(params)
        if (code == 0) {
          const {
            list,
            total
          } = data
          if (flag) {
            if (flag == 'Y') this.setData({
              totalY: total
            })
            else this.setData({
              totalN: total
            })
            return
          }
          if (list.length) {
            const usedBool = usedFlag === 'Y'
            list.map(e => {
              const time = usedBool ? e.usedTime : e.createdDate
              e.time = (usedBool ? '核销' : '领券') + '时间:' + new Date(time).format('yyyy-MM-dd hh:mm')
              if (!usedBool) {
                if (e.customerInviteRecordDTOList?.length) {
                  let {
                    inviteStatus,
                    inviteDate,
                  } = e.customerInviteRecordDTOList.at(-1)
                  //inviteStatus  1待预约 2已预约 3取消预约 4已施工 5过期到店
                  e.appointmentTime = {
                    1: '待预约',
                    2: new Date(inviteDate).format('yyyy-MM-dd hh:mm'),
                    3: '取消预约',
                    4: '已施工',
                    5: '超时未到店',
                    6: '已撤销'
                  } [inviteStatus]
                  e.appointmentStatus = inviteStatus
                }
              }
            })
            this.setData({
              [`useCouponList[${pageNum - 1}]`]: list
            })
          }
          this.setData({
            total
          })
          wx.hideLoading()
        } else throw message
      } catch (error) {
        if (pageNum != 1) this.setData({
          pageNum: pageNum - 1
        })
        wx.$api.showToast(String(error))
      }
      this.setData({
        useCouponFlag: true,
        refresherrefreshFlag: false
      })
    },
    getTotal() {
      this.getMerchantCustomerCouponList('N')
      this.getMerchantCustomerCouponList('Y')
    },
    couponChange(e) {
      let {
        current
      } = e.detail
      this.setData({
        couponIndex: current
      })
    },
    async jump(e) {
      const {
        url,
        item
      } = e.currentTarget.dataset, {
        userType
      } = wx.$storage.get('userInfo')
      if (url == 'createCoupon') {
        if (item.num == 0) return
        if (userType != 2) {
          wx.$api.showToast('请使用员工账号生成礼券')
          return
        }
        const {
          openId
        } = await USERAPI.getWXUserInfo(),
          tmplIds = [
            environment.index == 4 ? 'toTSRMUqD_-FdCdbkdbbjCpatWMppBweprqkwn37LwU' : 'JSFEfwQTbdMxS6mu73ShCzI0GGuhuvZBbuqN8CbBWEc', //
            environment.index == 4 ? 'f5KiQeNfMK6HW8p409mXRyDX-xsfukEdkyocACXrmJw' : '4pxTz0Ncu-5RV2MQfBgQ76_l45fYhLxEwCsf46YkMb4' //积分到账
          ]
        await wx.requestSubscribeMessage({
          tmplIds
        })
        boutiquecouponApi.saveSaNotifyInfo({
          merchantCouponId: item.id,
          saOpenId: openId
        })
        this.goto(url, item)
      } else this.goto(url, item)

    },
    goto(url, item) {
      wx.$router.go(`/packageStoreAssistant/packageBoutiqueCoupon/pages/${url}/${url}`, item)
    },
    clearData(usedFlag) {
      this.setData({
        usedFlag,
        useCouponList: [],
        pageNum: 1,
        useCouponFlag: false,
        nothing: false,
      })
      this.getMerchantCustomerCouponList()
    },
    changeTab(e) {
      let {
        type
      } = e.currentTarget.dataset, {
        pageNum,
        usedFlag
      } = this.data
      const result = type ? 'Y' : 'N'
      if (pageNum == 1 && usedFlag == result) return
      this.clearData(result)
    },
    refresherrefresh(val = true) {
      this.setData({
        refresherrefreshFlag: val
      })
      let {
        usedFlag
      } = this.data
      this.getTotal()
      this.getMerchantCouponList()
      this.clearData(usedFlag)
      this.getOrderBoardData()
    },
    scrollonReachBottom() {
      let {
        total,
        pageNum
      } = this.data
      if (Math.ceil(total / 10) > pageNum) {
        this.setData({
          pageNum: pageNum + 1
        })
        this.getMerchantCustomerCouponList()
      } else this.setData({
        nothing: true
      })
    },
    // 获取看板数据
    getOrderBoardData() {
      const USERINFO = wx.$storage.get('userInfo')
      boutiquecouponApi.getBoardData({
        merchantId: USERINFO.merchantId
      }).then(res => {
        let {
          orderBoardData
        } = this.data
        if (res) {
          for (let i = 0; i < orderBoardData.length; i++) {
            let propName = orderBoardData[i].prop
            orderBoardData[i].count = res[propName] || 0
          }
          this.setData({
            orderBoardData
          })
        }
      })
    }
  }
})