shopMall.js 6.94 KB
import ActivityApi from "~/api/activityApi/index";
Component({
  properties: {
    discoveryChannel:Object,
    fresh:{
      type:Boolean,
      observer: function (val) {
        if (val) {
          this.resetFilter()
          this.getIntergralList()
          this.getBoardData()
        }
      }
    }
  },
	


  data: {
    filterInit: true,
    show: false,
    currentBtn: 0,
    showCodeDialog: false,
    staffCodeUrl: '',
    showAuth: false,
    timeList: [{
      type: 0,
      name: '全部',
    }, {
      type: 1,
      name: '今天',
    }, {
      type: 2,
      name: '昨天',
    }, {
      type: 3,
      name: '本月',
    }],
    filtterQuery: {
      verificationStartTime: '',
      verificationEndTime: '',
      minIntegral: '',
      maxIntegral: '',
      term: '',
      currentPage: 1,
      pageSize: 10,
    },
    show: false,
    list: [],
    noResult: false,
    orderBoardData:[{
      title:'本月核销订单数',
      icon:'order_icon_1',
      count:0
    },{
      title:'本月核销积分',
      icon:'order_icon_3',
      count:0
    },{
      title:'累计核销订单数',
      icon:'order_icon_2',
      count:0
    },{
      title:'累计核销积分',
      icon:'order_icon_3',
      count:0
    }]
  },

  methods: {
    activityRefresh() { },
    toShowFilter() {
      this.setData({
        show: true
      })
    },
    cancel() {
      this.setData({
        show: false
      })
    },
    select(e) {
      const { index, type } = e.currentTarget.dataset
      const now = new Date()
      const monthFirst = this.dateFormat(new Date(now.getFullYear(), now.getMonth(), 1))
      const monthLast = this.dateFormat(new Date(now.getFullYear(), now.getMonth() + 1, 0, 23, 59, 59))
      const today = this.dateFormat(now)
      const yesterday = this.dateFormat(now.setDate(now.getDate() - 1))
      const STARTNAME = 'filtterQuery.verificationStartTime'
      const ENDNAME = 'filtterQuery.verificationEndTime'
      let start = '', end = ''
      if (type == 1) start = today, end = today
      if (type == 2) start = yesterday, end = yesterday
      if (type == 3) start = monthFirst, end = monthLast
      this.setData({
        currentBtn: index,
        [STARTNAME]: start,
        [ENDNAME]: end,
        filterInit: type ? false : true
      })
    },
    dateFormat(time) {
      return new Date(time).format('yyyy-MM-dd')
	  //console.log()
    },
    filtterFn(e) {
      const { type } = e.currentTarget.dataset
      const NAME = 'filtterQuery.' + type
      const value = e.detail.value
      this.setData({
        [NAME]: value,
        filterInit: false
      })
    },
    resetFilter() {
      const filtterQuery = {
        time: '',
        verificationStartTime: '',
        verificationEndTime: '',
        minIntegral: '',
        maxIntegral: '',
        term: '',
        currentPage: 1,
        pageSize: 10,
      }
      this.setData({
        filtterQuery,
        currentBtn: 0,
        filterInit:true
      })
    },
    getIntergralList() {
      let {  list,filtterQuery } = this.data
      let query = this.checkValue()
      const min = +query.minIntegral,max = +query.maxIntegral
      if(min&&max&&min>max){
        return wx.$api.showToast('积分最小值不能大于积分最大值')
      }
      this.cancel()
      ActivityApi.intergralList(query).then(res => {
        this.setData({
          list: filtterQuery.currentPage > 1 ? [...list, ...res.list] : res.list,
          noResult: res.list.length ? false : true,
          total: res.total
        })
      })
    },
    checkValue() {
      let { filtterQuery } = this.data
      let query = {}
      for (let k in filtterQuery) {
        if (filtterQuery[k]) {
          let start = '', end = ''
          if (k == 'verificationStartTime') {
            start = filtterQuery[k] + ' 00:00:00'
            filtterQuery[k] = start
          } else if (k == 'verificationEndTime') {
            end = filtterQuery[k] + ' 23:59:59'
            filtterQuery[k] = end
          }
          Object.assign(query, { [k]: filtterQuery[k] })
        }
      }
      return query
    },
    toSearch() {
      this.getIntergralList()
    },
    comfirm() {
      this.getIntergralList()
    },
    toPage() {
      wx.navigateTo({
        url: '/packageStoreAssistant/packageMine/pages/ShopMallHX/index',
      });
    },
    // 上拉触底加载
    scrollonReachBottom(e) {
      let { total, filtterQuery } = this.data
      // 判断当前页是否是末页
      if (filtterQuery.currentPage * filtterQuery.pageSize >= total) return
      this.setData({
        'filtterQuery.currentPage': filtterQuery.currentPage + 1
      }, () => {
        this.getIntergralList()
      })
    },
    openCodeDialog(){
        const {merchantId} = wx.$storage.get('userInfo') || {}
        ActivityApi.getshareStaffCodeApi({merchantId}).then(res => {
            console.log(res)
            if(res){
                this.setData({
                    showCodeDialog: true,
                    staffCodeUrl: res
                })
            }
        })
        
    },
    saveCode(){
        const _this = this
        const {staffCodeUrl} = this.data
        wx.getSetting({
          success: (res)=> {
            if(res.authSetting['scope.writePhotosAlbum']===false){
              _this.setData({
                showAuth: true
              })
            }else{
              this.setData({
                 showCodeDialog: false,
              })
              wx.$api.showToast('正在保存图片...')
              wx.downloadFile({
                url: staffCodeUrl,
                success(res) {
                  if (res.statusCode === 200) {
                    wx.saveImageToPhotosAlbum ({
                      filePath: res.tempFilePath,
                      success(res) {
                        setTimeout(() => {
                          wx.$api.showToast('保存成功!')
                        }, 2000)
                      },
                      fail({ errMsg }) {
                        _this.setData({
                          showAuth: errMsg == "saveVideoToPhotosAlbum:fail auth deny",
                        })
                      }
                    })
                  }
                }
              })
            }
          }
        })
    },
    cancelCodeDialog(){
        this.setData({
            showCodeDialog: false
        })
    },
    // 获取订单看板数据
    getBoardData(){
      const USERINFO = wx.$storage.get('userInfo')
      ActivityApi.getHxOrderBoard({merchantId:USERINFO.merchantId}).then(res=>{
        let {orderBoardData} = this.data
        if(res){
          orderBoardData[0].count = res.monthOrderCount
          orderBoardData[1].count = res.monthIntegralValue
          orderBoardData[2].count = res.totalOrderCount
          orderBoardData[3].count = res.totalIntegralValue
          this.setData({
            orderBoardData
          })
        }
      })
    },
  },
  lifetimes: {
    // attached() {
    //   this.getIntergralList()
    // }
  },
})