scan-result.js 3.6 KB
import {
  getBindtapData,
  getQueryByUrl
} from '../../utils/util';

let app = getApp();
Page({
  data: {
    authorizeVisible: false,
    qrCodeCode: "",
    prizeInfo: null, //扫码结果
    userInfo: {}, //用户信息
    scaning: false,
  },
  onShareAppMessage() {},
  showAuth() {
    this.setData({
      authorizeVisible: true
    })
  },
  onShow() {
    wx.setNavigationBarTitle({
      title: '积分获取'
    })
  },
  onLoad(options) {
    // 手输码
    let code = options.code || "";
    if (code) {
      this.setData({
        code: code
      })
    }
    this.initData();
  },

  initData() {
    // 可能要拉授权
    this.queryMember().then((result) => {
      this.queryScanIntegralQrcode().then((result) => {}).catch((err) => {});
    });
  },

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

  /**
   * 扫积分码功能 扫积分码
   */
  queryScanIntegralQrcode(c) {
    return new Promise((resolve, reject) => {
      let {
        code
      } = this.data;
      if (c) code = c;
      if (code) {
        wx.showLoading({
          title: "扫码中",
          mask: true,
        })
        this.setData({
          scaning: true,
        })
        wx.setNavigationBarTitle({
          title: '积分获取'
        })
        app.post({
          url: app.api.scanIntegralQrcode,
          data: {
            qrCodeCode: code
          }
        }).then((result) => {
          wx.hideLoading();
          this.setData({
            prizeInfo: result,
            scaning: false
          })
          if (result.qrCodeStatus != 1) {
            wx.setNavigationBarTitle({
              title: '积分获取失败'
            })
          } else {
            wx.setNavigationBarTitle({
              title: '积分获取成功'
            })
          }
        }).catch((err) => {
          wx.hideLoading();
          this.setData({
            prizeInfo: {},
            scaning: false
          })
          wx.setNavigationBarTitle({
            title: '积分获取失败'
          })
        });
      } else {
        this.setData({
          prizeInfo: {}
        })
        wx.setNavigationBarTitle({
          title: '积分获取失败'
        })
      }
    });
  },


  /**
   * 点击扫码按钮
   */
  onScanHandler() {
    let _this = this;
    wx.scanCode({
      onlyFromCamera: true,
      success(res) {
        // 扫码结果
        let q = res.result;
        // 获取积分码
        let c = getQueryByUrl("c", q);
        if (c) {
          _this.queryScanIntegralQrcode(c);
        } else {
          wx.showModal({
            content: '未发现积分码,换一个二维码试试',
            showCancel: false,
            success(res) {}
          })
        }
      },
      fail(err) {
        console.log("err:", err);
      }
    })
  },

  /**
   * 查看我的积分
   */
  onMyIntegralHandler(evt) {
    app.router.push({
      openType: "redirect",
      path: "integralDetail"
    })
  },

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

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

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

      default:
        break;
    }
  },
})