category.js 2.9 KB

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