index.js 2.8 KB

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