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

let app = getApp();
Page({
  data: {
    authorizeVisible: false,
    productDetailVisible: false,
    bannerList: [], // 轮播图列表
    total: 0,
    page: 1,
    size: 10,
    productList: [], // 产品列表
    curProductDetail: {}, // 选中的产品详情
    qrCodeCode: "",
    userInfo: {}, //用户信息
    code: "", // 手输入码
  },
  onShareAppMessage() {},
  showAuth() {
    this.setData({
      authorizeVisible: true
    })
  },
  onLoad(options) {

    this.queryProduct();
    this.queryBanner();

    let scene = decodeURIComponent(options.scene) || "";
    // scene = "ab4cbd54dcf04d29a6c23d00a900b332"
    if (scene) {
      this.setData({
        scene
      })
    }
    this.initData();
  },

  initData() {
    let {
      scene
    } = this.data;
    if (scene) {
      // 可能要拉授权
      this.queryMember().then((result) => {
        this.queryScanPersonQrcode().then((result) => {}).catch((err) => {});
      });
    }
  },


  // 到达底部
  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();
      })
    });
  },

  /**
   * 获取会员信息
   */
  queryMember() {
    return new Promise((resolve, reject) => {
      app.post({
        url: app.api.member,
        data: {}
      }).then((result) => {
        this.setData({
          userInfo: result
        })
        resolve();
      })
    });
  },

  /**
   * 扫个人码
   */
  queryScanPersonQrcode() {
    return new Promise((resolve, reject) => {
      let {
        scene
      } = this.data;
      if (scene) {
        app.post({
          toast: false,
          url: app.api.scanPersonQrcode,
          data: {
            memberCode: scene
          }
        }).then((result) => {
          this.setData({
            prizeInfo: result
          })
        })
      }
    });
  },

  /**
   * 点击详情
   */
  onProductItemHandler(evt) {
    let curProductDetail = getBindtapData(evt);
    this.setData({
      curProductDetail: curProductDetail,
      productDetailVisible: true
    })
  },
  /**
   * 点击扫码按钮
   */
  onScanHandler() {
    wx.scanCode({
      onlyFromCamera: true,
      success(res) {
        // 扫码结果
        let result = res.result;
        let {
          qrCodeCode,
          memberCode
        } = result;
      },
      fail(err) {
        console.log("err:", err);
      }
    })
  },

  /**
   * 点击搜索
   */
  onSearchHandler() {
    app.router.push({
      path: "scanResult",
      query: {
        code: this.data.code
      }
    })
  },

  /**
   * 绑定手输code
   * @param {*} e
   */
  bindCodeInput(e) {
    this.setData({
      code: e.detail.value
    })
  },

  hideMask() {
    this.setData({
      productDetailVisible: false,
      authorizeVisible: false,
    })
  },
  // 子组件事件
  evtcomp(evt) {
    let {
      name,
      data
    } = evt.detail;
    switch (name) {

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

        // 授权完毕
      case "_evt_auth_complete":
        this.initData();
        this.hideMask();
        break;

      default:
        break;
    }
  },
})