catalog.js 13 KB

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