catalog.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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: 4,
  25. showNavList: false,
  26. footCart: {},
  27. referrer: 0,
  28. sourceKey: '',
  29. openCoupon: false,
  30. refresh: false,
  31. hidden: false
  32. },
  33. toggleNav() {
  34. this.setData({
  35. showNavList: !this.data.showNavList
  36. })
  37. },
  38. switchNav(event) {
  39. let name = event.currentTarget.dataset.name;
  40. wx.switchTab({
  41. url: `/pages/${name}/${name}`,
  42. });
  43. },
  44. onLoad: function (options) {
  45. let that = this;
  46. // 页面初始化 options为页面跳转所带来的参数
  47. if (options.referrer){
  48. that.setData({
  49. referrer: parseInt(options.referrer),
  50. sourceKey: options.sourceKey,
  51. openCoupon: true
  52. });
  53. }
  54. // that.setData({
  55. // goodsBizType: app.globalData.appGoodsBizType
  56. // });
  57. wx.setNavigationBarTitle({
  58. title: '分类'
  59. });
  60. },
  61. getCatalog: function () {
  62. wx.showLoading({
  63. title: '加载中...',
  64. })
  65. //CatalogList
  66. let that = this;
  67. util.request(api.CatalogList).then(function (res) {
  68. if (!wx.getStorageSync('currentCategory')) {
  69. that.setData({
  70. navList: res.data.categoryList,
  71. currentCategory: res.data.currentCategory,
  72. brandList: res.data.brandList
  73. });
  74. wx.setStorageSync('currentCategory', res.data.currentCategory);
  75. } else {
  76. if (that.data.currentCategory && that.data.currentCategory.id > 0) {
  77. that.setData({
  78. navList: res.data.categoryList
  79. });
  80. console.log(res.data.categoryList);
  81. console.log(res.data.currentCategory);
  82. console.log(res.data.brandList);
  83. } else {
  84. that.setData({
  85. navList: res.data.categoryList,
  86. currentCategory: res.data.currentCategory,
  87. brandList: res.data.brandList
  88. });
  89. }
  90. }
  91. if (that.data.goodsList.length == 0 || wx.getStorageSync('storeId') != wx.getStorageSync('mapCatalogStoreId')) {
  92. wx.setStorageSync('mapCatalogStoreId', wx.getStorageSync('storeId'));
  93. that.setData({
  94. page: 1
  95. });
  96. that.getCategoryData();
  97. }
  98. // if (wx.getStorageSync('storeId') != wx.getStorageSync('mapCatalogStoreId')) {
  99. // wx.setStorageSync('mapCatalogStoreId', wx.getStorageSync('storeId'));
  100. // that.setData({
  101. // page: 1
  102. // });
  103. // that.getCategoryData();
  104. // }
  105. wx.hideLoading();
  106. });
  107. util.request(api.GoodsCount).then(function (res) {
  108. that.setData({
  109. goodsCount: res.data.goodsCount
  110. });
  111. });
  112. },
  113. getCurrentCategory: function (id) {
  114. let that = this;
  115. util.request(api.CatalogCurrent, { id: id })
  116. .then(function (res) {
  117. that.setData({
  118. currentCategory: res.data.currentCategory,
  119. brandList: res.data.brandList,
  120. goodsList: [],
  121. page: 1
  122. });
  123. // console.log(res.data.brandList);
  124. that.getGoodsList();
  125. });
  126. },
  127. onReady: function () {
  128. // 页面渲染完成
  129. },
  130. onShow: function () {
  131. let that = this;
  132. that.setData({
  133. goodsBizType: app.globalData.appGoodsBizType
  134. });
  135. // 页面显示
  136. if (wx.getStorageSync('userInfo') || wx.getStorageSync('token')) {
  137. if (wx.getStorageSync('storeId')) {
  138. util.request(api.ChooseStoreId, {
  139. storeId: wx.getStorageSync('storeId'),
  140. merchSn: wx.getStorageSync('merchSn')
  141. }, 'POST').then(function (res) {
  142. if (res.errno === 0) {
  143. wx.setStorageSync('storeId', wx.getStorageSync('storeId'));
  144. wx.setStorageSync('merchSn', wx.getStorageSync('merchSn'));
  145. that.reLoad();
  146. }
  147. });
  148. that.getFootCart();
  149. that.getCatalog();
  150. }
  151. } else {
  152. wx.navigateTo({
  153. url: '/pages/auth/btnAuth/btnAuth',
  154. })
  155. }
  156. },
  157. onHide: function () {
  158. // 页面隐藏
  159. this.setData({
  160. showNavList: false
  161. })
  162. },
  163. onUnload: function () {
  164. // 页面关闭
  165. },
  166. reLoad: function () {
  167. let that = this;
  168. if (wx.getStorageSync('storeId')) {
  169. if (wx.getStorageSync('userId')) {
  170. wx.request({
  171. url: api.updateLoginUser,
  172. data: {
  173. userId: wx.getStorageSync('userId'), storeId: wx.getStorageSync('storeId'), merchSn: wx.getStorageSync('merchSn')
  174. },
  175. method: 'POST',
  176. header: {
  177. 'Content-Type': 'application/json'
  178. },
  179. success: function (wxRes) {
  180. if (wxRes.data.errno === 0) {
  181. // console.log("用户信息更新成功");
  182. }
  183. },
  184. fail: function (err) {
  185. console.log("failed");
  186. }
  187. });
  188. }
  189. }
  190. },
  191. getList: function () {
  192. var that = this;
  193. util.request(api.ApiRootUrl + 'api/catalog/' + that.data.currentCategory.id)
  194. .then(function (res) {
  195. that.setData({
  196. categoryList: res.data,
  197. });
  198. });
  199. },
  200. switchCate: function (event) {
  201. var that = this;
  202. var currentTarget = event.currentTarget;
  203. if (this.data.currentCategory.id == event.currentTarget.dataset.id) {
  204. return false;
  205. }
  206. this.getCurrentCategory(event.currentTarget.dataset.id);
  207. },
  208. //
  209. getCategoryData: function () {
  210. let that = this;
  211. that.setData({
  212. goodsList: []
  213. });
  214. that.getGoodsList();
  215. },
  216. getGoodsList() {
  217. // wx.showLoading({
  218. // title: '加载中...',
  219. // });
  220. var that = this;
  221. if (that.data.currentCategory){
  222. util.request(api.CatalogProductList, {
  223. page: that.data.page,
  224. size: that.data.size,
  225. order: that.data.currentSortOrder,
  226. sort: that.data.currentSortType,
  227. // discount: that.data.filterDiscount,
  228. goodsBizType: that.data.goodsBizType,
  229. categoryId: that.data.currentCategory.id
  230. })
  231. .then(function (res) {
  232. if (res.errno === 0) {
  233. let goodsList = that.data.goodsList.concat(res.data.data);
  234. that.setData({
  235. goodsList: goodsList,
  236. filterCategory: res.data.filterCategory,
  237. refresh: true
  238. });
  239. if(that.data.page == 4){
  240. that.setData({refresh: true});
  241. }else{
  242. that.setData({hidden: true })
  243. }
  244. }
  245. });
  246. }
  247. wx.hideLoading();
  248. },
  249. getFootCart: function () {
  250. let that = this;
  251. util.request(api.GetFootCart).then(function (res) {
  252. if (res.errno === 0) {
  253. that.setData({
  254. footCart: res.data,
  255. });
  256. }
  257. });
  258. },
  259. //购物车减少
  260. cutNumber: function (e) {
  261. let that = this;
  262. var goodsId = e.currentTarget.dataset.goodsId;
  263. var productId = e.currentTarget.dataset.productId;
  264. var goodsList = that.data.goodsList;
  265. // goodsList.forEach(function (val, index, arr) {
  266. // if (val.product_id == productId) {
  267. // val.cart_num = val.cart_num - 1;
  268. // if (val.cart_num >= 0) {
  269. // goodsList[index] = val;
  270. // that.setData({goodsList: goodsList});
  271. // }
  272. // }
  273. // });
  274. // that.setData({goodsList: goodsList});
  275. util.request(api.CartMinus, { goodsId: goodsId, productId: productId, number: 1 }, 'POST').then(function (res) {
  276. if (res.errno === 0 && null != res.data) {
  277. var goodsList = that.data.goodsList;
  278. that.getFootCart();
  279. goodsList.forEach(function (val, index, arr) {
  280. if (val.product_id == productId) {
  281. val.cart_num = res.data;
  282. goodsList[index] = val;
  283. if(val.cart_num == 0){
  284. util.showErrorToast('不能再减了');
  285. }
  286. }
  287. }, that);
  288. that.setData({ goodsList: goodsList });
  289. }
  290. });
  291. },
  292. //购物车增加
  293. addNumber: function (e) {
  294. let that = this;
  295. var goodsId = e.currentTarget.dataset.goodsId;
  296. var productId = e.currentTarget.dataset.productId;
  297. var goodsList = that.data.goodsList;
  298. // goodsList.forEach(function (val, index, arr) {
  299. // if (val.product_id == productId) {
  300. // val.cart_num = val.cart_num + 1;
  301. // goodsList[index] = val;
  302. // }
  303. // });
  304. // that.setData({goodsList: goodsList});
  305. util.request(api.CartAdd, { goodsId: goodsId, productId: productId, number: 1 }, 'POST').then(function (res) {
  306. if (res.errno === 0 && null != res.data) {
  307. that.getFootCart();
  308. goodsList.forEach(function (val, index, arr) {
  309. res.data.cartList.forEach(function (cartVal, cartIndex, cartArr) {
  310. if (val.product_id == cartVal.product_id) {
  311. val.cart_num = cartVal.number;
  312. goodsList[index] = val;
  313. }
  314. });
  315. }, that);
  316. that.setData({ goodsList: goodsList });
  317. }
  318. });
  319. },
  320. openSortFilter: function (event) {
  321. let that = this;
  322. let currentId = event.currentTarget.id;
  323. console.log('currentId:'+currentId);
  324. switch (currentId) {
  325. case 'defaultActivity':
  326. that.setData({
  327. 'goodsBizType': '00',
  328. goodsList: [],
  329. page: 1
  330. });
  331. app.globalData.appGoodsBizType = '00';
  332. this.getGoodsList();
  333. break;
  334. case 'discountActivity':
  335. that.setData({
  336. 'goodsBizType': '02',
  337. goodsList: [],
  338. page: 1
  339. });
  340. app.globalData.appGoodsBizType = '02';
  341. this.getGoodsList();
  342. break;
  343. case 'groupActivity':
  344. that.setData({
  345. 'goodsBizType': '10',
  346. goodsList: [],
  347. page: 1
  348. });
  349. app.globalData.appGoodsBizType = '10';
  350. this.getGoodsList();
  351. break;
  352. case 'ordActivity':
  353. that.setData({
  354. 'goodsBizType': '11',
  355. goodsList: [],
  356. page: 1
  357. });
  358. app.globalData.appGoodsBizType = '11';
  359. this.getGoodsList();
  360. break;
  361. case 'sellSort':
  362. let tmpSortOrder = 'asc';
  363. if (this.data.currentSortOrder == 'asc') {
  364. tmpSortOrder = 'desc';
  365. }
  366. this.setData({
  367. 'currentSortType': 'sell',
  368. 'currentSortOrder': tmpSortOrder,
  369. goodsList: [],
  370. page: 1
  371. });
  372. this.getGoodsList();
  373. break;
  374. case 'priceSort':
  375. tmpSortOrder = 'asc';
  376. if (this.data.currentSortOrder == 'asc') {
  377. tmpSortOrder = 'desc';
  378. }
  379. this.setData({
  380. 'currentSortType': 'price',
  381. 'currentSortOrder': tmpSortOrder,
  382. goodsList: [],
  383. page: 1
  384. });
  385. this.getGoodsList();
  386. break;
  387. default:
  388. //综合排序
  389. this.setData({
  390. 'currentSortType': 'default',
  391. 'currentSortOrder': 'desc',
  392. goodsList: [],
  393. page: 1
  394. });
  395. this.getGoodsList();
  396. }
  397. },
  398. takeShareCoupon() {
  399. let that = this;
  400. util.request(api.CouponTransActivit, {
  401. referrer: that.data.sourceKey,
  402. sourceKey: that.data.sourceKey
  403. }).then(function (res) {
  404. if (res.errno === 0) {
  405. util.showErrorToast("领取成功");
  406. that.setData({
  407. couponList: res.data,
  408. openCoupon: false
  409. });
  410. } else if (res.errno === 2) {
  411. util.showErrorToast(res.errmsg)
  412. that.setData({
  413. couponList: res.data,
  414. openCoupon: false
  415. });
  416. }
  417. });
  418. },
  419. closeCoupon: function () {
  420. var that = this;
  421. that.setData({
  422. openCoupon: false
  423. });
  424. },
  425. bindtapGoodsDetail: function (e) {
  426. var that = this;
  427. let url = '';
  428. var itemId = e.currentTarget.dataset.itemId;
  429. if (that.data.filterDiscount != 2) {
  430. url = '/pages/goods/goods?id=' + itemId;
  431. } else {
  432. url = '/pages/groupDetail/groupDetail?id=' + itemId;
  433. }
  434. wx.navigateTo({
  435. url: url,
  436. })
  437. },
  438. sercherCategory: function (e) {
  439. var that = this;
  440. let url = '';
  441. var replyType = e.currentTarget.dataset.replyType;
  442. var currentIndex = e.currentTarget.dataset.currentIndex;
  443. // console.log('replyType:' + e.currentTarget.dataset.replyType);
  444. // console.log('goodsBizType:' + that.data.goodsBizType);
  445. // 跳转页面
  446. wx.navigateTo({
  447. url: '/pages/category/category?id=' + replyType + '&&goodsBizType=' + that.data.goodsBizType + '&&currentIndex=' + currentIndex
  448. })
  449. },
  450. sercherCategoryByBrand: function (e) {
  451. var that = this;
  452. let url = '';
  453. var replyType = e.currentTarget.dataset.replyType;
  454. var currentIndex = e.currentTarget.dataset.currentIndex;
  455. console.log('replyType:' + replyType);
  456. console.log('goodsBizType:' + that.data.goodsBizType);
  457. // 跳转页面
  458. wx.navigateTo({
  459. url: '/pages/categoryBrand/categoryBrand?brandId=' + replyType + '&&goodsBizType=' + that.data.goodsBizType + '&&currentIndex=' + currentIndex
  460. })
  461. },
  462. // onReachBottom() {
  463. // var that = this;
  464. // wx.showLoading({
  465. // title: '加载中...',
  466. // })
  467. // that.setData({
  468. // page: that.data.page + 1
  469. // });
  470. // if(that.data.page > that.data.total){
  471. // that.setData({
  472. // hidden: false
  473. // });
  474. // }
  475. // that.getGoodsList();
  476. // },
  477. onLoaderMoreMovies: function () {
  478. var that = this;
  479. wx.showNavigationBarLoading();
  480. if (that.data.goodsList.length > 0) {
  481. wx.showLoading({
  482. title: '加载中...',
  483. });
  484. that.setData({
  485. page: that.data.page + 1
  486. });
  487. }
  488. console.log('加载');
  489. that.getGoodsList();
  490. wx.hideNavigationBarLoading()
  491. }
  492. })