prize-detail.js 5.05 KB
import {
  wishbillGiftQuery
} from '../../const/custom-data';

import {
  getBindtapData,
  pxToRpx
} from '../../utils/util';
import Date from '../../utils/date';

import QR from '../../utils/qrcode'


let app = getApp();
Page({
  data: {
    userInfo: {},
    used: false, //优惠券已使用,另外的样式
    wishInfo: {},
    couponInfo: {},
    nearbyStoreVisible: false,
    qrImagePath: "",
    location: {},
  },
  onShareAppMessage() {},
  onLoad(options) {
    // let wishInfo = options;
    let wishInfo = wishbillGiftQuery.order;
    let couponInfo = wishInfo.coupon;

    // 设置起止时间
    couponInfo.startDateStr = new Date(couponInfo.startDate).toString("yyyy.MM.dd");
    couponInfo.endDateStr = new Date(couponInfo.endDate).toString("yyyy.MM.dd");

    // 判断是否使用
    // 自提券 使用状态(0=未使用 1=已过期,2=已使用)
    let used = couponInfo.state != 0;
    // used = true;

    // 设置数据
    this.setData({
      wishInfo: wishInfo,
      couponInfo: couponInfo,
      used: used
    })
    this.initData();
  },
  initData() {
    app.queryIndex().then((result) => {
      this.setData({
        userInfo: app.globalData.userInfo
      })

      // 设置二维码
      let couponInfo = this.data.couponInfo;
      let qrSize = this.setCanvasSize(300);
      console.log("couponInfo:", couponInfo);
      let codeContent = couponInfo.couponCode || '';
      this.createQrCode(codeContent, 'qrcanvas', qrSize.w, qrSize.h);

    })
  },
  /**
   * 请求门店地址
   */
  queryShop() {
    this.selectComponent("#nearbyStoreComp").queryShop().then((result) => {
      this.setData({
        nearbyStoreVisible: true
      })
    })
  },
  // 查看可用门店
  onNearbyStoreHandler(evt) {
    this.wxLocation();
  },
  // 使用微信位置
  wxLocation() {
    let _this = this;
    wx.getLocation({
      type: 'wgs84',
      success(res) {
        _this.setData({
          location: res
        })
        _this.queryShop();
      },
      fail(err) {
        wx.getSetting({
          success: (res) => {
            if (!res.authSetting['scope.userLocation']) {
              // 未授权
              wx.showModal({
                title: '提示',
                content: '小程序请求访问地理位置',
                confirmText: '前往授权',
                success(res) {
                  if (res.confirm) {
                    wx.openSetting({
                      complete() {
                        //设置完后重拉一遍位置,拉不到就算了
                        wx.getLocation({
                          type: 'wgs84', // 默认为 wgs84 返回 gps 坐标,gcj02 返回可用于 wx.openLocation 的坐标
                          success: function (res) {
                            _this.setData({
                              location: res
                            })
                            _this.queryShop();
                          },
                          fail: function () {
                            _this.queryShop();
                          }
                        })
                      }
                    })
                  } else if (res.cancel) {
                    // 模态窗取消
                    _this.queryShop();
                  }
                }
              })
            }
          }
        })
      }
    })
  },
  createQrCode(content, canvasId, cavW, cavH) {
    //调用插件中的draw方法,绘制二维码图片
    QR.api.draw(content, canvasId, cavW, cavH);
    this.canvasToTempImage(canvasId);
  },
  //获取临时缓存图片路径,存入data中
  canvasToTempImage(canvasId) {
    let that = this;
    wx.canvasToTempFilePath({
      canvasId, // 这里canvasId即之前创建的canvas-id
      success: function (res) {
        let tempFilePath = res.tempFilePath;
        console.log(tempFilePath);
        that.setData({ // 如果采用mpvue,即 this.imagePath = tempFilePath
          qrImagePath: tempFilePath,
        });
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  //适配不同屏幕大小的canvas
  setCanvasSize(sz) {
    var size = {};
    try {
      var res = wx.getSystemInfoSync();
      var scale = 750 / sz; //不同屏幕下canvas的适配比例;设计稿是750宽
      var width = res.windowWidth / scale;
      var height = width; //canvas画布为正方形
      size.w = width;
      size.h = height;
    } catch (e) {
      // Do something when catch error
      console.log("获取设备信息失败" + e);
    }
    return size;
  },

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

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

        // 提交完毕
      case "_evt_submit_store_complete":
        // 刷新状态
        this.hideMask();
        wx.showModal({
          content: '预约成功',
          showCancel: false,
          success(res) {}
        });
        break;

      default:
        break;
    }
  },
})