category.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. 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, {
  36. id: this.data.id
  37. })
  38. .then(function(res) {
  39. if (res.errno == 0) {
  40. that.setData({
  41. navList: res.data.brotherCategory,
  42. currentCategory: res.data.currentCategory
  43. });
  44. wx.setNavigationBarTitle({
  45. title: res.data.parentCategory.name
  46. })
  47. // 当id是L1分类id时,这里需要重新设置成L1分类的一个子分类的id
  48. if (res.data.parentCategory.id == that.data.id) {
  49. that.setData({
  50. id: res.data.currentCategory.id
  51. });
  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. onReachBottom: function () {
  84. var that=this;
  85. var pagenum = that.data.page + 1; //获取当前页数并+1
  86. if(pagenum <=that.data.pages){
  87. that.setData({
  88. page: pagenum, //更新当前页数
  89. })
  90. that.getGoodsList();//重新调用请求获取下一页数据
  91. }else{
  92. util.showErrorToast("已经是最后一页了");
  93. }
  94. },
  95. getGoodsList: function() {
  96. var that = this;
  97. util.request(api.GoodsList, {
  98. categoryId: that.data.id,
  99. page: that.data.page,
  100. limit: that.data.limit
  101. })
  102. .then(function(res) {
  103. var arr1 = that.data.goodsList; //从data获取当前datalist数组
  104. var arr2 = res.data.list; //从此次请求返回的数据中获取新数组
  105. arr1 = arr1.concat(arr2); //合并数组
  106. that.setData({
  107. goodsList: arr1,
  108. pages: res.data.pages //得到总页数
  109. });
  110. });
  111. },
  112. onUnload: function() {
  113. // 页面关闭
  114. },
  115. switchCate: function(event) {
  116. if (this.data.id == event.currentTarget.dataset.id) {
  117. return false;
  118. }
  119. var that = this;
  120. var clientX = event.detail.x;
  121. var currentTarget = event.currentTarget;
  122. if (clientX < 60) {
  123. that.setData({
  124. scrollLeft: currentTarget.offsetLeft - 60
  125. });
  126. } else if (clientX > 330) {
  127. that.setData({
  128. scrollLeft: currentTarget.offsetLeft
  129. });
  130. }
  131. this.setData({
  132. id: event.currentTarget.dataset.id,
  133. page:1, //从第一页开始查
  134. goodsList:[]
  135. });
  136. this.getCategoryInfo();
  137. }
  138. })