| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- // components/product-list/product-list.js
- // 获取应用实例
- const {API} = require('../../service/api.js');
- Component({
- properties: {
- // header嵌入的位置,index:首页
- position:{
- type:String,
- value:''
- },
- filterObj:{
- type:Object,
- value:{}
- }
- },
- observers:{
- "filterObj": function(newVal){
- this.getClassifyCommodity();
- }
- },
- data: {
- goodsList: [],
- pageNum:1,
- },
- methods: {
- // 获取今日热门商品列表
- getHotCommodity(){
- API.getLeftProds().then(res=>{
- if(res){
- this.setData({
- goodsList:res.prodList
- })
- };
- })
- },
- // 获取上新商品列表
- getNewCommodity(){
- API.getMiddleProds().then(res=>{
- if(res){
- this.setData({
- goodsList:res.prodList
- })
- };
- })
- },
- // 获取预售商品列表
- getPresaleCommodity(){
- API.getRightProds().then(res=>{
- if(res){
- this.setData({
- goodsList:res.prodList
- })
- }
- })
- },
- // 获取分类商品
- getClassifyCommodity(){
- let data = {
- ...this.data.filterObj,
- pageNum:this.data.pageNum,
- pageSize:5
- }
- API.getProdsList(data).then(res=>{
- if(res){
- this.setData({
- goodsList:this.data.pageNum === 1?res.mercProdList:[...this.data.goodsList,...res.mercProdList]
- })
- }
- })
- },
- // 添加购物车
- addCart(prod){
- console.log(prod);
- let prodInfo = prod.currentTarget.dataset.prod;
- this.triggerEvent('addCart',prodInfo);
- // let data = {
- // mercId:prodInfo.mercId,
- // prodId:prodInfo.prodId,
- // prodAttrId:prodInfo.prodAttrList[0],
- // count:1
- // }
- // API.addCart(data).then(res=>{
- // console.log(res);
- // })
- },
- // 观察滚动触底事件
- onReachBottom(e){
- this.setData({
- pageNum:this.data.pageNum+1
- });
- this.observerFunction();
- },
- observerFunction(){
- if(this.data.position === 'hot') this.getHotCommodity();
- if(this.data.position === 'new') this.getNewCommodity();
- if(this.data.position === 'presale') this.getPresaleCommodity();
- if(this.data.position === 'sort' || this.data.position === 'index') this.getClassifyCommodity();
- },
- },
- ready: function(){
- this.observerFunction();
- }
- })
|