category.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. var util = require('../../utils/util.js');
  2. var api = require('../../config/api.js');
  3. Page({
  4. data: {
  5. navList: [],
  6. goodsList: [],
  7. id: 0,
  8. currentCategory: {},
  9. scrollLeft: 0,
  10. scrollTop: 0,
  11. scrollHeight: 0,
  12. page: 1,
  13. limit: 10,
  14. pages:1, //总页数
  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. const windowInfo = wx.getWindowInfo();
  25. that.setData({
  26. scrollHeight: windowInfo.windowHeight
  27. })
  28. this.getCategoryInfo()
  29. },
  30. getCategoryInfo: function() {
  31. let that = this;
  32. util.request(api.GoodsCategory, {
  33. id: this.data.id
  34. })
  35. .then(function(res) {
  36. if (res.errno == 0) {
  37. that.setData({
  38. navList: res.data.brotherCategory,
  39. currentCategory: res.data.currentCategory
  40. });
  41. wx.setNavigationBarTitle({
  42. title: res.data.parentCategory.name
  43. })
  44. // 当id是L1分类id时,这里需要重新设置成L1分类的一个子分类的id
  45. if (res.data.parentCategory.id == that.data.id) {
  46. that.setData({
  47. id: res.data.currentCategory.id
  48. });
  49. }
  50. //nav位置
  51. let currentIndex = 0;
  52. let navListCount = that.data.navList.length;
  53. for (let i = 0; i < navListCount; i++) {
  54. currentIndex += 1;
  55. if (that.data.navList[i].id == that.data.id) {
  56. break;
  57. }
  58. }
  59. if (currentIndex > navListCount / 2 && navListCount > 5) {
  60. that.setData({
  61. scrollLeft: currentIndex * 60
  62. });
  63. }
  64. that.getGoodsList();
  65. } else {
  66. //显示错误信息
  67. }
  68. });
  69. },
  70. onReady: function() {
  71. // 页面渲染完成
  72. },
  73. onShow: function() {
  74. // 页面显示
  75. },
  76. onHide: function() {
  77. // 页面隐藏
  78. },
  79. //触底开始下一页
  80. onReachBottom: function () {
  81. var that=this;
  82. var pagenum = that.data.page + 1; //获取当前页数并+1
  83. if(pagenum <=that.data.pages){
  84. that.setData({
  85. page: pagenum, //更新当前页数
  86. })
  87. that.getGoodsList();//重新调用请求获取下一页数据
  88. }else{
  89. util.showErrorToast("已经是最后一页了");
  90. }
  91. },
  92. getGoodsList: function() {
  93. var that = this;
  94. util.request(api.GoodsList, {
  95. categoryId: that.data.id,
  96. page: that.data.page,
  97. limit: that.data.limit
  98. })
  99. .then(function(res) {
  100. var arr1 = that.data.goodsList; //从data获取当前datalist数组
  101. var arr2 = res.data.list; //从此次请求返回的数据中获取新数组
  102. arr1 = arr1.concat(arr2); //合并数组
  103. that.setData({
  104. goodsList: arr1,
  105. pages: res.data.pages //得到总页数
  106. });
  107. });
  108. },
  109. onUnload: function() {
  110. // 页面关闭
  111. },
  112. switchCate: function(event) {
  113. if (this.data.id == event.currentTarget.dataset.id) {
  114. return false;
  115. }
  116. var that = this;
  117. var clientX = event.detail.x;
  118. var currentTarget = event.currentTarget;
  119. if (clientX < 60) {
  120. that.setData({
  121. scrollLeft: currentTarget.offsetLeft - 60
  122. });
  123. } else if (clientX > 330) {
  124. that.setData({
  125. scrollLeft: currentTarget.offsetLeft
  126. });
  127. }
  128. this.setData({
  129. id: event.currentTarget.dataset.id,
  130. page:1, //从第一页开始查
  131. goodsList:[]
  132. });
  133. this.getCategoryInfo();
  134. }
  135. })