微信公众号禁止分享功能

最近在项目中遇到一个禁止分享功能的需求,通过这种方法可以解决需求中的问题
##创建notShares .js文件


//获取微信配置

const notShares = {

    getWechatConfNotShare(Noturl) {

      const vm = this;

      if(isWeiChat()){

        var access_token = Cookie.get("access_token");

        console.log(`禁止分享开始-1`,Noturl);

        axios({

            method: 'post',

            url: getSign.url,

            headers: {'Content-Type': 'application/x-www-form-urlencoded'},

            data: {

              url: Noturl,

              access_token:access_token,

            }

          })

          .then(function (response) {

            const data = response.data;

            if (data.status == 1) {

              vm.wechatJsApiNotShare(data.data)

            } else {

              console.log(data.error);

            }

          });

      }

    },

    getWechatConfNotShare_2(Noturl) {

      // // 判断 禁止分享页面

      const vm = this;

      if(isWeiChat()){

        var access_token = Cookie.get("access_token");

        console.log(`关闭禁止分享开始-2`,Noturl);

        axios({

            method: 'post',

            url: getSign.url,

            headers: {'Content-Type': 'application/x-www-form-urlencoded'},

            data: {

              url: Noturl,

              access_token:access_token,

            }

          })

          .then(function (response) {

            const data = response.data;

            if (data.status == 1) {

              vm.wechatJsApiNotShare_2(data.data)

            } else {

              console.log("-----xxxxx------");

            }

          });

      }

    },

    // 将获取到的配置信息注入微信jssdk

    wechatJsApiNotShare_2(conf) {

      wx.config({

        debug: false,  // 值为 true 开启 debug 模式

        appId: conf.appid,  // 公众号的唯一标识

        timestamp: conf.timestamp,  // 签名的时间戳

        nonceStr: conf.nonceStr,  // 签名的随机串

        signature: conf.signature,  // 签名

        jsApiList: [

          'showOptionMenu',

          'getLocation'

        ]  // 必填,需要使用的JS接口列表,所有JS接口列表见附录2

      });

      wx.ready(function () {

        wx.checkJsApi({

          jsApiList: ['showOptionMenu', 'getLocation'],

          success: function (res) {

            // 检测获取的配置是否 OK

          }

        });

        wx.showOptionMenu();

      });

      wx.error(function (res) {

        // alert('wx.error: '+JSON.stringify(res));

      });

    },

    // 将获取到的配置信息注入微信jssdk

    wechatJsApiNotShare(conf) {

      console.log('conf:',conf);

      wx.config({

        debug: false,  // 值为 true 开启 debug 模式

        appId: conf.appid,  // 公众号的唯一标识

        timestamp: conf.timestamp,  // 签名的时间戳

        nonceStr: conf.nonceStr,  // 签名的随机串

        signature: conf.signature,  // 签名

        jsApiList: [

          'hideOptionMenu',

          'getLocation'

        ]  // 必填,需要使用的JS接口列表,所有JS接口列表见附录2

      });

      wx.ready(function () {

        wx.checkJsApi({

          jsApiList: ['hideOptionMenu', 'getLocation'],

          success: function (res) {

            // 检测获取的配置是否 OK

          }

        });

        wx.hideOptionMenu();

      });

      wx.error(function (res) {

        // alert('wx.error: '+JSON.stringify(res));

      });

    },

    getRootPath(){

    //获取当前网址,如: http://localhost:8083/uimcardprj/share/meun.jsp

    var curWwwPath=window.document.location.href;

    //获取主机地址之后的目录,如: uimcardprj/share/meun.jsp

    var pathName=window.document.location.pathname;

    var pos=curWwwPath.indexOf(pathName);

    //获取主机地址,如: http://localhost:8083

    var localhostPaht=curWwwPath.substring(0,pos);

    //获取带"/"的项目名,如:/uimcardprj

    var projectName=pathName.substring(0,pathName.substr(1).indexOf('/')+1);

    return(localhostPaht+projectName);

    },

  };

##哪个页面需要就引入

import notShares from '@/mixins/notShare'
created(){
notShares.getWechatConfNotShare(window.location.href)
}

如有更好的解决方法,欢迎留言。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值