第一步,需要先去小程序官方挑選一下訂閱模板拿到模板id
?
訂閱按鈕在頭部導航上,所以
<u-navbar :bgColor="bgColor"><view class="u-nav-slot" slot="left" @click="goSubscribe"><image :src="getimg('subscribe.png')" style="width: 44rpx;height: 44rpx;"></image><view class="le_tt">訂閱</view></view></u-navbar>
?
// 訂閱goSubscribe() {// 獲取時間let time = new Date()let today = time.getDate()let moni1 = time.setDate(today + 1);let yM2 = new Date(moni1).getFullYear() + '-' + (this.checkTime(new Date(moni1).getMonth() + 1)) + '-'let temp2 = yM2 + this.checkTime(new Date(moni1).getDate()) //明天let _this = this;let tempid = ['你的模板id']uni.getSetting({withSubscriptions: true,success(res) {// 通過Object.keys() 獲取返回的數據包含了哪些鍵名,返回的類型是個數組let obj = Object.keys(res.subscriptionsSetting)// mainSwitch 是消息訂閱的總開關if (!res.subscriptionsSetting.mainSwitch) {uni.showToast({title: '請去 "設置-消息訂閱" 開啟消息通知',icon: 'none',duration: 2000});} else {if (obj.includes(tempid[0])) { //判斷是否勾選了“總是保持以上選擇……”addSubscribe({date: temp2}).then(res => {})uni.showToast({title: '您已經設置了總是提醒了喲',icon: 'none',duration: 2000});return} else {_this.isSubscribe = trueuni.requestSubscribeMessage({tmplIds: tempid,success(res) {if (res[tempid[0]] == 'accept') {addSubscribe({date: temp2}).then(res => {uni.showToast({icon: 'none',title: res.msg,duration: 2000});})_this.isSubscribe = false} else {_this.isSubscribe = false}},fail(err) {console.log(err);}})}}}})},