index.js 5.86 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
// static/nativeComponents/CubeNav/index.js
const app = getApp()
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    datas: {
      type: Object
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    imgList: [],
    getColumnWidth: '',
    getImgSize: 0,
    navShow: false,
    wrapW: 0,
    w: 0,
    swiperList: [],
    swiperItemW: 0,
    navLeft: 0,
    cube2W: 0,
    cube3H: 0,
李嘉林 committed
28 29
    navListStyle: [],
    isLoading: false,
30 31 32 33 34 35 36 37 38 39 40 41 42 43
  },
  ready() {
    this.init()
  },

  /**
   * 组件的方法列表
   */
  methods: {
    init() {

      let _this = this;

      let columnWidthArr = ['100%', '50%', '33.3%', '25%', '20%', '16.6%']
程智春 committed
44 45

      this.data.datas.componentData.imgList.forEach(item => {
李嘉林 committed
46
        console.log(item,'---------------44')
程智春 committed
47 48
        item.imageUrl = item.imageUrl ? app.DFSImg(item.imageUrl,400,400) : ''
      })
49 50 51
      this.setData({
        imgList: this.data.datas.componentData.imgList.filter(item => item.visible == undefined || item.visible == 1),
        getColumnWidth: columnWidthArr[this.data.datas.componentData.columnNum - 1],
李嘉林 committed
52 53 54
        getImgSize: (100 - this.data.datas.componentData.imgSize) / 2,
        navListStyle: [this.navListStyleFun(0), this.navListStyleFun(1), this.navListStyleFun(2)],
        isLoading: true,
55
      })
李嘉林 committed
56
      console.log(this.data.navListStyle,'----navListStyle')
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75

      let arr = []
      let count = this.data.datas.componentData.columnNum * this.data.datas.componentData.rowNum;
      let num = Math.ceil(this.data.imgList.length / count);
      for (let i = 0; i < num; i++) {
        arr.push([]);
      }
      this.data.imgList.forEach((item, index) => {
        let i = Math.floor(index / count);
        arr[i].push(item);
      });

      this.setData({
        swiperList: arr
      })

      this.createSelectorQuery().select('.cube-item').boundingClientRect(function(res) {
        console.log('swiper-wrap', res)
        if (res) {
程智春 committed
76 77
          if (_this.data.datas.componentData.rowNum == 1){
            _this.setData({
程智春 committed
78
              cube3H: res.height * _this.data.datas.componentData.rowNum
程智春 committed
79 80 81 82
            })
          } else if (_this.data.datas.componentData.rowNum == 2){
            if(_this.data.imgList.length / _this.data.datas.componentData.columnNum > 1){
              _this.setData({
程智春 committed
83
                cube3H: res.height * _this.data.datas.componentData.rowNum
程智春 committed
84 85 86
              })
            }else{
              _this.setData({
程智春 committed
87
                cube3H: res.height * 1
程智春 committed
88 89 90 91 92
              })
            }
          } else if (_this.data.datas.componentData.rowNum == 3){
            if (_this.data.datas.componentData.columnNum == 1){
              _this.setData({
程智春 committed
93
                cube3H: res.height * (_this.data.imgList / _this.data.datas.componentData.rowNum)
程智春 committed
94 95 96
              })
            }else{
              _this.setData({
程智春 committed
97
                cube3H: res.height * (Math.ceil(_this.data.imgList.length / _this.data.datas.componentData.columnNum))
程智春 committed
98 99 100
              })
            }
          }
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
        }

      }).exec()

      console.log('轮播图swiperList', this.data.swiperList)

      this.createSelectorQuery().select('.cube2').boundingClientRect(function(res) {
        console.log('来这里了', res)
        if (res) {
          let cube2W = res.width

          _this.setData({
            cube2W: cube2W
          })
          let wArr = [cube2W / 1, cube2W / 2, cube2W / 3, cube2W / 4, cube2W / 5, cube2W / 6]


          if (
            Math.floor(
              _this.data.imgList.length /
              (_this.data.datas.componentData.columnNum * _this.data.datas.componentData.rowNum)
            ) == 0
          ) {
            _this.setData({
              wrapW: cube2W
            })
          } else {
            _this.setData({
              wrapW: Math.floor(
                  _this.data.imgList.length /
                  (_this.data.datas.componentData.columnNum * _this.data.datas.componentData.rowNum)
                ) *
                cube2W + (_this.data.imgList.length %
                  (_this.data.datas.componentData.columnNum *
                    _this.data.datas.componentData.rowNum)) *
                wArr[_this.data.datas.componentData.columnNum - 1]
            })
          }
          _this.setData({
            w: wArr[_this.data.datas.componentData.columnNum - 1]
          })
程智春 committed
142
          console.log(_this.data.w)
143 144 145 146 147 148 149 150 151 152 153 154
          if (_this.data.wrapW > cube2W) {
            _this.setData({
              navShow: true
            })
          } else {
            _this.setData({
              navShow: false
            })
          }
        }
      }).exec()
    },
李嘉林 committed
155 156 157 158
    navListStyleFun(index) {
      let item = this.data.datas.componentData.navList[index];
      return `--t-font-size:${item.tFontSize*2}rpx;--t-color:${item.tColor};--t-weight:${item.tWeight?500:''};--sub-t-font-size:${item.subTFontSize*2}rpx;--sub-t-color:${item.subTColor};--sub-t-weight:${item.subTWeight?500:''};--figure-size:${item.figureSize}%;--figure-border-radius:${item.figureBorderRadius*2}rpx;background-image:url(${item.bgImg});--box-bg-color:${item.boxBgColor};`;
    },
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
    touchmove(e) {
      let _this = this
      this.debounce(function() {
        let moveX = _this.data.wrapW - _this.data.cube2W;
        _this.createSelectorQuery().select('#wrapW').boundingClientRect(function(res) {
          if (res) {
            let x = Math.abs(res.left) / moveX;
            _this.setData({
              navLeft: 30 * x
            })
          }
        }).exec()
      }, 100)()

    },
    debounce(func, wait) {
      let timeout = "";
      return v => {
        if (timeout) {
          clearTimeout(timeout);
        }
        timeout = setTimeout(() => {
          func(v);
        }, wait);
      };
    },
    onclickHandle(e){
      let item = e.currentTarget.dataset.item
      console.log(item)
侯体倬 committed
188
      app.trackCpn(e, this.data.datas.componentName, item)
李嘉林 committed
189
      console.log(item,'---------------------180')
程智春 committed
190
      app.$themeToLink(item.link)
191 192
    }
  }
侯体倬 committed
193
})