https://github.com/die-welle/koa-weixin-jssdk
koa weixin jssdk middleware
https://github.com/die-welle/koa-weixin-jssdk
jssdk koa koa-weixin-jssdk koa2 wechat wechat-jssdk weixin weixin-jssdk
Last synced: 6 months ago
JSON representation
koa weixin jssdk middleware
- Host: GitHub
- URL: https://github.com/die-welle/koa-weixin-jssdk
- Owner: die-welle
- License: mit
- Created: 2016-02-17T13:53:25.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2019-04-01T16:52:36.000Z (about 6 years ago)
- Last Synced: 2024-11-14T05:35:17.227Z (6 months ago)
- Topics: jssdk, koa, koa-weixin-jssdk, koa2, wechat, wechat-jssdk, weixin, weixin-jssdk
- Language: JavaScript
- Size: 65.4 KB
- Stars: 7
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE-MIT
Awesome Lists containing this project
README
# koa-weixin-jssdk
[](https://travis-ci.org/die-welle/koa-weixin-jssdk)
koa weixin jssdk middleware
## Quick Start
```js
import koa from 'koa';
import weixinJSSDK from '../src';const port = process.env.PORT || 3000;
const app = koa();app.use(weixinJSSDK({
appId: '', // [required] weixin-jssdk app id
secret: '', // weixin-jssdk secret
pathName: '/jssdk', // [optional] eg: http://imyourfather.com/jssdkonError: (err, ctx, next) => {
console.error(err);
ctx.body = 'error';
},
}));app.listen(port);
```## Advanced Usage
##### Custom store `access_token` and `ticket`
By default, it would cache `access_token` and `ticket` in runtime memory, but you can store them in somewhere else.
```js
app.use(weixinJSSDK({
appId: '',
secret: '',
async onGetToken(url) {
return redis.getAsync('ACCESS_TOKEN'); // this is an example
},
async onSetToken(token, expiresIn) {
return redis.setSync('ACCESS_TOKEN', token, { expiresIn });
},
async onGetTicket(url) {
return redis.getAsync('TICKET');
},
async onSetTicket(ticket, expiresIn) {
return redis.setSync('TICKET', ticket, { expiresIn });
},
// other configs...
}));
```##### Third-party weixin service
Maybe you already have a Third-party weixin service and have a access token, you could use custom `fetchTicket` or `fetchToken` function instead of `secret`.
```js
app.use(weixinJSSDK({
appId: '', // [required]
async fetchToken() {
// await fetch(...)
return { access_token: '', expires_in: 7200 };
},
// other configs...
}));
```Or `fetchTicket`
```js
app.use(weixinJSSDK({
appId: '', // [required]
async fetchTicket() {
// await fetch(...)
return { ticket: 'TICKET', expires_in: 7200 };
},
// other configs...
}));
```## Installation
Using [npm](https://www.npmjs.com/):
$ npm install koa-weixin-jssdk --save
## License
MIT