微信公众号禁止分享功能

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

创建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)
}

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

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
禁止转载,如需转载请通过简信或评论联系作者。

友情链接更多精彩内容