WeChat public account jssdk: the permission value is offline verifying

The WeChat official account jssdk reports an error: the permission value is offline verifying , especially when closing the menu. Some pages open the page menu function, and other pages have to be closed again.

First look at the official explanation:

The permission value is offline verifying error is because the config is not executed correctly, or the called JSAPI is not passed to the jsApiList parameter of the config. It is recommended to check in the following order:

1. Confirm that the config is passed correctly.

2. If the JSAPI is called when the page is loaded, it must be written in the callback of wx.ready.

3. Confirm that the jsApiList parameter of config contains this JSAPI.

The translation is:

This error is reported because wx.config is not configured successfully, or there is a problem with jsApiList parameter configuration.

Solution:

1. Check whether the configuration of jssdk is normal (it only needs to have successfully configured items).

2. The configuration of ios in the vue or react packaging project is invalid, and the Android is normal. Can be changed to compatibility mode (see method: getConfigUrl() )

import {
    
     GET } from '@/utils/request';
import wx from 'weixin-js-sdk';

// 获取当前url配置jssdk
function getConfigUrl() {
    
    
    let u = window.navigator.userAgent;
    let isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
    //安卓需要使用当前URL进行微信API注册(即当场调用location.href.split('#')[0])
    //iOS需要使用进入页面的初始URL进行注册,(即在任何pushstate发生前,调用location.href.split('#')[0])
    let url = '';
    if (isiOS) {
    
    
        url = (`${
      
      window.localStorage.getItem('_iosWXConfig_') || window.location.href.split('#')[0]}`);//获取初始化的url相关参数
    } else {
    
    
        url = (window.location.href.split('#')[0]);
    }
    return url;
}

// 配置jssdk
async function setWXJSSDk() {
    
    
    const res = await GET('wechat/getSdkMessage', {
    
    
        url: getConfigUrl()
    });

    // 通过config接口注入权限验证配置
    wx.config({
    
    
        // debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
        appId: res.appId, // 必填,公众号的唯一标识
        timestamp: res.timestamp, // 必填,生成签名的时间戳
        nonceStr: res.noncestr, // 必填,生成签名的随机串
        signature: res.signature,// 必填,签名
        // 必填,需要使用的JS接口列表
        jsApiList: [
            'checkJsApi',
            'updateTimelineShareData',
            'updateAppMessageShareData',
            'onMenuShareTimeline',
            'onMenuShareAppMessage',
            'hideAllNonBaseMenuItem',
            'showAllNonBaseMenuItem'
        ]
    });
    return;
}

// 关闭所有菜单
async function wxHideMenu(item) {
    
    
    if (item.path === '/' || item.path === '/classroomreservation') return; //中转页-分享页面不调用
    await setWXJSSDk()
    wx.ready(() => {
    
    
        wx.hideAllNonBaseMenuItem();
        wx.checkJsApi({
    
    
            jsApiList: [
                'updateTimelineShareData',
                'updateAppMessageShareData',
                'onMenuShareTimeline',
                'onMenuShareAppMessage',
                'hideAllNonBaseMenuItem',
                'showAllNonBaseMenuItem'
            ], // 需要检测的JS接口列表,所有JS接口列表见附录2,
            success: function (res) {
    
    
                console.log(res);
            }
        })
    });
}

// 打开微信菜单分享
async function wxMenuShare(wxShareSet) {
    
    
    // 通过ready接口处理成功验证
    await setWXJSSDk();
    wx.ready(() => {
    
    
        wx.updateTimelineShareData(wxShareSet);
        wx.updateAppMessageShareData(wxShareSet);
        wx.onMenuShareTimeline(wxShareSet);
        wx.onMenuShareAppMessage(wxShareSet);
        wx.showAllNonBaseMenuItem();
    });
}

export {
    
    
    wxHideMenu,
    wxMenuShare,
}

3. Check whether jsApiList is configured with "hideOptionMenu" and "hideMenuItems". If not necessary, you can change it to: "hideAllNonBaseMenuItem" and "showAllNonBaseMenuItem" (experience comes from the actual project stepping on the pit, and there is a problem with repeated closing and opening)

Guess you like

Origin blog.csdn.net/weixin_44461275/article/details/124608679