將下面代碼 放入鏈接要進(jìn)入的頁面 (比如分享頁面/share 分享鏈接為/index, 則將此段代碼放在index 頁面)
index 頁面
在的mounted函數(shù)中
function getQueryString(name) {
//根據(jù)字段看網(wǎng)址是否拼接&字符串
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
var r = window.location.search.substr(1).match(reg);
if (r != null) return unescape(r[2]);
return null;
}
var from = getQueryString("from");
var appinstall = getQueryString("appinstall");
var sec = getQueryString("sec");
var timekey = getQueryString("timekey");
if (from || appinstall || sec || timekey) {
//假如拼接上了
var shareUrl = "http://badminton.mohennet.xyz/index/"
window.location.href=shareUrl;
}
}
在分享頁面引入
import wx from "weixin-js-sdk";
如若沒有稼锅,命令行內(nèi)輸入
npm install weixin-js-sdk --save-dev
share頁面
在mounted函數(shù)中引用一下代碼
WXshare() {
console.log("分享");
var that = this;
var shareUrl = "http://xxxxxx.xyz/index/";
var url=shareUrl.split('#')[0]
var imgUrl = "https://image--ccccc.oss-cn-hangzhou.aliyuncs.com/publish/share.jpg";
(that.cindex = this.$route.query.cindex),
(that.remain = 5 - this.$route.query.cindex);
that.axios.get("http://xxxxx.xyz/sign_data").then(res => {
console.log(res);
var nonceStr = res.data.nonceStr;
var timestamp = res.data.timestamp;
var signature = res.data.signature;
wx.config({
debug: false, // 開啟調(diào)試模式,調(diào)用的所有api的返回值會在客戶端alert出來须蜗,若要查看傳入的參數(shù)芜繁,可以在pc端打開,參數(shù)信息會通過log打出,僅在pc端時才會打印演痒。
appId: "wxceed9c3cd39ce5d7", // 必填始花,公眾號的唯一標(biāo)識
timestamp: timestamp, // 必填妄讯,生成簽名的時間戳
nonceStr: nonceStr, // 必填,生成簽名的隨機(jī)串
signature: signature, // 必填酷宵,簽名亥贸,見附錄1
jsApiList: [
"onMenuShareTimeline",
"onMenuShareAppMessage",
"translateVoice"
] // 必填,需要使用的JS接口列表浇垦,所有JS接口列表見附錄2
});
wx.onMenuShareAppMessage({
title: "挑戰(zhàn)世界冠軍炕置,贏10元話費(fèi)", // 分享標(biāo)題
desc: "比復(fù)興號還快的羽毛球,你能抓住嗎?", // 分享描述
link: url.split('#')[0], // 分享鏈接
imgUrl: imgUrl, // 分享圖標(biāo)
type: "link", // 分享類型,music朴摊、video或link默垄,不填默認(rèn)為link
dataUrl: "", // 如果type是music或video,則要提供數(shù)據(jù)鏈接仍劈,默認(rèn)為空
success: function(res) {
console.log(window.location.href.split('#')[0])
console.log(res);
// 用戶確認(rèn)分享后執(zhí)行的回調(diào)函數(shù)
that.showshare = false;
console.log("分享1");
},
cancel: function() {
console.log("分享2");
// 用戶取消分享后執(zhí)行的回調(diào)函數(shù)
}
});
wx.onMenuShareTimeline({
title: "挑戰(zhàn)世界冠軍厕倍,贏10元話費(fèi)", // 分享標(biāo)題
desc: "比復(fù)興號還快的羽毛球,你能抓住嗎贩疙?", // 分享描述
link: url.split('#')[0],
imgUrl: imgUrl, // 分享圖標(biāo)
success: function() {
console.log(window.location.href.split('#')[0])
console.log("分享1");
// 用戶確認(rèn)分享后執(zhí)行的回調(diào)函數(shù)
that.showshare = false;
},
cancel: function() {
console.log("分享2");
// 用戶取消分享后執(zhí)行的回調(diào)函數(shù)
}
});
});
}