category.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. city:this.data.addressInfo.city
  56. })
  57. .then( (res)=>{
  58. if (res.errno == 0) {
  59. that.setData({
  60. navList: res.data.brotherCategory,
  61. currentCategory: res.data.currentCategory
  62. });
  63. // 当id是L1分类id时,这里需要重新设置成L1分类的一个子分类的id
  64. // 周期服务包特殊处理
  65. if(this.data.attribute=='3'||this.data.attribute=='2'){
  66. wx.setNavigationBarTitle({
  67. title: res.data.currentCategory.name
  68. })
  69. that.setData({
  70. id: res.data.currentCategory.id
  71. });
  72. }else{
  73. wx.setNavigationBarTitle({
  74. title: res.data.parentCategory.name
  75. })
  76. if (res.data.parentCategory.id == that.data.id) {
  77. that.setData({
  78. id: res.data.currentCategory.id
  79. });
  80. }
  81. }
  82. //nav位置
  83. let currentIndex = 0;
  84. let navListCount = that.data.navList.length;
  85. for (let i = 0; i < navListCount; i++) {
  86. currentIndex += 1;
  87. if (that.data.navList[i].id == that.data.id) {
  88. break;
  89. }
  90. }
  91. if (currentIndex > navListCount / 2 && navListCount > 5) {
  92. that.setData({
  93. scrollLeft: currentIndex * 60
  94. });
  95. }
  96. that.getGoodsList();
  97. } else {
  98. //显示错误信息
  99. }
  100. });
  101. },
  102. onReady: function () {
  103. // 页面渲染完成
  104. },
  105. onShow: function () {
  106. // 页面显示
  107. },
  108. onHide: function () {
  109. // 页面隐藏
  110. },
  111. getGoodsList: function () {
  112. var that = this;
  113. util.request(api.GoodsList, {
  114. categoryId: this.data.attribute=='3'?'-1':that.data.id,
  115. attribute:this.data.attribute=='3'?'3':'',
  116. page: that.data.page,
  117. limit: that.data.limit,
  118. city:that.data.addressInfo.city,
  119. //shopId:that.data.addressInfo.shopId
  120. })
  121. .then(function (res) {
  122. console.info(res);
  123. console.info(that.data.page);
  124. if (that.data.page == 1) {
  125. that.setData({
  126. goodsList: res.data.list,
  127. totalPages: res.data.pages
  128. });
  129. } else
  130. that.setData({
  131. goodsList: that.data.goodsList.concat(res.data.list),
  132. totalPages: res.data.pages
  133. });
  134. console.info(that.data.goodsList);
  135. });
  136. },
  137. onUnload: function () {
  138. // 页面关闭
  139. },
  140. onReachBottom() {
  141. if (this.data.totalPages > this.data.page) {
  142. this.setData({
  143. page: this.data.page + 1
  144. });
  145. this.getGoodsList();
  146. } else {
  147. wx.showToast({
  148. title: '没有更多商品了',
  149. icon: 'none',
  150. duration: 2000
  151. });
  152. return false;
  153. }
  154. },
  155. switchCate: function (event) {
  156. if (this.data.id == event.currentTarget.dataset.id) {
  157. return false;
  158. }
  159. var that = this;
  160. var clientX = event.detail.x;
  161. var currentTarget = event.currentTarget;
  162. if (clientX < 60) {
  163. that.setData({
  164. scrollLeft: currentTarget.offsetLeft - 60
  165. });
  166. } else if (clientX > 330) {
  167. that.setData({
  168. scrollLeft: currentTarget.offsetLeft
  169. });
  170. }
  171. this.setData({
  172. id: event.currentTarget.dataset.id,
  173. navIndex: event.currentTarget.dataset.index,
  174. page:1
  175. });
  176. this.getGoodsList();
  177. // this.getCategoryInfo();
  178. }
  179. })