最近入職的公司主要作微信端的h5,因此在所不免要引用sdk。雖然官方文檔寫的還算清楚,可是仍是有坑。javascript
<script type="text/javascript" src="http://res.wx.qq.com/open/js/jweixin-1.4.0.js"></script>
export default { wxShowMenu: function (that,sign='') { let url = window.location.href.split('#')[0] that.$http.post('/xxx', //請求大家公司後臺的接口 獲取相關的配置 that.$getSingQuery({ appKey: 'xxx', url })) .then(res => { var getMsg = res.data.data; // console.log('微信配置----------') // console.log(res.data) wx.config({ debug: false, //生產環境須要關閉debug模式 測試環境下能夠設置爲true 能夠在開發者工具中查看問題 appId: getMsg.appid, //appId經過微信服務號後臺查看 timestamp: getMsg.timestamp, //生成簽名的時間戳 nonceStr: getMsg.noncestr, //生成簽名的隨機字符串 signature: getMsg.sign, //簽名 jsApiList: [ //須要調用的JS接口列表 'updateAppMessageShareData', //自定義「分享給朋友」及「分享到QQ」按鈕的分享內容(1.4.0) 新接口 'updateTimelineShareData', //自定義「分享到朋友圈」及「分享到QQ空間」按鈕的分享內容(1.4.0) 老接口 'onMenuShareTimeline', //分享到朋友圈 老接口 'onMenuShareAppMessage',//分享給盆友 老接口 'getLocation' //獲取定位 ] }); wx.error(function (res) { // alert(JSON.stringify(res)) console.log(res) // config信息驗證失敗會執行error函數,如簽名過時致使驗證失敗,具體錯誤信息能夠打開config的debug模式查看,也能夠在返回的res參數中查看,對於SPA能夠在這裏更新簽名。 }); wx.ready(function () { if(sign=='location'){ //因爲 獲取定位每每是頁面一加載 就提示獲取地理位置 因此能夠直接在寫在 wx.ready wx.getLocation({ type: 'wgs84', // 默認爲wgs84的gps座標,若是要返回直接給openLocation用的火星座標,可傳入'gcj02' success: function (res) { var latitude = res.latitude; // 緯度,浮點數,範圍爲90 ~ -90 var longitude = res.longitude; // 經度,浮點數,範圍爲180 ~ -180。 var speed = res.speed; // 速度,以米/每秒計 var accuracy = res.accuracy; // 位置精度 that.latitude=res.latitude; that.longitude=res.longitude; that.geocodeRegeo()//逆地理編碼 調用你vue實例裏的方法 do something... } }); } }); }) .catch(error => { alert(error) console.log(error) }) } }
import WXConfig from './assets/js/wx' //微信分享 Vue.prototype.WXConfig = WXConfig
微信JS-SDK說明文檔:同一個url僅需調用一次,對於變化url的SPA的web app可在每次url變化時進行調用。html
因此 咱們在你須要的頁面 mounted
時, this.WXConfig.wxShowMenu(this);
調用就能夠。vue
我這裏將this傳入 只是爲了能直接在wx.js 調用vue上的一些方法。好比axiosjava
mounted: function () { this.WXConfig.wxShare(this); },
經過按鈕自定義觸發ios
html <div class="fxbox bor_b" @click="shareFriend">分享給朋友</div> <div class="fxbox bor_b" @click="shareFriendCircle">分享到朋友圈</div> js shareFriendCircle () { let that = this wx.onMenuShareTimeline({ title: this.dataCode.title, // 分享標題 desc: this.dataCode.desc, //分享描述 link: this.dataCode.link,// 分享連接 imgUrl: this.dataCode.imgUrl, // 分享圖標 success () { console.log('分享給朋友圈 舊') } }); }, // 分享給朋友 舊 shareFriend () { let that = this wx.onMenuShareAppMessage({ title: this.dataCode.title, // 分享標題 desc: this.dataCode.desc, //分享描述 link: this.dataCode.link,// 分享連接 imgUrl: this.dataCode.imgUrl, // 分享圖標 success () { console.log('分享給朋友 舊') } }); },
新接口 'updateAppMessageShareData' //自定義「分享給朋友」及「分享到QQ」按鈕的分享內容(1.4.0) 'updateTimelineShareData',//自定義「分享到朋友圈」及「分享到QQ空間」按鈕的分享內容(1.4.0) 老接口 'onMenuShareTimeline', //分享到朋友圈 'onMenuShareAppMessage',//分享給盆友
注意git
success
回調函數 指的是 你的那些title desc...自定義設置成功了的回調函數,而不是用戶主動點擊微信右上角的三個點,點擊分享給朋友或者朋友圈,分享成功的回調函數。 success
回調函數 是指 用戶成功分享給朋友或者朋友圈的回調函數ios
下表現正常 ,在部分安卓機下失效了建議使用老接口 無此問題web
還有一點, link: this.dataCode.link,// 分享連接
該連接域名或路徑必須與當前頁面對應的公衆號JS安全域名一致算法
分享連接請不要出現奇怪的字符,或者URL編碼一下 好比:|,會致使 連接域名與js安全域名一致 但依然報安全域名的錯誤axios