forked from gzzfw/frontEnd/gzDyh

zhangyongtian
2024-09-13 899e81654c9389785d58f9dbdf2ea7d2b2bc9082
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
// pages/speechToText/index.js
const $$ = require('../../utils/util');
const app = getApp();
 
Page({
 
  /**
   * 页面的初始数据
   */
  recordMannager: wx.getRecorderManager(),
  data: {
    imgUrl: $$.url.img,
    key: '',
    value: '',
    showModal: false, //按住说话显示
  },
 
  // 录音结束触发
  _endRecord(e) {
    this._transferText(e);
  },
 
  touchStart(e) {
    let that = this;
    wx.getSetting({
      success(res) {
        if (res.authSetting['scope.record'] === false) {
          $$.hideLoading();
          $$.showModal({
            content: '抱歉!此功能需授权麦克风录音功能',
            confirmText: '跳转授权',
            success: (res) => {
              if (res.confirm) {
                wx.openSetting({
                  success(res) {
                    if (res.authSetting['scope.record']) {
                      $$.showToast({
                        title: '授权成功'
                      });
                    } else {
                      $$.showToast({
                        title: '授权失败'
                      });
                    }
                  },
                });
              }
            },
          });
          return false;
        }
        that.setData({
          showModal: true
        });
        // 开始说话
        const options = {
          duration: 60000,
          sampleRate: 16000,
          numberOfChannels: 1,
          encodeBitRate: 96000,
          format: 'pcm',
        };
        that.recordMannager.start(options);
        that.recordMannager.onStart(() => console.log('开始录音'));
        that.recordMannager.onError((e) => {
          console.log('onError', e);
          $$.showToast({
            title: '抱歉!录音时间过短,请重新录入'
          });
          that.setData({
            second: 60,
            showModal: false
          });
        });
 
      },
    });
  },
 
  touchEnd() {
    let that = this;
    that.recordMannager.onStop((e) => that._endRecord(e));
    that.recordMannager.stop();
    that.setData({
      showModal: false
    })
    console.log('结束录音');
  },
 
 
 
  // 语音转文字
  _transferText(e) {
    console.log('开始识别', e);
    $$.showLoading();
    let speakUrl = e.tempFilePath;
    let that = this;
    wx.uploadFile({
      url: `${$$.baseUrl}${$$.url.sys}/api/wechat/xfyun/speech`,
      filePath: speakUrl,
      name: 'fileNames',
      header: {
        Authorization: app.globalData.token
      },
      complete(res) {
        $$.hideLoading();
        if (res.errMsg === 'uploadFile:ok') {
          const {
            code,
            data,
            msg
          } = JSON.parse(res.data);
          if (code === '0' || code === 0) {
            that.setData({
              value: that.data.value + data || '',
            });
          } else {
            $$.showToast({
              icon: 'error',
              title: msg
            });
          }
        } else {
          $$.showToast({
            icon: 'error',
            title: '录音转写失败'
          });
        }
      },
    });
  },
 
  // 表单修改
  handleChange(e) {
    let key = e.currentTarget.dataset.key,
      value = e.detail;
    this.setData({
      value: value,
      [key]: value.length
    });
  },
 
  // 下一步 or 上一步
  handleNext(e) {
    // 获取当前页面栈
    var pages = getCurrentPages();
 
    console.log('this.data.value', this.data.value);
    console.log('this.data.key', this.data.key);
    // 上一个页面
    var prevPage = pages[pages.length - 2];
    // 直接设置数据对象到上一页面的data中
    prevPage.setData({
      twoValue: this.data.value,
      twoKey: this.data.key,
    });
    wx.navigateBack({
      delta: 1,
    })
 
  },
 
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let {
      type
    } = options
    console.log('type', type);
    this.setData({
      key: type
    })
  },
})