index.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. const app = getApp();
  2. //创建录音管理
  3. const recorderManager = wx.getRecorderManager();
  4. //创建播放管理器
  5. const innerAudioContext = wx.createInnerAudioContext();
  6. innerAudioContext.autoplay = false;
  7. var filescount = 0;
  8. var files = [];
  9. var uploadFlag = 0; //1=上传出错 0=正常
  10. var filesindex = 0;
  11. Page({
  12. /**
  13. * 页面的初始数据
  14. */
  15. data: {
  16. mode: 0,
  17. taskContent: {
  18. title: '任务内容',
  19. showImg: '/images/icons/recording.png',
  20. playImg: '/images/icons/play.png',
  21. blImg: '/images/icons/bl.png',
  22. readingImg: '/images/icons/recording.png',
  23. luyinzhongImg: '/images/icons/luyinzhong.png',
  24. stopPlayImg: '/images/icons/zanting.png',
  25. delimg: '/images/icons/newdel.png',
  26. taskRadioType: '1',
  27. radioUrl: '',
  28. second: 10
  29. },
  30. rightImage:"/images/icons/toRight.png",
  31. images:[],
  32. videos:[],
  33. voices: '',
  34. imagesAdd: '/images/icons/camera.png',
  35. wyPhone:"",
  36. depId:0,
  37. content:"",
  38. depName:"",
  39. form:{
  40. images: [],
  41. videos: [],
  42. voices: []
  43. },
  44. isHide:0
  45. },
  46. /**
  47. * 生命周期函数--监听页面加载
  48. */
  49. onLoad: function (options) {
  50. let that = this;
  51. this.setData({
  52. mode: options.mode
  53. })
  54. wx.setNavigationBarTitle({
  55. title: options.name
  56. })
  57. recorderManager.onStart(() => {
  58. var taskContent = that.data.taskContent;
  59. taskContent.showImg = taskContent.luyinzhongImg;
  60. taskContent.taskRadioType = '2';
  61. that.setData({
  62. taskContent: taskContent
  63. })
  64. })
  65. recorderManager.onPause(() => {
  66. })
  67. recorderManager.onStop((res) => {
  68. var path = res.tempFilePath;
  69. var task = that.data.taskContent;
  70. task.taskRadioType = '3';
  71. task.showImg = task.playImg;
  72. task.second = Math.ceil(res.duration / 1000);
  73. that.setData({
  74. voices: path,
  75. taskContent: task
  76. });
  77. // const { tempFilePath } = res
  78. })
  79. recorderManager.onError((res) => {
  80. wx.showToast({
  81. title: '录音失败',
  82. icon: 'none',
  83. duration: 2000
  84. });
  85. var task = that.data.taskContent;
  86. task.taskRadioType = '1';
  87. task.showImg = task.readingImg;
  88. that.setData({
  89. voices: '',
  90. taskContent: task
  91. });
  92. });
  93. this.getUserInfo();
  94. },
  95. gotopage: function(e){
  96. var url = e.currentTarget.dataset.url;
  97. var type = e.currentTarget.dataset.type;
  98. var title = e.currentTarget.dataset.title;
  99. app.gotopage(url, { title: title, type: type });
  100. },
  101. /**
  102. * 生命周期函数--监听页面显示
  103. */
  104. onShow: function () {
  105. var arr = [];
  106. var that = this;
  107. app.ajaxReadyCallback = res => {
  108. var apiname = res.data.apiname;
  109. if(apiname == 'wyConfig'){
  110. console.log(res);
  111. that.setData({
  112. wyPhone:res.data.data.orgPhone
  113. })
  114. }else if(apiname == 'addorder') {
  115. wx.showToast({
  116. title: '提交成功!',
  117. icon: 'success',
  118. duration: 1500
  119. });
  120. setTimeout(function () {
  121. wx.navigateBack({ delta: 1 });
  122. }, 1500);
  123. }else if(apiname == 'userInfo') {
  124. that.setData({
  125. depId:res.data.data.depId,
  126. depName:res.data.data.depName,
  127. })
  128. }
  129. }
  130. this.wyConfig();
  131. },
  132. checkboxChange: function(e){
  133. var data = e.detail.value;
  134. if(data.length >0){
  135. this.setData({
  136. isHide:1
  137. })
  138. }else {
  139. this.setData({
  140. isHide:0
  141. })
  142. }
  143. },
  144. callPhone(){
  145. var that = this;
  146. wx.makePhoneCall({
  147. phoneNumber: that.data.wyPhone,
  148. })
  149. },
  150. wyConfig(){
  151. app.ajax({
  152. url: app.globalData.serverUrl + '/org/queryConfig',
  153. type: 'POST',
  154. apiname: 'wyConfig',
  155. data: {
  156. }
  157. });
  158. },
  159. btnsubmit(){
  160. var that = this;
  161. if(!this.data.content){
  162. wx.showToast({
  163. title: '请输入内容',
  164. icon: 'none',
  165. duration: 2000
  166. });
  167. return ;
  168. }
  169. if(this.data.depId==0){
  170. wx.showToast({
  171. title: '请选择部门',
  172. icon: 'none',
  173. duration: 2000
  174. });
  175. return ;
  176. }
  177. //数据初始化
  178. files = [];
  179. filescount = 0;
  180. uploadFlag = 0;
  181. filesindex = 0;
  182. var formdata = {
  183. images: this.data.images,
  184. voices: this.data.voices?[this.data.voices]:[],
  185. videos: this.data.videos,
  186. }
  187. if (formdata.voices.length > 0) {
  188. for (var i in formdata.voices){
  189. var file = {
  190. path: formdata.voices[i],
  191. type: 'voice'
  192. }
  193. files.push(file);
  194. }
  195. }
  196. if (formdata.images.length > 0) {
  197. for (var i in formdata.images) {
  198. var file = {
  199. path: formdata.images[i],
  200. type: 'image'
  201. }
  202. files.push(file);
  203. }
  204. }
  205. if (formdata.videos.length > 0) {
  206. for (var i in formdata.videos) {
  207. var file = {
  208. path: formdata.videos[i],
  209. type: 'video'
  210. }
  211. files.push(file);
  212. }
  213. }
  214. //上传文件
  215. if(files.length > 0){
  216. filescount = files.length;
  217. wx.showLoading({
  218. title: '提交中',
  219. mask: true
  220. });
  221. this.uploadFiles();
  222. }else{
  223. this.addorder();
  224. }
  225. },
  226. addorder: function(){
  227. var that = this;
  228. var form = that.data.form;
  229. var formimages = '';
  230. if (form.images.length > 0) {
  231. formimages = form.images.join(",");
  232. }
  233. var formvoices = '';
  234. if (form.voices.length > 0) {
  235. formvoices = form.voices.join(",");
  236. }
  237. var formvideos = '';
  238. if (form.videos.length > 0) {
  239. formvideos = form.videos.join(",");
  240. }
  241. var m = this.data.mode;
  242. if(this.data.mode===1){
  243. var m = 2;
  244. }
  245. if(this.data.mode===2){
  246. var m = 1;
  247. }
  248. app.ajax({
  249. url: app.globalData.serverUrl + '/Complain/add',
  250. type: 'POST',
  251. apiname:'addorder',
  252. data: {
  253. depId: that.data.depId,
  254. content: that.data.content,
  255. images: formimages,
  256. voices: formvoices,
  257. videos: formvideos,
  258. hide:that.data.isHide,
  259. }
  260. });
  261. },
  262. contentChange(e){
  263. this.setData({
  264. content: e.detail.value
  265. });
  266. },
  267. onTouchstartRadio: function(event) {
  268. var radioType = this.data.taskContent.taskRadioType;
  269. if (radioType == 1) {
  270. //开始录音
  271. var options = {
  272. duration: 15000,
  273. sampleRate: 44100,
  274. numberOfChannels: 1,
  275. encodeBitRate: 192000,
  276. format: 'mp3',
  277. frameSize: 50
  278. }
  279. recorderManager.start(options);
  280. }
  281. },
  282. onTouchendRadio: function(event) {
  283. var radioType = this.data.taskContent.taskRadioType;
  284. var that = this;
  285. if (radioType == 2) {
  286. recorderManager.stop();
  287. }
  288. },
  289. //播放录音
  290. playRadio: function(event) {
  291. var taskContent = this.data.taskContent;
  292. var radioType = taskContent.taskRadioType;
  293. if (radioType == 3) {
  294. var radioUrl = this.data.voices;
  295. innerAudioContext.src = radioUrl;
  296. innerAudioContext.play();
  297. innerAudioContext.onPlay(() => {
  298. taskContent.taskRadioType = 4;
  299. taskContent.showImg = taskContent.stopPlayImg;
  300. this.setData({
  301. taskContent: taskContent
  302. })
  303. });
  304. innerAudioContext.onError((res) => {
  305. // console.log(res)
  306. });
  307. //监听音频自然播放至结束的事件
  308. innerAudioContext.onEnded((res) => {
  309. taskContent.taskRadioType = 3;
  310. taskContent.showImg = taskContent.playImg;
  311. this.setData({
  312. taskContent: taskContent
  313. })
  314. });
  315. } else if (radioType == 4) {
  316. innerAudioContext.stop();
  317. //监听音频停止事件
  318. innerAudioContext.onStop((res) => {
  319. taskContent.taskRadioType = 3;
  320. taskContent.showImg = taskContent.playImg;
  321. this.setData({
  322. taskContent: taskContent
  323. })
  324. });
  325. }
  326. },
  327. delfile: function(e){
  328. var type = e.currentTarget.dataset.type;
  329. var path = e.currentTarget.dataset.path;
  330. if(type == 'image'){
  331. var imgs = this.data.images;
  332. var newsimgs = [];
  333. for(var i in imgs){
  334. if (imgs[i] == path){
  335. continue;
  336. }
  337. newsimgs.push(imgs[i]);
  338. }
  339. this.setData({
  340. images: newsimgs
  341. });
  342. this.checkfiles();
  343. }else if(type == 'video'){
  344. var videos = this.data.videos;
  345. var newvideos = [];
  346. for (var i in videos) {
  347. if (videos[i] == path) {
  348. continue;
  349. }
  350. newvideos.push(videos[i]);
  351. }
  352. this.setData({
  353. videos: newvideos
  354. });
  355. this.checkfiles();
  356. }else{
  357. var task = this.data.taskContent;
  358. task.showImg = task.readingImg;
  359. task.taskRadioType = '1';
  360. this.setData({
  361. voices: '',
  362. taskContent: task
  363. });
  364. }
  365. },
  366. choosefile : function(){
  367. let that = this;
  368. wx.showActionSheet({
  369. itemList: ['选择图片','选择视频'],
  370. success(res) {
  371. if (res.tapIndex == 0){
  372. if (that.data.images.length >= 3){
  373. wx.showToast({
  374. title: '图片最多只能上传3张',
  375. icon: 'none',
  376. duration: 2000
  377. });
  378. }else{
  379. that.chooseImg();
  380. }
  381. } else if (res.tapIndex == 1){
  382. if (that.data.videos.length >= 1) {
  383. wx.showToast({
  384. title: '视频最多只能上传1个',
  385. icon: 'none',
  386. duration: 2000
  387. });
  388. } else {
  389. that.chooseVideo();
  390. }
  391. }
  392. },
  393. fail(res) {
  394. // console.log('fail')
  395. },
  396. })
  397. },
  398. chooseImg: function(){
  399. var imgs = this.data.images;
  400. var that = this;
  401. var count = 3 - imgs.length;
  402. wx.chooseImage({
  403. count: count,
  404. sizeType: ['compressed'],
  405. sourceType: ['album', 'camera'],
  406. success(res) {
  407. var files = res.tempFilePaths;
  408. imgs = imgs.concat(files);
  409. that.setData({
  410. images: imgs
  411. });
  412. that.checkfiles();
  413. }
  414. })
  415. },
  416. checkfiles: function(){
  417. if (this.data.videos.length >= 1 && this.data.images.length >= 3) {
  418. this.setData({
  419. choosefinish: true,
  420. });
  421. }else{
  422. this.setData({
  423. choosefinish: false,
  424. });
  425. }
  426. if (this.data.videos.length >= 1 || this.data.images.length >= 1){
  427. this.setData({
  428. choosefinishline: true
  429. });
  430. }else{
  431. this.setData({
  432. choosefinishline: false
  433. });
  434. }
  435. },
  436. chooseVideo: function () {
  437. var that = this;
  438. let maxDuration = 15;
  439. wx.chooseVideo({
  440. sourceType: ['album', 'camera'],
  441. compressed: true,
  442. maxDuration: maxDuration,
  443. camera: 'back',
  444. success(res) {
  445. if (res.duration > maxDuration){
  446. wx.showToast({
  447. title: '视频时长不能超过' + maxDuration +'秒',
  448. icon: 'none',
  449. duration: 2000
  450. });
  451. return false;
  452. }
  453. let videos = [];
  454. videos.push(res.tempFilePath);
  455. that.setData({
  456. videos: videos
  457. });
  458. that.checkfiles();
  459. }
  460. })
  461. },
  462. uploadFiles: function(){
  463. var that = this;
  464. if (files.length > 0){
  465. wx.uploadFile({
  466. url: app.globalData.uploadServerUrl + 'upload/upfile',
  467. filePath: files[filesindex].path,
  468. name: 'uploadFile',
  469. formData: {
  470. // 'ttype': mode,
  471. // 'orgId': orgId
  472. },
  473. success(res) {
  474. var data =JSON.parse(res.data);
  475. if (data.code != 0) {
  476. wx.hideLoading();
  477. wx.showToast({
  478. title: '文件上传失败1',
  479. icon: 'none',
  480. duration: 2000
  481. });
  482. that.setData({
  483. form:{
  484. images:[],
  485. voices:[],
  486. videos:[]
  487. }
  488. })
  489. return false;
  490. }else{
  491. var form = that.data.form;
  492. if (files[filesindex].type == 'image'){
  493. form.images.push(data.data.path);
  494. } else if (files[filesindex].type == 'video'){
  495. form.videos.push(data.data.path);
  496. }else{
  497. form.voices.push(data.data.path);
  498. }
  499. filescount--;
  500. if(filescount == 0){ //提交表单
  501. that.addorder();
  502. }else{ //继续上传
  503. filesindex++;
  504. that.uploadFiles();
  505. }
  506. }
  507. },
  508. fail(res){
  509. wx.hideLoading();
  510. wx.showToast({
  511. title: '文件上传失败',
  512. icon: 'none',
  513. duration: 2000
  514. });
  515. that.setData({
  516. form: {
  517. images: [],
  518. voices: [],
  519. videos: []
  520. }
  521. })
  522. return false;
  523. }
  524. })
  525. }
  526. },
  527. getUserInfo(){
  528. app.ajax({
  529. url: app.globalData.serverUrl + '/User/info',
  530. type: 'POST',
  531. apiname:'userInfo',
  532. data: {
  533. }
  534. });
  535. },
  536. })