小程序唤起扫一扫功能

第一步:草料二维码编辑

		草料二维码生成一个对象:如 {code:12345678}。

第二步,点击按钮扫一扫 ,只写相关代码

 getScancode: function() {
    
       //调起扫一扫
    var _this = this;
    wx.scanCode({
    
    
      success: (res) => {
    
    
        let result = JSON.parse(res.result)
        if(result.code){
    
      //是否存在code,自定义的数据
          wx.showLoading({
    
    title: '加载中', icon: 'loading'})
          _this.postCode(result.code)  //这是调取后台方法
        }else{
    
    
          wx.showModal({
    
    
            title: '状态码错误',
            showCancel: false
          })
        }
      }
    })
  },
  // 拿到code,调用后台返回数据
  postCode:function(params){
    
    
    let that = this
    getInformation(params).then(function(res){
    
    
      console.log(res)
      if(res.data.status == 200){
    
    
        let data = res.data.datas
        that.setData({
    
    
         	//取到的值写自己的业务逻辑
        })
        wx.hideLoading()
      }
    })
  },
 

猜你喜欢

转载自blog.csdn.net/weixin_41760500/article/details/107616586