[微信JSSDK] 解决SDK注入权限验证 安卓正常,IOS出现config fail

实测有效 解决微信游览器和企业微信游览器JSSDK注入权限验证 安卓正常,IOS出现config fail

一开始咱们想到的是可能微信这边的Bug,但细想一下应该不是。由于可能涉及到了IOS的底层原理的问题,多是不受微信所控。(有问题欢迎拍砖)php

出现问题得解决问题啊,不能把问题晾在那边无论,这是程序员的尊严!html

我这个是SPA应用,因此拿其中一个vue项目来作探讨,其余SPA应用同理vue


首先咱们想到在安卓中生效,在IOS中不生效是什么缘由?

咱们把全部设置都检查了一遍,最终发现是当前路由location.href不一致的问题程序员

咱们能够去尝试一下去到具体某个页面:浏览器

在Android下微信复制当前连接而后粘贴到输入框里,会发现路由是具体到某个路由。例如:www.xxxx.com/news/xxxx微信

在IOS下微信复制当前连接而后粘贴到输入框里,会发现路由是首页。例如:wwwx.xxxx.com/indexapp

因此问题就定位在了url上,此次我只拿调取扫一扫功能,其他功能自行加上。url

###那咱们只须要判断访问设备是安卓仍是IOS便可spa

首先在index.html页面中引入JSSDK文件.net

而后在App.vue文件中

if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
  const url = location.href
  const res = await getSignature(url) //获取设置config的参数
  let { timestamp, noncestr, signature, appId } = res.data
  wx.config({
    beta: true,
    debug: false,
    appId: appId,
    timestamp: timestamp,
    nonceStr: noncestr,
    signature: signature,
    jsApiList: ['scanQRCode']
  });
  wx.ready(function () {
    console.log('设备已经可使用')
  })
}

具体到某个页面的时候 例如:news下

if (/(Android)/i.test(navigator.userAgent)) {
  let url = location.href
  const res = await getSignature(url) //获取设置config的参数
  let { timestamp, noncestr, signature, appId } = res.data
  wx.config({
    beta: true,
    debug: false,
    appId: appId,
    timestamp: timestamp,
    nonceStr: noncestr,
    signature: signature,
    jsApiList: ['scanQRCode']
  });
  wx.ready(function () {
    console.log('设备已经可使用')
  })
}

仅限于在微信自带的游览器上。企业微信自带的游览器这方法是不行的。

经过微信企业浏览器扫码获取到的微信浏览器信息以下:(图片摘取于CSDN

微信客户端扫码获取到的信息以下:

对比企业微信游览其和微信游览器的信息,多出了wxwork。那么咱们只须要添加多一个判断条件就行了

在App.vue文件中

if (/(wxwork)/i.test(navigator.userAgent)) {
  return
}
if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
  const url = location.href
  const res = await getSignature(url) //获取设置config的参数
  let { timestamp, noncestr, signature, appId } = res.data
  wx.config({
    beta: true,
    debug: false,
    appId: appId,
    timestamp: timestamp,
    nonceStr: noncestr,
    signature: signature,
    jsApiList: ['scanQRCode']
  });
  wx.ready(function () {
    console.log('设备已经可使用')
  })
}

在news文件中

if (/(Android)/i.test(navigator.userAgent) || /(wxwork)/i.test(navigator.userAgent)) {
  let url = location.href
  const res = await getSignature(url) //获取设置config的参数
  let { timestamp, noncestr, signature, appId } = res.data
  wx.config({
    beta: true,
    debug: false,
    appId: appId,
    timestamp: timestamp,
    nonceStr: noncestr,
    signature: signature,
    jsApiList: ['scanQRCode']
  });
  wx.ready(function () {
    console.log('设备已经可使用')
  })
}

以上

相关文章
相关标签/搜索