feat: 内容由嗣钰提供

This commit is contained in:
liuyafei 2023-11-17 14:49:31 +08:00
parent dd4aea4129
commit 26b05995d8
2 changed files with 84 additions and 1 deletions

View File

@ -263,4 +263,6 @@
] ]
} }
``` ```
##### utils [Utils](./Utils.md)

81
Utils.md Normal file
View File

@ -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 = `
<oauth>
<data>
<list>
<name age="32">Tom</name>
</list>
<list>
<name age="28">Jerry</name>
</list>
</data>
</oauth>`;
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);
};
```