最近在项目中遇到一个禁止分享功能的需求,通过这种方法可以解决需求中的问题
##创建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)
}
如有更好的解决方法,欢迎留言。