123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563 |
- 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:"",
- form:{
- images: [],
- videos: [],
- voices: []
- },
- isHide:0
- },
- /**
- * 生命周期函数--监听页面加载
- */
- 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'){
- console.log(res);
- 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();
- },
- checkboxChange: function(e){
- var data = e.detail.value;
- if(data.length >0){
- this.setData({
- isHide:1
- })
- }else {
- this.setData({
- isHide:0
- })
- }
- },
- 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;
- if(this.data.mode===1){
- var m = 2;
- }
- if(this.data.mode===2){
- var m = 1;
- }
- app.ajax({
- url: app.globalData.serverUrl + '/Hiddendanger/add',
- type: 'POST',
- apiname:'addorder',
- data: {
- depId: that.data.depId,
- content: that.data.content,
- images: formimages,
- voices: formvoices,
- videos: formvideos,
- // hide:that.data.isHide,
- }
- });
- },
- 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: {
-
- }
- });
- },
- })
|