禁用或隐藏微信公众号和小程序页面分享按钮

1,712 阅读1分钟

1.小程序项目页面禁用分享功能

只需要调用Api即可;在需要禁用分享的页面生命周期函数--监听页面加载中添加wx.hideShareMenu()即可。

onLoad: function (options) {
    // 隐藏分享
    wx.hideShareMenu()
   }

2.基于vue开发的微信公众号页面禁用分享功能,即隐藏分享按钮;

2.1 封装一个禁用分享的js文件wx_hideShare.js

import axios from './axios';//引入axios.js文件
import wx from 'weixin-js-sdk';//引入微信sdk
/* 微信自定义禁用分享 */
export const WeChatHideShare = (url) => {
  if (url.includes('#')) {
    url = url.split('#')[0];
  }

  // 获取时间戳(调后端接口)
  axios
    .get(
      '/SDSmart/wechatShare/getsignature',
      {
        params: { url: decodeURIComponent(url) },
      }
      //   headers
    )
    .then((res) => {
      //   console.log(res.data);
      let { timestamp, noncestr, signature } = res.data;

      wx.config({
        debug: false,
        // debug: true,
        appId: 'wx0xxxxxxxvghvgyvgy'//微信公众号的appId
        timestamp: timestamp, //必填,生成签名的时间戳
        nonceStr: noncestr, // 必填,生成签名的随机串
        signature: signature, // 必填,签名
        jsApiList: ['hideMenuItems', 'hideOptionMenu'], // 必填,需要使用的JS接口列表
      });
      // 检测
      wx.ready(() => {
        //根据wx.config的配置设置隐藏项目
        wx.hideMenuItems({
          menuList: ['menuItem:share:appMessage', 'menuItem:share:timeline'],
        });

        //根据wx.config的配置隐藏所有
        wx.hideOptionMenu();
      });
    })
    .catch((err) => {
      console.log('JSSDK share error:', err);
    });
};

2.2 在需要禁用分享功能的页面引入封装的wx_hideShare.js,在created里面调用即可。

import { WeChatHideShare } from '@/utils/wx_hideShare';
created() {
    // 微信禁用分享;
    let authUrl = window.location.href;
    setTimeout(() => {
      WeChatHideShare(authUrl);
    }, 400);
  },