zl程序教程

您现在的位置是:首页 >  前端

当前栏目

微信小程序扫码封装

2023-09-27 14:27:34 时间

在app.js中封装

wxScanCode: function () {
  return new Promise((resolve, reject) => {
    wx.scanCode({
      success: resolve,
      fail: reject
    });
  });
},

调用

//引入app.js 
const app = getApp();

//调用
 app.wxScanCode().then(res => {
      if (res.result) {
          wx.setStorage({
            key: constant.scanDeviceId,
            data: res.result,
          });
  	}
 }).catch(e => {
   if (!/cancel/.test(e.errMsg)) {
     wx.showToast({
       title: "扫码解析失败",
       image: "../../images/bell.png",
     })
   }
 });