最近入職的公司主要做微信端的h5,所以在所難免要引用sdk。雖然官方文檔寫的還算清楚,但是還是有坑。
1.在index.html中 引入微信sdk
<script type="text/javascript" src="http://res.wx.qq.com/open/js/jweixin-1.4.0.js"></script>
2.在assets/js 下新建文件 wx.js
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)
})
}
}
3.在main.js 將WXConfig綁在vue原型上 這樣哪個頁面需要初始化 直接通過原型就可以拿到
import WXConfig from './assets/js/wx' //微信分享
Vue.prototype.WXConfig = WXConfig
4.在需要的頁面 進行初始化
微信JS-SDK說明文檔:同一個url僅需調用一次,對於變化url的SPA的web app可在每次url變化時進行調用。
所以 我們在你需要的頁面 mounted
時, this.WXConfig.wxShowMenu(this);
調用就可以。
我這裏將this傳入 只是爲了能直接在wx.js 調用vue上的一些方法。比如axios
mounted: function () {
this.WXConfig.wxShare(this);
},
通過按鈕自定義觸發
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('分享給朋友 舊')
}
});
},
5.新老接口的區別
新接口
'updateAppMessageShareData' //自定義“分享給朋友”及“分享到QQ”按鈕的分享內容(1.4.0)
'updateTimelineShareData',//自定義“分享到朋友圈”及“分享到QQ空間”按鈕的分享內容(1.4.0)
老接口
'onMenuShareTimeline', //分享到朋友圈
'onMenuShareAppMessage',//分享給盆友
注意
- 新接口中的
success
回調函數 指的是 你的那些title desc...自定義設置成功了的回調函數,而不是用戶主動點擊微信右上角的三個點,點擊分享給朋友或者朋友圈,分享成功的回調函數。 - 老接口
success
回調函數 是指 用戶成功分享給朋友或者朋友圈的回調函數 - 經測試 使用新接口 在
ios
下表現正常 ,在部分安卓機下失效了
建議使用老接口 無此問題
6.補充
還有一點, link: this.dataCode.link,// 分享鏈接
該鏈接域名或路徑必須與當前頁面對應的公衆號JS安全域名一致
分享鏈接請不要出現奇怪的字符,或者URL編碼一下 比如:|,會導致 鏈接域名與js安全域名一致 但依然報安全域名的錯誤
7.掃一掃
往期文章
- 數據結構與算法-LeetCode 格雷編碼(No.89)
- [數據結構與算法-LeetCode 種花問題(No.605)