最近比較流行的答題小程序通常會有這麼一個功能,分享到羣增加答題次數,分享到好友無效,那麼問題來了,我們如何能夠判斷用戶分享到的是羣還是好友呢?
通過查找API文檔我發現會有這個一個東西withShareTicket 通過這個來判斷是否分享到的是羣還是好友,想要獲取到這個需要這onShow方法裏填加一個東西
- //設置分享獲取shareTicket
- wx.showShareMenu({
- withShareTicket: true,
- })
- //分享
- onShareAppMessage: function (res) {
- let that = this
- return {
- title: '分享的標題',
- path: '分享的頁面鏈接',
- success: function (res) {
- //getSystemInfo是爲了獲取當前設備信息,判斷是android還是ios,如果是android
- //還需要調用wx.getShareInfo(),只有當成功回調纔是轉發羣,ios就只需判斷shareTickets
- //獲取用戶設備信息
- wx.getSystemInfo({
- success: function (d) {
- console.log(d);
- //判斷用戶手機是IOS還是Android
- if (d.platform == 'android') {
- wx.getShareInfo({//獲取羣詳細信息
- shareTicket: res.shareTickets,
- success: function (res) {
- //這裏寫你分享到羣之後要做的事情,比如增加次數什麼的
- },
- fail: function (res) {//這個方法就是分享到的是好友,給一個提示
- wx.showModal({
- title: '提示',
- content: '分享好友無效,請分享羣',
- success: function (res) {
- if (res.confirm) {
- console.log('用戶點擊確定')
- } else if (res.cancel) {
- console.log('用戶點擊取消')
- }
- }
- })
- }
- })
- }
- if (d.platform == 'ios') {//如果用戶的設備是IOS
- if (res.shareTickets != undefined) {
- console.log("分享的是羣");
- wx.getShareInfo({
- shareTicket: res.shareTickets,
- success: function (res) {
- //分享到羣之後你要做的事情
- }
- })
- } else {//分享到個人要做的事情,我給的是一個提示
- console.log("分享的是個人");
- wx.showModal({
- title: '提示',
- content: '分享好友無效,請分享羣',
- success: function (res) {
- if (res.confirm) {
- console.log('用戶點擊確定')
- } else if (res.cancel) {
- console.log('用戶點擊取消')
- }
- }
- })
- }
- }
- },
- fail: function (res) {
- }
- })
- }
- }
- },
這裏說一下分享到羣的話,需要把返回的 iv,encryptedData,傳給後臺換取openGId。
微信小程序判斷分享的是羣還是好友
最近比較流行的答題小程序通常會有這麼一個功能,分享到羣增加答題次數,分享到好友無效,那麼問題來了,我們如何能夠判斷用戶分享到的是羣還是好友呢?
通過查找API文檔我發現會有這個一個東西withShareTicket 通過這個來判斷是否分享到的是羣還是好友,想要獲取到這個需要這onShow方法裏填加一個東西
- //設置分享獲取shareTicket
- wx.showShareMenu({
- withShareTicket: true,
- })
- //分享
- onShareAppMessage: function (res) {
- let that = this
- return {
- title: '分享的標題',
- path: '分享的頁面鏈接',
- success: function (res) {
- //getSystemInfo是爲了獲取當前設備信息,判斷是android還是ios,如果是android
- //還需要調用wx.getShareInfo(),只有當成功回調纔是轉發羣,ios就只需判斷shareTickets
- //獲取用戶設備信息
- wx.getSystemInfo({
- success: function (d) {
- console.log(d);
- //判斷用戶手機是IOS還是Android
- if (d.platform == 'android') {
- wx.getShareInfo({//獲取羣詳細信息
- shareTicket: res.shareTickets,
- success: function (res) {
- //這裏寫你分享到羣之後要做的事情,比如增加次數什麼的
- },
- fail: function (res) {//這個方法就是分享到的是好友,給一個提示
- wx.showModal({
- title: '提示',
- content: '分享好友無效,請分享羣',
- success: function (res) {
- if (res.confirm) {
- console.log('用戶點擊確定')
- } else if (res.cancel) {
- console.log('用戶點擊取消')
- }
- }
- })
- }
- })
- }
- if (d.platform == 'ios') {//如果用戶的設備是IOS
- if (res.shareTickets != undefined) {
- console.log("分享的是羣");
- wx.getShareInfo({
- shareTicket: res.shareTickets,
- success: function (res) {
- //分享到羣之後你要做的事情
- }
- })
- } else {//分享到個人要做的事情,我給的是一個提示
- console.log("分享的是個人");
- wx.showModal({
- title: '提示',
- content: '分享好友無效,請分享羣',
- success: function (res) {
- if (res.confirm) {
- console.log('用戶點擊確定')
- } else if (res.cancel) {
- console.log('用戶點擊取消')
- }
- }
- })
- }
- }
- },
- fail: function (res) {
- }
- })
- }
- }
- },
這裏說一下分享到羣的話,需要把返回的 iv,encryptedData,傳給後臺換取openGId(羣ID)。
好了,今天就整理這麼多了,感覺我的判斷邏輯不是很嚴謹,歡迎來指出,大家一塊學習。