list.js 3.0 KB

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