You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

144 lines
3.0 KiB

5 days ago
# 加密功能依赖安装说明
## 必需依赖
为了使用接口加密功能,需要安装以下 npm 包:
### 1. crypto-js
用于 AES 对称加密
```bash
npm install crypto-js
```
### 2. jsencrypt
用于 RSA 非对称加密
```bash
npm install jsencrypt
```
## 安装步骤
### 方式一:使用 HBuilderX
1. 打开项目根目录
2. 右键点击项目名称
3. 选择"使用命令行窗口打开所在目录"
4. 执行以下命令:
```bash
npm install crypto-js jsencrypt
```
### 方式二:手动安装
如果项目根目录没有 `package.json`,需要先创建:
1. 创建 `package.json` 文件:
```json
{
"name": "stm32-iot-app",
"version": "1.2.0",
"description": "STM32 IoT UniApp",
"main": "main.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
"crypto-js": "^4.2.0",
"jsencrypt": "^3.3.2"
},
"author": "",
"license": "MIT"
}
```
2. 执行安装命令:
```bash
npm install
```
## 验证安装
安装完成后,检查项目根目录下是否生成了 `node_modules` 文件夹,并包含以下目录:
- `node_modules/crypto-js`
- `node_modules/jsencrypt`
## 常见问题
### Q1: HBuilderX 提示找不到模块?
**解决方案:**
1. 确保已正确安装依赖
2. 重启 HBuilderX
3. 清除项目缓存:菜单栏 → 运行 → 清除项目缓存
4. 重新编译项目
### Q2: 小程序端提示 crypto-js 不可用?
**解决方案:**
某些小程序平台对 Node.js 模块支持有限,可能需要:
1. 使用小程序原生 API
2. 使用兼容版本的加密库
3. 参考各平台文档进行适配
### Q3: App 端运行报错?
**解决方案:**
1. 检查 `manifest.json` 中的 App 模块配置
2. 确保在自定义基座中测试
3. 云打包时确保包含了 node_modules
## 平台兼容性
| 平台 | crypto-js | jsencrypt | 备注 |
|------|-----------|-----------|------|
| H5 | ✅ | ✅ | 完全支持 |
| App (iOS) | ✅ | ✅ | 完全支持 |
| App (Android) | ✅ | ✅ | 完全支持 |
| 微信小程序 | ⚠️ | ⚠️ | 需要测试验证 |
| 支付宝小程序 | ⚠️ | ⚠️ | 需要测试验证 |
| 其他小程序 | ⚠️ | ⚠️ | 需要测试验证 |
**注意:** 小程序平台可能需要额外配置或使用替代方案。
## 替代方案(针对小程序)
如果在小程序端遇到兼容性问题,可以考虑:
### 1. 使用小程序原生加密 API
```javascript
// 微信小程序
wx.crypto.getRandomValues()
```
### 2. 使用 uni-app 插件市场的加密插件
搜索关键词:
- AES 加密
- RSA 加密
- 数据加密
### 3. 后端降级处理
在小程序端关闭加密,仅在 App 和 H5 端使用:
```javascript
// config.js
export default {
enableEncrypt: process.env.UNI_PLATFORM === 'h5' ||
process.env.UNI_PLATFORM === 'app-plus',
// ...
}
```
## 技术支持
- UniApp 官方文档https://uniapp.dcloud.net.cn/
- crypto-js 文档https://github.com/brix/crypto-js
- jsencrypt 文档https://github.com/travist/jsencrypt