All Projects → JasonBoy → Wechat Jssdk

JasonBoy / Wechat Jssdk

Licence: mit
🐧WeChat JS-SDK integration with NodeJS

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Wechat Jssdk

Wuss Weapp
🐳wuss-weapp 一款高质量,组件齐全,高自定义的微信小程序UI组件库
Stars: ✭ 338 (-40.81%)
Mutual labels:  wechat, weixin
Pay
可能是我用过的最优雅的 Alipay 和 WeChat 的支付 SDK 扩展包了
Stars: ✭ 4,176 (+631.35%)
Mutual labels:  wechat, weixin
Wechat kit
flutter版微信登录/分享/支付
Stars: ✭ 347 (-39.23%)
Mutual labels:  wechat, weixin
Wechat App Issues
💥 微信小程序踩坑集合
Stars: ✭ 318 (-44.31%)
Mutual labels:  wechat, weixin
Wecron
✔️ 微信上的定时提醒 - Cron on WeChat
Stars: ✭ 537 (-5.95%)
Mutual labels:  wechat, weixin
Vbot
💬The best wechat robot base on web api!
Stars: ✭ 3,301 (+478.11%)
Mutual labels:  wechat, weixin
Werobot
WeRoBot 是一个微信公众号开发框架
Stars: ✭ 3,973 (+595.8%)
Mutual labels:  wechat, weixin
Weixin Spider
微信公众号爬虫,公众号历史文章,文章评论,文章阅读及在看数据,可视化web页面,可部署于Windows服务器。基于Python3之flask/mysql/redis/mitmproxy/pywin32等实现,高效微信爬虫,微信公众号爬虫,历史文章,文章评论,数据更新。
Stars: ✭ 287 (-49.74%)
Mutual labels:  wechat, weixin
Wechat Bot
带二次开发接口的PC微信聊天机器人
Stars: ✭ 458 (-19.79%)
Mutual labels:  wechat, weixin
Pageslider
朋友圈广告 - 移动端滑屏翻页插件
Stars: ✭ 442 (-22.59%)
Mutual labels:  wechat, weixin
Wemall
wemall7 开源版本 (不含商城)
Stars: ✭ 315 (-44.83%)
Mutual labels:  wechat, weixin
Wechat sender
随时随地发送消息到微信
Stars: ✭ 474 (-16.99%)
Mutual labels:  wechat, weixin
Wepy Mall
微信小程序--基于wepy 商城(微店)微信小程序 欢迎学习交流
Stars: ✭ 3,224 (+464.62%)
Mutual labels:  wechat, weixin
Sns auth
通用第三方登录SDK,支持微信,微信扫码,QQ,微博登录,支付宝登录,Facebook,Line,Twitter,Google
Stars: ✭ 520 (-8.93%)
Mutual labels:  wechat, weixin
Ocbarrage
iOS 弹幕库 OCBarrage, 同时渲染5000条弹幕也不卡, 轻量, 可拓展, 高度自定义动画, 超高性能, 简单易上手; A barrage render-engine with high performance for iOS. At the same time, rendering 5000 barrages is also very smooth, lightweight, scalable, highly custom animation, ultra high performance, simple and easy to use!
Stars: ✭ 294 (-48.51%)
Mutual labels:  wechat, weixin
Lykchat
便捷、稳定(7*24不间断服务)、安全、支持发送多媒体的信息发送系统,为系统管理人员提供一个基于个人微信号的信息发送工具。
Stars: ✭ 353 (-38.18%)
Mutual labels:  wechat, weixin
Weixinresource
微信开发资源汇总 | WeChat Development Resources Summary
Stars: ✭ 279 (-51.14%)
Mutual labels:  wechat, weixin
Weapp Demo Breadtrip
基于面包旅行 API 制作的微信小程序示例
Stars: ✭ 282 (-50.61%)
Mutual labels:  wechat, weixin
Wecheat
开箱即用的微信公众平台API模拟服务器,帮助你开发与调试微信公众平台应用
Stars: ✭ 440 (-22.94%)
Mutual labels:  wechat, weixin
Weindex
微信相关资源汇总索引
Stars: ✭ 466 (-18.39%)
Mutual labels:  wechat, weixin

wechat-jssdk

npm node Building Status Coverage Status npm code style: prettier

Next-Generation WeChat JS-SDK integration with NodeJS.

If you need node-v6 support ,use v4

If you need node-v4 support ,use v3

中文使用文档 Changelog

wechat-jssdk-demo

Features

Usage

npm install wechat-jssdk --save
# or
yarn add wechat-jssdk
const {Wechat} = require('wechat-jssdk');
const wx = new Wechat(wechatConfig);

Wechat Config

wechatConfig info:

{
  //set your oauth redirect url, defaults to localhost
  "wechatRedirectUrl": "http://yourdomain.com/wechat/oauth-callback",
  //"wechatToken": "wechat_token", //not necessary required
  "appId": "appid",
  "appSecret": "app_secret",
  card: true, //enable cards
  payment: true, //enable payment support
  merchantId: '', //
  paymentSandBox: true, //dev env
  paymentKey: '', //API key to gen payment sign
  paymentCertificatePfx: fs.readFileSync(path.join(process.cwd(), 'cert/apiclient_cert.p12')),
  //default payment notify url
  paymentNotifyUrl: `http://your.domain.com/api/wechat/payment/`,
  //mini program config
  "miniProgram": {
    "appId": "mp_appid",
    "appSecret": "mp_app_secret",
  }
}

Setup your Wechat ENV

1.Set your own URL in Wechat Website

Usually wechat will provide you a MP_verify_XHZon7GAGRdcAFxx.txt like file to ask you to put that on your website root, which will be accessed by wechat on http://yourdomain.com/MP_verify_XHZon7GAGRdcAFxx.txt to verify that you own the domain.

2.You should also provide a api for your browser to get token for the current url, see demo

//express app for example:
router.get('/get-signature', (req, res) => {
  wx.jssdk.getSignature(req.query.url).then(signatureData => {
    res.json(signatureData);
  });
});
//koa2/koa-router app for example:
router.get('/get-signature', async ctx => {
  ctx.body = await wx.jssdk.getSignature(ctx.request.query.url);
});

3.Now you can get to the next step in your browser to pass the verification.

Browser Side Usage

You can use it from the browser side as follows. Since we have configured the browser field in package.json, the bundlers (e.g., webpack or rollup, etc.) will resolve the module to wechat-jssdk/dist/client.umd.js.

const WechatJSSDK = require('wechat-jssdk');
//ES6 import
import WechatJSSDK from 'wechat-jssdk';
const wechatObj = new WechatJSSDK(config)

// or if you do not have a bundle process, just add the script tag, and access "WechatJSSDK" from window, e.g:
const wechatObj = new window.WechatJSSDK(config)

where config will be:

const config = {
  //below are mandatory options to finish the wechat signature verification
  //the 4 options below should be received like api '/get-signature' above
  'appId': 'app_id',
  'nonceStr': 'your_nonceStr',
  'signature': 'url_signature',
  'timestamp': 'your_timestamp',
  //below are optional
  //enable debug mode, same as debug
  'debug': true,
  'jsApiList': [], //optional, pass all the jsapi you want, the default will be ['onMenuShareTimeline', 'onMenuShareAppMessage']
  'customUrl': '' //set custom weixin js script url, usually you don't need to add this js manually
}
const wechatObj = new WechatJSSDK(config);
wechatObj.initialize()
  .then(w => {
    //set up your share info, "w" is the same instance as "wechatObj"
  })
  .catch(err => {
    console.error(err);
  });

after signature signed successfully, you can customize the share information:

//customize share on chat info
//sugar method for `wechatObj.callWechatApi('onMenuShareAppMessage', {...})`
wechatObj.shareOnChat({
  type: 'link',
  title: 'title',
  link: location.href,
  imgUrl: '/logo.png',
  desc: 'description',
  success: function (){},
  cancel: function (){}
});
//customize share on timeline info
//sugar method
wechatObj.shareOnMoment({
  type: 'link',
  title: 'title',
  link: location.href,
  imgUrl: '/logo.png'
});

You can also access the original wechat object wx from wechatObj.getOriginalWx().

Call other wechat apis: wechatObj.callWechatApi(apiName, config):

wechatObj.callWechatApi('onMenuShareAppMessage', {
  type: 'link',
  title: 'title',
  link: location.href,
  imgUrl: '/logo.png',
  desc: 'description',
  success: function (){},
  cancel: function (){}
});

or with the original one: wechatObj.getOriginalWx().onMenuShareAppMessage(config)

OAuth

Wechat support web OAuth to get user profile in wechat app. In your page, provide a link, which you can get by wx.oauth.snsUserInfoUrl which is the default oauth url, to the wechat OAuth page, also you need provide a callback url(as show below) to get the wechat code after user click Agree button, the callback url is configured in the wechatConfig object above while initializing, but you can customize your own callback url by using wx.oauth.generateOAuthUrl(customUrl, scope, state) api.

//in node:
const wx = new Wechat(config);
const url = wx.oauth.generateOAuthUrl('http://mycustom.com/oauth-callback', 'snsapi_userinfo', 'custom_state');
res.render("oauth-page", {
  wechatOAuthUrl: url,
});
//insert "wechatOAuthUrl" into your html:

//custom callback url, agree clicked by user, come back here:
router.get('/oauth-callback', function (req, res) {
  wx.oauth.getUserInfo(req.query.code)
    .then(function(userProfile) {
      console.log(userProfile)
      res.render("demo", {
        wechatInfo: userProfile
      });
    });
});

Cards and Offers

since(v3.1)
Set card: true in config to enable the cards support on server side, see demo. For cards APIs, see cards apis

Payment

since(v3.1)
Set payment: true in config to enable the payment support on server side, you should also provide payment related info. See demo. For payment APIs, see payment apis

Mini Program

since(v4)
To enable mini program support(see API), you can just set mini program appId & appSecret in config:

const { Wechat, MiniProgram } = require('wechat-jssdk');
const wechatConfig = {
  "appId": "appid",
  "appSecret": "app_secret",
  //...other configs
  //...
  //mini program config
  "miniProgram": {
    "appId": "mp_appid",
    "appSecret": "mp_app_secret",
  }
};
const wx = new Wechat(wechatConfig);
//access mini program instance
wx.miniProgram.getSession('code');

//Use MiniProgram directly
const miniProgram = new MiniProgram({
  miniProgram: {
    "appId": "mp_appid",
    "appSecret": "mp_app_secret",
  }
})

Using Stores

Store are used to save url signatures into files, dbs, etc..., but also keep a copy in memory for better performence. The default store used is FileStore which will persist tokens and signatures into wechat-info.json file every 10 minutes, also it will load these info from the file in next initialization. Built in Stores: FileStore, MongoStore,

Using Custom Stores:

...
const {Wechat, MongoStore, FileStore} = require('wechat-jssdk');
const wx = new Wechat({
	appId: 'xxx',
	...,
	//file store
	//store: new FileStore(),
	//======
	//pass the MongoStore instance to config
	//default 127.0.0.1:27017/wechat db, no need to pass anything to constructor
	store: new MongoStore({
		//dbName: 'myWechat', //default wechat
		dbAddress: 'mongodb://127.0.0.1:27017/wechat', //set the whole connection uri by yourself
		dbOptions: {}, //set mongoose connection config
	})
})

Create your own Store

You can also create own Store to store tokens anywhere you want, by doing that, you may need to extend the base Store class, and reimplement the apis you need:

const {Store} = require('wechat-jssdk');
class CustomStore extends Store {
	constructor (options) {
		super();
		console.log('using my own store!');
	}
}

APIs

see API wiki

A Blog About This

Debug

Add DEBUG=wechat* when start your app to enable wechat-jssdk debug DEBUG=wechat* node your-app.js

Demo

In v3.1+, the demo page is updated to test the new added Cards & Offers and Payment support. Copy the demo/wechat-config-sample.js to demo/wechat-config.js, and use your own appId, appSecret, and other configs like payment if your want to enable them.

Use npm start or npm run dev to start the demo.

LICENSE

MIT @ 2016-present jason

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].