FSound_wev8.js
11.1 KB
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
define(['mUtil', "Component","wev-loading", "audioHelper"], function (mUtil, Component, WevLoading, audioHelper) {
var FSound = function (options) {
Component.super(this, options);
var _sound = {}, $audiostatus, $field, $comp, $wevField, currentStatus;
this.type = "FSound";
this.tpl = this.type + "_html";
this.css = this.type + "_css";
this.components = {
loading: new WevLoading({
text: " ",
clsName: "wev-audio-loading wev-audio-divhidden"
})
};
var vm = this.viewModel = {
form: "",
field: {
label: "",//显示名称
name: "", //对应字段
value: "",//字段值
defaultValue: ""
},
required: false,
readonly: false
};
this.beforeMount = function () {
if(!$p("billid") && vm.field.value == "" && vm.field.defaultValue != ""){
vm.field.value = vm.field.defaultValue;
}
var clienttype = "browser";
if (mUtil.checkEmpJsApi("getAudioRecord")) {
clienttype = "emp";
}else if (mUtil.runtime.isEmobile6()) {
clienttype = "emobile";
} else if (mUtil.isFunction(window.eb_startRecord)) {
clienttype = "wechat";
}
vm.client = clienttype;
vm.errorclass = "error";
vm.playclass = "playing";
vm.field.dflabel = vm.field.label || mUtil.getLabel(128154, "语音");
};
this.mounted = function () {
var that = this;
$comp = that.$el.children(".wev-comp-" + that.type);
$audiostatus = $(".wev-audio-status", $comp);
$field = $("input[name='fieldname_" + vm.field.name + "']", $comp);
$wevField = this.$el.find(".wev-field");
if(!$p("billid") && !vm.readonly){
_sound.statusChange("init");
}else if(!$p("billid") && vm.readonly && !vm.field.value){// 新建布局空值且只读
this.reset();
}else{
_sound.setTipsClass(false);
}
if(!vm.readonly){
$(".wev-audio-container", $comp).on('click', function () {
switch (currentStatus) {
case "record":
window.sound_viewmodelid = vm.id;
audioHelper.pauseAllAudios();
if(vm.client == "emp"){
mUtil.invokeEmApi("getAudioRecord", {
success: function(res){
var localData = res.localData;
that.parseSound(localData);
}
});
}else if(vm.client == "wechat"){
_sound.statusChange("record");
eb_startRecord(vm.id);
}else{
location = "emobile:speech:_p_addSound_uploaded:" + vm.id + "_" + ((new Date()).valueOf());
}
break;
case "recording":
currentStatus = "notsupport";//避免再次点击
$audiostatus.removeClass(vm.playclass);
_sound.setTipsClass(false);
eb_stopRecord("_p_addSound_uploaded"); break;
case "notsupport":
case "ready":
break;
default:
_sound.statusChange("error");
break;
}
});
}
$(".wev-clear-btn", $comp).on('click', function () {
$('audio', $comp).remove();
$('.wev-audio', $comp).removeAttr('data-audio');
$('.wev-audio-container', $comp).off('click.listenaudio');
$field.val("");
_sound.statusChange("init");
});
};
this.getData = function () {
return mUtil.parseJSON("fieldname_" + vm.field.name, $field.val());
};
this.reset = function () {
var fieldValue = vm.field.value;
$field.val(fieldValue);
if (fieldValue) {
if(!isNaN(fieldValue)){
_sound.getFileId(fieldValue, function(fileId){_sound.eventsBind(fileId);});
} else {
_sound.eventsBind(fieldValue);
}
} else {
_sound.setTipsClass(true);
if (!vm.readonly) {
_sound.statusChange("init");
} else {
mUtil.getLabel(5289, "语音为空",function(msg){
$(".wev-audio-tips", $comp).text(msg);
});
currentStatus = "notsupport";
}
}
$wevField.removeClass("wev-required-remind");
};
this.checkRequired = function () {
var required = vm.required && !$field.val();
$wevField.toggleClass("wev-required-remind", required);
return required && vm.field.dflabel;
};
this.setRequired = function (required) {
vm.required = required;
$wevField.toggleClass("wev-field-required", required);
!required && $wevField.removeClass("wev-required-remind");
};
this.checkModeDocCategory = function(issetModeDocCategory){
if(!vm.readonly && !issetModeDocCategory){
var $dom = this.$el,
$container = $dom.find('.wev-audio-container');
mUtil.getLabel(388817, "附件上传目录未设置!",function(msg){
$container.addClass("wev-upload-hide");
$dom.find(".wev-upload-tip").removeClass("wev-upload-hide").html(msg);
});
}
};
_sound.eventsBind = function (audioPath) {
var $audioHtml = $('<audio preload="auto"><source></audio>'),
$audiotips = $(".wev-audio-tips", $comp),
$wevAudio = $('.wev-audio', $comp),
$audioSource = $audioHtml.children('source'),
loadedMeta = false;
$wevAudio.data('audio', audioPath);
if(!isNaN(audioPath)){
var formIns = mUtil.getInstance(vm.form), empowStr = formIns ? formIns.viewModel.empowStr : '';
audioPath = mUtil.getAttachUrl(audioPath, empowStr);
$audioSource.attr('type', 'audio/mpeg');
} else if(!/\.(wav|mp3|wma|asf|ogg|au)$/i.test(audioPath)) {
$audioSource.attr('type', 'audio/mpeg');
}
$audioSource.attr('src', audioPath);
if (!vm.readonly) {
$(".wev-clear-btn", $comp).addClass("wev-audio-clearshow");
}
$('.wev-audio-container', $comp).append($audioHtml);
$audioHtml[0].addEventListener('loadedmetadata', function(){
if(loadedMeta){//只触发加载一次
return;
}
loadedMeta = true;
_sound.setTipsClass(true);
mUtil.getLabel(5291, "点击播放",function(msg){
$audiotips.text(msg);
});
this.setAttribute('loaded', '');
currentStatus = 'ready';
});
audioHelper.playAudio($wevAudio[0]);
};
this.setValueByDB = function(value){
vm.field.value = value;
this.reset(value);
};
this.parseSound = function(audioKey){
$audiostatus.removeClass(vm.playclass);
_sound.setTipsClass(false);
$(".wev-clear-btn", $comp).addClass("wev-audio-clearshow");
var $formObj = $("#" + vm.form);
var workflowid = $formObj.find("input[name='workflowid']").val();
var modelid = $formObj.find("input[name='modelid']").val();
var formData = new FormData();
formData.append("workflowid", workflowid);
formData.append("modelid", modelid);
formData.append("audioBase64", audioKey);
var actionUrl = mUtil.getActionUrl(this.type, { action: "parseSound", client: vm.client }, this.pageid);
mUtil.ajax(actionUrl, formData, function (responseText) {
if (!responseText) {
mUtil.getLabel(5298, "语音数据提交后服务端返回值为空",function(msg){
console.error(msg);
});
return;
}
var result = $.parseJSON(responseText);
var status = result.status, data = result.data;
if (status == "1") {
if(data.audioPath){
$field.val(data.audioPath);
_sound.getFileId(data.audioPath, function(fileId){_sound.eventsBind(fileId);});
}else{
_sound.setTipsClass(true);
$(".wev-audio-tips", $comp).text("语音加载为空");
currentStatus = "notsupport";
}
} else if (status == "0") {
_sound.setTipsClass(true);
mUtil.getLabel(5299, "语音数据上传失败,请联系管理员查看。",function(msg){
$(".wev-audio-tips", $comp).text(msg);
Mobile_NS.msg(msg);
});
}
}, {
type: "POST",
processData: false,
contentType: false,
error: function (responseText) {
mUtil.getLabel(5300, "语音数据上传出错:",function(msg){
Mobile_NS.alert(msg + JSON.stringify(responseText));
});
}
});
};
_sound.statusChange = function (status) {
var $audiotips = $(".wev-audio-tips", $comp);
if (status == "error") {
$audiostatus.removeClass(vm.playclass).addClass(vm.errorclass);
var audio = $('audio', $comp).length && $('audio', $comp)[0];
if(audio){
audio.pause();
audio.currentTime = 0;
}
$audiotips.text("");
} else if (status == "init") {
$(".wev-audio-time", $comp).text("");
$audiostatus.removeClass(vm.playclass).removeClass(vm.errorclass);
$(".wev-clear-btn", $comp).removeClass("wev-audio-clearshow");
_sound.setTipsClass(true);
if (vm.client == "emp" || vm.client == "emobile" || vm.client == "wechat") {//emobile || wechat
status = "record";
mUtil.getLabel(5326, "开始录制", function (msg) {
$audiotips.text(msg);
});
} else {//其他暂不支持录音
status = "notsupport";
mUtil.getLabel(5327, "暂不支持录音", function (msg) {
$audiotips.text(msg);
});
//todo
}
} else if (status == "record") {//增加云桥录制动画
status = "recording";
$audiostatus.addClass(vm.playclass).removeClass(vm.errorclass);
mUtil.getLabel(5329, "录制中,点击结束", function (msg) {
$audiotips.text(msg);
});
} else {
$audiostatus.removeClass(vm.playclass).addClass(vm.errorclass);
mUtil.getLabel(5330, "播放异常", function (msg) {
$audiotips.text(msg);
});
}
currentStatus = status;
};
_sound.setTipsClass = function (showOrnot) {
this.components.loading.toggle(!showOrnot);
$(".wev-audio-tips", $comp).toggleClass("wev-audio-divhidden", !showOrnot);
};
_sound.getFileId = function(docIds, callback) {
var actionUrl = mUtil.getActionUrl(this.type, { action: "getFileIds", values: docIds }, this.pageid);
mUtil.getJSON(actionUrl, function (result) {
callback && callback(result.data && result.data[0]);
});
}
_sound.setTipsClass = _sound.setTipsClass.bind(this);
_sound.getFileId = _sound.getFileId.bind(this);
};
window._p_addSound_uploaded = function (result) {
try {
var mecid = window.sound_viewmodelid;
var instance = mUtil.getInstance(mecid);
if (result && instance) {
instance.parseSound(result);
}
} catch (e) {
mUtil.getLabel(5301, "语音插件异常",function(msg){
Mobile_NS.alert(msg + " >> " + e.message);
});
}
};
return Component.init(FSound);
});