catalog.js 9.6 KB

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