jt
2024-09-06 91cefc81af0efef669d0d4f4dc9d2ea1981753fd
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
(function($, wvs) {
    var mwvs = function(uii, cback) {
        var dst;
        var src;
        var recordCancel = false;
        var recorder = null;
        var audio_tips = document.getElementById("audio_tips");
        var startTimestamp = null;
        var stopTimestamp = null;
        var stopTimer = null;
        var path1 = null;
        if(mui.os.android) {
            var format = "amr";
        } else if(mui.os.ios) {
            var format = "wav";
        }
        var MIN_SOUND_TIME = 800; //录音使用变量
        var rate = "16000";
        var channel = "1";
        var cuid = '';
        var speech = '';
        var token_url = "https://aip.baidubce.com/oauth/2.0/token?";
        var len = '';
        var grant_type = "client_credentials";
        var APIkey = "jDBhIehFi6fAISLx01y2oeTY";
        var SecretKey = "BtA42GbxGr6vfGppc8spPc5k62bz2keT";
        var AppID = "14292369";
        var access_token = "";
        var fileArr = [];
        if(!uii)
            return;
        var ui = uii;
 
        function msgTextFocus() {
            ui.boxMsgText.focus();
            setTimeout(function() {
                ui.boxMsgText.focus();
            }, 150);
        }
        var setSoundAlertVisable = function(show) {
            if(show) {
                ui.boxSoundAlert.style.display = 'block';
                ui.boxSoundAlert.style.opacity = 1;
            } else {
                ui.boxSoundAlert.style.opacity = 0; //fadeOut 完成再真正隐藏
                setTimeout(function() {
                    ui.boxSoundAlert.style.display = 'none';
                }, 200);
            }
        };
 
        ui.boxMsgSound.addEventListener('hold', function(event) {
            if(!ui.boxMsgSound.classList.contains('holdonmsg'))
                ui.boxMsgSound.classList.add('holdonmsg');
            recordCancel = false;
            if(stopTimer) clearTimeout(stopTimer);
            audio_tips.innerHTML = "倾听中...";
            ui.boxSoundAlert.classList.remove('rprogress-sigh');
            setSoundAlertVisable(true);
            recorder = plus.audio.getRecorder();
            if(recorder == null) {
                plus.nativeUI.toast("不能获取录音对象");
                return;
            }
            startTimestamp = (new Date()).getTime();
            recorder.record({
                format: 'wav',
                samplerate: '16000',
                filename: "_doc/audio/"
            }, function(path) {
                fileArr.push(path); //录音完成 进入队列 方便删除本地录音。  
                if(recordCancel) return;
                Audio2dataURL(path); //语音转base64 发送百度
            }, function(e) {
                plus.nativeUI.toast("录音时出现异常: " + e.message);
            });
        }, false);
        ui.body.addEventListener('drag', function(event) {
            console.log('drag');
            if(Math.abs(event.detail.deltaY) > 50) {
                if(!recordCancel) {
                    recordCancel = true;
                    if(!audio_tips.classList.contains("cancel")) {
                        audio_tips.classList.add("cancel");
                    }
                    audio_tips.innerHTML = "松开手指,取消发送";
                }
            } else {
                if(recordCancel) {
                    recordCancel = false;
                    if(audio_tips.classList.contains("cancel")) {
                        audio_tips.classList.remove("cancel");
                    }
                    audio_tips.innerHTML = "手指上划,取消发送";
                }
            }
        }, false);
        ui.boxMsgSound.addEventListener('release', function(event) {
            if(ui.boxMsgSound.classList.contains('holdonmsg'))
                ui.boxMsgSound.classList.remove('holdonmsg');
            if(audio_tips.classList.contains("cancel")) {
                audio_tips.classList.remove("cancel");
                audio_tips.innerHTML = "倾听中...请继续说...";
            }
            //
            stopTimestamp = (new Date()).getTime();
            if(stopTimestamp - startTimestamp < MIN_SOUND_TIME) {
                audio_tips.innerHTML = "录音时间太短";
                ui.boxSoundAlert.classList.add('rprogress-sigh');
                recordCancel = true;
                stopTimer = setTimeout(function() {
                    setSoundAlertVisable(false);
                }, 800);
            } else {
                audio_tips.innerHTML = "识别中...";
                //                    setSoundAlertVisable(false);
            }
            recorder.stop();
        }, false);
        ui.boxMsgSound.addEventListener("touchstart", function(e) {
            e.preventDefault();
        });
        ui.photoimg.addEventListener('tap', function(event) {
            var btnArray = [{
                title: "拍照"
            }, {
                title: "从相册选择"
            }];
            plus.nativeUI.actionSheet({
                title: "选择照片来源",
                cancel: "取消",
                buttons: btnArray
            }, function(e) {
                var index = e.index;
                switch(index) {
                    case 0:
                        break;
                    case 1:
                        var cmr = plus.camera.getCamera();
                        cmr.captureImage(function(path) {
                            files = [];
                            files.push(path)
                            uploadd(files);
                        }, function(err) {
 
                        });
                        break;
                    case 2:
                        plus.gallery.pick(function(path) {
                                files = [];
                                files = path.files;
                                uploadd(files);
                            },
                            function(e) {
 
                            }, {
                                filter: "image",
                                multiple: true,
                                system: false,
                                maximum: 10,
                                onmaxed: function() {
                                    plus.nativeUI.alert('最多只能选择' + 10 + '张图片');
                                }
                            });
                        break;
                }
            });
        }, false);
        ui.boxMsgText.addEventListener('input', function() {
            var msg = ui.boxMsgText.innerHTML.replace(/(^\s*)|(\s*$)/g, "");
            console.log(msg);
            if(msg) {
                ui.speetext.style.display = 'none';
                ui.sendmsgg.style.display = '';
            } else {
                ui.sendmsgg.style.display = 'none';
                ui.speetext.style.display = 'block';
            }
        })
 
        /**
         * 录音语音文件转base64字符串
         * @param {Object} path
         */
        function Audio2dataURL(path) {
            plus.io.resolveLocalFileSystemURL(path, function(entry) {
                entry.file(function(file) {
                    var reader = new plus.io.FileReader();
                    reader.onloadend = function(e) {
                        var strResult = e.target.result;
                        var index = strResult.indexOf('base64,') + 7;
                        var base64Str = strResult.slice(index, strResult.length);
                        delVoiceFile(); //删除本地语音
                        len = file.size;
                        if(!app.isNetWork()) {
                            alert("网络异常");
                        } else {
                            voiceToWord(base64Str);
                        }
                    };
                    reader.readAsDataURL(file);
                }, function(e) {
                    alert("读写出现异常: " + e.message);
                })
            })
        }
        token();
 
        function token() {
            if(access_token)
                return;
            mui.ajax('https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id=' + APIkey + '&client_secret=' + SecretKey, {
                //data: data2,
                type: 'post',
                contentType: "application/json; charset=utf-8",
                timeout: 5000,
                success: function(resp) {
                    //console.log(JSON.stringify(resp));
                    access_token = resp.access_token;
                },
                error: function(xhr, type, errorThrown) {
                    console.log(type + "网络请求出错");
                }
            });
        }
        //将语音转成文字
        function voiceToWord(speech) {
            var data1 = {
                "rate": rate,
                "format": format,
                "channel": channel,
                "cuid": AppID,
                "token": access_token,
                "speech": speech,
                "dev_pid": 1536,
                "len": len
            };
            mui.ajax('http://vop.baidu.com/server_api', {
                data: data1,
                type: 'post',
                contentType: "application/json; charset=utf-8",
                timeout: 5000,
                success: function(resp) {
                    //                        alert(JSON.stringify(resp));
                    if(resp.result == undefined || resp.result == '') {
                        audio_tips.innerHTML = "没听清,再说一遍";
                        stopTimer = setTimeout(function() {
                            setSoundAlertVisable(false);
                        }, 800);
                        return;
                    }
                    if(ui.speetext.children[0].classList.contains('mui-icon-compose')) {
                        ui.boxMsgText.style.display = 'block';
                        ui.boxMsgSound.style.display = 'none';
                        ui.speetext.children[0].classList.remove('mui-icon-compose');
                    }
                    setSoundAlertVisable(false);
                    ui.speetext.style.display = 'none';
                    ui.sendmsgg.style.display = '';
                    ui.boxMsgText.innerHTML = resp.result[0];
                },
                error: function(xhr, type, errorThrown) {
                    if(type == 'timeout') {
                        pageUtils.showToast("录音超时");
                    } else {
                        pageUtils.showToast("网络请求出错");
                    }
                }
            });
        }
        //这里录音发送完成就不需要了,所以没有必要存在手机里,直接删除音频
        function delVoiceFile() {
            //return; //先测试不删除
            for(var i = 0; i < fileArr.length; i++) {
                plus.io.resolveLocalFileSystemURL(fileArr[i], function(entry) {
                    if(entry.isFile) {
                        setTimeout(function() {
                            entry.remove(function(entry) {
                                fileArr.pop();
                                //alert('删除成功');
                            }, function(e) {
                                alert('语音缓存删除失败' + e.message);
                            });
                        }, 500);
 
                    }
                }, function(e) {
                    alert("Resolve file URL failed: " + e.message);
                });
            }
        }
        //讨论 上传图片
        function uploadd(files) {
            for(var i = 0; i < files.length; i++) {
                file = files[i];
                var b = (i + 1 == files.length) ? true : false;
                audio_tips.innerHTML = "发送中...";
                setSoundAlertVisable(true);
                var server = interaddress + 'simditor/upload';
                var utask = plus.uploader.createUpload(server, //Indexx
                    {
                        method: "POST"
                    },
                    function(t, status) {
                        console.log(b)
                        if(b)
                            setSoundAlertVisable(false);
                        //上传完成
                        if(status == 200) {
                            console.log(t.responseText);
                            var ret = JSON.parse(t.responseText);
                            if(ret.success == true) {
                                //                                var imgurl = ret.file_path.replace(ret.file_path.match(new RegExp('/(\\w+).?'))[0], interaddress);
                                var img = '<img alt="" src="' + ret.file_path + '" />';
                                cback({
                                    img: img,
                                    isall: b
                                });
                            } else {
                                mui.alert(ret.msg);
                            }
                        } else {
                            if(utask) {
                                utask.abort();
                                utask = null;
                            }
                        }
                    });
                utask.addEventListener("statechanged", function(tas, status) {
                    if(!utask) {
                        return;
                    }
                    var time = new Date();
                    switch(tas.state) {
                        case 3: // 已接收到数据
                            var B = (parseInt(tas.uploadedSize) / parseInt(tas.totalSize)).toFixed(2) * 100;
                            audio_tips.innerHTML = "发送中..." + B;
                            break;
                    }
                });
                utask.addFile(file, {
                    key: 'upfile'
                });
                utask.start();
            }
        }
    }
    window.voicespeech = mwvs;
})(mui, window.voicespeech = {})