category.js 3.0 KB

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