Git Product home page Git Product logo

bs-weixin's Introduction

Node.js的微信公众号开发

使用nodejs+express进行微信开发

环境

  • node.js 最新版本
  • redis 最新版本
  • express.js 4.x

安装

//install
$npm install

修改微信信息:config.js

exports.WX = {
    TOKEN: "xxxxxx",
    APPID: "xxxxxx",
    APPSECRET: "xxxxxx"
};

修改成自己申请的公众号的token和appid和appsecret即可。

//start
$npm start

启动的端口为8888,在bin\www文件修改,访问

http://localhost:8888/weixin/index.html

有结果说明成功。

###外网映射

下载ngrock,官方有配置使用说明。再次访问映射外网的地址测试验证:

http://外网域名/weixin/index.html

##开发步骤

开发模式接入

  • 第一步:填写服务器配置

按照官方文档完成配置《接入指南》

URL地址填写上面:http://外网域名/weixin/index.html

  • 第二步:验证服务器地址的有效性

routes目录下的weixin.js"/index\.html"路由函数中:

if(this.req.method === "GET"){
    var signature = this.get("signature");
    var timestamp = this.get("timestamp");
    var nonce = this.get("nonce");
    var echostr = this.get("echostr");

    if(WeiXinUtil.checkSignature(signature, timestamp, nonce)){
        this.res.end(echostr);
    }else{
        this.res.end("hello");
    }
}

验证有效性的请求为GET请求,该请求由微信服务器请求,所以放在GET中处理。

微信服务器向我们的服务发送了四个参数:signaturetimestampnonceechostr,我们需要对signature进行验证,验证方法在

WeiXinUtil.checkSignature

函数中,

/**
 * 验证signature
 * @param signature
 * @param timestamp
 * @param nonce
 * @returns {boolean}
 */
checkSignature: function(signature, timestamp, nonce){
    var arr = [token, timestamp, nonce];
    arr.sort();

    var content = arr.join("");
    return sha1(content) === signature;
}

sha1为npm的sha1

最终将echostr返回给微信服务器

bs-weixin's People

Contributors

oo10 avatar

Stargazers

 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.