生成小程序码

生成固定路由的小程序码

-打开微信公众平台-工具-生成小程序码(根据小程序路由地址生成小程序码)

生成带有随机参数的小程序码

微信小程序-获取接口调用凭据
微信小程序-获取不限制的小程序码

  1. 先获取接口凭证access_token
    -secret:小程序唯一凭证密钥,即 AppSecret,不要轻易重置,后端可以查询或者产品有记录保留。
  //获取access_token
  function geTaccessToken() {
    $.ajax({
      url: 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx2c70be73fc38f158&secret=xxxx,
      type: "get",
      success: function (res) {
        console.log(res, 'access_token')
      },
      error: function () { }
    });
  }
  1. 根据access_token获取小程序码
 //获取小程序码
  function getimg(access_token, id) {
    let params = {
      "page": "pagesNew/NewsDetail/index",
      "scene": "id=" + id,
      "check_path": true,
      "env_version": "release"
    }
    $.ajax({
      url: `https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=${access_token}`,
      type: "post",
      data: params,
      success: function (res) {
        //api拿到的是二进制代码转换成base64 后又放到了七牛云上,从七牛云那边返回来的图片地址在进行展示
        console.log('小程序码', res)
      },
      error: function () { }
    });
  }
1.前端请求报错:

-前端异步请求有可能出现报错,出现的原因可能是因为有频率限制,其他地方也再用这个接口,这样都请求会被限制的,获取小程序码也可以后端接口处理
error.png
2.小程序进行配置发版:

-扫小程序码进入小程序指定页面接受参数的方式和普通的不一样,所以需要在小程序onLoad中配置

 if (options.scene) { 
   const scene = decodeURIComponent(options.scene)                
   this.id = scene.split('=')[1] 
 }
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容