管灌系统农户端微信小程序(嘉峪关应用)
README.md
@@ -71,6 +71,115 @@
4. **云开发**:项目可能使用了微信小程序云开发功能,用于数据存储和云函数。
## API通信模块
API目录包含了项目中所有与后端通信相关的文件,提供了统一的网络请求处理机制。
### 文件结构
- **request.js**: 网络请求核心模块,封装了微信小程序的`wx.request`接口
  - `request()`: 基础请求函数,处理请求配置、请求头设置和响应处理
  - `get()`: GET请求简化方法
  - `post()`: POST请求简化方法
  - 自动处理token验证、错误处理和loading状态
- **config.js**: 配置文件,定义API基础URL和环境配置
  - 支持多项目环境:嘉峪关(JYG)、民勤(MQ)、测试环境(TEST)
  - `setBaseUrl()`: 动态切换项目环境的函数
- **env.js**: 环境变量配置,定义当前运行环境(生产/测试)
- **statusCode.js**: HTTP状态码常量定义,用于统一处理响应状态
### 使用方法
1. **GET请求**:
   ```javascript
   const { get } = require('../../api/request')
   // 获取项目列表
   get({
     url: '/wx/irrigation/getSimpleProjects',
     isShowLoding: true
   }).then(res => {
     if (res.success) {
       // 处理成功响应
       console.log(res.content)
     } else {
       wx.showToast({
         title: res.msg || '请求失败',
         icon: 'none'
       })
     }
   }).catch(err => {
     console.error('请求失败:', err)
     wx.showToast({
       title: '请求失败',
       icon: 'none'
     })
   })
   ```
2. **POST请求**:
   ```javascript
   const { post } = require('../../api/request')
   // 获取轮灌组列表
   post({
     url: '/wx/irrigation/getSimpleGroups',
     data: {
       projectId: '2025032017240700001',
       planName: '2025032501',
       startupMode: 1,
       operatorId: 2024090516595200300,
       schedules: [
         {
           groupId: 2025032017371700000,
           duration: 5
         }
       ]
     },
     isShowLoding: true
   }).then(res => {
     if (res.success) {
       // 处理成功响应
       console.log(res.content)
     } else {
       wx.showToast({
         title: res.msg || '请求失败',
         icon: 'none'
       })
     }
   }).catch(err => {
     console.error('请求失败:', err)
     wx.showToast({
       title: '请求失败',
       icon: 'none'
     })
   })
   ```
3. **切换项目环境**:
   ```javascript
   const { setBaseUrl } = require('../../api/config')
   // 切换到民勤项目
   setBaseUrl('MQ')
   // 切换到嘉峪关项目
   setBaseUrl('JYG')
   // 切换到测试环境
   setBaseUrl('TEST')
   ```
所有网络请求会自动处理:
- token 认证(通过请求头 Authorization)
- 项目标识(通过请求头 tag)
- 应用标识(通过请求头 appId)
- 错误处理和响应解析
- 加载状态显示(通过 isShowLoding 参数控制)
## 代码风格和规范
1. **文件命名**: