wechat-plus 使用node开发微信公众号
来源:互联网 发布:网络交换器 编辑:程序博客网 时间:2024/05/31 19:47
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
如有问题 可在原文中留言
阅读全文
0 0
- wechat-plus 使用node开发微信公众号
- Node.js微信公众号开发
- Node开发微信公众号
- nodejs微信公众号开发第一步(接入指南)--wechat模块
- 微信开发使用wechat.class.php调用API,测试发现返回no access,公众号出现暂时无法提供服务
- node js 微信公众号,模板消息开发测试
- 7天搞定Node.js微信公众号开发
- 2017最新Node.js开发微信公众号视频教程
- 使用微信公众号开发<一>
- 使用Python开发微信公众号
- 使用Python开发微信公众号
- 微信公众号开发--使用redis
- Wechat公众平台开发
- 用Python版微信SDK wechat-sdk去给微信公众号创建菜单
- nodejs微信开发--自动回复 wechat
- (Node+Vue+微信公众号开发)企业级产品全栈开发速成周末班
- 代码仔的实验室_微信公众平台开发框架 & Wechat Public Platform published on GitHub
- 微信公众平台开发 Node.js SDK
- 根据MAC地址生成软件的序列号
- 帧布局
- 自定义栈和队列
- vue表单验证组件 v-verify-plugin
- 二分图匹配之最佳匹配 km算法详解
- wechat-plus 使用node开发微信公众号
- node 控制 树莓派做的天气闹钟
- JS三元运算符
- 读《程序员生存定律》心得体会
- java云课堂 单词长度
- Hessian源码剖析(七)
- poj2195
- 动态规划问题的解决步骤
- Android相机的闪关灯