Przeglądaj źródła

始宁农业小程序:订阅消息

panyong 2 lat temu
rodzic
commit
321cced5c9

+ 50 - 2
htmldev/shiningWxMini/pages/home/home.js

@@ -2,6 +2,7 @@ const pages = require('../../mixin/pages')
 const { getProductCategoryList } = require('../../api/common')
 const { getLunboList } = require('./api/index')
 const app = getApp()
+const tmplIds = '1kIBK-L1ksxYCxY7Pmezhp4FSS-BOqiIAl6K7PAGvDE'
 let leftHeight = 0
 let rightHeight = 0
 let query = wx.createSelectorQuery()
@@ -229,8 +230,7 @@ Page({
       }
     }
   },
-  handleNav(e) {
-    const { item } = e.currentTarget.dataset
+  handleNav(item) {
     if (Object.prototype.toString.call(item) === '[object Object]' && item.path) {
       const path = item.path
 
@@ -251,5 +251,53 @@ Page({
     wx.navigateTo({
       url: `/pages/${page}/${page}`
     })
+  },
+  async handleSubscribeMessage(e) {
+    const { item } = e.currentTarget.dataset
+    const that = this
+
+    try {
+      const { errMsg, subscriptionsSetting } = await wx.getSetting({ withSubscriptions: true })
+      if (errMsg === 'getSetting:ok') {
+        // 用户打开了订阅消息总开关
+        if (subscriptionsSetting.mainSwitch) {
+          // 用户同意总是保持是否推送消息的选择, 这里表示以后不会再拉起推送消息的授权
+          if (subscriptionsSetting.itemSettings != null) {
+            const moIdState = subscriptionsSetting.itemSettings[tmplIds]
+            if (moIdState === 'accept') {
+              // 接受了消息推送
+            } else if (moIdState === 'reject') {
+              // 拒绝消息推送
+            } else if (moIdState === 'ban') {
+              // 已被后台封禁
+            }
+
+            that.handleNav(item)
+          } else {
+            // 当用户没有点击 ’总是保持以上选择,不再询问‘  按钮。那每次执到这都会拉起授权弹窗
+            wx.showModal({
+              title: '提示',
+              content: '请授权开通服务通知',
+              showCancel: true,
+              success: function (res) {
+                if (res.confirm) {
+                  wx.requestSubscribeMessage({
+                    tmplIds: [tmplIds],
+                    success(res) {
+                      that.handleNav(item)
+                    },
+                    fail(err) {
+                      that.handleNav(item)
+                    }
+                  })
+                }
+              }
+            })
+          }
+        } else {
+          // 订阅消息未开启
+        }
+      }
+    } catch (err) {}
   }
 })

+ 1 - 1
htmldev/shiningWxMini/pages/home/home.wxml

@@ -61,7 +61,7 @@
       wx:for="{{headerNav}}"
       wx:key="index"
       data-item="{{item}}"
-      bind:tap="handleNav">
+      bind:tap="handleSubscribeMessage">
       <image src="../../image/home/{{item.icon}}"></image>
       <text>{{item.name}}</text>
     </view>