index.vue 4.21 KB
Newer Older
李嘉林 committed
1 2 3 4 5 6 7 8 9 10 11
// 会员码
<template>
  <div class="memberCode">
    <div class="main">
      <div class="title flex">
        <i class="iconfont icon-saomiaochenggong"></i>
        <p>向商家展示会员码</p>
      </div>
      <div class="codeMain">
        <!-- 条形码 -->
        <div class="barCode">
李嘉林 committed
12
          <canvas canvas-id="barcode" style="width:600rpx;height:180rpx;"/>
李嘉林 committed
13 14
        </div>
        <!-- 二维码 -->
李嘉林 committed
15 16 17 18 19 20
        <div class="qrCode flex">
          <canvas
            class="canvas-code"
            canvas-id="myQrcode"
            :style="{ background: '#fff', width: '308rpx', height: '308rpx' }"
          />
李嘉林 committed
21 22 23 24 25 26 27
        </div>
      </div>
    </div>
  </div>
</template>

<script type="text/ecmascript-6">
李嘉林 committed
28 29 30 31
import QRCode from "@/utils/weapp-qrcode.js";
import wxbarcode from "wxbarcode"
const app = getApp();
const { log } = app;
李嘉林 committed
32 33 34 35 36 37
export default {
  name: "memberCode",
  data() {
    return {
      mobilephone: "",
      getStep: 0,
李嘉林 committed
38 39
      currentBrightness: 0,
      qrCodeImg: "",
李嘉林 committed
40 41 42 43
    };
  },
  components: {},
  computed: {},
李嘉林 committed
44 45 46 47 48 49
  created() {
    console.log("进入created")
  },
  onLoad() {
    console.log("进入onLoad")
  },
李嘉林 committed
50 51 52 53 54 55 56 57 58 59 60 61
  onShow() {
    let _this = this;
    wx.getScreenBrightness({
      success: (res) => {
        _this.currentBrightness = res.value;
      },
      fail: (res) => {
        console.log(res, "-------getScreenBrightness--fail");
      },
    });
    console.log(this.currentBrightness, "---currentBrightness");
    console.log("---onShow");
李嘉林 committed
62
    this.getSync();
李嘉林 committed
63 64 65 66 67 68 69
    this.setScreenBrightness(1);
  },
  onHide() {
    this.setScreenBrightness(this.currentBrightness);
  },
  onUnload() {
    this.setScreenBrightness(this.currentBrightness);
李嘉林 committed
70 71 72 73
  },
  methods: {
    // 获取缓存用户信息
    getSync() {
李嘉林 committed
74
      log.info(this.getStep,'getSync')
李嘉林 committed
75
      if (this.getStep > 10) return;
李嘉林 committed
76
      this.getStep++;
李嘉林 committed
77
      let loginUserInfo = wx.getStorageSync("loginUserInfo") || null;
李嘉林 committed
78 79 80
      log.info(this.mobilephone,loginUserInfo,'loginUserInfo')
      console.log(this.mobilephone,loginUserInfo,'loginUserInfo')
      if (loginUserInfo && loginUserInfo.mobilephone) {
李嘉林 committed
81
        this.mobilephone = loginUserInfo.mobilephone;
李嘉林 committed
82 83 84 85 86 87 88 89 90
        this.getStep = 0;
        this.init();
      } else {
        setTimeout(() => {
          this.getSync();
        }, 200);
      }
    },
    init() {
李嘉林 committed
91
      console.log("开始初始化");
李嘉林 committed
92
      log.info('开始初始化生成码')
李嘉林 committed
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
      this.getBarCode();
      this.getQrCode();
    },
    getBarCode() {
      wxbarcode.barcode("barcode", this.mobilephone, 600, 180);
    },
    getQrCode() {
      let _this = this;
      const systemInfo = wx.getSystemInfoSync();
      const width = (154 * systemInfo.windowWidth) / 375;
      const height = width;
      new QRCode("myQrcode", {
        text: _this.mobilephone,
        width,
        height,
        padding: 4, // 生成二维码四周自动留边宽度,不传入默认为0
        correctLevel: QRCode.CorrectLevel.L, // 二维码可辨识度
        callback: (res) => {
          console.log(res.path, "---res.path");
          _this.qrCodeImg = res.path;
          // 接下来就可以直接调用微信小程序的api保存到本地或者将这张二维码直接画在海报上面去,看各自需求
        },
      });
    },
    setScreenBrightness(val = 0.7) {
      if (wx.setScreenBrightness) {
        //设置屏幕亮度  参数值:0-1,越大越亮
        wx.setScreenBrightness({
          value: val,
        });
      } else {
        console.log("------微信版本过低------");
      }
    },
李嘉林 committed
127 128 129 130 131 132 133 134
  },
};
</script>

<style lang="scss" scoped>
.memberCode {
  width: 100vw;
  height: 100vh;
李嘉林 committed
135
  background: #f3f3f3;
李嘉林 committed
136
  overflow: auto;
李嘉林 committed
137
  .main {
李嘉林 committed
138 139
    margin: 20px 12px;
    background: #fff;
李嘉林 committed
140 141
    border-radius: 4px;
    overflow: hidden;
李嘉林 committed
142
  }
李嘉林 committed
143
  .title {
李嘉林 committed
144 145 146
    align-items: center;
    padding: 16px 20px;
    color: #666;
李嘉林 committed
147
    background: #fafafa;
李嘉林 committed
148
    font-size: 14px;
李嘉林 committed
149
    .iconfont {
李嘉林 committed
150 151 152 153 154
      font-size: 12px;
      color: #333;
      margin-right: 10px;
    }
  }
李嘉林 committed
155 156
  .codeMain {
    padding: 20px 26px 60px;
李嘉林 committed
157
    background: #fff;
李嘉林 committed
158 159 160 161 162 163 164 165
    .barCode {

    }
    .qrCode{
      margin: 28px auto 0;
      width: 308rpx;
      height: 308rpx;
      border: 1px solid #F3F3F3;
李嘉林 committed
166
      padding: 16px;
李嘉林 committed
167 168 169
      justify-content: center;
      align-items: center;
    }
李嘉林 committed
170 171 172
  }
}
</style>