category.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. size: 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. getCategoryInfo: function() {
  33. let that = this;
  34. util.request(api.GoodsCategory, {
  35. id: this.data.id
  36. })
  37. .then(function(res) {
  38. if (res.errno == 0) {
  39. that.setData({
  40. navList: res.data.brotherCategory,
  41. currentCategory: res.data.currentCategory
  42. });
  43. wx.setNavigationBarTitle({
  44. title: res.data.parentCategory.name
  45. })
  46. // 当id是L1分类id时,这里需要重新设置成L1分类的一个子分类的id
  47. if (res.data.parentCategory.id == that.data.id) {
  48. that.setData({
  49. id: res.data.currentCategory.id
  50. });
  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. var that = this;
  84. util.request(api.GoodsList, {
  85. categoryId: that.data.id,
  86. page: that.data.page,
  87. size: that.data.size
  88. })
  89. .then(function(res) {
  90. that.setData({
  91. goodsList: res.data.goodsList,
  92. });
  93. });
  94. },
  95. onUnload: function() {
  96. // 页面关闭
  97. },
  98. switchCate: function(event) {
  99. if (this.data.id == event.currentTarget.dataset.id) {
  100. return false;
  101. }
  102. var that = this;
  103. var clientX = event.detail.x;
  104. var currentTarget = event.currentTarget;
  105. if (clientX < 60) {
  106. that.setData({
  107. scrollLeft: currentTarget.offsetLeft - 60
  108. });
  109. } else if (clientX > 330) {
  110. that.setData({
  111. scrollLeft: currentTarget.offsetLeft
  112. });
  113. }
  114. this.setData({
  115. id: event.currentTarget.dataset.id
  116. });
  117. this.getCategoryInfo();
  118. }
  119. })