index.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // pages/collectionGoods/index.js
  2. import { getCollectUserList, getProductHot, collectDel} from '../../api/store.js';
  3. const app=getApp();
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. parameter: {
  10. 'navbar': '1',
  11. 'return': '1',
  12. 'title': '收藏商品',
  13. 'color': false
  14. },
  15. host_product:[],
  16. loadTitle:'加载更多',
  17. loading:false,
  18. loadend:false,
  19. collectProductList:[],
  20. limit:8,
  21. page:1,
  22. },
  23. /**
  24. * 授权回调
  25. */
  26. onLoadFun:function(){
  27. this.get_user_collect_product();
  28. this.get_host_product();
  29. },
  30. /**
  31. * 生命周期函数--监听页面加载
  32. */
  33. onLoad: function (options) {
  34. },
  35. /**
  36. * 获取收藏产品
  37. */
  38. get_user_collect_product:function(){
  39. var that=this;
  40. if (this.data.loading) return;
  41. if (this.data.loadend) return;
  42. that.setData({ loading: true, loadTitle:''});
  43. getCollectUserList({ page: that.data.page,limit: that.data.limit}).then(res=>{
  44. var collectProductList = res.data;
  45. var loadend = collectProductList.length < that.data.limit;
  46. console.log(collectProductList.length);
  47. that.data.collectProductList = app.SplitArray(collectProductList, that.data.collectProductList);
  48. that.setData({
  49. collectProductList: that.data.collectProductList,
  50. loadend: loadend,
  51. loadTitle: loadend ? '我也是有底线的' : '加载更多',
  52. page: that.data.page + 1,
  53. loading: false
  54. });
  55. }).catch(err=>{
  56. that.setData({ loading: false, loadTitle: "加载更多" })
  57. });
  58. },
  59. /**
  60. * 取消收藏
  61. */
  62. delCollection:function(e){
  63. var id = e.target.dataset.id, that = this, index = e.target.dataset.index;
  64. collectDel(id).then(res=>{
  65. return app.Tips({ title: '取消收藏成功', icon: 'success' }, function () {
  66. that.data.collectProductList.splice(index, 1);
  67. that.setData({ collectProductList: that.data.collectProductList });
  68. });
  69. });
  70. },
  71. /**
  72. * 获取我的推荐
  73. */
  74. get_host_product: function () {
  75. var that = this;
  76. getProductHot().then(res=>{
  77. that.setData({ host_product: res.data });
  78. });
  79. },
  80. /**
  81. * 页面上拉触底事件的处理函数
  82. */
  83. onReachBottom: function () {
  84. this.get_user_collect_product();
  85. }
  86. })