每回遇到微信分享都是一個坑,目前的商城項目使用Vue開發,採用history的路由模式,配置微信分享又遇到了不少問題,最後終於解決了,現將解決的過程分享一下。vue
原文justyeh.top/post/39android
Vue,historyios
debug模式下報falsegit
這個沒得說,就是調用wx.config
方法的參數錯誤形成的,請確認如下事項:github
jsApiList
中decodeURIComponent
debug返回ok,分享不成功web
wx.ready
方法中全部須要使用JS-SDK的頁面必須先注入配置信息,不然將沒法調用(同一個url僅需調用一次,對於變化url的SPA的web app可在每次url變化時進行調用,目前Android微信客戶端不支持pushState的H5新特性,因此使用pushState來實現web app的頁面會致使簽名失敗,此問題會在Android6.2中修復)。vue-router
上面那段話摘自官方文檔npm
開發者須要注意的事項:segmentfault
wx.config
方法,android獲取簽名的url就傳window.location.href
router/index.jsapi
......
import { wechatAuth } from "@/common/wechatConfig.js";
......
const router = new Router({
mode: "history",
base: process.env.BASE_URL,
routes: [
{
path: "/",
name: "home",
meta: {
title: "首頁",
showTabbar: true,
allowShare: true
},
},
{
path: "/cart",
name: "cart",
meta: {
title: "購物車",
showTabbar: true
},
component: () => import("./views/cart/index.vue")
}
......
]
});
router.afterEach((to, from) => {
let authUrl = `${window.location.origin}${to.fullPath}`;
let allowShare = !!to.meta.allowShare;
if (!!window.__wxjs_is_wkwebview) {// IOS
if (window.entryUrl == "" || window.entryUrl == undefined) {
window.entryUrl = authUrl; // 將後面的參數去除
}
wechatAuth(authUrl, "ios", allowShare);
} else {
// 安卓
setTimeout(function () {
wechatAuth(authUrl, "android", allowShare);
}, 500);
}
});
複製代碼
代碼要點:
wechatConfig.js
import http from "../api/http";
import store from "../store/store";
export const wechatAuth = async (authUrl, device, allowShare) => {
let shareConfig = {
title: "xx一站式服務平臺",
desc: "xxxx",
link: allowShare ? authUrl : window.location.origin,
imgUrl: window.location.origin + "/share.png"
};
let authRes = await http.get("/pfront/wxauth/jsconfig", {
params: {
url: decodeURIComponent(device == "ios" ? window.entryUrl : authUrl)
}
});
if (authRes && authRes.code == 101) {
wx.config({
//debug: true,
appId: authRes.data.appId,
timestamp: authRes.data.timestamp,
nonceStr: authRes.data.nonceStr,
signature: authRes.data.signature,
jsApiList: ["updateAppMessageShareData", "updateTimelineShareData", "onMenuShareAppMessage", "onMenuShareTimeline"]
});
wx.ready(() => {
wx.updateAppMessageShareData({
title: shareConfig.title,
desc: shareConfig.desc,
link: shareConfig.link,
imgUrl: shareConfig.imgUrl,
success: function () {//設置成功
//shareSuccessCallback();
}
});
wx.updateTimelineShareData({
title: shareConfig.title,
link: shareConfig.link,
imgUrl: shareConfig.imgUrl,
success: function () {//設置成功
//shareSuccessCallback();
}
});
wx.onMenuShareTimeline({
title: shareConfig.title,
link: shareConfig.link,
imgUrl: shareConfig.imgUrl,
success: function () {
shareSuccessCallback();
}
});
wx.onMenuShareAppMessage({
title: shareConfig.title,
desc: shareConfig.desc,
link: shareConfig.link,
imgUrl: shareConfig.imgUrl,
success: function () {
shareSuccessCallback();
}
});
});
}
};
function shareSuccessCallback() {
if (!store.state.user.uid) {
return false;
}
store.state.cs.stream({
eid: "share",
tpc: "all",
data: {
uid: store.state.user.uid,
truename: store.state.user.truename || ""
}
});
http.get("/pfront/member/share_score", {
params: {
uid: store.state.user.uid
}
});
}
複製代碼
原先計劃不能分享的頁面就使用hideMenuItems方法隱藏掉相關按鈕,在ios下試了一下,有些bug:顯示按鈕的頁面切換的影藏按鈕的頁面,分享按鈕有時依然存在,刷新就沒問題,估計又是一個深坑,沒精力在折騰了,就改成隱私頁面分享到首頁,公共頁面分享原地址,若是有什麼好的解決辦法,請聯繫我!
一開始我有參考sf上的一篇博客segmentfault.com/a/119000001…,按照上面的代碼,android手機都能成功,可是IOS有一個奇怪的問題,就是分享間歇性的失效,同一個頁面,剛剛調起分享成功,再試一次就失敗(沒有圖標、title,只能跳轉到首頁),通過「不斷」努力的嘗試,應該是解決了問題,說一下過程:
一開始覺得是異步喚起沒成功的問題,就和android同樣給IOS調用wechatAuth方法也加了個定時器,測了一遍沒效果,放棄
起始js-sdk是經過npm安裝的,版本上帶了個test,有點不放心,改成直接使用script標籤引用官方的版本
從新讀了一遍文檔,發現onMenuShareTimeline等方法即將廢棄,就把jsApiList改成jsApiList:['updateAppMessageShareData','updateTimelineShareData']
,改後就變成了IOS能夠成功,android分享失敗
百度updateAppMessageShareData安卓失敗緣由,參考這個連接www.jianshu.com/p/1b6e04c29…,把老的api也加到jsApiList中,仔細、反覆試了試兩種設備都ok,好像是成功了,說"好像"是由於內心沒底啊,各類「魔法」代碼!
最後,在這裏但願騰訊官方能不能走點心,更新文檔及時點,demo能不能提供完整點....
參考連接
segmentfault.com/a/119000001… www.jianshu.com/p/1b6e04c29… segmentfault.com/a/119000001… github.com/vuejs/vue-r…