小程序分享鏈接onShareAppMessage的具體用法
onShareAppMessage用法:
只需要在button標簽中加入open-type="share",小程序ui就會自動識別分享鏈接功能
<button data-name="shareBtn" open-type="share">分享</button>
js中代碼如下:
onShareAppMessage: function( options ){
var that = this;
// 設置菜單中的轉發(fā)按鈕觸發(fā)轉發(fā)事件時的轉發(fā)內容
var shareObj = {
title: "轉發(fā)的標題", // 默認是小程序的名稱(可以寫slogan等)
path: '/pages/share/share', // 默認是當前頁面,必須是以‘/'開頭的完整路徑
imageUrl: '', //自定義圖片路徑,可以是本地文件路徑、代碼包文件路徑或者網(wǎng)絡圖片路徑,支持PNG及JPG,不傳入 imageUrl 則使用默認截圖。顯示圖片長寬比是 5:4
success: function(res){
// 轉發(fā)成功之后的回調
if(res.errMsg == 'shareAppMessage:ok'){
}
},
fail: function(){
// 轉發(fā)失敗之后的回調
if(res.errMsg == 'shareAppMessage:fail cancel'){
// 用戶取消轉發(fā)
}else if(res.errMsg == 'shareAppMessage:fail'){
// 轉發(fā)失敗,其中 detail message 為詳細失敗信息
}
},
complete: fucntion(){
// 轉發(fā)結束之后的回調(轉發(fā)成不成功都會執(zhí)行)
}
};
// 來自頁面內的按鈕的轉發(fā)
if( options.from == 'button' ){
var eData = options.target.dataset;
console.log( eData.id); // shareBtn
// 此處可以修改 shareObj 中的內容
shareObj.path = '/pages/goods/goods?goodId='+eData.id;
}
// 返回shareObj
return shareObj;
}
在實際應用中,會碰到這種情況:
微信小程序分享時,需要調用一個ajax(Promise)請求,然后return 一個對象,怎么同步實現(xiàn)?
比如:微信小程序分享時會調用 onShareAppMessage 方法,他會return 一個對象作為分享時的參數(shù)。但是我需要在他return之前調用一個ajax方法getShareCode,怎么樣同步實現(xiàn)?
//將字符串鏈接轉為二維碼,如:轉換前 itemid/344*fromuser/4909*shopid/75,轉換后 KtIQE4j9OP4JNGS2dsZy
getShareCode: function () {
var that = this;
util.request(api.CreateShareCode, {
sharecode: 'itemid/' + that.data.productid + '*fromuser/' + user.getBuyerUserId() + '*shopid/' + that.data.shopId
}).then(function (res) {
if (res.statusCode === 0) {
that.setData({ newShareCode: res.sharedata });
}
});
},
//分享功能
onShareAppMessage: function () {
this.getShareCode();
let that = this;
var newShareCode = that.data.newShareCode;
var shareBackUrl = 'pages/goods/goods?scene=' + newShareCode;
return {
title: that.data.goods.title,
path: shareBackUrl
}
},
嘗試用async await 和 Promise都沒有得到想要的結果。
不能用async await原因是, 如果 onShareAppMessage 是async函數(shù),分享時會調用這個方法,但是分享的事件是走的默認的分享,沒用使用我return的參數(shù)對象。Promise同理。
而且return的對象寫到請求方法里面也會出現(xiàn)上面的問題:方法會被調用,但是分享事件沒有用return的參數(shù)。如下:
//分享功能
onShareAppMessage: function () {
var that = this;
util.request(api.CreateShareCode, {
sharecode: 'itemid/' + that.data.productid + '*fromuser/' + user.getBuyerUserId() + '*shopid/' + that.data.shopId
}).then(function (res) {
if (res.statusCode === 0) {
var newShareCode = res.sharedata;
var shareBackUrl = 'pages/goods/goods?scene=' + newShareCode;
return {
title: that.data.goods.title,
path: shareBackUrl
}
}
});
},
jQuery的ajax請求可以這么設置同步請求:
$.ajaxSetup({
async: false
});
async 方法,別人調用的時候,會立刻返回一個Promise,而return里的path,則是在返回的那個getShareCode里獲取的。微信調用這個方法拿的是返回值,也就是一個Promise,而Promise里沒有他需要的那些參數(shù),所以就是默認的分享了。
換句話說,這個Share回調不允許有異步操作。能改成同步就改,不能改的話,就得改代碼邏輯了。
結果發(fā)現(xiàn)這個Share回調還真不允許有異步操作。
曲線救國的方法就多了,比如:
1、在頁面加載的時候先請求好數(shù)據(jù),存在data里
2、寫個阻塞的函數(shù)
3、封裝自己的分享函數(shù)onShareAppMessage實現(xiàn)分享參數(shù)的動態(tài)獲取
到此這篇關于小程序分享鏈接onShareAppMessage的具體用法的文章就介紹到這了,更多相關小程序分享鏈接onShareAppMessage內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
javascript跨域總結之window.name實現(xiàn)的跨域數(shù)據(jù)傳輸
本文給大家介紹window.name實現(xiàn)的跨域數(shù)據(jù)傳輸,自己親自實踐了一下,真的非常好用,特此分享到腳本之家網(wǎng)站供大家參考2015-11-11

