小程序提供了三种方式实现事件码,具体技术文档见:小程序码

WeiPHP5.0也对应实现了这三种事件码

weiapp_demo代码见qrcode部分

A模式下,只需要根据自己的需求修改path和width参数即可获取二维码的URL,用户此码后就跳转到跳转的path页面

  // 获取A模式下的二维码
  getCodeByA: function () {
    var that = this,
      url = app.url + 'weiapp/Api/getwxacode&PHPSESSID=' + wx.getStorageSync('PHPSESSID');
    wx.request({
      url: url,
      data: { type: 'A', param: { path: 'pages/qrcode/qrcode', width: 400 } },
      success: function (res) {
        if (res.data.status == 0) {
          that.showError(res.data.msg)
        } else {
          that.setData({ 'a_src': res.data.url })
        }
      }
    })
  },

B模式下,只需要根据自己的需求修改scene和width参数即可获取二维码的URL

  // 获取B模式下的二维码
  getCodeByB: function () {
    var that = this,
      url = app.url + 'weiapp/Api/getwxacode&PHPSESSID=' + wx.getStorageSync('PHPSESSID');
    wx.request({
      url: url,
      data: { type: 'B', param: { scene: 'qrcode', width: 400 } },
      success: function (res) {
        if (res.data.status == 0) {
          that.showError(res.data.msg)
        } else {
          that.setData({ 'b_src': res.data.url })
        }
      }
    })

  },

使用B生成的二维码,用户扫码后统一进入小程序首页,如果要跳转到指定页面或者做指定动作,可以在首页加入以下类型的业务判断代码

// 这是首页的 js
Page({
  onLoad: function(options) {
    var scene = options.scene
    if(scene=='qrcode'){
        //跳转到qrcode页面,如果qrcode在底部栏有配置,需要使用wx.switchTab来跳转
        wx.redirectTo({
              url: '/pages/qrcode/qrcode'
       })
    }
  }
})

C模式下,只需要根据自己的需求修改path和width参数即可获取二维码的URL,用户此码后就跳转到跳转的path页面

  // 获取C模式下的二维码
  getCodeByC: function () {
    var that = this,
      url = app.url + 'weiapp/Api/getwxacode&PHPSESSID=' + wx.getStorageSync('PHPSESSID');
    wx.request({
      url: url,
      data: { type: 'C', param: { path: 'pages/qrcode/qrcode', width: 400 } },
      success: function (res) {
        if (res.data.status == 0) {
          that.showError(res.data.msg)
        } else {
          that.setData({ 'c_src': res.data.url })
        }
      }
    })
  },