Browse Source

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

panyong 2 years ago
parent
commit
d882d76472

+ 3 - 0
htmldev/shiningWxMini/README.md

@@ -16,3 +16,6 @@
 
 * 商户管理-首页
   - [ ] 我的发布 缺商品主图
+
+* 村长说农货
+  - [ ] 转发数据统计接口

+ 43 - 0
htmldev/shiningWxMini/pages/mine/mine.js

@@ -2,6 +2,7 @@ const uploadJS = require('../../mixin/upload.js')
 const { updateUserInfo, userLoginOut } = require('../../api/common')
 const { sessionStorageKey } = require('../../api/request')
 const app = getApp()
+const tmplIds = 'Js5UzHZ44aqJ3fnyLeHFlP-x71T5Bq5vB_OOV5zUDcg'
 
 Page({
 
@@ -215,5 +216,47 @@ Page({
     this.setData({
       booLock: false
     })
+  },
+  // 参考 https://www.cnblogs.com/onesea/p/15005037.html
+  async handleSubscribeMessage() {
+    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') {
+              // 已被后台封禁
+            }
+          } else {
+            // 当用户没有点击 ’总是保持以上选择,不再询问‘  按钮。那每次执到这都会拉起授权弹窗
+            wx.showModal({
+              title: '提示',
+              content: '请授权开通服务通知',
+              showCancel: true,
+              success: function (res) {
+                if (res.confirm) {
+                  wx.requestSubscribeMessage({
+                    tmplIds: [tmplIds],
+                    success(res) {
+                      console.log(res)
+                    },
+                    fail(er) {
+                      console.log(er)
+                    }
+                  })
+                }
+              }
+            })
+          }
+        } else {
+          // 订阅消息未开启
+        }
+      }
+    } catch (err) {}
   }
 })

+ 25 - 25
htmldev/shiningWxMini/pages/mine/mine.wxml

@@ -41,34 +41,34 @@
     style="visibility: {{form.user_phone ? 'visible' : 'hidden'}};">账号:{{form.user_phone}}
   </view>
   <view class="main">
-    <view
-      class="business-account"
-      wx:if="{{userInfo.shop_status === 1}}">
-      <image src="../../image/mine/bg.png"></image>
-      <view>
-        <view class="describe">已有商家账号</view>
-        <view class="box">
-          <view class="business-account-name-wrap">
-            <view class="business-account-name">{{userInfo.shop_name}}</view>
-            <view class="dashboard">
-              <view>商品数量:{{userInfo.product_total}}</view>
-              <view>预售商品数量:{{userInfo.sale_product_total}}</view>
-            </view>
-          </view>
-          <button
-            class="btn"
-            type="default"
-            data-page="businessHome"
-            bind:tap="jump">商户管理
-          </button>
-        </view>
-      </view>
-    </view>
+    <!--    <view-->
+    <!--      class="business-account"-->
+    <!--      wx:if="{{userInfo.shop_status === 1}}">-->
+    <!--      <image src="../../image/mine/bg.png"></image>-->
+    <!--      <view>-->
+    <!--        <view class="describe">已有商家账号</view>-->
+    <!--        <view class="box">-->
+    <!--          <view class="business-account-name-wrap">-->
+    <!--            <view class="business-account-name">{{userInfo.shop_name}}</view>-->
+    <!--            <view class="dashboard">-->
+    <!--              <view>商品数量:{{userInfo.product_total}}</view>-->
+    <!--              <view>预售商品数量:{{userInfo.sale_product_total}}</view>-->
+    <!--            </view>-->
+    <!--          </view>-->
+    <!--          <button-->
+    <!--            class="btn"-->
+    <!--            type="default"-->
+    <!--            data-page="businessHome"-->
+    <!--            bind:tap="jump">商户管理-->
+    <!--          </button>-->
+    <!--        </view>-->
+    <!--      </view>-->
+    <!--    </view>-->
     <view
       class="list"
-      wx:if="{{userInfo.shop_status !== 1}}"
+      wx:if="{{userInfo.shop_status === 1}}"
       data-page="businessApply"
-      bind:tap="jump">
+      bind:tap="handleSubscribeMessage">
       <image class="label" src="../../image/mine/user.png"></image>
       <text class="value">申请入住(成为农户)</text>
     </view>