美文网首页
微信公号开发之:thinkjs踩坑checkSignature

微信公号开发之:thinkjs踩坑checkSignature

作者: RU12_ | 来源:发表于2020-08-05 20:45 被阅读0次

checkSignature是用来验证微信服务器是否可以和自己的服务器通讯的狗玩意儿~~~~~

  1. 参数的获取,这里的验证步骤得用this.get()获取参数;
  2. 需要安装sha1包,npm install sha1;
  3. 官方文档用的php代码演示,直接返回的布尔值。thinkjs直接返回布尔值会报以下错误{"errcode":-106,"errmsg":"token check fail"} 解决办法,return this.json();
  const sha1 = require('sha1');

  async checkSignatureAction () {

        let signature = this.get('signature');

        let echostr = this.get('echostr');

        let token = 'TOKEN';//公号后台配置的TOKEN

        let timestamp = this.get('timestamp');

        let nonce = this.get('nonce');

        let array = new Array(token, timestamp, nonce);

        array.sort();

        let str = array.toString().replace(/,/g, "");

        let code = sha1(str);

        if (code == signature) return this.json(echostr); 

        return this.json('error');
   }

相关文章

网友评论

      本文标题:微信公号开发之:thinkjs踩坑checkSignature

      本文链接:https://www.haomeiwen.com/subject/mivorktx.html