uniapp subscription information

Table of contents

foreword

renderings

hint

The first step configuration 

The second step code

analyze

 the code

Follow up

Points to note for subscription information

total code

Method 2: Encapsulation


foreword

After the mini program is launched, there is no problem when using it. However, when a certain function of the mini program is completed, there is no prompt, so how will the user know whether the task is completed, so we have to make a WeChat subscription information reminder

renderings

hint

If you call this method frequently, there are two templates on it, and sometimes it will only display one template. If this problem occurs, don’t worry, don’t worry about it, it will be fine after a while

This function is divided into two parts

The first step configuration 

In Function ---> Subscription Information ---> My Template ---> Select 

Long-term subscriptions are different from one-time subscriptions. Long-term subscriptions are limited to service categories; while one-time subscriptions are available for all

schematic diagram

Click to choose

schematic diagram

The second step code

analyze

The event of subscription information is triggered when the user clicks or after the payment is successful .

tmplIds: is the template id

official website

 the code

 subScribeMsg() { // 订阅信息
        uni.getSetting({
          withSubscriptions: true,
          success(res) {
            console.log('1', res.subscriptionsSetting);
            if (!res.subscriptionsSetting.mainSwitch) {
              uni.openSetting({ //打开设置页
                success(res) {
                  console.log(res.authSetting);
                }
              })
            } else {
              uni.requestSubscribeMessage({
                tmplIds: ['UyBPbADlZfsCj89rh_xvfZGlxTW5J5KURpZtt9CNFrY',
                  'BGgZe98QHr0I1S1GrtGps5_rLX6n9cW1AsXhL4YkHHc',
                ],
                success(res) {
                  console.log(res);
                  if (res['BGgZe98QHr0I1S1GrtGps5_rLX6n9cW1AsXhL4YkHHc'] == "accept") { // 用户点击确定后
                    console.log('1111');
                  }
                }
              })
            }
          }
        })
      },

Follow up

Points to note for subscription information

1. Subscription information can only be used in click events or payment callbacks,

But in the click event, it should be noted that the subscription information requestSubscribeMessage must be executed first and then other methods triggered by the click event should be executed, otherwise it will be reported

requestSubscribeMessage:fail can only be invoked by user TAP gesture

Can only be implemented by user clicks

 2. At the beginning, because the judgment condition of uni.getSetting was wrong, it failed to execute the requestSubscribeMessage method.

report

43101 user refuse to accept the msg rid: 5f7184ff-5448020d-6384be15。

Pro-test is effective and can be used directly

total code

 subScribeMsg() {
        var that = this
        uni.getSetting({
          withSubscriptions: true,
          success(res) {
            console.log('1', res, '订阅信息', res.subscriptionsSetting);
            if (!res.subscriptionsSetting.mainSwitch) {
              uni.openSetting({ //打开设置页
                success(res) {
                  console.log('打开设置页', res.authSetting);
                }
              })
            } else {
              uni.requestSubscribeMessage({
                tmplIds: ['UyBPbADlZfsCj89rh_xvfZGlxTW5J5KURpZtt9CNFrY',
                  'BGgZe98QHr0I1S1GrtGps5_rLX6n9cW1AsXhL4YkHHc',
                ],
                success(res) {
                  console.log('requestSubscribeMessage 订阅信息', res);
                  if (res['BGgZe98QHr0I1S1GrtGps5_rLX6n9cW1AsXhL4YkHHc'] == "accept") { // 用户点击确定后
                    console.log('用户订阅点击确定按钮');
                    // that.getSubMsg()
                  } else {
                    console.log('拒绝');
                  }
                },
                fail(errMessage) {
                  console.log("订阅消息 失败 ", errMessage);
                },
                // 不管成功或失败都要执行
                complete() {
                  if (that.ordercode == null) return that.getOrder();
                  if (that.ordercode != null) return that.getPayOrder();
                }
              })
            }
          },
        })
      },

You can put the click event in complete, that.getSubMsg() is to call the back-end interface to control the time for sending service notifications,

In my item click event

  1. First execute the this.subScribeMsg() method, click the requestSubscribeMessage event to display the pop-up window,
  2. Regardless of whether this method succeeds or fails, let it perform the following other requests,
  3. Jump to other pages (intermediate stack pages) after the request is successful
  4. Judge whether the event is successful on the middle stack page, if successful, pass the code, call the backend service notification interface,
  5. The service notification will be sent to WeChat after the event succeeds, and it will be displayed on WeChat

Method 2: Encapsulation

Because the subscription method needs to be used somewhere else in the project, the subscription method is encapsulated

Mine is encapsulated in common → userInfo.js, file

function subScribeMsg() {
  return new Promise((resolve, reject) => {
    uni.getSetting({
      withSubscriptions: true,
      success(res) {
        console.log('1', res, '订阅信息', res.subscriptionsSetting);
        if (!res.subscriptionsSetting.mainSwitch) {
          uni.openSetting({
            success(res) {
              console.log('打开设置页', res.authSetting);
            }
          })
        } else {
          uni.requestSubscribeMessage({
            tmplIds: ['BGgZe98QHr0I1S1GrtGps0y3uhvURtQNkbMAzI2D8g8',
              'UyBPbADlZfsCj89rh_xvfR2ZP1iwtmPcMFA0sUOJwog',
            ],
            success(res) {
              console.log('requestSubscribeMessage 订阅信息', res);
              resolve(res)
              if (res['UyBPbADlZfsCj89rh_xvfR2ZP1iwtmPcMFA0sUOJwog'] == "accept") { // 用户点击确定后
                console.log('用户订阅点击确定按钮');
                // that.getSubMsg()
              } else {
                console.log('拒绝');
              }
            },
            fail(errMessage) {
              reject(errMessage)
              console.log("订阅消息 失败 ", errMessage);
            },
            complete() {
              // if (that.ordercode == null) return that.getOrder(appoint);
              // if (that.ordercode != null) return that.getPayOrder(appoint);
              // that.startDisabled = false
            }
          })
        }
      },
    })
  })
}


module.exports = {
  subScribeMsg
}

 Called in the project

import {
    subScribeMsg
  } from '@/common/userInfo.js'


// 点击时触发订阅信息
async aClick(){

    await subScribeMsg()

     if (this.ordercode == null) return this.getOrder();
     if (this.ordercode != null) return this.getPayOrder();

}

Remarks (Encapsulate the subscription method, use return new Promise, and use await subScribeMsg() in the click trigger subscription information, in order to allow the user to click the cancel or confirm button in the subscription pop-up window to perform the following operations) Otherwise, it will directly perform the following operations

Guess you like

Origin blog.csdn.net/LJM51200/article/details/128575773