catalog.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. console.log(that.data);
  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. getFootCart: function () {
  158. let that = this;
  159. util.request(api.GetFootCart).then(function (res) {
  160. if (res.errno === 0) {
  161. that.setData({
  162. footCart: res.data,
  163. });
  164. }
  165. });
  166. },
  167. //购物车减少
  168. cutNumber: function (e) {
  169. let that = this;
  170. var goodsId = e.currentTarget.dataset.goodsId;
  171. var productId = e.currentTarget.dataset.productId;
  172. var goodsList = that.data.goodsList;
  173. // goodsList.forEach(function (val, index, arr) {
  174. // if (val.product_id == productId) {
  175. // val.cart_num = val.cart_num - 1;
  176. // if (val.cart_num >= 0) {
  177. // goodsList[index] = val;
  178. // that.setData({goodsList: goodsList});
  179. // }
  180. // }
  181. // });
  182. // that.setData({goodsList: goodsList});
  183. util.request(api.CartMinus, { goodsId: goodsId, productId: productId, number: 1 }, 'POST').then(function (res) {
  184. if (res.errno === 0 && null != res.data) {
  185. var goodsList = that.data.goodsList;
  186. that.getFootCart();
  187. goodsList.forEach(function (val, index, arr) {
  188. if (val.product_id == productId) {
  189. val.cart_num = res.data;
  190. goodsList[index] = val;
  191. }
  192. }, that);
  193. that.setData({ goodsList: goodsList });
  194. }
  195. });
  196. },
  197. //购物车增加
  198. addNumber: function (e) {
  199. let that = this;
  200. var goodsId = e.currentTarget.dataset.goodsId;
  201. var productId = e.currentTarget.dataset.productId;
  202. var goodsList = that.data.goodsList;
  203. // goodsList.forEach(function (val, index, arr) {
  204. // if (val.product_id == productId) {
  205. // val.cart_num = val.cart_num + 1;
  206. // goodsList[index] = val;
  207. // }
  208. // });
  209. // that.setData({goodsList: goodsList});
  210. util.request(api.CartAdd, { goodsId: goodsId, productId: productId, number: 1 }, 'POST').then(function (res) {
  211. if (res.errno === 0 && null != res.data) {
  212. that.getFootCart();
  213. goodsList.forEach(function (val, index, arr) {
  214. res.data.cartList.forEach(function (cartVal, cartIndex, cartArr) {
  215. if (val.product_id == cartVal.product_id) {
  216. val.cart_num = cartVal.number;
  217. goodsList[index] = val;
  218. }
  219. });
  220. }, that);
  221. that.setData({ goodsList: goodsList });
  222. }
  223. });
  224. },
  225. openSortFilter: function (event) {
  226. let that = this;
  227. let currentId = event.currentTarget.id;
  228. console.log('currentId:'+currentId);
  229. switch (currentId) {
  230. case 'defaultActivity':
  231. that.setData({
  232. 'goodsBizType': '00'
  233. });
  234. this.getGoodsList();
  235. break;
  236. case 'discountActivity':
  237. that.setData({
  238. 'goodsBizType': '02'
  239. });
  240. this.getGoodsList();
  241. break;
  242. case 'groupActivity':
  243. that.setData({
  244. 'goodsBizType': '10'
  245. });
  246. this.getGoodsList();
  247. break;
  248. case 'ordActivity':
  249. that.setData({
  250. 'goodsBizType': '11'
  251. });
  252. this.getGoodsList();
  253. break;
  254. case 'sellSort':
  255. let tmpSortOrder = 'asc';
  256. if (this.data.currentSortOrder == 'asc') {
  257. tmpSortOrder = 'desc';
  258. }
  259. this.setData({
  260. 'currentSortType': 'sell',
  261. 'currentSortOrder': tmpSortOrder,
  262. });
  263. this.getGoodsList();
  264. break;
  265. case 'priceSort':
  266. tmpSortOrder = 'asc';
  267. if (this.data.currentSortOrder == 'asc') {
  268. tmpSortOrder = 'desc';
  269. }
  270. this.setData({
  271. 'currentSortType': 'price',
  272. 'currentSortOrder': tmpSortOrder,
  273. });
  274. this.getGoodsList();
  275. break;
  276. default:
  277. //综合排序
  278. this.setData({
  279. 'currentSortType': 'default',
  280. 'currentSortOrder': 'desc',
  281. });
  282. this.getGoodsList();
  283. }
  284. },
  285. takeShareCoupon() {
  286. let that = this;
  287. util.request(api.CouponTransActivit, {
  288. referrer: that.data.sourceKey,
  289. sourceKey: that.data.sourceKey
  290. }).then(function (res) {
  291. if (res.errno === 0) {
  292. util.showErrorToast("领取成功");
  293. that.setData({
  294. couponList: res.data,
  295. openCoupon: false
  296. });
  297. } else if (res.errno === 2) {
  298. util.showErrorToast(res.errmsg)
  299. that.setData({
  300. couponList: res.data,
  301. openCoupon: false
  302. });
  303. }
  304. });
  305. },
  306. closeCoupon: function () {
  307. var that = this;
  308. that.setData({
  309. openCoupon: false
  310. });
  311. },
  312. bindtapGoodsDetail: function (e) {
  313. var that = this;
  314. let url = '';
  315. var itemId = e.currentTarget.dataset.itemId;
  316. if (that.data.filterDiscount != 2) {
  317. url = '/pages/goods/goods?id=' + itemId;
  318. } else {
  319. url = '/pages/groupDetail/groupDetail?id=' + itemId;
  320. }
  321. wx.navigateTo({
  322. url: url,
  323. })
  324. },
  325. sercherCategory: function (e) {
  326. var that = this;
  327. let url = '';
  328. var id = e.currentTarget.dataset.replyType;
  329. // console.log('replyType:' + e.currentTarget.dataset.replyType);
  330. // console.log('goodsBizType:' + that.data.goodsBizType);
  331. // 跳转页面
  332. wx.navigateTo({
  333. url: '/pages/category/category?id=' + id + '&&goodsBizType=' + that.data.goodsBizType
  334. })
  335. }
  336. })