category.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. // 页面初始化 options为页面跳转所带来的参数
  22. var that = this;
  23. if (options.id) {
  24. that.setData({
  25. id: parseInt(options.id)
  26. });
  27. }
  28. wx.getSystemInfo({
  29. success: function (res) {
  30. that.setData({
  31. scrollHeight: res.windowHeight
  32. });
  33. }
  34. });
  35. this.getCategoryInfo();
  36. },
  37. getCategoryInfo: function () {
  38. let that = this;
  39. util.request(api.GoodsCategory, { id: this.data.id })
  40. .then(function (res) {
  41. if (res.errno == 0) {
  42. that.setData({
  43. navList: res.data.brotherCategory,
  44. currentCategory: res.data.currentCategory
  45. });
  46. //nav位置
  47. let currentIndex = 0;
  48. let navListCount = that.data.navList.length;
  49. for (let i = 0; i < navListCount; i++) {
  50. currentIndex += 1;
  51. if (that.data.navList[i].id == that.data.id) {
  52. break;
  53. }
  54. }
  55. if (currentIndex > navListCount / 2 && navListCount > 5) {
  56. that.setData({
  57. scrollLeft: currentIndex * 60
  58. });
  59. }
  60. that.getGoodsList();
  61. } else {
  62. //显示错误信息
  63. }
  64. });
  65. },
  66. onReady: function () {
  67. // 页面渲染完成
  68. },
  69. onShow: function () {
  70. // 页面显示
  71. },
  72. onHide: function () {
  73. // 页面隐藏
  74. },
  75. /**
  76. * 页面上拉触底事件的处理函数
  77. */
  78. onReachBottom: function () {
  79. this.getGoodsList()
  80. },
  81. getGoodsList: function () {
  82. var that = this;
  83. if (that.data.totalPages <= that.data.page-1) {
  84. that.setData({
  85. nomore: true
  86. })
  87. return;
  88. }
  89. util.request(api.GoodsList, {categoryId: that.data.id, page: that.data.page, size: that.data.size})
  90. .then(function (res) {
  91. that.setData({
  92. goodsList: that.data.goodsList.concat(res.data.goodsList),
  93. page: res.data.currentPage+1,
  94. totalPages: res.data.totalPages
  95. });
  96. });
  97. },
  98. onUnload: function () {
  99. // 页面关闭
  100. },
  101. switchCate: function (event) {
  102. if (this.data.id == event.currentTarget.dataset.id) {
  103. return false;
  104. }
  105. var that = this;
  106. var clientX = event.detail.x;
  107. var currentTarget = event.currentTarget;
  108. if (clientX < 60) {
  109. that.setData({
  110. scrollLeft: currentTarget.offsetLeft - 60
  111. });
  112. } else if (clientX > 330) {
  113. that.setData({
  114. scrollLeft: currentTarget.offsetLeft
  115. });
  116. }
  117. this.setData({
  118. id: event.currentTarget.dataset.id,
  119. page:1,
  120. totalPages: 1,
  121. goodsList: [],
  122. nomore: false
  123. });
  124. this.getCategoryInfo();
  125. }
  126. })