每回遇到微信分享都是一個坑,目前的商城項目使用Vue開發,採用history的路由模式,配置微信分享又遇到了不少問題,最後終於解決了,現將解決的過程分享一下。
原文http://justyeh.top/post/39vue
Vue,historyandroid
debug模式下報falseios
這個沒得說,就是調用wx.config
方法的參數錯誤形成的,請確認如下事項:git
jsApiList
中decodeURIComponent
debug返回ok,分享不成功github
wx.ready
方法中全部須要使用JS-SDK的頁面必須先注入配置信息,不然將沒法調用(同一個url僅需調用一次,對於變化url的SPA的web app可在每次url變化時進行調用,目前Android微信客戶端不支持pushState的H5新特性,因此使用pushState來實現web app的頁面會致使簽名失敗,此問題會在Android6.2中修復)。
上面那段話摘自官方文檔web
開發者須要注意的事項:vue-router
wx.config
方法,android獲取簽名的url就傳window.location.href
router/index.jsnpm
...... 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); } });
代碼要點:segmentfault
wechatConfig.jsapi
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上的一篇博客http://www.javashuo.com/article/p-qrtxmfme-nt.html,按照上面的代碼,android手機都能成功,可是IOS有一個奇怪的問題,就是分享間歇性的失效,同一個頁面,剛剛調起分享成功,再試一次就失敗(沒有圖標、title,只能跳轉到首頁),通過「不斷」努力的嘗試,應該是解決了問題,說一下過程:
jsApiList:['updateAppMessageShareData','updateTimelineShareData']
,改後就變成了IOS能夠成功,android分享失敗最後,在這裏但願騰訊官方能不能走點心,更新文檔及時點,demo能不能提供完整點....
參考連接
http://www.javashuo.com/article/p-qrtxmfme-nt.html
https://www.jianshu.com/p/1b6e04c2944a
http://www.javashuo.com/article/p-wqdbgrwg-gt.html
https://github.com/vuejs/vue-router/issues/481