程序小哥參與的原生小程序,目前累計用戶240W,在這裏整理了一下期間遇到的坑,也給其餘朋友一些經驗。html
微信公衆平臺前往進行註冊,生成appid和app secrect, appid在登陸微信開發者工具時須要。json
注意:canvas
直接查看微信文檔小程序
onShareAppMessage() {
return {
title,
summary,
imageUrl,
path,
success
}
}
複製代碼
<button open-type="share">分享</button>
wx.saveImageToPhotosAlbum({
filePath,
success: s => {
wx.showToast({
title: '已保存圖片',
icon: 'success'
})
},
fail: f => {
console.log('f', f)
}
})
複製代碼
onLoad(e) {
wx.showShareMenu({
withShareTicket: true
})
}
複製代碼
在打開分享頁面的用戶,經過啓動小程序的場景 scene 值爲1044
打開頁面,會在頁面加載帶上 shareTicket 參數。
注意:因爲小程序生命週期的關係,根據業務不一樣狀況來判斷場景值。後端
App({
// 每次進入就觸發
onShow(e) {
// 分享羣的場景標識
if (e.scene === 1044 && e.shareTicket) {
this.globalData.shareTicket = e.shareTicket
}
// 分享我的的場景標識
if (e.scene === 1007) {
this.globalData.shareTicket = ''
}
}
})
複製代碼
在經過 wx.getShareInfo(Object object)方法, 後端解密,返回一個 openGIdbash
export function fetchGroupId(shareTicket) {
let { appName } = getApp().globalData
return new Promise((resolve, reject) => {
wx.getShareInfo({
shareTicket,
complete(shareKey) {
const data = {
sessionKey: getApp().globalData.sessionKey
}
delete shareKey.errMsg
Object.assign(data, shareKey)
request({
url,
method: 'POST',
data
}).then(r => {
if (r.code) {
// 解密失敗從新登陸從新解密
login(appName).then(() => {
fetchGroupId(shareTicket).then(openGId => resolve(openGId))
})
} else {
resolve(r.data.openGId)
}
})
}
})
})
}
複製代碼
經過微信控件顯示羣名服務器
<open-data type="groupName" open-gid="{{ groupId }}" />
複製代碼