const app = getApp(); //创建录音管理 const recorderManager = wx.getRecorderManager(); //创建播放管理器 const innerAudioContext = wx.createInnerAudioContext(); innerAudioContext.autoplay = false; var filescount = 0; var files = []; var uploadFlag = 0; //1=上传出错 0=正常 var filesindex = 0; Page({ /** * 页面的初始数据 */ data: { mode: 0, taskContent: { title: '任务内容', showImg: '/images/icons/recording.png', playImg: '/images/icons/play.png', blImg: '/images/icons/bl.png', readingImg: '/images/icons/recording.png', luyinzhongImg: '/images/icons/luyinzhong.png', stopPlayImg: '/images/icons/zanting.png', delimg: '/images/icons/newdel.png', taskRadioType: '1', radioUrl: '', second: 10 }, rightImage:"/images/icons/toRight.png", images:[], videos:[], voices: '', imagesAdd: '/images/icons/camera.png', wyPhone:"", depId:0, content:"", depName:"", repairPriority:2, repairPriorityName:'普通', form:{ images: [], videos: [], voices: [] }, }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { let that = this; this.setData({ mode: options.mode }) wx.setNavigationBarTitle({ title: options.name }) recorderManager.onStart(() => { var taskContent = that.data.taskContent; taskContent.showImg = taskContent.luyinzhongImg; taskContent.taskRadioType = '2'; that.setData({ taskContent: taskContent }) }) recorderManager.onPause(() => { }) recorderManager.onStop((res) => { var path = res.tempFilePath; var task = that.data.taskContent; task.taskRadioType = '3'; task.showImg = task.playImg; task.second = Math.ceil(res.duration / 1000); that.setData({ voices: path, taskContent: task }); // const { tempFilePath } = res }) recorderManager.onError((res) => { wx.showToast({ title: '录音失败', icon: 'none', duration: 2000 }); var task = that.data.taskContent; task.taskRadioType = '1'; task.showImg = task.readingImg; that.setData({ voices: '', taskContent: task }); }); this.getUserInfo(); }, gotopage: function(e){ var url = e.currentTarget.dataset.url; var type = e.currentTarget.dataset.type; var title = e.currentTarget.dataset.title; app.gotopage(url, { title: title, type: type }); }, /** * 生命周期函数--监听页面显示 */ onShow: function () { var arr = []; var that = this; app.ajaxReadyCallback = res => { var apiname = res.data.apiname; if(apiname == 'wyConfig'){ that.setData({ wyPhone:res.data.data.orgPhone }) }else if(apiname == 'addorder') { wx.showToast({ title: '提交成功!', icon: 'success', duration: 1500 }); setTimeout(function () { wx.navigateBack({ delta: 1 }); }, 1500); }else if(apiname == 'userInfo') { that.setData({ depId:res.data.data.depId, depName:res.data.data.depName, }) } } this.wyConfig(); }, callPhone(){ var that = this; wx.makePhoneCall({ phoneNumber: that.data.wyPhone, }) }, wyConfig(){ app.ajax({ url: app.globalData.serverUrl + '/org/queryConfig', type: 'POST', apiname: 'wyConfig', data: { } }); }, btnsubmit(){ var that = this; // if(!this.data.content){ // wx.showToast({ // title: '请输入内容', // icon: 'none', // duration: 2000 // }); // return ; // } if(this.data.depId==0){ wx.showToast({ title: '请选择部门', icon: 'none', duration: 2000 }); return ; } //数据初始化 files = []; filescount = 0; uploadFlag = 0; filesindex = 0; var formdata = { images: this.data.images, voices: this.data.voices?[this.data.voices]:[], videos: this.data.videos, } if (formdata.voices.length > 0) { for (var i in formdata.voices){ var file = { path: formdata.voices[i], type: 'voice' } files.push(file); } } if (formdata.images.length > 0) { for (var i in formdata.images) { var file = { path: formdata.images[i], type: 'image' } files.push(file); } } if (formdata.videos.length > 0) { for (var i in formdata.videos) { var file = { path: formdata.videos[i], type: 'video' } files.push(file); } } //上传文件 if(files.length > 0){ filescount = files.length; wx.showLoading({ title: '提交中', mask: true }); this.uploadFiles(); }else{ this.addorder(); } }, addorder: function(){ var that = this; var form = that.data.form; var formimages = ''; if (form.images.length > 0) { formimages = form.images.join(","); } var formvoices = ''; if (form.voices.length > 0) { formvoices = form.voices.join(","); } var formvideos = ''; if (form.videos.length > 0) { formvideos = form.videos.join(","); } var m = this.data.mode; var sourceType = 1; if(this.data.mode===1){ var m = 2; } if(this.data.mode===2){ var m = 1; } if(this.data.mode == 42){ var m = 0 ; var sourceType = 4 } app.ajax({ url: app.globalData.serverUrl + '/orders/add', type: 'POST', apiname:'addorder', data: { depId: that.data.depId, content: that.data.content, repairPriority: that.data.repairPriority, images: formimages, voices: formvoices, videos: formvideos, workTypeMode:m, sourceType:sourceType } }); }, contentChange(e){ this.setData({ content: e.detail.value }); }, onTouchstartRadio: function(event) { var radioType = this.data.taskContent.taskRadioType; if (radioType == 1) { //开始录音 var options = { duration: 15000, sampleRate: 44100, numberOfChannels: 1, encodeBitRate: 192000, format: 'mp3', frameSize: 50 } recorderManager.start(options); } }, onTouchendRadio: function(event) { var radioType = this.data.taskContent.taskRadioType; var that = this; if (radioType == 2) { recorderManager.stop(); } }, //播放录音 playRadio: function(event) { var taskContent = this.data.taskContent; var radioType = taskContent.taskRadioType; if (radioType == 3) { var radioUrl = this.data.voices; innerAudioContext.src = radioUrl; innerAudioContext.play(); innerAudioContext.onPlay(() => { taskContent.taskRadioType = 4; taskContent.showImg = taskContent.stopPlayImg; this.setData({ taskContent: taskContent }) }); innerAudioContext.onError((res) => { // console.log(res) }); //监听音频自然播放至结束的事件 innerAudioContext.onEnded((res) => { taskContent.taskRadioType = 3; taskContent.showImg = taskContent.playImg; this.setData({ taskContent: taskContent }) }); } else if (radioType == 4) { innerAudioContext.stop(); //监听音频停止事件 innerAudioContext.onStop((res) => { taskContent.taskRadioType = 3; taskContent.showImg = taskContent.playImg; this.setData({ taskContent: taskContent }) }); } }, delfile: function(e){ var type = e.currentTarget.dataset.type; var path = e.currentTarget.dataset.path; if(type == 'image'){ var imgs = this.data.images; var newsimgs = []; for(var i in imgs){ if (imgs[i] == path){ continue; } newsimgs.push(imgs[i]); } this.setData({ images: newsimgs }); this.checkfiles(); }else if(type == 'video'){ var videos = this.data.videos; var newvideos = []; for (var i in videos) { if (videos[i] == path) { continue; } newvideos.push(videos[i]); } this.setData({ videos: newvideos }); this.checkfiles(); }else{ var task = this.data.taskContent; task.showImg = task.readingImg; task.taskRadioType = '1'; this.setData({ voices: '', taskContent: task }); } }, choosefile : function(){ let that = this; wx.showActionSheet({ itemList: ['选择图片','选择视频'], success(res) { if (res.tapIndex == 0){ if (that.data.images.length >= 3){ wx.showToast({ title: '图片最多只能上传3张', icon: 'none', duration: 2000 }); }else{ that.chooseImg(); } } else if (res.tapIndex == 1){ if (that.data.videos.length >= 1) { wx.showToast({ title: '视频最多只能上传1个', icon: 'none', duration: 2000 }); } else { that.chooseVideo(); } } }, fail(res) { // console.log('fail') }, }) }, chooseImg: function(){ var imgs = this.data.images; var that = this; var count = 3 - imgs.length; wx.chooseImage({ count: count, sizeType: ['compressed'], sourceType: ['album', 'camera'], success(res) { var files = res.tempFilePaths; imgs = imgs.concat(files); that.setData({ images: imgs }); that.checkfiles(); } }) }, checkfiles: function(){ if (this.data.videos.length >= 1 && this.data.images.length >= 3) { this.setData({ choosefinish: true, }); }else{ this.setData({ choosefinish: false, }); } if (this.data.videos.length >= 1 || this.data.images.length >= 1){ this.setData({ choosefinishline: true }); }else{ this.setData({ choosefinishline: false }); } }, chooseVideo: function () { var that = this; let maxDuration = 15; wx.chooseVideo({ sourceType: ['album', 'camera'], compressed: true, maxDuration: maxDuration, camera: 'back', success(res) { if (res.duration > maxDuration){ wx.showToast({ title: '视频时长不能超过' + maxDuration +'秒', icon: 'none', duration: 2000 }); return false; } let videos = []; videos.push(res.tempFilePath); that.setData({ videos: videos }); that.checkfiles(); } }) }, uploadFiles: function(){ var that = this; if (files.length > 0){ wx.uploadFile({ url: app.globalData.uploadServerUrl + 'upload/upfile', filePath: files[filesindex].path, name: 'uploadFile', formData: { // 'ttype': mode, // 'orgId': orgId }, success(res) { var data =JSON.parse(res.data); if (data.code != 0) { wx.hideLoading(); wx.showToast({ title: '文件上传失败1', icon: 'none', duration: 2000 }); that.setData({ form:{ images:[], voices:[], videos:[] } }) return false; }else{ var form = that.data.form; if (files[filesindex].type == 'image'){ form.images.push(data.data.path); } else if (files[filesindex].type == 'video'){ form.videos.push(data.data.path); }else{ form.voices.push(data.data.path); } filescount--; if(filescount == 0){ //提交表单 that.addorder(); }else{ //继续上传 filesindex++; that.uploadFiles(); } } }, fail(res){ wx.hideLoading(); wx.showToast({ title: '文件上传失败', icon: 'none', duration: 2000 }); that.setData({ form: { images: [], voices: [], videos: [] } }) return false; } }) } }, getUserInfo(){ app.ajax({ url: app.globalData.serverUrl + '/User/info', type: 'POST', apiname:'userInfo', data: { } }); }, })