最近在項(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)
}
如有更好的解決方法,歡迎留言。