zhouzhongyuan / qa

Questions recods
MIT License
5 stars 1 forks source link

微信公众号开发 #4

Closed zhouzhongyuan closed 7 years ago

zhouzhongyuan commented 7 years ago

测试账号地址

开发者工具

在线调试工具

如何获取ACCESS_TOKEN ?

http://jezhang.ngrok.cc/token

zhouzhongyuan commented 7 years ago

自定义菜单

{
    "menu": {
        "button": [
            {
                "type": "view", 
                "name": "芝麻开门吧", 
                "url": "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx739b9f74ab475e99redirect_uri=http%3A%2F%2F192.168.1.106%2F%23%2Fhome%2FReceiptViewresponse_type=codescope=snsapi_userinfostate=STATEconnect_redirect=1#wechat_redirect", 
                "sub_button": [ ]
            }, 
            {
                "name": "菜单", 
                "sub_button": [
                    {
                        "type": "view", 
                        "name": "中原", 
                        "url": "http://blog.zhouzhongyuan.com/", 
                        "sub_button": [ ]
                    }, 
                    {
                        "type": "view", 
                        "name": "视频", 
                        "url": "http://v.qq.com/", 
                        "sub_button": [ ]
                    }, 
                    {
                        "type": "click", 
                        "name": "赞一下我们", 
                        "key": "V1001_GOOD", 
                        "sub_button": [ ]
                    }
                ]
            }
        ]
    }
}

现在的自定义菜单

{
    "button": [
        {
            "type": "view", 
            "name": "芝麻开门吧Alibaba", 
            "url": "http://wechat.zhouzhongyuan.com/map", 
            "sub_button": [ ]
        }
    ]
}
zhouzhongyuan commented 7 years ago

自动回复 接收消息

router.post('/', function (req, res, next) {
    res.writeHead(200, {'Content-Type': 'application/xml'});

    var data = req.body.xml;
    var resMsg = '<xml>' +
        '<ToUserName><![CDATA[' + data.fromusername + ']]></ToUserName>' +
        '<FromUserName><![CDATA[' + data.tousername + ']]></FromUserName>' +
        '<CreateTime>' + parseInt(new Date().valueOf() / 1000) + '</CreateTime>' +
        '<MsgType><![CDATA[text]]></MsgType>' +
        '<Content><![CDATA['+data.content+']]></Content>' +
        '</xml>';
    console.log(resMsg);
    res.end(resMsg);
});

untitled diagram

需要body-parser-xml:

//解析xml
var bodyParser = require('body-parser');
require('body-parser-xml')(bodyParser);

app.use(bodyParser.xml({
  limit: '1MB',   // Reject payload bigger than 1 MB
  xmlParseOptions: {
    normalize: true,     // Trim whitespace inside text nodes
    normalizeTags: true, // Transform tags to lowercase
    explicitArray: false // Only put nodes in array if >1
  }
}));
zhouzhongyuan commented 7 years ago

基础支持: 多媒体文件上传接口 /media/upload 请求地址: http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token=4Y7cMJ8Nm6dkQRxhkvhAJ9pcYggn6nsUYQFnjOvyuOhvwFHu--poLlLYEHmGPfFLN8shyjiCepHAbbnrLcz2_9VMJZ6iWgF-nAmeKXXzsoZ7hsbco6g3UhIXqogIqJWlTYEaABAJOU&type=image 返回结果:

{
    "type": "image", 
    "media_id": "z36yLDpsYmZLgF8SEFDgrL9GXdl3ZRR_4tHLcibosGYD7AE3c7EXLIgY7K9-Jtym", 
    "created_at": 1486522605
}

这是一张黄图

多媒体文件有大小限制。

音乐

Allegro fro

视频

google drive intro xOmvljHGmtue37scDoNXCMHi4GGOW8_HrLAXf1D9juEClsbKDplYacde2zh_ZCMY

图文

注意

上传下载多媒体文件

图片(image): 1M,支持JPG格式 语音(voice):2M,播放长度不超过60s,支持AMR\MP3格式 视频(video):10MB,支持MP4格式 缩略图(thumb):64KB,支持JPG格式 媒体文件在后台保存时间为3天,即3天后media_id失效。

zhouzhongyuan commented 7 years ago

Invalid Signature

微信 JS 接口签名校验工具

  1. url(当前网页的URL,不包含#及其后面部分。注意:对于没有只有域名没有 path 的 URL ,浏览器会自动加上 / 作为 path,如打开 http://qq.com 则获取到的 URL 为 http://qq.com/
  2. 在打开这个页面的时候,出现了如下提示 screenshot_20170208-145941 一开始没有考虑这个问题,每次都是直接点击“直接访问”。后来经过2小时,还是提示invalid Signature,各种验证都验证过了。才想到可能是这句话“非微信官方网页,继续访问将转换成手机预览模式”,“手机预览模式”应该就是说类似浏览器打开这个页面,浏览器打开肯定没法使用微信的JSSDK。后来,把域名改成我自己的域名,没有提示“非微信官方网页,……”,直接就可以了,应该是微信把ngrok.cc加入了黑名单。

微信配置页面的安全域名应该是这样子的

wechat1edited