yuminge-app/yun-min-program-plugin-master/packageC/components/audio-upload/audio-upload.js

232 lines
5.8 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// packageC/components/audio-upload/audio-upload.js
var app = getApp();
Component({
/**
* 组件的属性列表
*/
properties: {
},
/**
* 组件的初始数据
*/
data: {
show: false,
time: 0,
localId: null, //返回音频的本地ID
recordStop: true,
timer: null,
duration: 0, //录音时长
sec: 0,
min: 0,
recorderManager: null
},
lifetimes: {
// 生命周期函数可以为函数或一个在methods段中定义的方法名
attached() {
this.data.recorderManager = wx.getRecorderManager();
this.data.recorderManager.onError((res) => {
console.log(res);
this.onDurationUpdate('clear');
app.tips('录音失败!');
});
this.data.recorderManager.onStop((res) => {
if (!this.data.cancelStatus) {
this.setData({
recordStop: true,
src: res.tempFilePath,
localId: res.tempFilePath
});
console.log(res.tempFilePath, '上传的路径');
app.tips('录音完成!');
this.upVoice();
} else {
app.tips('已取消录音!');
this.setData({
recordStop: true,
show: false,
duration: 0,
sec: 0,
min: 0
});
}
this.data.cancelStatus = false;
this.onDurationUpdate('clear');
});
this.data.recorderManager.onStart((res) => {
this.setData({
recordStop: false
});
this.onDurationUpdate('updata');
app.tips('开始录音不宜超过10分钟');
});
this.data.recorderManager.onPause((res) => {
this.setData({
recordStop: true
});
this.onDurationUpdate('clear');
app.tips('录音已暂停');
});
this.data.recorderManager.onResume((res) => {
this.setData({
recordStop: false
});
this.onDurationUpdate('updata');
app.tips('录音继续');
});
},
moved() {},
detached() {
if (this.data.timer) this.onDurationUpdate('clear');
},
},
/**
* 组件的方法列表
*/
methods: {
getAudioFile() {
wx.chooseMessageFile({
count: 1,
type: 'file',
extension: [".mp3", "mp3"],
success: (res) => {
this.uploadAudioM(res.tempFiles[0].path);
},
fail: (err) => {}
});
},
uploadAudioM(videoData) {
let urlStr = app.getNetAddresss("upload.uploadPic");
urlStr += "&upload_type=audio";
wx.showLoading({
title: '上传中',
});
wx.uploadFile({
url: urlStr, //仅为示例,非真实的接口地址
filePath: videoData,
name: "file",
formData: {},
success: (resdata) => {
let res = JSON.parse(resdata.data);
this.triggerEvent('uploaderAudio', res.data);
if (res.result != 1) app.tips(res.msg);
},
complete: () => {
wx.hideLoading();
}
});
},
openRecord() {
this.setData({
show: true
});
},
onDurationUpdate(status) {
if (status == 'updata') {
this.data.timer = setInterval(() => {
if (this.data.duration >= 599) {
this.stopRecord();
return;
}
this.data.duration += 1;
this.transformTimes();
}, 1000);
} else {
if (this.data.timer) clearInterval(this.data.timer);
}
},
cancel() {
this.data.cancelStatus = true;
this.data.recorderManager.stop();
},
stopRecord() {
this.data.recorderManager.stop();
},
startRecord() {
if (this.data.duration > 0) {
this.data.recorderManager.resume();
return;
}
this.data.recorderManager.start({
format: 'mp3'
});
},
pauseRecord() {
this.data.recorderManager.pause();
},
upVoice() {
let wx_token = wx.getStorageSync('wx_token');
let urlStr = app.getNetAddresss('upload.uploadPic');
urlStr += "&upload_type=audio";
wx.showToast({
icon: 'loading',
title: '正在上传'
}),
wx.uploadFile({
url: urlStr,
filePath: this.data.localId,
name: 'file',
header: {
"Content-Type": "multipart/form-data",
Cookie: "PHPSESSID=" + wx_token,
},
formData: {
// 和服务器约定的token, 一般也可以放在header中
'_wx_token': wx.getStorageSync('wx_token')
},
success: (res) => {
var data = JSON.parse(res.data);
if (data.result == 1) {
wx.showModal({
title: '提示',
content: data.msg,
showCancel: false
});
let _flie = data.data;
_flie.duration = this.data.duration;
this.triggerEvent('uploaderAudio', _flie);
this.setData({
recordStop: true,
show: false,
duration: 0,
sec: 0,
min: 0
});
} else {
wx.showModal({
title: '提示',
content: data.msg || '错误',
showCancel: false,
});
}
wx.hideToast();
},
fail: function (res) {
console.log(res);
wx.showModal({
title: '提示',
content: '网络请求失败,请确保网络是否正常',
showCancel: false,
success: function (res) {}
});
}
});
},
transformTimes() {
this.data.sec += 1;
this.setData({
sec: this.data.sec
});
if (this.data.sec >= 60) {
this.data.min += 1;
this.setData({
sec: 0,
min: this.data.min
});
}
}
}
});