catalog.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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. util.request(api.CatalogList).then(function (res) {
  62. if (that.data.currentCategory && that.data.currentCategory.id > 0) {
  63. that.setData({
  64. navList: res.data.categoryList,
  65. });
  66. } else {
  67. that.setData({
  68. navList: res.data.categoryList,
  69. currentCategory: res.data.currentCategory
  70. });
  71. }
  72. that.getCategoryData();
  73. });
  74. util.request(api.GoodsCount).then(function (res) {
  75. that.setData({
  76. goodsCount: res.data.goodsCount
  77. });
  78. });
  79. },
  80. getCurrentCategory: function (id) {
  81. let that = this;
  82. util.request(api.CatalogCurrent, { id: id })
  83. .then(function (res) {
  84. that.setData({
  85. currentCategory: res.data.currentCategory
  86. });
  87. that.getGoodsList();
  88. });
  89. },
  90. onReady: function () {
  91. // 页面渲染完成
  92. },
  93. onShow: function () {
  94. this.setData({
  95. goodsBizType: app.globalData.appGoodsBizType
  96. });
  97. // 页面显示
  98. if (wx.getStorageSync('userInfo') || wx.getStorageSync('token')) {
  99. this.getFootCart();
  100. this.getCatalog();
  101. } else {
  102. wx.navigateTo({
  103. url: '/pages/auth/btnAuth/btnAuth',
  104. })
  105. }
  106. },
  107. onHide: function () {
  108. // 页面隐藏
  109. this.setData({
  110. showNavList: false
  111. })
  112. },
  113. onUnload: function () {
  114. // 页面关闭
  115. },
  116. getList: function () {
  117. var that = this;
  118. util.request(api.ApiRootUrl + 'api/catalog/' + that.data.currentCategory.id)
  119. .then(function (res) {
  120. that.setData({
  121. categoryList: res.data,
  122. });
  123. });
  124. },
  125. switchCate: function (event) {
  126. var that = this;
  127. var currentTarget = event.currentTarget;
  128. if (this.data.currentCategory.id == event.currentTarget.dataset.id) {
  129. return false;
  130. }
  131. this.getCurrentCategory(event.currentTarget.dataset.id);
  132. },
  133. //
  134. getCategoryData: function () {
  135. let that = this;
  136. that.getGoodsList();
  137. },
  138. getGoodsList() {
  139. wx.showLoading({
  140. title: '加载中...',
  141. });
  142. var that = this;
  143. if (that.data.currentCategory){
  144. util.request(api.CatalogProductList, {
  145. page: that.data.page,
  146. size: that.data.size,
  147. order: that.data.currentSortOrder,
  148. sort: that.data.currentSortType,
  149. // discount: that.data.filterDiscount,
  150. goodsBizType: that.data.goodsBizType,
  151. categoryId: that.data.currentCategory.id
  152. })
  153. .then(function (res) {
  154. if (res.errno === 0) {
  155. that.setData({
  156. goodsList: res.data.data,
  157. filterCategory: res.data.filterCategory
  158. });
  159. wx.hideLoading();
  160. }
  161. });
  162. }
  163. },
  164. getFootCart: function () {
  165. let that = this;
  166. util.request(api.GetFootCart).then(function (res) {
  167. if (res.errno === 0) {
  168. that.setData({
  169. footCart: res.data,
  170. });
  171. }
  172. });
  173. },
  174. //购物车减少
  175. cutNumber: function (e) {
  176. let that = this;
  177. var goodsId = e.currentTarget.dataset.goodsId;
  178. var productId = e.currentTarget.dataset.productId;
  179. var goodsList = that.data.goodsList;
  180. // goodsList.forEach(function (val, index, arr) {
  181. // if (val.product_id == productId) {
  182. // val.cart_num = val.cart_num - 1;
  183. // if (val.cart_num >= 0) {
  184. // goodsList[index] = val;
  185. // that.setData({goodsList: goodsList});
  186. // }
  187. // }
  188. // });
  189. // that.setData({goodsList: goodsList});
  190. util.request(api.CartMinus, { goodsId: goodsId, productId: productId, number: 1 }, 'POST').then(function (res) {
  191. if (res.errno === 0 && null != res.data) {
  192. var goodsList = that.data.goodsList;
  193. that.getFootCart();
  194. goodsList.forEach(function (val, index, arr) {
  195. if (val.product_id == productId) {
  196. val.cart_num = res.data;
  197. goodsList[index] = val;
  198. if(val.cart_num == 0){
  199. util.showErrorToast('不能再减了');
  200. }
  201. }
  202. }, that);
  203. that.setData({ goodsList: goodsList });
  204. }
  205. });
  206. },
  207. //购物车增加
  208. addNumber: function (e) {
  209. let that = this;
  210. var goodsId = e.currentTarget.dataset.goodsId;
  211. var productId = e.currentTarget.dataset.productId;
  212. var goodsList = that.data.goodsList;
  213. // goodsList.forEach(function (val, index, arr) {
  214. // if (val.product_id == productId) {
  215. // val.cart_num = val.cart_num + 1;
  216. // goodsList[index] = val;
  217. // }
  218. // });
  219. // that.setData({goodsList: goodsList});
  220. util.request(api.CartAdd, { goodsId: goodsId, productId: productId, number: 1 }, 'POST').then(function (res) {
  221. if (res.errno === 0 && null != res.data) {
  222. that.getFootCart();
  223. goodsList.forEach(function (val, index, arr) {
  224. res.data.cartList.forEach(function (cartVal, cartIndex, cartArr) {
  225. if (val.product_id == cartVal.product_id) {
  226. val.cart_num = cartVal.number;
  227. goodsList[index] = val;
  228. }
  229. });
  230. }, that);
  231. that.setData({ goodsList: goodsList });
  232. }
  233. });
  234. },
  235. openSortFilter: function (event) {
  236. let that = this;
  237. let currentId = event.currentTarget.id;
  238. console.log('currentId:'+currentId);
  239. switch (currentId) {
  240. case 'defaultActivity':
  241. that.setData({
  242. 'goodsBizType': '00'
  243. });
  244. app.globalData.appGoodsBizType = '00';
  245. this.getGoodsList();
  246. break;
  247. case 'discountActivity':
  248. that.setData({
  249. 'goodsBizType': '02'
  250. });
  251. app.globalData.appGoodsBizType = '02';
  252. this.getGoodsList();
  253. break;
  254. case 'groupActivity':
  255. that.setData({
  256. 'goodsBizType': '10'
  257. });
  258. app.globalData.appGoodsBizType = '10';
  259. this.getGoodsList();
  260. break;
  261. case 'ordActivity':
  262. that.setData({
  263. 'goodsBizType': '11'
  264. });
  265. app.globalData.appGoodsBizType = '11';
  266. this.getGoodsList();
  267. break;
  268. case 'sellSort':
  269. let tmpSortOrder = 'asc';
  270. if (this.data.currentSortOrder == 'asc') {
  271. tmpSortOrder = 'desc';
  272. }
  273. this.setData({
  274. 'currentSortType': 'sell',
  275. 'currentSortOrder': tmpSortOrder,
  276. });
  277. this.getGoodsList();
  278. break;
  279. case 'priceSort':
  280. tmpSortOrder = 'asc';
  281. if (this.data.currentSortOrder == 'asc') {
  282. tmpSortOrder = 'desc';
  283. }
  284. this.setData({
  285. 'currentSortType': 'price',
  286. 'currentSortOrder': tmpSortOrder,
  287. });
  288. this.getGoodsList();
  289. break;
  290. default:
  291. //综合排序
  292. this.setData({
  293. 'currentSortType': 'default',
  294. 'currentSortOrder': 'desc',
  295. });
  296. this.getGoodsList();
  297. }
  298. },
  299. takeShareCoupon() {
  300. let that = this;
  301. util.request(api.CouponTransActivit, {
  302. referrer: that.data.sourceKey,
  303. sourceKey: that.data.sourceKey
  304. }).then(function (res) {
  305. if (res.errno === 0) {
  306. util.showErrorToast("领取成功");
  307. that.setData({
  308. couponList: res.data,
  309. openCoupon: false
  310. });
  311. } else if (res.errno === 2) {
  312. util.showErrorToast(res.errmsg)
  313. that.setData({
  314. couponList: res.data,
  315. openCoupon: false
  316. });
  317. }
  318. });
  319. },
  320. closeCoupon: function () {
  321. var that = this;
  322. that.setData({
  323. openCoupon: false
  324. });
  325. },
  326. bindtapGoodsDetail: function (e) {
  327. var that = this;
  328. let url = '';
  329. var itemId = e.currentTarget.dataset.itemId;
  330. if (that.data.filterDiscount != 2) {
  331. url = '/pages/goods/goods?id=' + itemId;
  332. } else {
  333. url = '/pages/groupDetail/groupDetail?id=' + itemId;
  334. }
  335. wx.navigateTo({
  336. url: url,
  337. })
  338. },
  339. sercherCategory: function (e) {
  340. var that = this;
  341. let url = '';
  342. var replyType = e.currentTarget.dataset.replyType;
  343. var currentIndex = e.currentTarget.dataset.currentIndex;
  344. // console.log('replyType:' + e.currentTarget.dataset.replyType);
  345. // console.log('goodsBizType:' + that.data.goodsBizType);
  346. // 跳转页面
  347. wx.navigateTo({
  348. url: '/pages/category/category?id=' + replyType + '&&goodsBizType=' + that.data.goodsBizType + '&&currentIndex=' + currentIndex
  349. })
  350. }
  351. })