index.js 14 KB

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