# wx-jsdk

> Next-Generation WeChat JS-SDK integration with NodeJS

Latest version **0.4.0** (published 2018-11-03) · MIT license · 0 weekly downloads

## Install

```sh
npm install wx-jsdk
pnpm add wx-jsdk
yarn add wx-jsdk
bun add wx-jsdk
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.4.0 |
| Published | 2018-11-03 |
| First published | 2018-11-03 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >= 6.0 |
| Dependencies | 7 |
| Unpacked size | 2.9 MB |
| Known vulnerabilities | 0 (+5 in 3 direct dependencies) |
| Install scripts | no |
| Author | Xiaolin LU |
| Maintainers | luxiaolin |
| Keywords | wechat, weixin, node, jssdk, wechat share, mini program |

## Links

- npm: https://www.npmjs.com/package/wx-jsdk
- Repository: https://github.com/LuXiaolin/wx-jsdk
- Homepage: https://github.com/LuXiaolin/wx-jsdk#readme
- Issues: https://github.com/LuXiaolin/wx-jsdk/issues
- npm.io page: https://npm.io/package/wx-jsdk

## Dependencies (7)

- [debug](https://npm.io/package/debug.md) ^4.0.1
- [xml2js](https://npm.io/package/xml2js.md) ^0.4.19
- [request](https://npm.io/package/request.md) ^2.88.0
- [date-fns](https://npm.io/package/date-fns.md) ^1.29.0
- [mongoose](https://npm.io/package/mongoose.md) ^5.2.14
- [lodash.isempty](https://npm.io/package/lodash.isempty.md) ^4.4.0
- [request-promise](https://npm.io/package/request-promise.md) ^4.2.2

## Alternatives

- [@expo/fingerprint](https://npm.io/package/@expo/fingerprint.md) — 6.2M weekly downloads
- [@azure/monitor-opentelemetry-exporter](https://npm.io/package/@azure/monitor-opentelemetry-exporter.md) — 850.0K weekly downloads
- [@azure/monitor-opentelemetry](https://npm.io/package/@azure/monitor-opentelemetry.md) — 624.0K weekly downloads
- [@posthog/ai](https://npm.io/package/@posthog/ai.md) — 423.3K weekly downloads
- [fakefilter](https://npm.io/package/fakefilter.md) — 63.9K weekly downloads

## Recent versions

- 0.4.0 (latest) — 2018-11-03

## README

## Usage

`npm install wx-jsdk --save` or    

const Wechat = require('wx-jsdk');
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/`,
}
```

### Setup your Wechat ENV  
1.Set your own URL in [Wechat Website](https://mp.weixin.qq.com)  
  
  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](#demo)  

  ```javascript
  //express app for example:
  router.get('/get-signature', async (req, res) => {
    wx.jssdk.getSignature(req.query.url).then(signatureData => {
      res.json(signatureData);
    });
    //use async/await
    //const signatureData = await wx.jssdk.getSignature(req.query.url);
    //res.json(signatureData);
  });
  ```
3.Now you can get to the next step in your browser to pass the verification.


## Browser Side Usage
```javascript
const WechatJSSDK = require('wx-jsdk/dist/client');
//ES6 import
import WechatJSSDK from 'wx-jsdk/dist/client';
//or import the original ES6 module from 'lib/client', 
// in which case you may need to include this into your webpack babel-loader process 
import WechatJSSDK from 'wx-jsdk/lib/client';
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:
```javascript
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
  //invoked if wechat signature sign succeeds,
  //'this' will be the jssdk instance if it's a normal function, 
  // in v3.0.10+, jssdk instance will be passed to the callback, (wxObj) => {}
  // in the up coming v4, "success"/"error" init callback will be replace by #initialize() which will return Promise, see below
  'success': jssdkInstance => {},
  //invoked if sign failed, in v3.0.10+, jssdk instance will be pass to the func, (err, wxObj) => {}
  'error': (err, jssdkInstance) => {},
  //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);
//in the up coming v4, use "initialize" as Promise:
const wechatObj2 = new WechatJSSDK(config);
wechatObj2.initialize()
  .then(w => {
    //set up your share info just like in "success" function in config above
  })
  .catch(err => {
    console.error(err);
  });
```
after signature signed successfully, you can customize the share information:

```javascript
//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)`:

```javascript
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)`

---
_Source: https://npm.io/package/wx-jsdk · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
