catalog.js 16 KB

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