小程序的mixins方案--使用Behavior

时间:2020-12-29 11:17:57   收藏:0   阅读:0

新建文件夹behavior,新建文件resList.js

// resList.js
const app = getApp()
module.exports = Behavior({
  behaviors: [],  //引入其它的 behavior
  properties: {},  //properties  同组件的属性
  data: {
    list: [], //列表数据
    recordCount: 0, //列表总条数
    pageNo: 1,
    pageSize: 20
  },  //data  同组件的数据
  methods: {
    // 参数
    _requestPageList(callback) {
      let opt = {
        type: 1, //(主要是和礼品管理共享接口区别排序)
        isValid: 1, //(是否有效,1是2否)此处默认填写1,只有有效期内的才能再礼品列表展示
        name: ""
      }
      // 第一个参数是请求的接口名
      this._requestPageListCom("giftPageList", opt, callback);
    },
    // 列表接口
    _requestPageListCom(url, params, callback) {
      let opt = Object.assign({}, params, {
        pageData: {
          pageNo: this.data.pageNo,
          pageSize: this.data.pageSize
        },
      })
      app.$request.post(app.$config[url], opt).then(res => {
        if (res.returnResult === 200) {
          const oldList = this.data.list
          const newGoodsList = res.returnData.data
          const nList = [...oldList, ...newGoodsList]
          let newData = {}; //新变更数据
          for(let i in nList){
            newData[‘list[‘+i+‘]‘] = nList[i]
          }
          this.setData(newData);//赋值列表数据
          this.setData({
            recordCount: res.returnData.recordCount
          })
          if(callback && typeof callback === ‘function‘){
            callback()
          }
        }
      })
    },
    // 下拉刷新
    onPullDownRefresh() {
      this.setData({
        list: [],
        pageNo: 1
      });
      this._requestPageList(() => {
        // 当处理完数据刷新后,wx.stopPullDownRefresh可以停止当前页面的下拉刷新
        wx.stopPullDownRefresh();
      });
    },
    // 上拉加载
    onReachBottom(){
      let _self = this;
      if (this._freshing || _self.data.list.length >= _self.data.recordCount || _self.data.recordCount === 0) return
      this._freshing = true;
      this.setData({
        pageNo: ++_self.data.pageNo
      });
      this._requestPageList(() => {
        _self._freshing = false;
      });
    },
  }
})

页面调用

let resListCom = require(‘../../mixins/resList‘)

Page({
  behaviors: [resListCom],
  data: {
    
  },
  onLoad: function () {
    this._requestPageList()
  },
  // 覆盖Behavior里的方法
  _requestPageList(callback) {
    let opt = {
      id: wx.getStorageSync(‘virtualUserID‘)
    }
    this._requestPageListCom("virtualIntegral", opt, callback);
  },
})

 

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!