fav.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. const app = getApp()
  2. var util = require('../../utils/util.js');
  3. Page({
  4. data: {
  5. loadMore: true,
  6. classification: {
  7. tabs: [],
  8. activeIndex: 0
  9. }
  10. },
  11. pageNum: 1,
  12. onLoad: function () {
  13. this._doRefreshMasonry();
  14. },
  15. onShow: function () {
  16. util.check_login_new().then((res) => {
  17. this.setData({
  18. needAuth: !res
  19. });
  20. })
  21. },
  22. noLogin: function () {
  23. this.setData({
  24. needAuth: true,
  25. showAuthModal: true
  26. })
  27. },
  28. /**
  29. * 授权成功回调
  30. */
  31. authSuccess: function () {
  32. const that = this;
  33. this.pageNum = 1;
  34. this.setData({
  35. loadMore: true,
  36. showEmpty: 0,
  37. needAuth: false,
  38. showAuthModal: false
  39. }, () => {
  40. that._doRefreshMasonry()
  41. })
  42. },
  43. authModal: function () {
  44. if (this.data.needAuth) {
  45. this.setData({ showAuthModal: !this.data.showAuthModal });
  46. return false;
  47. }
  48. return true;
  49. },
  50. onPullDownRefresh: function () {
  51. const that = this;
  52. this.pageNum = 1;
  53. this.setData({
  54. loadMore: true,
  55. showEmpty: 0
  56. }, () => {
  57. that.getInfo();
  58. that._doRefreshMasonry()
  59. })
  60. },
  61. onReachBottom: function () {
  62. this.data.loadMore && this._doAppendMasonry()
  63. },
  64. _doRefreshMasonry() {
  65. let that = this;
  66. this.masonryListComponent = this.selectComponent('#masonry');
  67. this.getData().then(res => {
  68. that.masonryListComponent.start(res).then(() => { })
  69. }).catch(() => {
  70. that.masonryListComponent.start([]).then(() => { })
  71. })
  72. },
  73. _doAppendMasonry() {
  74. let that = this;
  75. this.masonryListComponent = this.selectComponent('#masonry')
  76. // 获取接口数据后使用瀑布流组件append方法,当append完成后调用then,是否可触底价在的标志位可以在这里处理
  77. this.getData().then(res => {
  78. that.masonryListComponent.append(res).then(() => {
  79. console.log('refresh completed')
  80. })
  81. }).catch(() => {
  82. console.log('没有更多了')
  83. })
  84. },
  85. /**
  86. * 获取列表
  87. */
  88. getData: function () {
  89. return new Promise((resolve, reject) => {
  90. let that = this;
  91. let token = wx.getStorageSync('token');
  92. let gid = that.data.classificationId;
  93. wx.showLoading();
  94. app.util.request({
  95. 'url': 'entry/wxapp/index',
  96. 'data': {
  97. controller: 'recipe.get_fav_recipelist',
  98. token: token,
  99. pageNum: this.pageNum
  100. },
  101. dataType: 'json',
  102. success: function (res) {
  103. wx.stopPullDownRefresh();
  104. if (res.data.code == 0) {
  105. let list = res.data.data;
  106. that.pageNum++;
  107. resolve(list);
  108. } else {
  109. // 无数据
  110. let h = {
  111. loadMore: false
  112. }
  113. if (that.pageNum == 1) h.showEmpty = 1;
  114. that.setData(h);
  115. reject('');
  116. }
  117. wx.hideLoading();
  118. }
  119. })
  120. })
  121. }
  122. })