MyGarageNoCar.js 5.6 KB
import {
  request
} from '../../request/index.js'
const a = getApp()
Page({
  data: {
    imagesUrl: a.globalData.baseUrl,
    _num: 1,
    way: 1,
    MyGarageNoCar: [],

    list: [{
      id: 0,
      title: '标题1',
      create_time: '2019-09-06'
    }, {
      id: 1,
      title: '标题2',
      create_time: '2019-09-06'
    }, {
      id: 2,
      title: '标题3',
      create_time: '2019-09-06'
    }],

    startX: 0, //开始坐标
    startY: 0,
    page: 1,
    is_cloose: false,

  },


  touchE: function (e) {
    // console.log(e);
    var that = this
    if (e.changedTouches.length == 1) {
      //手指移动结束后触摸点位置的X坐标
      var endX = e.changedTouches[0].clientX;
      //触摸开始与结束,手指移动的距离
      var disX = that.data.startX - endX;
      var delBtnWidth = that.data.delBtnWidth;
      //如果距离小于删除按钮的1/2,不显示删除按钮
      var txtStyle = disX > delBtnWidth / 2 ? "left:-" + delBtnWidth + "rpx" : "left:0rpx";
      //获取手指触摸的是哪一项
      var index = e.currentTarget.dataset.index;
      var MyGarageNoCar = that.data.MyGarageNoCar;
      MyGarageNoCar[index].txtStyle = txtStyle;
      //更新列表的状态
      that.setData({
        MyGarageNoCar: MyGarageNoCar
      });
    }
  },
  //手指触摸动作开始 记录起点X坐标
  touchstart: function (e) {
    //开始触摸时 重置所有删除
    this.data.MyGarageNoCar.forEach(function (v, i) {
      if (v.isTouchMove) //只操作为true的
        v.isTouchMove = false;
    })
    this.setData({
      startX: e.changedTouches[0].clientX,
      startY: e.changedTouches[0].clientY,
      MyGarageNoCar: this.data.MyGarageNoCar
    })
  },
  //滑动事件处理
  touchmove: function (e) {
    var that = this,
      index = e.currentTarget.dataset.index, //当前索引
      startX = that.data.startX, //开始X坐标
      startY = that.data.startY, //开始Y坐标
      touchMoveX = e.changedTouches[0].clientX, //滑动变化坐标
      touchMoveY = e.changedTouches[0].clientY, //滑动变化坐标
      //获取滑动角度
      angle = that.angle({
        X: startX,
        Y: startY
      }, {
        X: touchMoveX,
        Y: touchMoveY
      });
    that.data.MyGarageNoCar.forEach(function (v, i) {
      v.isTouchMove = false
      //滑动超过30度角 return
      if (Math.abs(angle) > 30) return;
      if (i == index) {
        if (touchMoveX > startX) //右滑
          v.isTouchMove = false
        else //左滑
          v.isTouchMove = true
      }
    })
    //更新数据
    that.setData({
      MyGarageNoCar: that.data.MyGarageNoCar
    })
  },
  /**
   * 计算滑动角度
   * @param {Object} start 起点坐标
   * @param {Object} end 终点坐标
   */
  angle: function (start, end) {
    var _X = end.X - start.X,
      _Y = end.Y - start.Y
    //返回角度 /Math.atan()返回数字的反正切值
    return 360 * Math.atan(_Y / _X) / (2 * Math.PI);
  },


  changeTitle(e) { // 切换 
    let um = e.currentTarget.dataset.num
    this.setData({
      _num: um
    })
    if (um == 2) {
      this.getMyGarageNoCar()

    } else {
      this.onShow()
    }
    console.log(this.data._num);
  },
  onLoad: function (options) {

  },
  onShow: function () {
    this.getMyGarageNoCar()

  },
  getMyGarageNoCar() { //获取待
    let that = this
    let cat = that.data._num
    if (cat == 1) {
      that.setData({
        way: 1,
        is_cloose: false
      })
      that.getadd()
    } else {
      that.setData({
        way: 2,
        is_cloose: false
      })
      that.getadd()
    }

  },
  async getadd() {
    let that = this
    that.setData({
      MyGarageNoCar: []
    })
    try {
      const {
        data: {
          data
        }
      } = await request({
        url: 'api/my/zanGood',
        data: {
          way: that.data.way
        }

      })
      that.setData({
        MyGarageNoCar: data
      })
      if (data.length != 0) {
        that.setData({
          is_cloose: false
        })
      } else {
        that.setData({
          is_cloose: true
        })
      }
      console.log(that.data.MyGarageNoCar, "有赞");
    } catch (err) {
      console.log(err);
      that.setData({
        msg: err.msg
      })
      that.popTest()
    }

  },

  delOrder(e) { // 删除
    let that = this
    wx.showModal({
      title: '提示',
      content: '确定要删除该商品吗?',
      success: async function (res) {
        if (res.confirm) {
          try {
            const {
              data
            } = await request({
              url: 'api/new2/zanShareDel',
              data: {
                id: e.currentTarget.dataset.id
              }
            })
            console.log(data, "删除");
            if (data.code == 1) {
              let msg = data.msg
              a.popSuccessTest(msg)
              setTimeout(() => {
                that.getMyGarageNoCar()

              }, 1000);
            } else {
              let msg = data.msg
              a.popTest(msg)

            }
          } catch (err) {
            let msg = err.msg
            a.popTest(msg)


          }
        } else {

        }
      }

    })


  },
  storeDetail(e) {
    console.log(e);
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/detail/detail?id=' + id
    })
  },
  popSuccessTest() {
    wx.showToast({
      title: this.data.msg,
      icon: '',
      duration: 1300,
      mask: true
    })
  },
  popTest() {
    wx.showToast({
      title: this.data.msg,
      icon: 'none',
      duration: 1300,
      mask: true
    })
  },



})