/egg-wxcrypto

扩展支持微信消息加解密nodejs版本的egg插件

Primary LanguageTypeScriptMIT LicenseMIT

egg-wxcrypto

扩展支持微信消息加解密 nodejs 版本的 egg 插件

NPM version Codacy Badge Known Vulnerabilities npm download License

Sonar

Install

# use pnpm
$ pnpm install egg-wxcrypto

# use yarn
$ yarn add egg-wxcrypto

Usage

// {app_root}/config/plugin.js
exports.wxcrypto = {
  enable: true,
  package: 'egg-wxcrypto'
}

Configurations

egg-wxcrypto support all node-wxcrypto's configurations, check node-wxcrypto documents to get more information.

// {app_root}/config/config.default.js
exports.wxcrypto = {
  // token: '',
  // aesKey: '',
  // appId: '',
  // options: {
  //   normalizeTags: true,
  //   buildXmlOptions: {}, // 透传用于生成 xml 字符串的配置
  //   xmlOptions: {} // 透传用于解析 xml 字符串的配置
  // }
}

Use in service

// {app_root}/app/service/home.js
const { Service } = require('egg')

class HomeService extends Service {
  async index() {
    const { ctx, app } = this
    const data = {
      // ...
    }
    await ctx.decryptWxMsg(this.request.body, options) // decrypt
    await ctx.encryptWxMsg(data, options) // encrypt
    // or
    await app.wxcrypto.decrypt(encrypt, timestamp, nonce, options) // decrypt
    await app.wxcrypto.encrypt(data, options) // encrypt
  }
}

module.exports = HomeService

Extended Context Methods

// {app_root}/app/service/home.js
const { ctx } = this

ctx.buildXML(data, options) // data => xmlString
ctx.parseXML(xmlString, options) // xmlString => data

Change logs

Change logs

Questions & Suggestions

Please open an issue here.

License

MIT