Taro 关于微信订阅消息的调用

文章详细描述了如何在Taro.js应用中使用微信提供的requestSubscribeMessage方法来尝试订阅服务通知,包括获取模板、处理不同环境下的请求以及处理用户设置的检查。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

requestSubscribeMessage 是微信提供的方法
封装的调用requestSubscribeMessage的方法
示例图如下
在这里插入图片描述

import {
   
  getWechatTemplates,
  postSubscribeNotice
} from '@/magic-sdk/apis/wechat-service';
import {
   
  WechatTemplateType,
  SubscribeNoticeObjTypeOptions,
  WechatTemplateEvent
} from '@/magic-sdk/types/wechat-service';
import {
   
  getSetting,
  requestSubscribeMessage /**, showToast */,
  nextTick,
  SubscriptionsSetting
} from '@tarojs/taro';
import Modal from '@/components/Modal/Modal';
import {
    isEmpty } from 'lodash-es';
import {
    captureException } from '../sentry';
import {
    showSubscribeGuidePopup } from './popup';
import {
   } from './platform';

export interface OtherParams {
   
  objValue: string | number | undefined;
}

/**
 * 通过类型从后端获取订阅模板id并尝试订阅
 * @param type
 * @returns
 */
export async function tryWechatSubscribeByType(
  type: WechatTemplateType,
  otherParams: OtherParams = {
   
    objValue: undefined
  },
  successCb?: () => void
): Promise<boolean> {
   
  try {
   
    const {
    data: templateList } = await getWechatTemplates(type);
    const isWeapp = process.env.TARO_ENV === 'weapp';
    if (!templateList || !templateList.length) {
   
      // showToast({
    title: '抱歉,暂时还未开通该订阅消息服务', icon: 'none' });
      return false;
    }
    const templateIds = templateList.map(({ templateId }) => templateId);
    const subscriptionsSetting =
      (await getSubscriptionsSetting()) as SubscriptionsSetting;
    const noSettings =
      isWeapp 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值