沙盘演示系统应用的微信小程序
zuoxiao
2024-10-28 f66a9a2c565966e98720be5e20604c6f75030f82
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
// 开卡选择虚拟卡
const {
  get,
  post
} = require('../../api/request.js');
Page({
 
  /**
   * 页面的初始数据
   */
  data: {
    intakeId: 0,
    showDialog: false,
    vcId: "",
    activeIndex: -1,
    datetimeVisible: false,
    dateStartTime: new Date().getTime(),
    dateEndTime: new Date().getTime() + 24 * 60 * 60 * 1000,
    datetimeText: '',
    allCardPoints: [], // 所有虚拟卡
    irrigateProfile: [], //获取所选灌溉方式的详细数据
    dialog_mode: true,
    dialog_time: false,
    dialog_water: false,
    group_time: false, //是否显示选择分钟或小时的单选框
    openTime: "", //计划开泵的开始时间
    openTimeText: "",
    isShowOpenTime: "false",
    isOpenTiming: false, //是否是计划开阀时间样式的开阀
    postMinutes: 0, //定灌溉时长的时间
    showForceConfirm: false,
    showErrorDialog: false, //错误提示
    errorData: "",
    confirmBtn: {
      content: '确认'
    },
    radioValue: "0", //开阀方式选中的类型
    costiomTime: "", //自定义灌溉时间
    waterAmount: 0, //定量开阀的水量
    costiomWater: "", //输入的自定义水量
    isWXRefreshing: false,
    intakeName: "" //扫码开阀从首页传来的取水口名称
  },
  openValva(event) {
    const {
      item
    } = event.currentTarget.dataset; // 获取当前点击的 item
    if (item.isAlarmValue === true) {
      //金额报警
      wx.navigateTo({
        url: '/pages/rechargeMoney/rechargMoney?vcId=' + item.id,
      })
    } else if (item.inUse === false) {
      this.setData({
        vcId: item.id,
      })
      if (this.data.intakeName === "") {
        // 选择虚拟卡后
        this.initDialogData();
      } else {
        //扫码开阀逻辑
        this.postOpenValva(false);
      }
 
    }
  },
  handleAutoIrrigation() {},
 
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const {
      intakeId,
      intakeName
    } = options;
    this.setData({
      intakeId: intakeId,
      intakeName: intakeName
    })
    console.log('intakeId:' + this.data.intakeId);
    this.getCardList();
  },
 
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
 
  },
 
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    console.log("onShow>>>>" + this.data.isShowOpenTime)
  },
 
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
 
  },
 
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
 
  },
 
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.setData({
      isWXRefreshing: true
    });
    this.getCardList();
  },
 
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
 
  },
 
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
 
  },
  //获取虚拟卡列表
  getCardList() {
    get({
        url: 'wx/virtual_card/get',
        data: {
          clientId: getApp().globalData.clientId
        }
      })
      .then((data) => {
        if (data.success && data.code === "0001") {
          this.setData({
            allCardPoints: data.content
          });
          this.updateDisplayText();
        } else {
          wx.showToast({
            title: data.msg,
          })
        }
        console.log('Failed to add item:');
      })
      .catch((error) => {
        console.error('Failed to add item:', error);
      });
  },
  updateDisplayText() {
    const updatedList = this.data.allCardPoints.map(item => {
      let displayText = '';
      if (item.inUse) {
        displayText = '使用中';
      } else {
        if (item.isAlarmValue) {
          displayText = '充值';
        } else {
          displayText = '选择';
        }
      }
      return {
        ...item,
        displayText
      }; // 保留所有其他字段,并添加 displayText 字段
    });
    // 更新列表数据
    this.setData({
      allCardPoints: updatedList,
      isWXRefreshing: false
    });
  },
  handleClost() {
    this.setData({
      showDialog: false
    })
  },
  //选择时间后的处理
  handleTimeWrapperTap(e) {
    const index = e.currentTarget.dataset.index;
    console.log(index)
    if (index !== undefined) {
      let minutes;
      if (this.data.irrigateProfile[index].unit === "1") {
        minutes = this.hourToMinutes(this.data.irrigateProfile[index].defaultValue)
      } else {
        minutes = this.data.irrigateProfile[index].defaultValue;
      }
      this.setData({
        activeIndex: parseInt(index),
        postMinutes: minutes,
        costiomTime: "",
        group_time: false,
      });
    }
  },
  // 选择水量后的处理
  handleWaterWrapperTap(e) {
    const index = e.currentTarget.dataset.index;
    console.log(index)
    if (index !== undefined) {
      this.data.waterAmount = parseInt(this.data.irrigateProfile[index].defaultValue)
      this.setData({
        activeIndex: parseInt(index),
        costiomWater: ""
      });
    }
  },
  //获取常用用水时长及用水量
  getIrrigateProfileData(type) {
    get({
        url: 'wx/valve/irrigate_profile',
        data: {
          type: type
        }
      })
      .then((data) => {
        if (data.success && data.code === "0001") {
          const items = data.content.obj.map(item => {
            return {
              defaultValue: item.defaultValue,
              unit: item.unit,
              unitText: item.unit === "1" ? "小时" : item.unit === "2" ? "分钟" : "立方米"
            };
          });
          this.setData({
            dialog_mode: false,
            dialog_time: type === 1,
            dialog_water: type === 2,
            irrigateProfile: items
          });
        } else {
          wx.showToast({
            title: data.msg,
          })
        }
        console.log('Failed to add item:');
      })
      .catch((error) => {
        console.error('Failed to add item:', error);
      });
  },
  handleSetDuration() {
    this.getIrrigateProfileData(1);
  },
  onGroupOpenTimeChange(event) {
    const {
      value
    } = event.detail;
    console.log('onGroupOpenTimeChange:' + value);
    if (value == 1) {
      this.showPicker();
      this.setData({
        isOpenTiming: true,
        radioValue: value,
      })
    } else {
      this.setData({
        isShowOpenTime: false,
        isOpenTiming: false,
        radioValue: value,
      })
    }
  },
  //显示时间选择器
  showPicker(e) {
    this.setData({
      datetime: new Date().getTime(),
      datetimeVisible: true,
      isShowOpenTime: true
    });
  },
  onTimeInputChange(data) {
    console.log(data.detail.value)
    if (data.detail.value !== "") {
      this.setData({
        group_time: true,
        activeIndex: -1,
        postMinutes: parseInt(data.detail.value)
      })
    } else {
      this.setData({
        group_time: false,
        postMinutes: 0,
      })
    }
 
  },
  onConfirm(e) {
    const {
      value
    } = e?.detail;
    console.log(value);
    this.setData({
      openTime: value,
      openTimeText: "计划开阀时间:" + value
    })
  },
  handleSetWaterVolume() {
    this.getIrrigateProfileData(2);
  },
  dialogBack() {
    this.setData({
      dialog_time: false,
      dialog_water: false,
      dialog_mode: true,
      group_time: false
    })
  },
  //立即开泵
  postOpenValva(isforce) {
    wx.showLoading({
      title: '正在开阀请稍候...', // 加载提示文字
      mask: true // 是否显示透明蒙层,防止触摸穿透,默认为 false
    });
    const app = getApp();
    const data = {
      intakeId: this.data.intakeId, //取水口ID
      vcId: this.data.vcId, //虚拟卡ID
      intakeName: this.data.intakeName, //取水口名称
      operator: app.globalData.sessionId, //操作员
      forceOpen: !!isforce // 使用逻辑非操作符 !! 来确保 isForce 是布尔值  
    };
    post({
      url: "wx/valve/open_wx",
      data: data
    }).then(response => {
 
      // 处理成功响应
      console.log('请求成功:', response);
      // 加载完成后隐藏加载动画
      wx.hideLoading();
      //完成后回到首页
      wx.reLaunch({
        url: '/pages/home/home?param=true' // 首页的路径,根据实际情况填写
      });
 
    }).catch(error => {
      // 加载完成后隐藏加载动画
      wx.hideLoading();
      // 处理错误响应
      console.error('请求失败:', error);
      // if (error.code === "10005") {
      //   this.setData({
      //     showDialog: false,
      //     showForceConfirm: true
      //   })
      // } else {
      this.setData({
        showErrorDialog: true,
        errorData: error.msg
      })
      // }
    });
  },
  //定时关阀式开阀
  timeClose() {
    const app = getApp();
    wx.showLoading({
      title: '正在开阀请稍候...', // 加载提示文字
      mask: true // 是否显示透明蒙层,防止触摸穿透,默认为 false
    });
    const data = {
      intakeId: this.data.intakeId, //取水口ID
      vcId: this.data.vcId, //虚拟卡ID
      minutes: this.data.postMinutes, //分钟
      operator: app.globalData.sessionId //操作员
    };
    post({
      url: "wx/valve/timed_close",
      data: data
    }).then(response => {
      // 处理成功响应
      console.log('请求成功:', response);
      // 加载完成后隐藏加载动画
      wx.hideLoading();
      //完成后回到首页
      wx.reLaunch({
        url: '/pages/home/home?param=true' // 首页的路径,根据实际情况填写
      });
    }).catch(error => {
      // 加载完成后隐藏加载动画
      wx.hideLoading();
      // 处理错误响应
      console.error('请求失败:', error);
      this.setData({
        showErrorDialog: true,
        errorData: error.msg
      })
    });
  },
  //定时关阀式计划开阀
  timeClosePlaned() {
    const app = getApp();
    wx.showLoading({
      title: '正在通信请稍候...', // 加载提示文字
      mask: true // 是否显示透明蒙层,防止触摸穿透,默认为 false
    });
    const data = {
      intakeId: this.data.intakeId, //取水口ID
      vcId: this.data.vcId, //虚拟卡ID
      minutes: this.data.postMinutes, //分钟
      operator: app.globalData.sessionId, //操作员
      plannedOpenTime: this.data.openTime
    };
    post({
      url: "wx/valve/timed_close",
      data: data
    }).then(response => {
      // 处理成功响应
      console.log('请求成功:', response);
      // 加载完成后隐藏加载动画
      wx.hideLoading();
      //完成后回到首页
      wx.reLaunch({
        url: '/pages/home/home?param=true' // 首页的路径,根据实际情况填写
      });
    }).catch(error => {
      // 加载完成后隐藏加载动画
      wx.hideLoading();
      // 处理错误响应
      console.error('请求失败:', error);
      this.setData({
        showErrorDialog: true,
        errorData: error.msg
      })
    });
  },
  //定量开阀
  quantifyOpen() {
    const app = getApp();
    wx.showLoading({
      title: '正在开阀请稍候...', // 加载提示文字
      mask: true // 是否显示透明蒙层,防止触摸穿透,默认为 false
    });
    const data = {
      intakeId: this.data.intakeId, //取水口ID
      vcId: this.data.vcId, //虚拟卡ID
      waterAmount: this.data.waterAmount, //水量
      operator: app.globalData.sessionId //操作员
    };
    post({
      url: "wx/valve/quantify_close",
      data: data
    }).then(response => {
      // 处理成功响应
      console.log('请求成功:', response);
      // 加载完成后隐藏加载动画
      wx.hideLoading();
      //完成后回到首页
      wx.reLaunch({
        url: '/pages/home/home?param=true' // 首页的路径,根据实际情况填写
      });
    }).catch(error => {
      // 加载完成后隐藏加载动画
      wx.hideLoading();
      // 处理错误响应
      console.error('请求失败:', error);
      this.setData({
        showErrorDialog: true,
        errorData: error.msg
      })
    });
  },
  //定量开阀式计划开阀
  quantifyOpenTime() {
    const app = getApp();
    wx.showLoading({
      title: '正在通信请稍候...', // 加载提示文字
      mask: true // 是否显示透明蒙层,防止触摸穿透,默认为 false
    });
    const data = {
      intakeId: this.data.intakeId, //取水口ID
      vcId: this.data.vcId, //虚拟卡ID
      waterAmount: this.data.waterAmount, //水量
      operator: app.globalData.sessionId, //操作员
      plannedOpenTime: this.data.openTime
    };
    post({
      url: "wx/valve/planed_open_quantify_close",
      data: data
    }).then(response => {
      // 处理成功响应
      console.log('请求成功:', response);
      // 加载完成后隐藏加载动画
      wx.hideLoading();
      //完成后回到首页
      wx.reLaunch({
        url: '/pages/home/home?param=true' // 首页的路径,根据实际情况填写
      });
    }).catch(error => {
      // 加载完成后隐藏加载动画
      wx.hideLoading();
      // 处理错误响应
      console.error('请求失败:', error);
      this.setData({
        showErrorDialog: true,
        errorData: error.msg
      })
    });
  },
  // 通过小时转换成分钟
  hourToMinutes(hours) {
    // 检查输入是否合法
    if (isNaN(hours) || hours < 0) {
      console.error('请输入有效的小时数');
      return null;
    }
    // 将小时转换为分钟
    const minutes = hours * 60;
    return minutes;
  },
  minutesToHours(minutes) {
    // 检查输入是否合法
    if (isNaN(minutes) || hours < 0) {
      console.error('请输入有效的小时数');
      return null;
    }
    // 将小时转换为分钟
    const hours = minutes / 60;
    return hours;
  },
  //定时开泵按钮
  btnOpenOnTime() {
    if (this.data.postMinutes > 0) {
      if (this.data.isOpenTiming) {
        this.timeClosePlaned();
      } else {
        this.timeClose();
      }
    } else {
      wx.showToast({
        icon: 'error',
        title: '请选择或输入灌溉时间!',
      })
    }
 
  },
  cancelDialog() {
    this.setData({
      showForceConfirm: false
    });
  },
  //强制开阀
  confirmForceDialog() {
    this.postOpenValva(this.data.forceIntakeId, true);
  },
  //关闭错误提示
  closeErrorDialog() {
    this.setData({
      showErrorDialog: false
    })
  },
  hidePicker() {
    this.setData({
      isShowOpenTime: false,
      isOpenTiming: false,
      radioValue: "0"
    })
  },
  //初始化所有开泵的参数
  initDialogData() {
    this.setData({
      isShowOpenTime: "false",
      isOpenTiming: false, //是否是计划开阀时间样式的开阀
      postMinutes: 0, //定灌溉时长的时间
      showForceConfirm: false,
      showErrorDialog: false, //错误提示
      showDialog: true,
      dialog_mode: true,
      dialog_time: false,
      dialog_water: false,
      openTimeText: "",
      openTime: ""
    })
  },
  //定灌溉时长自定义输入时间时修改时间单位0:分钟,1:小时
  onGroupOpenTimeTypeChange(event) {
    let value = event.detail.value;
    if (value === "1") {
      this.setData({
        postMinutes: this.hourToMinutes(this.data.postMinutes)
      })
    } else {
      this.setData({
        postMinutes: this.minutesToHours(this.data.postMinutes)
      })
    }
  },
  //输入自定义水量的监听
  onWaterInputChange(data) {
    console.log(data.detail.value)
    if (data.detail.value !== "") {
      this.setData({
        activeIndex: -1,
        waterAmount: parseInt(data.detail.value)
      })
    } else {
      this.setData({
        waterAmount: 0,
      })
    }
  },
  // 定量开泵按钮
  btnOpenOnWater() {
    if (this.data.waterAmount > 0) {
      if (this.data.isOpenTiming) {
        this.quantifyOpenTime();
      } else {
        this.quantifyOpen();
      }
    } else {
      wx.showToast({
        icon: 'error',
        title: '请选择或输入灌溉时间!',
      })
    }
  },
 
 
})