index.js 6.46 KB
import {
  getBindtapData,
  getQueryByUrl
} from '../../utils/util';

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

    if (q) {
      // 设置会员码
      if (m) {
        this.setData({
          memberCode: m
        })
      }
      // 设置积分码
      if (c) {
        this.setData({
          code: c
        })
      }
    }
    this.queryProduct();
    this.queryBanner();

    if (m || c) {
      this.initData();
    }
  },

  onShow() {
    console.log("onShow memberCode:", this.data.memberCode);
    console.log("onShow code:", this.data.code);
  },

  initData() {
    let {
      memberCode,
      code
    } = this.data;
    // 如果存在扫码参数,则需要拉用户身份(未授权的情况下 要拉授权)
    this.queryMember().then((result) => {
      // 扫用户码
      this.queryScanPersonQrcode().then((result2) => {
        // 查询积分码
        if (code) {
          this.querySearchIntegralQrcode();
        }
      });
    });
  },

  // 到达底部
  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 {
        memberCode
      } = this.data;
      if (memberCode) {
        app.post({
          toast: false,
          url: app.api.scanPersonQrcode,
          data: {
            memberCode: memberCode
          }
        }).then((result) => {
          resolve();
        })
      } else {
        resolve();
      }
    });
  },



  /**
   * 请求积分码
   * 通过搜索查询
   */
  querySearchIntegralQrcode() {
    let code = this.data.code;
    this.setData({
      code: ""
    })
    if (code.length != 16) {
      wx.showModal({
        content: '请输入16位积分码',
        showCancel: false,
        success(res) {}
      })
      return;
    }
    app.router.push({
      path: "scanResult",
      query: {
        code: code
      }
    })
  },

  /**
   * 请求积分码
   * 通过扫码
   */
  queryScanIntegralQrcode() {
    let _this = this;
    wx.scanCode({
      onlyFromCamera: true,
      success(res) {
        // 扫码结果
        let q = res.result;
        console.log("q:", q)

        // 获取会员码
        let m = getQueryByUrl("m", q);
        console.log("m:", m)
        if (m) {
          _this.setData({
            memberCode: m
          })
          _this.queryScanPersonQrcode();
        }

        // 获取积分码
        let c = getQueryByUrl("c", q);
        if (c) {
          app.router.push({
            path: "scanResult",
            query: {
              code: c
            }
          })
        } else {
          wx.showModal({
            content: '未发现积分码,换一个二维码试试',
            showCancel: false,
            success(res) {}
          })
        }
      },
      fail(err) {
        let isCancel = err.errMsg.indexOf("cancel") != -1;
        if (!isCancel) {
          wx.showModal({
            content: '未发现积分码,换一个二维码试试',
            showCancel: false,
            success(res) {}
          })
        }
      }
    })
  },


  /**
   * 点击详情
   */
  onProductItemHandler(evt) {
    let curProductDetail = getBindtapData(evt);
    this.setData({
      curProductDetail: curProductDetail,
      productDetailVisible: true
    })
  },

  /**
   * 点击扫码按钮
   */
  onScanHandler() {
    if (!this.data.userInfo.memberCode) {
      this.queryMember().then((result) => {
        this.queryScanIntegralQrcode();
      });
      return;
    } else {
      this.queryScanIntegralQrcode();
    }
  },

  /**
   * 点击查询按钮
   */
  onSearchHandler() {
    if (!this.data.userInfo.memberCode) {
      this.queryMember().then((result) => {
        this.querySearchIntegralQrcode();
      });
    } else {
      this.querySearchIntegralQrcode();
    }
  },


  /**
   * 绑定手输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;
    }
  },
})