微信公眾號(hào)禁止分享功能

最近在項(xiàng)目中遇到一個(gè)禁止分享功能的需求,通過這種方法可以解決需求中的問題

創(chuàng)建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(`關(guān)閉禁止分享開始-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,  // 公眾號(hào)的唯一標(biāo)識(shí)

        timestamp: conf.timestamp,  // 簽名的時(shí)間戳

        nonceStr: conf.nonceStr,  // 簽名的隨機(jī)串

        signature: conf.signature,  // 簽名

        jsApiList: [

          'showOptionMenu',

          'getLocation'

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

      });

      wx.ready(function () {

        wx.checkJsApi({

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

          success: function (res) {

            // 檢測(cè)獲取的配置是否 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,  // 公眾號(hào)的唯一標(biāo)識(shí)

        timestamp: conf.timestamp,  // 簽名的時(shí)間戳

        nonceStr: conf.nonceStr,  // 簽名的隨機(jī)串

        signature: conf.signature,  // 簽名

        jsApiList: [

          'hideOptionMenu',

          'getLocation'

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

      });

      wx.ready(function () {

        wx.checkJsApi({

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

          success: function (res) {

            // 檢測(cè)獲取的配置是否 OK

          }

        });

        wx.hideOptionMenu();

      });

      wx.error(function (res) {

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

      });

    },

    getRootPath(){

    //獲取當(dāng)前網(wǎng)址,如: http://localhost:8083/uimcardprj/share/meun.jsp

    var curWwwPath=window.document.location.href;

    //獲取主機(jī)地址之后的目錄,如: uimcardprj/share/meun.jsp

    var pathName=window.document.location.pathname;

    var pos=curWwwPath.indexOf(pathName);

    //獲取主機(jī)地址,如: http://localhost:8083

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

    //獲取帶"/"的項(xiàng)目名,如:/uimcardprj

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

    return(localhostPaht+projectName);

    },

  };

哪個(gè)頁面需要就引入

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

如有更好的解決方法,歡迎留言。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
禁止轉(zhuǎn)載,如需轉(zhuǎn)載請(qǐng)通過簡(jiǎn)信或評(píng)論聯(lián)系作者。

友情鏈接更多精彩內(nèi)容