沙盘演示系统应用的微信小程序
zuoxiao
2024-10-25 917252ef3ea2b63c74d162cc67a6fbe103cb9b4d
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
const audio = wx.createInnerAudioContext()
const recorderManager = wx.getRecorderManager()
const {
  BASEURL
} = require('../../api/config')
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: [],
    feedBackList: Array(19).fill({
      createTime: "2023-05-06 12:36:25",
      responseTime: "2023-05-07 12:36:25"
    }),
    contentValue: '', //反馈详情
    accSavePath: '', //上传的音频文件接口返回的地址
    photoSavePath: [] //上传的照片文件返回的地址
  },
 
  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);
    });
  },
  //上传音频文件
  upACC(tempFilePath) {
    //上传录制的音频到服务器
    wx.uploadFile({
      url: BASEURL + "wx/webFile/upPhone", //接口地址
      name: 'file.acc', //上传文件名
      filePath: tempFilePath,
      success: function (res) { //后台返回给前端识别后的文字
 
      }
    })
  },
  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,
    });
 
  },
  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(e.detail.files[0].url)
      }; // 保留所有其他字段,并添加 displayText 字段
    });
    // 更新列表数据
    this.setData({
      originFiles: [...originFiles, ...updatedList], // 此时设置了 fileList 之后才会展示选择的图片
    });
  },
  handleSuccess(e) {
 
  },
  /**
   * 上传图片
   */
  onUploadPhoto(imgPath) {
    const uploadTask = wx.uploadFile({
      url: BASEURL + "wx/webFile/upPhone", // 仅为示例,非真实的接口地址
      filePath: imgPath,
      name: 'file',
      success: () => {
 
      },
      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.setData({
      isRefreshing: false
    });
  },
  feelBack() {
    wx.showLoading({
      title: '正在提交...', // 加载提示文字
      mask: true // 是否显示透明蒙层,防止触摸穿透,默认为 false
    });
    const app = getApp();
    const data = {
      content: this.data.contentValue, //取水口ID
      image: this.data.vcId, //虚拟卡ID
      operator: app.globalData.sessionId, //操作员
      forceOpen: !!isforce // 使用逻辑非操作符 !! 来确保 isForce 是布尔值  
    };
    post({
      url: "operation/feedback/add",
      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
      })
      // }
    });
  },
  handleDelete() {
    this.setData({
      isShowVoiceView: false,
      voiceTime: 0,
    })
  }
 
});