wechat-plus 使用node开发微信公众号

github:https://github.com/liuyinglong/node-wechat
npm:https://www.npmjs.com/package/wechat-plus

install

npm install wechat-plus --save

use

//引入
var WeChat=require("wechat-plus");
let weChat= new WeChat({
    token: "your token",
    appid: "your appid",
    secret: "your secret",
});

//文字消息
weChat.event.on("weChat_msg_text", function (res, msg) {
    // msg接受到的消息内容
    // res对象
    // replay 方法 第一个参数为消息 第二个参数为消息类型 默认为text
    res.reply({
        ToUserName: msg.FromUserName,
        FromUserName: msg.ToUserName,
        Content: "ee"   //回复内容
    },"text");
});

//语音消息
weChat.event.on("weChat_msg_voice", function (res, msg) {
    // msg接受到的消息内容
    // res对象
    // replay 方法 第一个参数为消息 第二个参数为消息类型
    res.reply({
        ToUserName: msg.FromUserName,
        FromUserName: msg.ToUserName,
        Content: "ee"   //回复内容
    },"text");
}); 

/**
 * 生成授权链接
 * @param redirectUrl
 * @param state 默认为空
 * @param scope 默认为"snsapi_userinfo"
 * @returns {string}
 */
let authUrl=weChat.authUrl("http://youAddress.com/code","STATE","snsapi_userinfo")

/**
 * 获取用户信息
 * @param code
 * @returns {Promise.<TResult>|Request}
 */
weChat.getUserInfo("CODE").then(function(res){
    //userInfo 为res.body
});


/**
 * 生成jsSDK签名
 * @param url 链接地址
 * @returns {Promise.<TResult>}
 *                 noncestr:随机字符串,
                   timestamp: 时间戳,
                   signature: 签名
 */

weChat.jssdkSignature("url").then(function(result){

})

express 路由示例

let express = require('express');
let router = express.Router();
let WeChat = require("wechat-plus");
let weChat=new WeChat({  //以下参数自行替换
    token: "******",
    appid: "***************************",
    secret: "***************************",
});

//文字消息事件
weChat.event.on("weChat_msg_text", function (res, msg) {
    //回复内容
    res.reply({
        ToUserName: msg.FromUserName,
        FromUserName: msg.ToUserName,
        Content: "ee"
    });
});

// 开发者认证
router.get('/wechat', function (req, res, next) {
    if (weChat.auth(req)) {
        res.send(req.query.echostr)
    } else {
        res.send("false")
    }
});

router.post("/wechat", function (req, res, next) {
    //用户消息处理函数 如果为进行消息监听 则向微信服务器返回空数据
    weChat.userMessage(req, res);
});

//获取用户信息 跳转到授权页面
router.get("/wechat/auth", function (req, res, next) {
    let t = weChat.authUrl("http://vpn.getlove.cn/api/wechat/code");
    res.redirect(t);
});


//根据code 获取用户的基本信息
router.get("/wechat/code", function (req, res, next) {
    weChat.getUserInfo(req.query.code).then(function(data){
        //data中包含用户信息
    }).catch(function(err){
        let t = weChat.authUrl("http://vpn.getlove.cn/api/wechat/code");
        res.redirect(t);
    })
});

//jssdk获取签名
router.post("/share", function (req, res, next) {
    weChat.jssdkSignature(req.body.url).then(function (data) {
        res.send(data);
    })
});


module.exports = router;

原文地址 :http://www.jianshu.com/p/eedfde5a408d
如有问题 可在原文中留言

转载于:https://www.cnblogs.com/getlove/p/7291952.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值