catalog.js 15 KB

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