product-list.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // components/product-list/product-list.js
  2. // 获取应用实例
  3. const {API} = require('../../service/api.js');
  4. Component({
  5. properties: {
  6. // header嵌入的位置,index:首页
  7. position:{
  8. type:String,
  9. value:''
  10. },
  11. filterObj:{
  12. type:Object,
  13. value:{}
  14. },
  15. isMargin:{
  16. type:Boolean,
  17. value:false
  18. }
  19. },
  20. observers:{
  21. "filterObj": function(newVal){
  22. this.getClassifyCommodity();
  23. }
  24. },
  25. data: {
  26. goodsList: [],
  27. pageNum:1,
  28. },
  29. methods: {
  30. // 获取今日热门商品列表
  31. getHotCommodity(){
  32. API.getLeftProds().then(res=>{
  33. if(res){
  34. this.setData({
  35. goodsList:res.prodList
  36. })
  37. };
  38. })
  39. },
  40. // 获取上新商品列表
  41. getNewCommodity(){
  42. API.getMiddleProds().then(res=>{
  43. if(res){
  44. this.setData({
  45. goodsList:res.prodList
  46. })
  47. };
  48. })
  49. },
  50. // 获取预售商品列表
  51. getPresaleCommodity(){
  52. API.getRightProds().then(res=>{
  53. if(res){
  54. this.setData({
  55. goodsList:res.prodList
  56. })
  57. }
  58. })
  59. },
  60. // 获取分类商品
  61. getClassifyCommodity(){
  62. if (!!this.data.filterObj.pageNum) {
  63. this.setData({
  64. pageNum:this.data.filterObj.pageNum
  65. })
  66. }
  67. let data = {
  68. ...this.data.filterObj,
  69. pageNum:this.data.pageNum,
  70. pageSize:10
  71. }
  72. API.getProdsList(data).then(res=>{
  73. if(res){
  74. res.mercProdList.forEach(item=>{
  75. if(!item.price && item.prodAttrList){
  76. item.price = item.prodAttrList[0].prodAttrPrice.price;
  77. }
  78. })
  79. this.setData({
  80. goodsList:this.data.pageNum === 1?res.mercProdList:[...this.data.goodsList,...res.mercProdList]
  81. })
  82. }
  83. })
  84. },
  85. // 添加购物车
  86. addCart(prod){
  87. console.log(prod);
  88. let prodInfo = prod.currentTarget.dataset.prod;
  89. prodInfo.buy = prod.currentTarget.dataset.buy;
  90. this.triggerEvent('addCart',prodInfo);
  91. },
  92. // 观察滚动触底事件
  93. onReachBottom(e){
  94. this.setData({
  95. pageNum:this.data.pageNum+1
  96. });
  97. this.observerFunction();
  98. },
  99. observerFunction(){
  100. if(this.data.position === 'hot') this.getHotCommodity();
  101. if(this.data.position === 'new') this.getNewCommodity();
  102. if(this.data.position === 'presale') this.getPresaleCommodity();
  103. if(this.data.position === 'sort' || this.data.position === 'index') this.getClassifyCommodity();
  104. },
  105. // 进入商品详情
  106. goProdDetail(e){
  107. wx.navigateTo({
  108. url: '/pages/prodDetail/prodDetail?prod='+ encodeURIComponent(JSON.stringify(e.currentTarget.dataset.prod)),
  109. })
  110. },
  111. },
  112. ready: function(){
  113. this.observerFunction();
  114. }
  115. })