沙盘演示系统应用的微信小程序
zuoxiao
2024-11-04 21525f47ca011b149a3674d0926cbe495ec872c8
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
const audio = wx.createInnerAudioContext()
const recorderManager = wx.getRecorderManager()
const {
  get,
  post
} = require('../../api/request.js');
const {
  BASEURL
} = require('../../api/config')
const app = getApp()
Page({
  data: {
    isRefreshing: false,
    currentTab: 0,
    recordingSrc: '',
    isRecording: false,
    isshowVoiceMask: false, //是否显示录音中
    isShowVoiceView: false,
    voiceTime: 0, //录音时长
    maxVoiceTime: 60, //最长录音时间
    contDownTime: 0,
    line2Opcity: 1,
    line3Opcity: 1,
    gridConfig: {
      column: 3,
      width: 160,
      height: 160,
    },
    originFiles: [],
 
    contentValue: '', //反馈详情
    accSavePath: [], //上传的音频文件接口返回的地址
    photoSavePath: [], //上传的照片文件返回的地址
    phoneNumber: "",
    lat: "",
    lng: "",
    loading: false,
    hasMore: true,
    pageCurr: 1,
    pageSize: 20,
    listData: [], //已提问题列表
  },
 
  onLoad() {
    wx.getSetting({
      success: (res) => {
        if (!res.authSetting['scope.record']) {
          // 如果用户没有授权录音权限,发起授权窗口
          wx.authorize({
            scope: 'scope.record',
            success() {
              // 用户已经同意小程序使用录音功能,可以继续操作
              console.log('用户已授权录音');
            },
            fail() {
              // 用户拒绝授权,可以给出提示或再次发起授权窗口
              console.log('用户拒绝了授权录音');
            }
          });
        } else {
          // 用户已经授权录音,可以直接进行录音操作
          console.log('用户已授权录音');
        }
      }
    });
    var that = this;
    recorderManager.onStop((res) => {
      console.log('录音停止', voiceTime);
      const voiceTime = Math.floor(res.duration / 1000)
      if (voiceTime < 2) {
        this.setData({
          isShowVoiceMask: false,
          contDownTime: this.data.maxVoiceTime,
          isShowVoiceView: false
        })
        wx.showToast({
          title: '录音时间过短',
          icon: 'error',
          time: 3000
        })
      } else {
 
        that.setData({
          voiceTime: voiceTime,
          isShowVoiceMask: false,
          contDownTime: this.data.maxVoiceTime,
          isShowVoiceView: true
        })
        console.log('res', res);
        const {
          tempFilePath
        } = res; //这里松开按钮 会返回录音本地路径
        audio.src = tempFilePath
        console.log(tempFilePath);
        that.upACC(tempFilePath);
      }
 
    });
    recorderManager.onStart(() => {
      console.log('录音开始');
    });
    recorderManager.onError((err) => {
      console.log('录音错误', err);
    });
    wx.getLocation({
      type: 'wgs84', // 返回可以用于 `wx.openLocation` 的经纬度  
      success: (res) => {
        console.log('获取位置成功', res);
        this.setData({
          lat: res.latitude,
          lng: res.longitude,
        });
      },
      fail: function (err) {
        console.error('获取位置失败', err);
      }
    });
  },
  onReady() {
    this.getList();
  },
  //上传音频文件
  upACC(tempFilePath) {
    //上传录制的音频到服务器
    wx.uploadFile({
      url: BASEURL + "wx/webFile/upPhone", //接口地址
      name: 'file', //上传文件名
      filePath: tempFilePath,
      header: {
        'tag': app.globalData.tag,
        'appId': app.globalData.AppID,
      },
      success: (res) => { //后台返回给前端识别后的文字
        console.log('录音上传成功', res);
        let jsonData = JSON.parse(res.data)
        let audioObj = {
          webPath: jsonData.content.webPath,
          id: jsonData.content.id
        };
        this.setData({
          accSavePath: [audioObj]
        })
      },
      fail: (err) => {
        // 处理上传失败的逻辑  
        console.log('录音上传失败', err);
      }
    })
  },
  //删除图片
  handleRemove(e) {
    console.log("handleRemove");
    const {
      index
    } = e.detail;
    const {
      originFiles
    } = this.data;
    let imgPath = e.detail.file.url;
    const updatedFiles = this.data.originFiles.map((file) => {
      //当上传失败点击时重新上传
      if (file.url === imgPath && file.uploadTask) {
        file.uploadTask.abort();
      }
    });
    originFiles.splice(index, 1);
    this.setData({
      originFiles: originFiles,
    });
 
  },
  handleClick(e) {
    console.log("handleClick");
    let imgPath = e.detail.file.url;
    const updatedFiles = this.data.originFiles.map((file) => {
      //当上传失败点击时重新上传
      if (file.url === imgPath && file.status === "reload") {
        this.onUploadPhoto(imgPath);
      }
    });
  },
  handleAdd(e) {
    console.log("handleAdd");
    const {
      files
    } = e.detail;
    const {
      originFiles
    } = this.data;
    const updatedList = files.map(item => {
      return {
        ...item,
        status: 'loading',
        uploadTask: this.onUploadPhoto(item.url)
      }; // 保留所有其他字段,并添加 displayText 字段
    });
    // 更新列表数据
    this.setData({
      originFiles: [...originFiles, ...updatedList], // 此时设置了 fileList 之后才会展示选择的图片
    });
  },
  handleSuccess(e) {
 
  },
  /**
   * 上传图片
   */
  onUploadPhoto(imgPath) {
    const uploadTask = wx.uploadFile({
      url: BASEURL + "wx/webFile/upPhoto", // 仅为示例,非真实的接口地址
      filePath: imgPath,
      name: 'file',
      header: {
        'tag': app.globalData.tag,
        'appId': app.globalData.AppID,
      },
      success: (res) => {
        console.log('图片上传成功', res);
        let jsonData = JSON.parse(res.data)
        // 更新图片上传成功状态
        const updatedFiles = this.data.originFiles.map((file) => {
          console.log('图片上传成功》》updatedFiles', file + "++++++++++" + imgPath);
          if (file.url === imgPath) {
            return {
              ...file,
              status: undefined, // 上传成功状态
              uploadTask: undefined, // 清理上传任务
              webPath: jsonData.content.webPath,
              id: jsonData.content.id
            };
          }
          return file;
        });
        // 将创建的对象添加到images数组中  
        this.setData({
          originFiles: updatedFiles,
        })
 
      },
      fail: (err) => {
        // 处理上传失败的逻辑  
        const updatedFiles = this.data.originFiles.map((file) => {
          if (file.url === imgPath) {
            return {
              ...file,
              percent: progress,
              status: "reload", // 上传失败状态
            };
          }
          return file;
        });
        this.setData({
          originFiles: updatedFiles,
        });
      }
    });
    if (uploadTask) {
      uploadTask.onProgressUpdate((res) => {
        //处理
        const progress = Math.round((res.progress / 100) * 100)
        const updatedFiles = this.data.originFiles.map((file) => {
          if (file.url === imgPath) {
            return {
              ...file,
              percent: progress,
              status: progress < 100 ? 'loading' : undefined, // 更新状态
              uploadTask: progress < 100 ? file.uploadTask : undefined, // 保持或删除uploadTask
            };
          }
          return file;
        });
        this.setData({
          originFiles: updatedFiles,
        });
      });
    }
    return uploadTask;
  },
  //按住按钮
  startHandel() {
    this.setData({
      isShowVoiceMask: true,
      contDownTime: this.data.maxVoiceTime,
      voiceTime: 0,
      isShowVoiceView: false
    })
    console.log("开始录音")
    recorderManager.start({
      duration: 0
    })
    this.startRecordingCountdown();
  },
  //松开按钮
  endHandle() {
    this.stopRecordingCountdown();
    console.log("结束")
    //触发录音停止
    recorderManager.stop()
  },
  // 播放
  handlePlay(e) {
    // 倒计时
    let time = this.data.voiceTime
    audio.play()
    let timer1 = -1,
      timer2 = -1,
      timer3 = -1,
      timer4 = -1,
      timer5 = -1,
      timer6 = -1;
    // 第一次播放为0 第二次播放2秒钟
    timer6 = setInterval(() => {
      console.log('时间', time);
      if (time <= 0) {
        this.setData({
          line2Opcity: 1,
          line3Opcity: 1
        })
        clearInterval(timer6)
        clearTimeout(timer2)
        clearTimeout(timer3)
        clearTimeout(timer4)
        clearTimeout(timer5)
        return
      }
      timer2 = setTimeout(() => {
        console.log('timer2');
        this.setData({
          line2Opcity: 0,
          line3Opcity: 0
        })
      }, 200)
      timer3 = setTimeout(() => {
        console.log('timer3');
        this.setData({
          line2Opcity: 1,
          line3Opcity: 0
        })
      }, 400)
      timer4 = setTimeout(() => {
        console.log('timer4');
        this.setData({
          line2Opcity: 1,
          line3Opcity: 1
        })
      }, 600)
      timer5 = setTimeout(() => {
        console.log('timer5');
        this.setData({
          line2Opcity: 0,
          line3Opcity: 0
        })
      }, 800)
    }, 800)
    // 倒计时
    timer1 = setInterval(() => {
      time--;
      if (time <= 0) {
        clearInterval(timer1)
        return
      }
    }, 1000)
 
  },
  //开启定时器
  startRecordingCountdown() {
    console.log("startRecordingCountdown");
    const that = this;
    this.recordingInterval = setInterval(() => {
      const newVoiceTime = that.data.voiceTime + 1;
      const newContDownTime = that.data.contDownTime - 1;
      that.setData({
        voiceTime: newVoiceTime,
        contDownTime: newContDownTime
      });
      if (this.data.voiceTime >= this.data.maxVoiceTime) {
        that.stopRecordingCountdown();
        that.endHandle();
      }
    }, 1000);
  },
 
  stopRecordingCountdown() {
    console.log("stopRecordingCountdown");
    if (this.recordingInterval) {
      clearInterval(this.recordingInterval);
      this.recordingInterval = null;
      console.log("Recording countdown stopped");
    }
  }, // 切换 Tabs
  switchTab: function (e) {
    const tab = parseInt(e.currentTarget.dataset.tab);
    this.setData({
      currentTab: tab
    });
  },
  onPullDownRefresh() {
    this.getList(true);
  },
  //上传问题
  feelBackPost() {
    wx.showLoading({
      title: '正在提交...', // 加载提示文字
      mask: true // 是否显示透明蒙层,防止触摸穿透,默认为 false
    });
    this.data.originFiles.map((file) => {
      let imageObj = {
        webPath: file.webPath,
        id: file.id
      };
      this.setData({
        photoSavePath: [...this.data.photoSavePath, imageObj]
      })
    })
    const app = getApp();
    const data = {
      images: this.data.photoSavePath,
      audios: this.data.accSavePath,
      content: this.data.contentValue,
      lng: this.data.lng,
      lat: this.data.lat,
      clientId: app.globalData.clientId,
      phone: this.data.phoneNumber
    };
    post({
      url: "wx/issue/addIssueReport",
      data: data
    }).then(response => {
      // 处理成功响应
      console.log('请求成功:', response);
      // 加载完成后隐藏加载动画
      wx.hideLoading();
      //完成后回到首页
      wx.showToast({
        title: '提交成功',
        icon: 'success',
        duration: 2000,
        success() {}
      })
      setTimeout(() => {
        wx.navigateBack({
          delta: 1
        });
      }, 2000);
 
    }).catch(error => {
      // 加载完成后隐藏加载动画
      wx.hideLoading();
      // 处理错误响应
      console.error('请求失败:', error);
 
      this.setData({
        showErrorDialog: true,
        errorData: error.msg
      })
      // }
    });
  },
  handleDelete() {
    this.setData({
      isShowVoiceView: false,
      voiceTime: 0,
    })
  },
  //提交
  submit() {
    if (this.data.contentValue !== "" || this.data.photoSavePath.length > 0 || this.data.accSavePath !== "") {
      if (this.data.phoneNumber !== "" && this.data.phoneNumber.length !== 11) {
        wx.showToast({
          title: '联系电话错误',
          icon: 'error',
          duration: 2000,
          success() {}
        })
      } else {
        this.feelBackPost();
      }
    } else {
      wx.showToast({
        title: '请输入一项反馈内容',
        icon: 'error',
        duration: 2000,
        success() {}
      })
    }
 
  },
  //监听手机号的输入
  phoneInput(e) {
    this.setData({
      phoneNumber: e.detail.value
    });
  },
  //监听
  contentInput(e) {
    this.setData({
      contentValue: e.detail.value
    });
  },
  //获取已提问题列表
  getList(isRefresh) {
    if (isRefresh) {
      this.setData({
        isRefreshing: false,
        pageCurr: 1,
        listData:[]
      });
    }
    const app = getApp();
    const params = {
      url: 'wx/issue/getIssueReports',
      data: {
        clientId: app.globalData.clientId
      }
    };
    get(params).then(data => {
      const updatedList = data.content.obj.map(item => {
        if (item.replyTime === "" || item.replyTime === null) {
          item.replyTime = "未回复"
        }
        return item;
      });
      this.setData({
        listData: [...this.data.listData, ...updatedList],
        isRefreshing: false, // 将triggered属性设置为false,表示下拉刷新已完成
        isWXRefreshing: false, // 将triggered属性设置为false,表示下拉刷新已完成
        loading: false,
        hasMore: this.data.pageCurr < data.content.pageTotal
      })
      this.updateDisplayText();
    }).catch(err => {
      // 错误回调
      this.setData({
        isRefreshing: false, // 将triggered属性设置为false,表示下拉刷新已完成
        isWXRefreshing: false, // 将triggered属性设置为false,表示下拉刷新已完成
        loading: false
      })
      wx.showToast({
        title: err.msg,
        icon: 'error',
        duration: 3000
      })
    });
  },
  //加载更多
  loadMore() {
    if (this.data.hasMore && !this.data.loading) {
      this.setData({
        loading: true,
        pageCurr: this.data.pageCurr + 1
      })
      this.getList();
    }
  },
  onDelete(e) {
    const item = e.currentTarget.dataset.item;
    const that = this;
    wx.showLoading({
      title: '正在删除请稍候...', // 加载提示文字
      mask: true // 是否显示透明蒙层,防止触摸穿透,默认为 false
    });
    const data = {
      issueReportId: item.issueReportId, //取水口ID
      clientId: app.globalData.clientId, //阀控器地址
    };
    post({
      url: "wx/issue/deleteIssueReport",
      data: data,
      timeout: 180000
    }).then(response => {
      // 处理成功响应
      console.log('请求成功:', response);
      // 加载完成后隐藏加载动画
      wx.hideLoading();
      //重新获取列表刷新数据
      this.getList(true);
    }).catch(error => {
      // 加载完成后隐藏加载动画
      wx.hideLoading();
      // 处理错误响应
      console.error('请求失败:', error);
    });
  }
 
});