category.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. var util = require('../../utils/util.js');
  2. var api = require('../../config/api.js');
  3. const app = getApp();
  4. Page({
  5. data: {
  6. navList: [],
  7. goodsList: [],
  8. id: 0,
  9. navIndex:0,
  10. attribute:'',
  11. currentCategory: {},
  12. scrollLeft: 0,
  13. scrollTop: 0,
  14. scrollHeight: 0,
  15. page: 1,
  16. limit: 10,
  17. totalPages: 1,
  18. addressInfo:{}
  19. },
  20. onLoad: function (options) {
  21. // 页面初始化 options为页面跳转所带来的参数
  22. console.log(options);
  23. var that = this;
  24. if (options.id) {
  25. that.setData({
  26. id: parseInt(options.id)
  27. });
  28. }
  29. if(options.attribute){
  30. that.setData({
  31. attribute: parseInt(options.attribute)
  32. });
  33. }
  34. wx.getSystemInfo({
  35. success: function (res) {
  36. that.setData({
  37. scrollHeight: res.windowHeight
  38. });
  39. }
  40. });
  41. var addressInfo=wx.getStorageSync('addressInfo');
  42. if(addressInfo){
  43. this.setData({
  44. addressInfo:addressInfo
  45. })
  46. }
  47. this.getCategoryInfo();
  48. },
  49. getCategoryInfo: function () {
  50. let that = this;
  51. util.request(api.GoodsCategory, {
  52. id: this.data.attribute?'-1':this.data.id,
  53. attribute:this.data.attribute,
  54. shopId:this.data.addressInfo.shopId
  55. })
  56. .then( (res)=>{
  57. if (res.errno == 0) {
  58. that.setData({
  59. navList: res.data.brotherCategory,
  60. currentCategory: res.data.currentCategory
  61. });
  62. // 当id是L1分类id时,这里需要重新设置成L1分类的一个子分类的id
  63. // 周期服务包特殊处理
  64. if(this.data.attribute=='3'||this.data.attribute=='2'){
  65. wx.setNavigationBarTitle({
  66. title: res.data.currentCategory.name
  67. })
  68. that.setData({
  69. id: res.data.currentCategory.id
  70. });
  71. }else{
  72. wx.setNavigationBarTitle({
  73. title: res.data.parentCategory.name
  74. })
  75. if (res.data.parentCategory.id == that.data.id) {
  76. that.setData({
  77. id: res.data.currentCategory.id
  78. });
  79. }
  80. }
  81. //nav位置
  82. let currentIndex = 0;
  83. let navListCount = that.data.navList.length;
  84. for (let i = 0; i < navListCount; i++) {
  85. currentIndex += 1;
  86. if (that.data.navList[i].id == that.data.id) {
  87. break;
  88. }
  89. }
  90. if (currentIndex > navListCount / 2 && navListCount > 5) {
  91. that.setData({
  92. scrollLeft: currentIndex * 60
  93. });
  94. }
  95. that.getGoodsList();
  96. } else {
  97. //显示错误信息
  98. }
  99. });
  100. },
  101. onReady: function () {
  102. // 页面渲染完成
  103. },
  104. onShow: function () {
  105. // 页面显示
  106. },
  107. onHide: function () {
  108. // 页面隐藏
  109. },
  110. getGoodsList: function () {
  111. var that = this;
  112. util.request(api.GoodsList, {
  113. categoryId: this.data.attribute=='3'?'-1':that.data.id,
  114. attribute:this.data.attribute=='3'?'3':'',
  115. page: that.data.page,
  116. limit: that.data.limit,
  117. city:that.data.addressInfo.city,
  118. shopId:that.data.addressInfo.shopId
  119. })
  120. .then(function (res) {
  121. console.info(res);
  122. console.info(that.data.page);
  123. if (that.data.page == 1) {
  124. that.setData({
  125. goodsList: res.data.list,
  126. totalPages: res.data.pages
  127. });
  128. } else
  129. that.setData({
  130. goodsList: that.data.goodsList.concat(res.data.list),
  131. totalPages: res.data.pages
  132. });
  133. console.info(that.data.goodsList);
  134. });
  135. },
  136. onUnload: function () {
  137. // 页面关闭
  138. },
  139. onReachBottom() {
  140. if (this.data.totalPages > this.data.page) {
  141. this.setData({
  142. page: this.data.page + 1
  143. });
  144. this.getGoodsList();
  145. } else {
  146. wx.showToast({
  147. title: '没有更多商品了',
  148. icon: 'none',
  149. duration: 2000
  150. });
  151. return false;
  152. }
  153. },
  154. switchCate: function (event) {
  155. if (this.data.id == event.currentTarget.dataset.id) {
  156. return false;
  157. }
  158. var that = this;
  159. var clientX = event.detail.x;
  160. var currentTarget = event.currentTarget;
  161. if (clientX < 60) {
  162. that.setData({
  163. scrollLeft: currentTarget.offsetLeft - 60
  164. });
  165. } else if (clientX > 330) {
  166. that.setData({
  167. scrollLeft: currentTarget.offsetLeft
  168. });
  169. }
  170. this.setData({
  171. id: event.currentTarget.dataset.id,
  172. navIndex: event.currentTarget.dataset.index,
  173. page:1
  174. });
  175. this.getGoodsList();
  176. // this.getCategoryInfo();
  177. }
  178. })