catalog.js 11 KB

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