catalog.js 14 KB

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