catalog.js 15 KB

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