product-list.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. },
  16. observers:{
  17. "filterObj": function(newVal){
  18. this.getClassifyCommodity();
  19. }
  20. },
  21. data: {
  22. goodsList: [],
  23. pageNum:1,
  24. },
  25. methods: {
  26. // 获取今日热门商品列表
  27. getHotCommodity(){
  28. API.getLeftProds().then(res=>{
  29. if(res){
  30. this.setData({
  31. goodsList:res.prodList
  32. })
  33. };
  34. })
  35. },
  36. // 获取上新商品列表
  37. getNewCommodity(){
  38. API.getMiddleProds().then(res=>{
  39. if(res){
  40. this.setData({
  41. goodsList:res.prodList
  42. })
  43. };
  44. })
  45. },
  46. // 获取预售商品列表
  47. getPresaleCommodity(){
  48. API.getRightProds().then(res=>{
  49. if(res){
  50. this.setData({
  51. goodsList:res.prodList
  52. })
  53. }
  54. })
  55. },
  56. // 获取分类商品
  57. getClassifyCommodity(){
  58. let data = {
  59. ...this.data.filterObj,
  60. pageNum:this.data.pageNum,
  61. pageSize:5
  62. }
  63. API.getProdsList(data).then(res=>{
  64. if(res){
  65. this.setData({
  66. goodsList:this.data.pageNum === 1?res.mercProdList:[...this.data.goodsList,...res.mercProdList]
  67. })
  68. }
  69. })
  70. },
  71. // 添加购物车
  72. addCart(prod){
  73. console.log(prod);
  74. let prodInfo = prod.currentTarget.dataset.prod;
  75. this.triggerEvent('addCart',prodInfo);
  76. // let data = {
  77. // mercId:prodInfo.mercId,
  78. // prodId:prodInfo.prodId,
  79. // prodAttrId:prodInfo.prodAttrList[0],
  80. // count:1
  81. // }
  82. // API.addCart(data).then(res=>{
  83. // console.log(res);
  84. // })
  85. },
  86. // 观察滚动触底事件
  87. onReachBottom(e){
  88. this.setData({
  89. pageNum:this.data.pageNum+1
  90. });
  91. this.observerFunction();
  92. },
  93. observerFunction(){
  94. if(this.data.position === 'hot') this.getHotCommodity();
  95. if(this.data.position === 'new') this.getNewCommodity();
  96. if(this.data.position === 'presale') this.getPresaleCommodity();
  97. if(this.data.position === 'sort' || this.data.position === 'index') this.getClassifyCommodity();
  98. },
  99. },
  100. ready: function(){
  101. this.observerFunction();
  102. }
  103. })