微信小程序获取消息订阅状态、订阅消息、绑定小程序

1,056 阅读1分钟
  • 获取订阅状态 wx.getSetting

调用wx.getSetting方法,回调函数中res.subscriptionsSetting会返回订阅状态,若订阅成功,则res.subscriptionsSetting.itemSettings中订阅模板id都为accept状态,reject为拒绝状态

    wx.getSetting({withSubscriptions:true}).then(res=>{
	let bol=false
	if(res.subscriptionsSetting.itemSettings){
            const obj=res.subscriptionsSetting.itemSettings                                         Object.keys(obj).forEach((key)=>{
			if(obj[key]!=='accept'){
                              bol=true
                                }
                            })
			}else{
                              bol=true
			}
	if(bol){
		this.showSubscribe=true
            }
	})
  • 订阅消息 wx.requestSubscribeMessage

调用wx.requestSubscribeMessage方法,templIds配置模板id

   wx.requestSubscribeMessage({
            tmplIds: [
            'eqwoDN2hQ-mKzM6vlfxdad9zEWLdlNCUadKdXItgvaZE', // 模板id
            'GeqwoDN2hQ-mKzM6vlfxdad9zEWLdlNCUadKdXItgvad'
            ],
            success (res) {
                    ...
		}
	})
  • 绑定小程序 wx.login

调用wx.login方法,获取code号传入后端

  wx.login({
	success (res) {
		 if (res.code) {
                    const data={code:res.code}
                    BindingWx(data) // 对应后端接口
		} 
	}
})