wxSubscribe

论坛示例

为帮助用户更好更快的使用插件,论坛维护了一个示例,示例中包含示例代码、知识点讲解、注意事项等,供您参考。

概述

wxSubscribe 封装了微信一次性订阅sdk,为了不跟wx插件冲突本插件没有单独添加微信sdk,和wx插件使用相同的sdk,so本插件需要配合wx插件使用。监听不到用户是否点了授权还是取消,需要监听resume事件 然后调用服务端发送服务通知,看成功与否便知道用户是否点了授权还是取消。

微信一次性订阅文档地址:

https://developers.weixin.qq.com/doc/oplatform/Mobile_App/One-time_subscription_info.html

subscribe

微信一次性订阅授权

subscribe({appId:'appidxx',templateId:"templateidxx",scene:999,reserved:"helloworld",},function(ret,err))

params

appId

  • 类型:字符串
  • 描述:应用唯一标识,在微信开放平台提交应用审核通过后获得。

templateId

  • 类型:字符串
  • 描述:订阅消息模板ID,在微信开放平台提交应用审核通过后获得。

scene

  • 类型:数值型(0-1)
  • 描述:重定向后会带上scene参数,开发者可以填0-10000 的整形值,用来标识订阅场值。

reserved

  • 类型:字符串
  • 描述:用于保持请求和回调的状态,授权请后原样带回给第三方。该参数可用于防止csrf攻击(跨站请求伪造攻击),建议第三方带上该参数,可设置为简单的随机数加session进行校验,开发者可以填写a-zA-Z0-9的参数值,最多128字节,要求做urlencode。

callback(ret, err)

ret:

  • 类型:JSON对象
  • 内部字段:

    {
        status: true   //布尔型;true||false
    }

err:

  • 类型:JSON对象
  • 内部字段:
    {

        code: 0 //数字类型;
                //错误码:
                //-1(未知错误),
                //0(成功)
    }

示例代码


    openId='';
    appId='wxddxxxxx'
    templateId="oGWCD1LiUgqBjsxxxxx"
    scene=1000,
    reserved="helloworld"
    access_token='';
    apiready = function(){
        api.addEventListener({
            name:'resume'
        }, function(ret, err){     
           setTimeout(function(){if(access_token)postsubscribe();},500);
        });

        var wx = api.require('wx');
        wx.auth({
            apiKey: 'apikeyxx'
        }, function(ret, err) {
            if (ret.status) {
                wx.getToken({
                    code: ret.code
                }, function(ret, err) {
                    if (ret.status) {
                        console.log('getopenid='+JSON.stringify(ret));
                        openId=ret.openId
                        toSubscribe();
                    } 
                });
            }
        });
    }
    
    function toSubscribe(){
           wxSubscribe = api.require('wxSubscribe');
        var data={
            appId:appId,
            templateId:templateId,
            scene:scene,
            reserved:scene,
        }
           acc5WX.subscribe(data, function(ret, err){
            console.log(JSON.stringify(ret));
            getWXToken(ret);//其实这是后端代码调用的事情了,我只是为了测试方便写在了前端,项目发布时需要把这个功能放在后端写,这也是安全问题
        });
    }
   
    function  getWXToken(ret){
       	api.ajax({
            url: 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='+appId+'&secret=xxxxsecretxxxxx',
            method: 'get',
            headers: {
                'Content-Type': 'application/json;charset=utf-8'
            }
        }, function(ret2, err) {
            if (ret) {
                console.log(JSON.stringify(ret2));
                access_token=ret2.access_token
                wxsubmit(ret2.access_token,ret.openid,ret.template_id,ret.scene)
            } 
        });
    }
    function wxsubmit(){
       api.ajax({
            url: 'https://api.weixin.qq.com/cgi-bin/message/template/subscribe?access_token='+access_token,
            method: 'post',
            data: {
                body: {
                      touser: openId,//填接收消息的用户openid
                      template_id: templateId,
                      scene: scene,
                      url:'http://www.xxx.com',//点击消息跳转的链接,需要有ICP备案
                      title: "xxx欢迎订阅xxx",
                      data: {
                        content: {
                          value: "你要对用户说的话",
                          color: "#00ff00"//可以是'red'也可以是'#fff000'
                        }
                      }
                }
            }
        }, function(ret, err) {
            if (ret) {
                console.log('postsubscribe='+JSON.stringify(ret));
            }
        });

可用性

iOS系统,Android系统

可提供的1.0.0及更高版本

是否仍需要帮助? 请保持联络!
最后更新于 2024/04/24