管灌系统农户端微信小程序(嘉峪关应用)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
Page({
  /**
   * 页面的初始数据
   */
  data: {
    planCode: '',
    startTime: '',
    projects: [],
    isRefreshing: false, // 下拉刷新状态
    planId: '' // 保存计划ID
  },
 
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.id) {
      this.setData({
        planId: options.id
      });
      // 从列表页面进入时,所有项目默认收起
      const fromListPage = options.fromList === 'true';
      this.loadIrrigationDetail(options.id, !fromListPage);
    }
  },
 
  /**
   * 加载灌溉计划详情
   * @param {string} id 灌溉计划ID
   * @param {boolean} isFirstLoad 是否首次加载
   */
  loadIrrigationDetail: function (id, isFirstLoad = false) {
    // 开始加载,设置刷新状态
    this.setData({
      isRefreshing: true
    });
 
    // 保存当前项目的展开状态
    const currentExpandStates = {};
    if (!isFirstLoad && this.data.projects.length > 0) {
      this.data.projects.forEach((project, index) => {
        currentExpandStates[project.id] = project.expanded;
      });
    }
 
    // 这里应该是从API获取数据
    // 以下是模拟数据
    const mockData = {
      planCode: 'IRG20240317001',
      startTime: '2024-03-17 08:00',
      projects: [
        {
          id: 1,
          name: '项目A',
          expanded: isFirstLoad ? true : false, // 仅在非列表页进入且首次加载时展开第一个项目
          groups: [
            {
              id: 1,
              name: '轮灌组A-1',
              status: 'irrigated', // 已灌溉
              statusText: '已灌溉',
              startTime: '2024-03-17 08:00',
              endTime: '2024-03-17 09:30',
              duration: 90
            },
            {
              id: 2,
              name: '轮灌组A-2',
              status: 'irrigating', // 正在灌溉
              statusText: '正在灌溉',
              startTime: '2024-03-17 09:30',
              endTime: '2024-03-17 11:00',
              duration: 90
            }
          ]
        },
        {
          id: 2,
          name: '项目B',
          expanded: false,
          groups: [
            {
              id: 3,
              name: '轮灌组B-1',
              status: 'waiting', // 未灌溉
              statusText: '未灌溉',
              startTime: '2024-03-17 11:00',
              endTime: '2024-03-17 12:30',
              duration: 90
            },
            {
              id: 4,
              name: '轮灌组B-2',
              status: 'waiting', // 未灌溉
              statusText: '未灌溉',
              startTime: '2024-03-17 12:30',
              endTime: '2024-03-17 14:00',
              duration: 90
            }
          ]
        },
        {
          id: 3,
          name: '项目C',
          expanded: false,
          groups: [
            {
              id: 5,
              name: '轮灌组C-1',
              status: 'waiting', // 未灌溉
              statusText: '未灌溉',
              startTime: '2024-03-17 14:00',
              endTime: '2024-03-17 15:30',
              duration: 90
            }
          ]
        }
      ]
    };
 
    // 计算每个项目的总灌溉时长
    mockData.projects = this.calculateProjectTotalDuration(mockData.projects);
 
    // 如果不是首次加载,恢复项目的展开状态
    if (!isFirstLoad) {
      mockData.projects.forEach(project => {
        if (currentExpandStates[project.id] !== undefined) {
          project.expanded = currentExpandStates[project.id];
        }
      });
    }
 
    // 模拟网络请求延迟
    setTimeout(() => {
      // 设置页面数据
      this.setData({
        planCode: mockData.planCode,
        startTime: mockData.startTime,
        projects: mockData.projects,
        isRefreshing: false // 结束刷新状态
      });
    }, 1000);
 
    // 实际项目中应该使用wx.request获取数据
    // wx.request({
    //   url: 'https://your-api-url/irrigation/' + id,
    //   method: 'GET',
    //   success: (res) => {
    //     if (res.data && res.data.code === 0) {
    //       // 获取新数据
    //       let projects = res.data.data.projects;
    //       
    //       // 如果是首次加载,设置默认展开状态
    //       if (isFirstLoad) {
    //         projects = projects.map((project, index) => {
    //           return {
    //             ...project,
    //             expanded: index === 0 // 默认只展开第一个项目
    //           };
    //         });
    //       } else {
    //         // 如果不是首次加载,恢复项目的展开状态
    //         projects = projects.map(project => {
    //           return {
    //             ...project,
    //             expanded: currentExpandStates[project.id] !== undefined 
    //               ? currentExpandStates[project.id] 
    //               : false
    //           };
    //         });
    //       }
    //       
    //       // 计算每个项目的总灌溉时长
    //       projects = this.calculateProjectTotalDuration(projects);
    //       
    //       this.setData({
    //         planCode: res.data.data.planCode,
    //         startTime: res.data.data.startTime,
    //         projects: projects,
    //         isRefreshing: false // 结束刷新状态
    //       });
    //     } else {
    //       wx.showToast({
    //         title: '获取数据失败',
    //         icon: 'none'
    //       });
    //       this.setData({
    //         isRefreshing: false // 结束刷新状态
    //       });
    //     }
    //   },
    //   fail: () => {
    //     wx.showToast({
    //       title: '网络错误',
    //       icon: 'none'
    //     });
    //     this.setData({
    //       isRefreshing: false // 结束刷新状态
    //     });
    //   }
    // });
  },
 
  /**
   * 计算每个项目的总灌溉时长
   */
  calculateProjectTotalDuration: function(projects) {
    return projects.map(project => {
      // 计算项目下所有轮灌组的总时长
      const totalDuration = project.groups.reduce((total, group) => {
        return total + (group.duration || 0);
      }, 0);
      
      return {
        ...project,
        totalDuration: totalDuration
      };
    });
  },
 
  /**
   * 切换项目展开/收起状态
   */
  toggleProject: function (e) {
    const index = e.currentTarget.dataset.index;
    const expanded = this.data.projects[index].expanded;
    
    // 更新项目的展开状态
    const key = `projects[${index}].expanded`;
    this.setData({
      [key]: !expanded
    });
  },
 
  /**
   * 导航到轮灌组详情页面
   */
  navigateToGroupDetail: function (e) {
    const projectName = e.currentTarget.dataset.projectName;
    const groupName = e.currentTarget.dataset.groupName;
    const groupId = e.currentTarget.dataset.groupId;
    const status = e.currentTarget.dataset.status;
    
    console.log('跳转到组详情页面,原始状态:', status);
    
    // 导航到组详情页面,并传递参数
    // 注意:需要将status转换为isIrrigating参数,以便在组详情页面正确显示命令状态
    wx.navigateTo({
      url: `/pages/groupDetail/groupDetail?projectName=${projectName}&groupName=${groupName}&groupId=${groupId}&status=${status}&isIrrigating=${status === 'irrigating' ? 'true' : 'false'}`
    });
  },
 
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
 
  },
 
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
 
  },
 
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
 
  },
 
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
 
  },
 
  /**
   * 下拉刷新处理函数
   */
  onPullDownRefresh: function () {
    // 重新加载数据,但保持展开状态
    if (this.data.planId) {
      this.loadIrrigationDetail(this.data.planId, false);
    } else {
      this.setData({
        isRefreshing: false
      });
    }
  },
 
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
 
  },
 
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
 
  }
})