1
0

catalog.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. if (val.product_id == cartVal.product_id) {
  220. val.cart_num = cartVal.number;
  221. goodsList[index] = val;
  222. }
  223. });
  224. }, that);
  225. that.setData({ goodsList: goodsList });
  226. }
  227. });
  228. },
  229. openSortFilter: function (event) {
  230. let that = this;
  231. let currentId = event.currentTarget.id;
  232. console.log('currentId:'+currentId);
  233. switch (currentId) {
  234. case 'defaultActivity':
  235. that.setData({
  236. 'goodsBizType': '00'
  237. });
  238. app.globalData.appGoodsBizType = '00';
  239. this.getGoodsList();
  240. break;
  241. case 'discountActivity':
  242. that.setData({
  243. 'goodsBizType': '02'
  244. });
  245. app.globalData.appGoodsBizType = '02';
  246. this.getGoodsList();
  247. break;
  248. case 'groupActivity':
  249. that.setData({
  250. 'goodsBizType': '10'
  251. });
  252. app.globalData.appGoodsBizType = '10';
  253. this.getGoodsList();
  254. break;
  255. case 'ordActivity':
  256. that.setData({
  257. 'goodsBizType': '11'
  258. });
  259. app.globalData.appGoodsBizType = '11';
  260. this.getGoodsList();
  261. break;
  262. case 'sellSort':
  263. let tmpSortOrder = 'asc';
  264. if (this.data.currentSortOrder == 'asc') {
  265. tmpSortOrder = 'desc';
  266. }
  267. this.setData({
  268. 'currentSortType': 'sell',
  269. 'currentSortOrder': tmpSortOrder,
  270. });
  271. this.getGoodsList();
  272. break;
  273. case 'priceSort':
  274. tmpSortOrder = 'asc';
  275. if (this.data.currentSortOrder == 'asc') {
  276. tmpSortOrder = 'desc';
  277. }
  278. this.setData({
  279. 'currentSortType': 'price',
  280. 'currentSortOrder': tmpSortOrder,
  281. });
  282. this.getGoodsList();
  283. break;
  284. default:
  285. //综合排序
  286. this.setData({
  287. 'currentSortType': 'default',
  288. 'currentSortOrder': 'desc',
  289. });
  290. this.getGoodsList();
  291. }
  292. },
  293. takeShareCoupon() {
  294. let that = this;
  295. util.request(api.CouponTransActivit, {
  296. referrer: that.data.sourceKey,
  297. sourceKey: that.data.sourceKey
  298. }).then(function (res) {
  299. if (res.errno === 0) {
  300. util.showErrorToast("领取成功");
  301. that.setData({
  302. couponList: res.data,
  303. openCoupon: false
  304. });
  305. } else if (res.errno === 2) {
  306. util.showErrorToast(res.errmsg)
  307. that.setData({
  308. couponList: res.data,
  309. openCoupon: false
  310. });
  311. }
  312. });
  313. },
  314. closeCoupon: function () {
  315. var that = this;
  316. that.setData({
  317. openCoupon: false
  318. });
  319. },
  320. bindtapGoodsDetail: function (e) {
  321. var that = this;
  322. let url = '';
  323. var itemId = e.currentTarget.dataset.itemId;
  324. if (that.data.filterDiscount != 2) {
  325. url = '/pages/goods/goods?id=' + itemId;
  326. } else {
  327. url = '/pages/groupDetail/groupDetail?id=' + itemId;
  328. }
  329. wx.navigateTo({
  330. url: url,
  331. })
  332. },
  333. sercherCategory: function (e) {
  334. var that = this;
  335. let url = '';
  336. var replyType = e.currentTarget.dataset.replyType;
  337. var currentIndex = e.currentTarget.dataset.currentIndex;
  338. // console.log('replyType:' + e.currentTarget.dataset.replyType);
  339. // console.log('goodsBizType:' + that.data.goodsBizType);
  340. // 跳转页面
  341. wx.navigateTo({
  342. url: '/pages/category/category?id=' + replyType + '&&goodsBizType=' + that.data.goodsBizType + '&&currentIndex=' + currentIndex
  343. })
  344. }
  345. })