uniapp develops WeChat public account H5 to hide the extension button in the upper right corner

uniapp develops WeChat public account H5 to hide the extension button in the upper right corner

1. First use npm to install WeChat’s jsApi (you should be able to do this, just like vue uses npm to install dependencies)

初始化:
npm init -y
安装微信模块:
npm install jweixin-module --save



H5的话 要引入js
 <script src="http://res2.wx.qq.com/open/js/jweixin-1.4.0.js" type="text/javascript" charset="utf-8"></script>

2. Create a new wxApi.js file under common of uniapp

/*
    微信(公众号)支付方法
*/
const wx = require('jweixin-module');

const wexinHide = (data) => {
    
    
	let [appId, timestamp, nonceStr, signature] = [data.appId, data.timestamp, data.nonceStr, data.signature];
	wx.config({
    
    
		debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
		appId, // 必填,公众号的唯一标识
		timestamp, // 必填,生成签名的时间戳
		nonceStr, // 必填,生成签名的随机串
		signature, // 必填,签名,见附录1
		jsApiList: ["hideOptionMenu"] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
	});
	
	wx.ready(function(){
    
    
	  // config信息验证后会执行ready方法,所有接口调用都必须在config接口获得结果之后,config是一个客户端的异步操作,所以如果需要在页面加载时就调用相关接口,则须把相关接口放在ready函数中调用来确保正确执行。对于用户触发时才调用的接口,则可以直接调用,不需要放在ready函数中。
		wx.hideOptionMenu(); //wx.hideAllNonBaseMenuItem() 和 wx.hideMenuItems()方法不起作用

	});
	wx.error(function(res){
    
    
		console.log("信息验证失败",res);
	  // config信息验证失败会执行error函数,如签名过期导致验证失败,具体错误信息可以打开config的debug模式查看,也可以在返回的res参数中查看,对于SPA可以在这里更新签名。
	});

}

export default {
    
    
	wexinHide,
}

3. Import and mount it in main.js (must use conditional compilation, otherwise it will report 'jweixin' of undefined when packaging the APP or running it on the mobile phone base, causing a white screen on the mobile phone)

// #ifdef H5
//导入 //(一定要用条件编译,否则在打包APP或在手机基座运行时会报 ‘jweixin’ of undefined,造成手机白屏)
import wxApi from './common/wxApi.js'
//挂载
Vue.prototype.$wxHide = wxApi.wexinHide
// #endif

4. Call the hide upper right corner function index.vue on the page

onLoad(param) {
    
    
    const that = this;
    // #ifdef H5
        // 隐藏右上角按钮功能
        let url = that.$http.baseUrl2+window.location.pathname;
        console.log("当前页面完整路径",url);
       	//  api/user/signature 接口用于获取wx.comfig验证信息,url为当前页面的完整路径,必传
        that.$http.get('api/user/signature',{
    
    url:url}).then(res=>{
    
    
            console.log("隐藏验证",res);
            let data1 = res.data;
            if (res.status_code == 200) {
    
    
                that.$wxHide(data1)
            } else {
    
    
                that.$tool.toast(res.message)
            }
        })
    // #endif
}

Guess you like

Origin blog.csdn.net/m0_52459016/article/details/122089441
Recommended