product-list.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. let data = {
  63. ...this.data.filterObj,
  64. pageNum:this.data.pageNum,
  65. pageSize:5
  66. }
  67. API.getProdsList(data).then(res=>{
  68. if(res){
  69. res.mercProdList.forEach(item=>{
  70. if(!item.price && item.prodAttrList){
  71. item.price = item.prodAttrList[0].prodAttrPrice.price;
  72. }
  73. })
  74. this.setData({
  75. goodsList:this.data.pageNum === 1?res.mercProdList:[...this.data.goodsList,...res.mercProdList]
  76. })
  77. }
  78. })
  79. },
  80. // 添加购物车
  81. addCart(prod){
  82. console.log(prod);
  83. let prodInfo = prod.currentTarget.dataset.prod;
  84. prodInfo.buy = true;
  85. this.triggerEvent('addCart',prodInfo);
  86. },
  87. // 观察滚动触底事件
  88. onReachBottom(e){
  89. this.setData({
  90. pageNum:this.data.pageNum+1
  91. });
  92. this.observerFunction();
  93. },
  94. observerFunction(){
  95. if(this.data.position === 'hot') this.getHotCommodity();
  96. if(this.data.position === 'new') this.getNewCommodity();
  97. if(this.data.position === 'presale') this.getPresaleCommodity();
  98. if(this.data.position === 'sort' || this.data.position === 'index') this.getClassifyCommodity();
  99. },
  100. // 进入商品详情
  101. goProdDetail(e){
  102. wx.navigateTo({
  103. url: '/pages/prodDetail/prodDetail?prod='+ encodeURIComponent(JSON.stringify(e.currentTarget.dataset.prod)),
  104. })
  105. },
  106. },
  107. ready: function(){
  108. this.observerFunction();
  109. }
  110. })