category.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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: 10000
  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, { id: this.data.id })
  36. .then(function (res) {
  37. if (res.errno == 0) {
  38. that.setData({
  39. navList: res.data.brotherCategory,
  40. currentCategory: res.data.currentCategory
  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, {categoryId: that.data.id, page: that.data.page, size: that.data.size})
  75. .then(function (res) {
  76. that.setData({
  77. goodsList: res.data.goodsList,
  78. });
  79. });
  80. },
  81. onUnload: function () {
  82. // 页面关闭
  83. },
  84. switchCate: function (event) {
  85. if (this.data.id == event.currentTarget.dataset.id) {
  86. return false;
  87. }
  88. var that = this;
  89. var clientX = event.detail.x;
  90. var currentTarget = event.currentTarget;
  91. if (clientX < 60) {
  92. that.setData({
  93. scrollLeft: currentTarget.offsetLeft - 60
  94. });
  95. } else if (clientX > 330) {
  96. that.setData({
  97. scrollLeft: currentTarget.offsetLeft
  98. });
  99. }
  100. this.setData({
  101. id: event.currentTarget.dataset.id
  102. });
  103. this.getCategoryInfo();
  104. }
  105. })