# use pnpm
$ pnpm install egg-wxcrypto
# use yarn
$ yarn add egg-wxcrypto
// {app_root}/config/plugin.js
exports.wxcrypto = {
enable: true,
package: 'egg-wxcrypto'
}
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 字符串的配置
// }
}
// {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
// {app_root}/app/service/home.js
const { ctx } = this
ctx.buildXML(data, options) // data => xmlString
ctx.parseXML(xmlString, options) // xmlString => data
Please open an issue here.