1
0

catalog.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. var util = require('../../utils/util.js');
  2. var api = require('../../config/api.js');
  3. Page({
  4. data: {
  5. navList: [],
  6. categoryList: [],
  7. currentCategory: {},
  8. scrollLeft: 0,
  9. scrollTop: 0,
  10. goodsCount: 0,
  11. scrollHeight: 0,
  12. //
  13. filterCategory: [],
  14. goodsList: [],
  15. categoryId: 0,
  16. currentSortType: 'default',
  17. currentSortOrder: 'desc',
  18. filterDiscount: 0,// 0不限 1特价 2活动
  19. page: 1,
  20. size: 50,
  21. showNavList: false,
  22. footCart: {},
  23. referrer: 0,
  24. sourceKey: '',
  25. openCoupon: false
  26. },
  27. toggleNav() {
  28. this.setData({
  29. showNavList: !this.data.showNavList
  30. })
  31. },
  32. switchNav(event) {
  33. let name = event.currentTarget.dataset.name;
  34. wx.switchTab({
  35. url: `/pages/${name}/${name}`,
  36. });
  37. },
  38. onLoad: function (options) {
  39. let that = this;
  40. // 页面初始化 options为页面跳转所带来的参数
  41. if (options.referrer) {
  42. that.setData({
  43. referrer: parseInt(options.referrer),
  44. sourceKey: options.sourceKey,
  45. openCoupon: true
  46. });
  47. }
  48. wx.setNavigationBarTitle({
  49. title: '分类'
  50. });
  51. },
  52. getCatalog: function () {
  53. //CatalogList
  54. let that = this;
  55. wx.showLoading({
  56. title: '加载中...',
  57. });
  58. util.request(api.CatalogList).then(function (res) {
  59. if (that.data.currentCategory && that.data.currentCategory.id > 0) {
  60. that.setData({
  61. navList: res.data.categoryList,
  62. });
  63. } else {
  64. that.setData({
  65. navList: res.data.categoryList,
  66. currentCategory: res.data.currentCategory
  67. });
  68. }
  69. wx.hideLoading();
  70. that.getCategoryData();
  71. });
  72. util.request(api.GoodsCount).then(function (res) {
  73. that.setData({
  74. goodsCount: res.data.goodsCount
  75. });
  76. });
  77. },
  78. getCurrentCategory: function (id) {
  79. let that = this;
  80. util.request(api.CatalogCurrent, { id: id })
  81. .then(function (res) {
  82. that.setData({
  83. currentCategory: res.data.currentCategory
  84. });
  85. that.getGoodsList();
  86. });
  87. },
  88. onReady: function () {
  89. // 页面渲染完成
  90. },
  91. onShow: function () {
  92. // 页面显示
  93. this.getFootCart();
  94. this.getCatalog();
  95. },
  96. onHide: function () {
  97. // 页面隐藏
  98. this.setData({
  99. showNavList: false
  100. })
  101. },
  102. onUnload: function () {
  103. // 页面关闭
  104. },
  105. getList: function () {
  106. var that = this;
  107. util.request(api.ApiRootUrl + 'api/catalog/' + that.data.currentCategory.id)
  108. .then(function (res) {
  109. that.setData({
  110. categoryList: res.data,
  111. });
  112. });
  113. },
  114. switchCate: function (event) {
  115. var that = this;
  116. var currentTarget = event.currentTarget;
  117. if (this.data.currentCategory.id == event.currentTarget.dataset.id) {
  118. return false;
  119. }
  120. this.getCurrentCategory(event.currentTarget.dataset.id);
  121. },
  122. //
  123. getCategoryData: function () {
  124. let that = this;
  125. that.getGoodsList();
  126. },
  127. getGoodsList() {
  128. var that = this;
  129. util.request(api.CatalogProductList, {
  130. page: that.data.page,
  131. size: that.data.size,
  132. order: that.data.currentSortOrder,
  133. sort: that.data.currentSortType,
  134. discount: that.data.filterDiscount,
  135. categoryId: that.data.currentCategory.id
  136. })
  137. .then(function (res) {
  138. if (res.errno === 0) {
  139. that.setData({
  140. goodsList: res.data.data,
  141. filterCategory: res.data.filterCategory
  142. });
  143. }
  144. });
  145. },
  146. getFootCart: function () {
  147. let that = this;
  148. util.request(api.GetFootCart).then(function (res) {
  149. if (res.errno === 0) {
  150. that.setData({
  151. footCart: res.data,
  152. });
  153. }
  154. });
  155. },
  156. //购物车减少
  157. cutNumber: function (e) {
  158. let that = this;
  159. var goodsId = e.currentTarget.dataset.goodsId;
  160. var productId = e.currentTarget.dataset.productId;
  161. var goodsList = that.data.goodsList;
  162. // goodsList.forEach(function (val, index, arr) {
  163. // if (val.product_id == productId) {
  164. // val.cart_num = val.cart_num - 1;
  165. // if (val.cart_num >= 0) {
  166. // goodsList[index] = val;
  167. // that.setData({goodsList: goodsList});
  168. // }
  169. // }
  170. // });
  171. // that.setData({goodsList: goodsList});
  172. util.request(api.CartMinus, { goodsId: goodsId, productId: productId, number: 1 }, 'POST').then(function (res) {
  173. if (res.errno === 0 && null != res.data) {
  174. var goodsList = that.data.goodsList;
  175. that.getFootCart();
  176. goodsList.forEach(function (val, index, arr) {
  177. if (val.product_id == productId) {
  178. val.cart_num = res.data;
  179. goodsList[index] = val;
  180. }
  181. }, that);
  182. that.setData({ goodsList: goodsList });
  183. }
  184. });
  185. },
  186. //购物车增加
  187. addNumber: function (e) {
  188. let that = this;
  189. var goodsId = e.currentTarget.dataset.goodsId;
  190. var productId = e.currentTarget.dataset.productId;
  191. var goodsList = that.data.goodsList;
  192. // goodsList.forEach(function (val, index, arr) {
  193. // if (val.product_id == productId) {
  194. // val.cart_num = val.cart_num + 1;
  195. // goodsList[index] = val;
  196. // }
  197. // });
  198. // that.setData({goodsList: goodsList});
  199. util.request(api.CartAdd, { goodsId: goodsId, productId: productId, number: 1 }, 'POST').then(function (res) {
  200. if (res.errno === 0 && null != res.data) {
  201. that.getFootCart();
  202. goodsList.forEach(function (val, index, arr) {
  203. res.data.cartList.forEach(function (cartVal, cartIndex, cartArr) {
  204. if (val.product_id == cartVal.product_id) {
  205. val.cart_num = cartVal.number;
  206. goodsList[index] = val;
  207. }
  208. });
  209. }, that);
  210. that.setData({ goodsList: goodsList });
  211. }
  212. });
  213. },
  214. openSortFilter: function (event) {
  215. let currentId = event.currentTarget.id;
  216. switch (currentId) {
  217. case 'defaultActivity':
  218. this.setData({
  219. 'filterDiscount': 0
  220. });
  221. this.getGoodsList();
  222. break;
  223. case 'discountActivity':
  224. this.setData({
  225. 'filterDiscount': 1
  226. });
  227. this.getGoodsList();
  228. break;
  229. case 'groupActivity':
  230. this.setData({
  231. 'filterDiscount': 2
  232. });
  233. this.getGoodsList();
  234. break;
  235. case 'sellSort':
  236. let tmpSortOrder = 'asc';
  237. if (this.data.currentSortOrder == 'asc') {
  238. tmpSortOrder = 'desc';
  239. }
  240. this.setData({
  241. 'currentSortType': 'sell',
  242. 'currentSortOrder': tmpSortOrder,
  243. });
  244. this.getGoodsList();
  245. break;
  246. case 'priceSort':
  247. tmpSortOrder = 'asc';
  248. if (this.data.currentSortOrder == 'asc') {
  249. tmpSortOrder = 'desc';
  250. }
  251. this.setData({
  252. 'currentSortType': 'price',
  253. 'currentSortOrder': tmpSortOrder,
  254. });
  255. this.getGoodsList();
  256. break;
  257. default:
  258. //综合排序
  259. this.setData({
  260. 'currentSortType': 'default',
  261. 'currentSortOrder': 'desc',
  262. });
  263. this.getGoodsList();
  264. }
  265. },
  266. takeShareCoupon() {
  267. let that = this;
  268. util.request(api.CouponTransActivit, {
  269. referrer: that.data.sourceKey,
  270. sourceKey: that.data.sourceKey
  271. }).then(function (res) {
  272. if (res.errno === 0) {
  273. util.showErrorToast("领取成功");
  274. that.setData({
  275. couponList: res.data,
  276. openCoupon: false
  277. });
  278. } else if (res.errno === 2) {
  279. util.showErrorToast(res.errmsg)
  280. that.setData({
  281. couponList: res.data,
  282. openCoupon: false
  283. });
  284. }
  285. });
  286. },
  287. closeCoupon: function () {
  288. var that = this;
  289. that.setData({
  290. openCoupon: false
  291. });
  292. },
  293. bindtapGoodsDetail: function (e) {
  294. var that = this;
  295. let url = '';
  296. var itemId = e.currentTarget.dataset.itemId;
  297. if (that.data.filterDiscount != 2) {
  298. url = '/pages/goods/goods?id=' + itemId;
  299. } else {
  300. url = '/pages/groupDetail/groupDetail?id=' + itemId;
  301. }
  302. wx.navigateTo({
  303. url: url,
  304. })
  305. },
  306. })