xusd
7 days ago 998218675eb243d43912c203174a6b72b299c0f8
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
// components/recording/index.js
const $$ = require('../../utils/util');
const app = getApp();
 
Component({
    /**
     * 组件的属性列表
     */
    properties: {},
 
    /**
     * 组件的初始数据
     */
    countdown: null,
    recordMannager: null,
    data: {
        imgUrl: $$.url.img,
        overlayShow: false,
        second: 60, // 录音时长
        recordVisible: false, // 是否开启录音
        popup: {
            visible: false,
            content: '',
        },
        tooltipVisible: false,
    },
 
    pageLifetimes: {
        hide: function () {
            if (this.countdown) {
                clearInterval(this.countdown);
            }
            if (this.recordMannager) {
                this.recordMannager.stop();
            }
        },
    },
 
    /**
     * 组件的方法列表
     */
    methods: {
        // 使用改文字
        _handleUseWords() {
            this.triggerEvent('getwords', this.data.popup.content);
            this.setData({ popup: { visible: false, content: '' }, overlayShow: false });
        },
        // 修改文字
        _handleChangeWords(e) {
            this.data.popup.content = e.detail;
            this.setData({ popup: this.data.popup });
        },
        // 语音转文字
        _transferText(e) {
            $$.showLoading();
            let speakUrl = e.tempFilePath;
            let that = this;
            wx.uploadFile({
                url: `${$$.baseUrl}${$$.url.sys}/api/v1/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({
                                popup: {
                                    visible: true,
                                    content: data,
                                },
                                tooltipVisible: data ? true : false,
                            });
                            setTimeout(() => {
                                that.setData({ tooltipVisible: false });
                            }, 2000);
                        } else {
                            $$.showToast({ icon: 'error', title: msg });
                        }
                    } else {
                        $$.showToast({ icon: 'error', title: '录音转写失败' });
                    }
                },
            });
        },
        // 录音开始触发
        _startRecord() {
            let that = this;
            this.countdown = setInterval(() => {
                if (that.data.second - 1 <= 0) {
                    that.recordMannager.stop();
                }
                that.setData({ second: that.data.second - 1 });
            }, 1000);
            this.setData({ second: 60, recordVisible: true });
        },
        // 录音结束触发
        _endRecord(e) {
            if (this.countdown) {
                clearInterval(this.countdown);
            }
            if (60 - this.data.second <= 5) {
                $$.showToast({ title: '抱歉!录音时间过短,请重新录入' });
                this.setData({ second: 60, recordVisible: false });
                return false;
            }
            this.setData({ second: 60, recordVisible: false });
            this._transferText(e);
        },
        // 点击 开始说话 and 结束说话
        _handleStartOrEnd() {
            // 结束说话
            if (this.data.recordVisible) {
                this.recordMannager.stop();
                return false;
            }
            $$.showLoading();
            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;
                    }
                    // 开始说话
                    if (!that.data.recordVisible) {
                        const recordMannager = wx.getRecorderManager();
                        recordMannager.onStart(() => that._startRecord());
                        recordMannager.onStop((e) => that._endRecord(e));
                        recordMannager.onError((e) => {
                            $$.showToast({ title: '抱歉!录音时间过短,请重新录入' });
                            that.setData({ second: 60, recordVisible: false });
                        });
                        const options = {
                            duration: 60000,
                            sampleRate: 16000,
                            numberOfChannels: 1,
                            encodeBitRate: 96000,
                            format: 'pcm',
                        };
                        recordMannager.start(options);
                        that.recordMannager = recordMannager;
                        $$.hideLoading();
                    }
                },
            });
        },
        // 进入录音界面
        _handleOpenRecording(e) {
            this.triggerEvent('onVisible', { visible: true });
            this.setData({ overlayShow: true });
        },
        // 关闭录音界面
        _handleHiddenRecording() {
            if (this.data.recordVisible) {
                return false;
            }
            this.triggerEvent('onVisible', { visible: false });
            this.setData({ overlayShow: false });
        },
        // 关闭文字显示
        _handleClosePopup() {
            this.setData({ popup: { visible: false } });
        },
        // 阻止冒泡
        _catchtop() {},
    },
});