tt.followOfficialAccount
收藏
我的收藏

注意:为了更好的保护小程序内的用户体验,平台将于2024年10月22日起 tt.followAwemeUser增加触发频次的限制,以避免频繁弹窗对用户造成骚扰。请开发者提前对能力调用进行自查和调整,确保小程序业务逻辑稳定查看公告

基础库 1.30.0 开始支持本方法,这是一个异步方法。

关注小程序绑定的头条号,可以通过checkFollowState接口获取当前关注状态。

前提条件
业务背景
使用限制
  • 只能在 bindtap 事件中使用该 api。
  • 在头条小程序中使用该 api。
注意事项
支持沙盒
相关教程

语法

tt.followOfficialAccount(options)

参数说明

options 为 object 类型,属性如下:

属性名类型默认值必填说明最低支持版本
successfunction
接口调用成功的回调函数
1.30.0
failfunction
接口调用失败的回调函数
1.30.0
completefunction
接口调用结束的回调函数(调用成功、失败都会执行)
1.30.0

回调成功

object 类型,属性如下:

属性名类型说明最低支持版本
errMsgstring
followOfficialAccount:ok
1.30.0
errCodenumber
api 调用状态码,如果为 0 表示调用成功
1.30.0

回调失败

object 类型,属性如下:

属性名类型说明最低支持版本
errMsgstring
followOfficialAccount:fail + 错误信息
1.30.0

扫码体验

请使用字节宿主APP扫码

代码示例

开发者工具中预览

<view class="cont">
  <button type="primary" bindtap="click" disabled="{{ isFollowed }}">
    {{ isFollowed ? 'isFollowed' : 'followOfficialAccounts' }}
  </button>
  <text>注:用于关注小程序绑定的头条号</text>
</view>
Page({
  data: {
    isFollowed: false
  },
  click() {
    tt.followOfficialAccount({
      success: res => {
        if (res.errCode === 0) {
          console.log('已关注过');
        } else {
          console.log(res.errMsg);
        }
        this.checkFollowState();
      },
      fail: err => {
        console.log(err)
      },
      complate: res => {
        console.log('已触发关注', res);
      }
    });
  },
  checkFollowState() {
    tt.checkFollowState({
      success: ({ result: isFollowed }) => {
        console.log(isFollowed, 'isFollowed');
        this.setData({
          isFollowed
        });
      },
      fail: ({ errMsg }) => {
        tt.showToast({
          title: errMsg
        });
      },
    });
  }
})
.cont {
  font-size: 25rpx;
  margin: 30rpx;
}

text {
  display: block;
  text-align: center;
  margin-top: 20rpx;
}