1
0

catalog.js 9.9 KB

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