在做小程序分享時,在上傳之前忘記測試分享功能,因為當時只使用了按鈕分享功能
按鈕分享代碼
<button class="class_guanzhu" data-src="這個是分享圖片地址" data-title="這個是分享標題" open-type="share">發(fā)給好友</button>
<button data-src="這個是分享圖片地址" data-title="這個是分享標題" open-type="share">發(fā)到朋友圈</button>
但是點擊小程序頂部的三點里面的分享
就發(fā)現發(fā)送給朋友與分享到朋友圈全是灰色不可以點擊的。
這是因為你沒有設置相關的配置。需要設置當前頁面的
以下的代碼是uni-app開發(fā)使用的,不是小程序開發(fā)工具使用的,請一定要注意。
//發(fā)送給朋友
onShareAppMessage(res) {
return {
title:this.sharedata.title,
path:this.sharedata.path,
imageUrl:this.sharedata.imageUrl,
desc:this.sharedata.desc,
content:this.sharedata.content,
success(res){
uni.showToast({
title:'分享成功'
})
},
fail(res){
uni.showToast({
title:'分享失敗',
icon:'none'
})
}
}
},
//uniapp微信小程序分享頁面到微信朋友圈
onShareTimeline(res) {
return {
title:this.sharedata.title,
query:'',
imageUrl:this.sharedata.imageurl,
success(res){
uni.showToast({
title:'分享成功'
})
},
fail(res){
uni.showToast({
title:'分享失敗',
icon:'none'
})
}
}
}
請點擊查看更多關于
微信小程序發(fā)送給朋友與分享到朋友圈的代碼
http://www.qinglibike.com/new.asp?id=929
下以提供其它代碼參考:
onLoad(){
wx.showShareMenu({
withShareTicket:true,
//設置下方的Menus菜單,才能夠讓發(fā)送給朋友與分享到朋友圈兩個按鈕可以點擊
menus:["shareAppMessage","shareTimeline"]
})
},
//發(fā)送給朋友
onShareAppMessage(res) {
// 此處的distSource為分享者的部分信息,需要傳遞給其他人
let distSource = uni.getStorageSync('distSource');
if (distSource) {
return {
title: '深山工作室歡迎你',
type: 0,
path: '/pages/index/index',
summary: "",
imageUrl: "這個是分享圖片地址"
}
}
},
//分享到朋友圈
onShareTimeline(res) {
let distSource = uni.getStorageSync('distSource');
if (distSource) {
return {
title: '深山工作室歡迎你',
type: 0,
query: 'id=' + distSource, //這個是參數
summary: "",
imageUrl: "這個是分享圖片地址"
}
}
},
注意事項
分享給朋友圈功能是2020年7月份新增的功能,目前僅支持安卓,IOS不支持
目前是內測階段,部分功能還是有問題的,例如分享到朋友圈功能,其中的query參數,這個參數在onLoad中是獲取不到的,也就是說從此頁面分享出去的,其他人進入此頁面是拿不到分享者的信息的。這樣就不能做分享者與用戶之間的關聯了。