catalog.js 9.5 KB

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