背景分析:
1、微信公眾平台需要先使用模板,不符合要求的就重新申請模板(提交了10余次才通過,要求很嚴格,按照要求修改即可)
2、前端需調起用戶授權(考慮到用戶體驗的問題,用戶沒有授權時會調用起來用戶授權彈框,當用戶授權完成后則無需再彈框提示授權問題)
onShow() { let loginRes = this.checkLogin(); if (!loginRes) { return false; } else { this.is_authorization = uni.getStorageSync('is_authorization'); // 獲取用戶當前訂閱消息的授權狀態 } },
// 開啟訂閱消息設置窗口
openMsg() { var that = this; // 獲取用戶的當前設置,判斷是否點擊了“總是保持以上,不在詢問”
wx.getSetting({ withSubscriptions: true, //是否獲取用戶訂閱消息的訂閱狀態,默認false不返回
success(res) { console.log("訂閱消息的訂閱狀態",res); if(that.is_authorization){ uni.showToast({ title: "您已訂閱商機消息提醒,無需重復訂閱!", icon: "none" }); }else{ if (res.subscriptionsSetting['bjshmNY6Z4R-EM_vunOfa0-YbbbpqDA0huiNy7FQytI'] == "reject") { //用戶點擊了“總是保持以上,不再詢問”
wx.showModal({ title: '溫馨提示', content: "檢測到您沒有開啟訂閱消息的權限,是否去設置?", success: function(modal) { if (modal.confirm) { // 點擊肯定
wx.openSetting({ withSubscriptions: true }); that.is_authorization = true; uni.setStorageSync('is_authorization',true); }else{ uni.showToast({ title:"您已拒絕訂閱消息授權,無法預約領取", icon:"error" }); that.is_authorization = false; } } }) } else { // 用戶沒有點擊“總是保持以上,不再詢問”則每次都會調起訂閱消息 // var templateid = that.setting.templateid.map(item => item.tempid);
let tmpid = ['bjshmNY6Z4R-EM_vunOfa0-YbbbpqDA0huiNy7FQytI']; uni.requestSubscribeMessage({ tmplIds: tmpid, success(res) { if (res[tmpid[0]] == "accept") { uni.showToast({ title: "訂閱成功", icon: "none" }); that.is_authorization = true; uni.setStorageSync('is_authorization',true); }else{ uni.showToast({ title:"您拒絕訂閱消息授權,無法預約領取", icon:"none" }); that.is_authorization = false; } }, fail: (res) => { wx.showModal({ title: '溫馨提示', content: "檢測到您沒有開啟訂閱消息的權限,是否去設置?", success: function(modal) { if (modal.confirm) { // 點擊肯定
wx.openSetting({ withSubscriptions: true }); that.is_authorization = true; uni.setStorageSync('is_authorization',true); }else{ uni.showToast({ title:"您已拒絕訂閱消息授權,無法預約領取", icon:"error" }); that.is_authorization = false; } } }) } }) } } } }) },
注意:由於無法獲取紅框部分的參數,所以無法判斷用戶是否已經授權,所以我就使用了一個參數來判斷 is_authorization 只要用戶點擊允許,這個變量就為true並且立馬存session,有且只有當該變量為true時才表示用戶已經授權了,也就不用再次要求彈框授權 否則就視為沒有授權。(吐槽:當用戶沒有勾選【總是】選項時就沒有itemSetting這個參數,也就無法判斷是否授權了。官方這么明顯的bug為啥一直存在,一點也不考慮用戶體驗的么,醉都醉了,既然推出這個玩意就整得稍微好點不行么,開發人員一直在爬坑!!!yue了)
3、后台使用鏈接 https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=“xxxxx”;; 一定是這個鏈接,否則會一直報錯40081沒有授權!
並且一定要有page 參數: 對應的值是前端頁面的首頁鏈接“pages/index/index”
否則發送的商機信息是沒有進入小程序入口的
正常如下: