diff --git a/Operation.md b/Operation.md index 6bf31e6..c057bc0 100644 --- a/Operation.md +++ b/Operation.md @@ -263,4 +263,6 @@ ] } -``` \ No newline at end of file +``` + +##### utils [Utils](./Utils.md) \ No newline at end of file diff --git a/Utils.md b/Utils.md new file mode 100644 index 0000000..d076ad7 --- /dev/null +++ b/Utils.md @@ -0,0 +1,81 @@ +#### Utils +工具类 + +##### parseXML: (xmlStr: string, options?: OpParseXMLOptions) => any +op.utils.parseXML 用来解析 XML 字符串并返回一个 JSON 对象。 + +参数说明: +TODO +示例代码: +```javaScript +exports.run = async (ctx, op) => { + const data = ` + + + + Tom + + + Jerry + + + `; + return op.utils.parseXML(data, { + ignoreAttributes: false, + attributeNamePrefix : "X_", + }); +}; +``` +以上代码返回: +```javaScript +{ + "oauth": { + "data": { + "list": [ + { + "name": { + "#text": "Tom", + "X_age": "32", + } + }, + { + "name": { + "#text": "Jerry", + "X_age": "28", + } + }, + ] + } + } +} + +``` + +##### formatString: (data: any, from: string | undefined, to: string) => string +op.utils.formatString 用以将字符串在 utf8、hex、base64 等格式之间进行转换。 + +示例代码: +TODO + +##### decodeSM4: (key: string, data: string, opts?: { mode?: string, iv?: string, inputEncoding?: string, outputEncoding?: string }) => string; +##### encodeSM4: (key: string, data: string, opts?: { mode?: string, iv?: string, inputEncoding?: string, outputEncoding?: string }) => string; + +op.utils.encodeSM4 与 op.utils.decodeSM4 分别用以进行 sm4 的加解密。 + +参数说明: +TODO + +示例代码: +```javaScript +exports.run = async (ctx, op) => { + // 子账号加密示例 + const secret = 'YOUR_SECRET'; + const key = op.utils.formatString(secret, 'utf8', 'hex').slice(0, 32); + const data = 'YOUR_PASSWORD'; + const encoded = '$SM4$' + op.utils.encodeSM4(key, data, { outputEncoding: 'base64' }); + log(encoded); + // 子账号解密示例 + const decoded = op.utils.decodeSM4(key, encoded.replace(/^\$SM4\$/, ''), { inputEncoding: 'base64', outputEncoding: 'utf8' }); + log(decoded === data); +}; +``` \ No newline at end of file