category.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. var util = require('../../utils/util.js');
  2. var api = require('../../config/api.js');
  3. Page({
  4. data: {
  5. // text:"这是一个页面"
  6. navList: [],
  7. goodsList: [],
  8. id: 0,
  9. currentCategory: {},
  10. scrollLeft: 0,
  11. scrollTop: 0,
  12. scrollHeight: 0,
  13. page: 1,
  14. size: 10,
  15. loadmoreText: '正在加载更多数据',
  16. nomoreText: '全部加载完成',
  17. nomore: false,
  18. totalPages: 1
  19. },
  20. onLoad: function (options) {
  21. console.log(util)
  22. // 页面初始化 options为页面跳转所带来的参数
  23. getApp().checkNetStatu();
  24. var that = this;
  25. if (options.id) {
  26. that.setData({
  27. id: parseInt(options.id)
  28. });
  29. }
  30. wx.setNavigationBarTitle({
  31. title: options.title
  32. })
  33. wx.getSystemInfo({
  34. success: function (res) {
  35. that.setData({
  36. scrollHeight: res.windowHeight
  37. });
  38. }
  39. });
  40. this.getCategoryInfo();
  41. },
  42. getCategoryInfo: function () {
  43. let that = this;
  44. that.setData({loadding: true})
  45. console.log(that.data.loadding)
  46. util.request(api.GoodsCategory, { id: this.data.id })
  47. .then(function (res) {
  48. if (res.errno == 0) {
  49. that.setData({
  50. navList: res.data.brotherCategory,
  51. currentCategory: res.data.currentCategory
  52. });
  53. //nav位置
  54. let currentIndex = 0;
  55. let navListCount = that.data.navList.length;
  56. for (let i = 0; i < navListCount; i++) {
  57. currentIndex += 1;
  58. if (that.data.navList[i].id == that.data.id) {
  59. break;
  60. }
  61. }
  62. if (currentIndex > navListCount / 2 && navListCount > 5) {
  63. that.setData({
  64. scrollLeft: currentIndex * 60
  65. });
  66. }
  67. that.getGoodsList();
  68. } else {
  69. //显示错误信息
  70. }
  71. });
  72. },
  73. onReady: function () {
  74. // 页面渲染完成
  75. },
  76. onShow: function () {
  77. // 页面显示
  78. },
  79. onHide: function () {
  80. // 页面隐藏
  81. },
  82. /**
  83. * 页面上拉触底事件的处理函数
  84. */
  85. onReachBottom: function () {
  86. this.getGoodsList()
  87. },
  88. getGoodsList: function () {
  89. var that = this;
  90. if (that.data.totalPages <= that.data.page-1) {
  91. that.setData({
  92. nomore: true
  93. })
  94. return;
  95. }
  96. util.request(api.GoodsList, {categoryId: that.data.id, page: that.data.page, size: that.data.size})
  97. .then(function (res) {
  98. that.setData({
  99. goodsList: that.data.goodsList.concat(res.data.goodsList),
  100. page: res.data.currentPage+1,
  101. totalPages: res.data.totalPages,
  102. loadding: false
  103. });
  104. if (that.data.totalPages <= that.data.page-1) {
  105. that.setData({
  106. nomore: true
  107. })
  108. }
  109. });
  110. },
  111. onUnload: function () {
  112. // 页面关闭
  113. },
  114. switchCate: function (event) {
  115. if (this.data.id == event.currentTarget.dataset.id) {
  116. return false;
  117. }
  118. var that = this;
  119. var clientX = event.detail.x;
  120. var currentTarget = event.currentTarget;
  121. if (clientX < 60) {
  122. that.setData({
  123. scrollLeft: currentTarget.offsetLeft - 60
  124. });
  125. } else if (clientX > 330) {
  126. that.setData({
  127. scrollLeft: currentTarget.offsetLeft
  128. });
  129. }
  130. this.setData({
  131. id: event.currentTarget.dataset.id,
  132. page:1,
  133. totalPages: 1,
  134. goodsList: [],
  135. nomore: false
  136. });
  137. this.getCategoryInfo();
  138. }
  139. })