1
0

catalog.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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: 50,
  25. showNavList: false,
  26. footCart: {},
  27. referrer: 0,
  28. sourceKey: '',
  29. openCoupon: false
  30. },
  31. toggleNav() {
  32. this.setData({
  33. showNavList: !this.data.showNavList
  34. })
  35. },
  36. switchNav(event) {
  37. let name = event.currentTarget.dataset.name;
  38. wx.switchTab({
  39. url: `/pages/${name}/${name}`,
  40. });
  41. },
  42. onLoad: function (options) {
  43. let that = this;
  44. // 页面初始化 options为页面跳转所带来的参数
  45. if (options.referrer){
  46. that.setData({
  47. referrer: parseInt(options.referrer),
  48. sourceKey: options.sourceKey,
  49. openCoupon: true
  50. });
  51. }
  52. // that.setData({
  53. // goodsBizType: app.globalData.appGoodsBizType
  54. // });
  55. wx.setNavigationBarTitle({
  56. title: '分类'
  57. });
  58. },
  59. getCatalog: function () {
  60. //CatalogList
  61. let that = this;
  62. util.request(api.CatalogList).then(function (res) {
  63. if (!wx.getStorageSync('currentCategory')) {
  64. that.setData({
  65. navList: res.data.categoryList,
  66. currentCategory: res.data.currentCategory,
  67. brandList: res.data.brandList
  68. });
  69. wx.setStorageSync('currentCategory', res.data.currentCategory);
  70. } else {
  71. if (that.data.currentCategory && that.data.currentCategory.id > 0) {
  72. that.setData({
  73. navList: res.data.categoryList
  74. });
  75. console.log(res.data.categoryList);
  76. console.log(res.data.currentCategory);
  77. console.log(res.data.brandList);
  78. } else {
  79. that.setData({
  80. navList: res.data.categoryList,
  81. currentCategory: res.data.currentCategory,
  82. brandList: res.data.brandList
  83. });
  84. }
  85. }
  86. that.getCategoryData();
  87. });
  88. util.request(api.GoodsCount).then(function (res) {
  89. that.setData({
  90. goodsCount: res.data.goodsCount
  91. });
  92. });
  93. },
  94. getCurrentCategory: function (id) {
  95. let that = this;
  96. util.request(api.CatalogCurrent, { id: id })
  97. .then(function (res) {
  98. that.setData({
  99. currentCategory: res.data.currentCategory,
  100. brandList: res.data.brandList
  101. });
  102. // console.log(res.data.brandList);
  103. that.getGoodsList();
  104. });
  105. },
  106. onReady: function () {
  107. // 页面渲染完成
  108. },
  109. onShow: function () {
  110. let that = this;
  111. that.setData({
  112. goodsBizType: app.globalData.appGoodsBizType
  113. });
  114. // 页面显示
  115. if (wx.getStorageSync('userInfo') || wx.getStorageSync('token')) {
  116. if (wx.getStorageSync('storeId')) {
  117. util.request(api.ChooseStoreId, {
  118. storeId: wx.getStorageSync('storeId'),
  119. merchSn: wx.getStorageSync('merchSn')
  120. }, 'POST').then(function (res) {
  121. if (res.errno === 0) {
  122. wx.setStorageSync('storeId', wx.getStorageSync('storeId'));
  123. wx.setStorageSync('merchSn', wx.getStorageSync('merchSn'));
  124. that.reLoad();
  125. }
  126. });
  127. that.getFootCart();
  128. that.getCatalog();
  129. } else {
  130. wx.switchTab({
  131. url: '/pages/index/index'
  132. });
  133. }
  134. } else {
  135. wx.navigateTo({
  136. url: '/pages/auth/btnAuth/btnAuth',
  137. })
  138. }
  139. },
  140. onHide: function () {
  141. // 页面隐藏
  142. this.setData({
  143. showNavList: false
  144. })
  145. },
  146. onUnload: function () {
  147. // 页面关闭
  148. },
  149. reLoad: function () {
  150. let that = this;
  151. if (wx.getStorageSync('storeId')) {
  152. if (wx.getStorageSync('userId')) {
  153. wx.request({
  154. url: api.updateLoginUser,
  155. data: {
  156. userId: wx.getStorageSync('userId'), storeId: wx.getStorageSync('storeId'), merchSn: wx.getStorageSync('merchSn')
  157. },
  158. method: 'POST',
  159. header: {
  160. 'Content-Type': 'application/json'
  161. },
  162. success: function (wxRes) {
  163. if (wxRes.data.errno === 0) {
  164. // console.log("用户信息更新成功");
  165. }
  166. },
  167. fail: function (err) {
  168. console.log("failed");
  169. }
  170. });
  171. }
  172. }
  173. },
  174. getList: function () {
  175. var that = this;
  176. util.request(api.ApiRootUrl + 'api/catalog/' + that.data.currentCategory.id)
  177. .then(function (res) {
  178. that.setData({
  179. categoryList: res.data,
  180. });
  181. });
  182. },
  183. switchCate: function (event) {
  184. var that = this;
  185. var currentTarget = event.currentTarget;
  186. if (this.data.currentCategory.id == event.currentTarget.dataset.id) {
  187. return false;
  188. }
  189. this.getCurrentCategory(event.currentTarget.dataset.id);
  190. },
  191. //
  192. getCategoryData: function () {
  193. let that = this;
  194. that.getGoodsList();
  195. },
  196. getGoodsList() {
  197. wx.showLoading({
  198. title: '加载中...',
  199. });
  200. var that = this;
  201. if (that.data.currentCategory){
  202. util.request(api.CatalogProductList, {
  203. page: that.data.page,
  204. size: that.data.size,
  205. order: that.data.currentSortOrder,
  206. sort: that.data.currentSortType,
  207. // discount: that.data.filterDiscount,
  208. goodsBizType: that.data.goodsBizType,
  209. categoryId: that.data.currentCategory.id
  210. })
  211. .then(function (res) {
  212. if (res.errno === 0) {
  213. that.setData({
  214. goodsList: res.data.data,
  215. filterCategory: res.data.filterCategory
  216. });
  217. wx.hideLoading();
  218. }
  219. });
  220. }
  221. wx.hideLoading();
  222. },
  223. getFootCart: function () {
  224. let that = this;
  225. util.request(api.GetFootCart).then(function (res) {
  226. if (res.errno === 0) {
  227. that.setData({
  228. footCart: res.data,
  229. });
  230. }
  231. });
  232. },
  233. //购物车减少
  234. cutNumber: function (e) {
  235. let that = this;
  236. var goodsId = e.currentTarget.dataset.goodsId;
  237. var productId = e.currentTarget.dataset.productId;
  238. var goodsList = that.data.goodsList;
  239. // goodsList.forEach(function (val, index, arr) {
  240. // if (val.product_id == productId) {
  241. // val.cart_num = val.cart_num - 1;
  242. // if (val.cart_num >= 0) {
  243. // goodsList[index] = val;
  244. // that.setData({goodsList: goodsList});
  245. // }
  246. // }
  247. // });
  248. // that.setData({goodsList: goodsList});
  249. util.request(api.CartMinus, { goodsId: goodsId, productId: productId, number: 1 }, 'POST').then(function (res) {
  250. if (res.errno === 0 && null != res.data) {
  251. var goodsList = that.data.goodsList;
  252. that.getFootCart();
  253. goodsList.forEach(function (val, index, arr) {
  254. if (val.product_id == productId) {
  255. val.cart_num = res.data;
  256. goodsList[index] = val;
  257. if(val.cart_num == 0){
  258. util.showErrorToast('不能再减了');
  259. }
  260. }
  261. }, that);
  262. that.setData({ goodsList: goodsList });
  263. }
  264. });
  265. },
  266. //购物车增加
  267. addNumber: function (e) {
  268. let that = this;
  269. var goodsId = e.currentTarget.dataset.goodsId;
  270. var productId = e.currentTarget.dataset.productId;
  271. var goodsList = that.data.goodsList;
  272. // goodsList.forEach(function (val, index, arr) {
  273. // if (val.product_id == productId) {
  274. // val.cart_num = val.cart_num + 1;
  275. // goodsList[index] = val;
  276. // }
  277. // });
  278. // that.setData({goodsList: goodsList});
  279. util.request(api.CartAdd, { goodsId: goodsId, productId: productId, number: 1 }, 'POST').then(function (res) {
  280. if (res.errno === 0 && null != res.data) {
  281. that.getFootCart();
  282. goodsList.forEach(function (val, index, arr) {
  283. res.data.cartList.forEach(function (cartVal, cartIndex, cartArr) {
  284. if (val.product_id == cartVal.product_id) {
  285. val.cart_num = cartVal.number;
  286. goodsList[index] = val;
  287. }
  288. });
  289. }, that);
  290. that.setData({ goodsList: goodsList });
  291. }
  292. });
  293. },
  294. openSortFilter: function (event) {
  295. let that = this;
  296. let currentId = event.currentTarget.id;
  297. console.log('currentId:'+currentId);
  298. switch (currentId) {
  299. case 'defaultActivity':
  300. that.setData({
  301. 'goodsBizType': '00'
  302. });
  303. app.globalData.appGoodsBizType = '00';
  304. this.getGoodsList();
  305. break;
  306. case 'discountActivity':
  307. that.setData({
  308. 'goodsBizType': '02'
  309. });
  310. app.globalData.appGoodsBizType = '02';
  311. this.getGoodsList();
  312. break;
  313. case 'groupActivity':
  314. that.setData({
  315. 'goodsBizType': '10'
  316. });
  317. app.globalData.appGoodsBizType = '10';
  318. this.getGoodsList();
  319. break;
  320. case 'ordActivity':
  321. that.setData({
  322. 'goodsBizType': '11'
  323. });
  324. app.globalData.appGoodsBizType = '11';
  325. this.getGoodsList();
  326. break;
  327. case 'sellSort':
  328. let tmpSortOrder = 'asc';
  329. if (this.data.currentSortOrder == 'asc') {
  330. tmpSortOrder = 'desc';
  331. }
  332. this.setData({
  333. 'currentSortType': 'sell',
  334. 'currentSortOrder': tmpSortOrder,
  335. });
  336. this.getGoodsList();
  337. break;
  338. case 'priceSort':
  339. tmpSortOrder = 'asc';
  340. if (this.data.currentSortOrder == 'asc') {
  341. tmpSortOrder = 'desc';
  342. }
  343. this.setData({
  344. 'currentSortType': 'price',
  345. 'currentSortOrder': tmpSortOrder,
  346. });
  347. this.getGoodsList();
  348. break;
  349. default:
  350. //综合排序
  351. this.setData({
  352. 'currentSortType': 'default',
  353. 'currentSortOrder': 'desc',
  354. });
  355. this.getGoodsList();
  356. }
  357. },
  358. takeShareCoupon() {
  359. let that = this;
  360. util.request(api.CouponTransActivit, {
  361. referrer: that.data.sourceKey,
  362. sourceKey: that.data.sourceKey
  363. }).then(function (res) {
  364. if (res.errno === 0) {
  365. util.showErrorToast("领取成功");
  366. that.setData({
  367. couponList: res.data,
  368. openCoupon: false
  369. });
  370. } else if (res.errno === 2) {
  371. util.showErrorToast(res.errmsg)
  372. that.setData({
  373. couponList: res.data,
  374. openCoupon: false
  375. });
  376. }
  377. });
  378. },
  379. closeCoupon: function () {
  380. var that = this;
  381. that.setData({
  382. openCoupon: false
  383. });
  384. },
  385. bindtapGoodsDetail: function (e) {
  386. var that = this;
  387. let url = '';
  388. var itemId = e.currentTarget.dataset.itemId;
  389. if (that.data.filterDiscount != 2) {
  390. url = '/pages/goods/goods?id=' + itemId;
  391. } else {
  392. url = '/pages/groupDetail/groupDetail?id=' + itemId;
  393. }
  394. wx.navigateTo({
  395. url: url,
  396. })
  397. },
  398. sercherCategory: function (e) {
  399. var that = this;
  400. let url = '';
  401. var replyType = e.currentTarget.dataset.replyType;
  402. var currentIndex = e.currentTarget.dataset.currentIndex;
  403. // console.log('replyType:' + e.currentTarget.dataset.replyType);
  404. // console.log('goodsBizType:' + that.data.goodsBizType);
  405. // 跳转页面
  406. wx.navigateTo({
  407. url: '/pages/category/category?id=' + replyType + '&&goodsBizType=' + that.data.goodsBizType + '&&currentIndex=' + currentIndex
  408. })
  409. },
  410. sercherCategoryByBrand: function (e) {
  411. var that = this;
  412. let url = '';
  413. var replyType = e.currentTarget.dataset.replyType;
  414. var currentIndex = e.currentTarget.dataset.currentIndex;
  415. console.log('replyType:' + replyType);
  416. console.log('goodsBizType:' + that.data.goodsBizType);
  417. // 跳转页面
  418. wx.navigateTo({
  419. url: '/pages/categoryBrand/categoryBrand?brandId=' + replyType + '&&goodsBizType=' + that.data.goodsBizType + '&&currentIndex=' + currentIndex
  420. })
  421. }
  422. })