index.js 2.35 KB
import {
  getBindtapData
} from '../../utils/util';

let app = getApp();
Page({
  data: {
    authorizeVisible: false,
    productDetailVisible: false,
    bannerList: [], // 轮播图列表
    total: 0,
    page: 1,
    size: 10,
    productList: [], // 产品列表
    curProductDetail: {} // 选中的产品详情
  },
  onShareAppMessage() {},
  showAuth() {
    this.setData({
      authorizeVisible: true
    })
  },
  onLoad(options) {
    this.queryProduct();
    this.queryBanner();
  },
  // 到达底部
  onReachBottom() {
    if (this.data.productList.length < this.data.total) {
      this.setData({
        page: this.data.page + 1
      });
      this.queryProduct();
    }
  },
  // 重置页面列表 点击搜索条件时需要
  resetPage() {
    this.setData({
      page: 1,
      productList: []
    })
  },
  /**
   * 请求产品
   */
  queryProduct() {
    return new Promise((resolve, reject) => {
      app.post({
        sid: false,
        url: app.api.product,
        data: {
          page: this.data.page,
          size: this.data.size,
        },
      }).then((result) => {
        let productList = result.list;
        productList = this.data.productList.concat(productList);
        this.setData({
          productList: productList,
          total: result.total
        })
        resolve();
      })
    });
  },
  /**
   * 请求bannder
   */
  queryBanner() {
    return new Promise((resolve, reject) => {
      app.post({
        sid: false,
        url: app.api.bannerList,
        data: {},
      }).then((result) => {
        this.setData({
          bannerList: result
        })
        resolve();
      })
    });
  },

  /**
   * 点击详情
   */
  onProductItemHandler(evt) {
    let curProductDetail = getBindtapData(evt);
    this.setData({
      curProductDetail: curProductDetail,
      productDetailVisible: true
    })
  },
  /**
   * 点击扫码按钮
   */
  onScanHandler() {
    wx.scanCode({
      onlyFromCamera: true,
      success(res) {
        // 扫码结果
        let result = res.result;
      }
    })
  },
  hideMask() {
    this.setData({
      productDetailVisible: false
    })
  },
  // 子组件事件
  evtcomp(evt) {
    let {
      name,
      data
    } = evt.detail;
    switch (name) {

      case "_evt_hide_product_detail_mask":
        this.hideMask();
        break;

      default:
        break;
    }
  },
})