zoukankan      html  css  js  c++  java
  • 微信订阅号

    微信订阅号

    申请订阅号,设置

    后台管理功能

    • 素材管理--群发图文消息

    • 自定义回复

    • 自定义菜单

    • 投票管理

    自定义开发

    • 基本配置

      注意:自定义功能和微信提供的功能只能启用一个

    • node发送http请求,和解析网页

    • 安装发送请求的包

    • 安装解析html的包

    • 练习,解析糗事百科

    const request = require("request");
    const cheerio = require("cheerio");
    
    //发送http请求
    request({
        method: "get",
        url: "http://www.qiushibaike.com",
        headers: {
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36"
        }
    }, (err, response, body) => {
        //解析body
        let $ = cheerio.load(body);
        $("#content-left .content span").each((index, item) => {
            console.log($(item).text());
        })
    })
    
    • 验证微信服务器
        app.get("/wx", (req, res) => {
            //接入验证微信服务器
            var query =  req.query;
            console.log(query.signature);
            console.log(query.echostr);
            console.log(query.timestamp);
            console.log(query.nonce);
            res.send(query.echostr);
        });
    
    • 接收消息,当用户给订阅号发消息,微信服务器会以post的形式把消息以xml的格式,发送给设置的服务器

    • 解析xml的包 xml2js

    • 安装 npm install xml2js --save
    app.post("/wx", (req, res) => {
        //接收post过来的数据,进行处理
        req.on("data", (buffer) => {
            //console.log(buffer.toString());
            //处理xml的包
            const parseString = require("xml2js").parseString;
            parseString(buffer.toString(), (err, result) => {
                console.log(result);
            });
        });
    });
    
    • 发送文本消息
    let touser = result.xml.FromUserName[0];
    let fromuser = result.xml.ToUserName[0];
    
    //回复消息
    if(result.xml.MsgType[0] === "text") {
    
        let txt = "回复你妹";
    
        //回复文本消息
        let msg = `
        <xml>
        <ToUserName><![CDATA[${touser}]]></ToUserName>
        <FromUserName><![CDATA[${fromuser}]]></FromUserName>
        <CreateTime>${Date.now()}</CreateTime>
        <MsgType><![CDATA[text]]></MsgType>
        <Content><![CDATA[${txt}]]></Content>
        </xml>
        `;
    
        res.send(msg);
    }else if(result.xml.MsgType[0] === "location") {
    
        let txt = result.xml.Label[0];
        //回复文本消息
        let msg = `
        <xml>
        <ToUserName><![CDATA[${touser}]]></ToUserName>
        <FromUserName><![CDATA[${fromuser}]]></FromUserName>
        <CreateTime>${Date.now()}</CreateTime>
        <MsgType><![CDATA[text]]></MsgType>
        <Content><![CDATA[${txt}]]></Content>
        </xml>
        `;
    
        res.send(msg);
    }
    
    • 发送图文消息

    设置自定义菜单

    • 获取ACCESS_TOKEN,ACCESS_TOKEN有过期时间(2小时),有每日2000次的限制

        http请求方式: GET
        https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
      
        获取返回值
        {"access_token":"ACCESS_TOKEN","expires_in":7200}
      
      //获取access_token
      const appid = "xxxx";
      const secret = "xxxxx";
      request("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+appid+"&secret="+secret, (err, response, body)=> {
          let json = JSON.parse(body);
          //access_token  -- json.access_token
          //console.log(json.access_token);
      });
      
    • 设置自定义菜单

      //创建自定义菜单
      //获取access_token
      request("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+appid+"&secret="+secret, (err, response, body)=> {
         let json = JSON.parse(body);
         //access_token  -- json.access_token
         //console.log(json.access_token);
      
         //创建自定义菜单
         let data =  {
             "button":[
                 {
                     "type":"click",
                     "name":"今日歌曲",
                     "key":"V1001_TODAY_MUSIC"
                 },
                 {
                     "name":"菜单",
                     "sub_button":[
                         {
                             "type":"view",
                             "name":"搜索",
                             "url":"http://www.soso.com/"
                         },
                         {
                             "type":"view",
                             "name":"视频",
                             "url":"http://v.qq.com/"
                         },
                         {
                             "type":"click",
                             "name":"赞一下我们",
                             "key":"V1001_GOOD"
                         }]
                 }]
         };
      
         let url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" + json.access_token;
      
         request({
             method: "post",
             url: url,
             headers: {
                 "Content-Type": "application/json"
             },
             body: JSON.stringify(data)
         }, (err, response, body) => {
             console.log(body);
         })
      });
      

    自定义分享到朋友圈的标题

    • 获取签名等信息
    //发送http请求,获取access_token
       request("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+appid+"&secret="+secret, (err, response, body)=>{
           let json = JSON.parse(body);
           //access_token  -- json.access_token
           //console.log(json.access_token);
    
    
           //发送请求获取jsticket
           request("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token="+ json.access_token +"&type=jsapi", (err, response, body) => {
               let json = JSON.parse(body);
               //ticket --> json.ticket
    
    
               const sign = require("./sign");
    
               let result = sign(json.ticket, 'http://wx001.ittun.com/take.html');
               console.log(result);
    
           });
       });
    
    • 页面上设置

      • 引入JavaScript
        <script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
      
      • 配置签名信息
      wx.config({
              debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
              appId: 'wxc629aa477087151a', // 必填,公众号的唯一标识
              timestamp: '1479299885', // 必填,生成签名的时间戳
              nonceStr: 'fb6t2dntsxopdvd', // 必填,生成签名的随机串
              signature: '2a45a7c3217cd243c6f4c5ca01c35edc8c832b9b',// 必填,签名,见附录1
              jsApiList: ['checkJsApi',
                  'onMenuShareTimeline',
                  'onMenuShareAppMessage'
                  ] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
          });
      
      • 设置分享的自定义信息
      wx.onMenuShareTimeline({
                 title: '互联网之子', // 分享标题
                 link: 'http://movie.douban.com/subject/25785114/', // 分享链接
                 imgUrl: 'http://demo.open.weixin.qq.com/jssdk/images/p2166127561.jpg', // 分享图标
                 success: function () {
                     // 用户确认分享后执行的回调函数
                     alert("wokao");
                 },
                 cancel: function () {
                     // 用户取消分享后执行的回调函数
                     alert("取消了")
                 }
             })
  • 相关阅读:
    逆元
    C++快读
    最长单调上升子序列(LIS) O(nlogn)求法
    【简●解】巴厘岛的雕塑
    【简●解】学校食堂
    【简●解】[HNOI2005]星际贸易
    差分约束系统小结
    【简•解】花园
    最小生成树小结
    概率及期望DP小结
  • 原文地址:https://www.cnblogs.com/bici/p/6126436.html
Copyright © 2011-2022 走看看