work-order.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. // pagesPublic/pages/work-order/work-order.js
  2. var dateTimePicker = require('../../../utils/dateTimePicker');
  3. const FormData = require('../../pages/menu/formData.js');
  4. const twoHours = 7200000;
  5. const limit = 10;
  6. import {
  7. baseUrl,
  8. request
  9. } from "../../../pages/api/canteen-request.js"
  10. import {
  11. imgUrl
  12. } from "../api/request"
  13. Page({
  14. /**
  15. * 页面的初始数据
  16. */
  17. data: {
  18. imgUrl,
  19. page: 1,
  20. page2: 1,
  21. isUser: true,
  22. currentIndex: 0,
  23. date: '2023-01-01',
  24. time: '12:00',
  25. dateTimeArray: null,
  26. dateTime: null,
  27. startYear: 2000,
  28. endYear: 2250,
  29. typeList: [],
  30. list: [],
  31. repairList: [],
  32. completeList: [],
  33. tempFileList: [],
  34. nodata: false,
  35. nodata2: false,
  36. total: 0,
  37. total2: 0,
  38. scroll_height:""
  39. },
  40. /**
  41. * 生命周期函数--监听页面加载
  42. */
  43. onLoad(options) {
  44. var windowWidth = wx.getSystemInfoSync().windowWidth;
  45. var windowHeight = wx.getSystemInfoSync().windowHeight;
  46. //rpx与px单位之间的换算 : 750/windowWidth = 屏幕的高度(rpx)/windowHeight
  47. var scroll_height = 750*windowHeight/windowWidth;
  48. this.setData({
  49. scroll_height:scroll_height
  50. })
  51. // 查询用户所属角色
  52. request({
  53. url: '/mini/worker/getUserType',
  54. method: 'GET',
  55. data: {
  56. userId: wx.getStorageSync('userid')
  57. }
  58. }).then(res => {
  59. if (res.result) {
  60. let userType = res.data;
  61. if ("0" == userType) {
  62. // 普通用户
  63. this.setData({
  64. isUser: true
  65. });
  66. // 获取完整的年月日 时分秒,以及默认显示的数组
  67. let obj = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);
  68. // 精确到分的处理,将数组的秒去掉
  69. let lastArray = obj.dateTimeArray.pop();
  70. let lastTime = obj.dateTime.pop();
  71. this.setData({
  72. dateTime: obj.dateTime,
  73. dateTimeArray: obj.dateTimeArray
  74. });
  75. // 设置所有报修类型
  76. this.setRepairType();
  77. } else if ('2' == userType) {
  78. this.setData({
  79. isUser: false
  80. });
  81. // 设置所有报修类型
  82. this.setRepairType();
  83. // 初始化待维修工单
  84. this.loadTobeRepair();
  85. // 初始化已完成工单
  86. // this.loadComplete();
  87. } else {
  88. wx.navigateBack();
  89. }
  90. } else {
  91. wx.navigateBack();
  92. }
  93. });
  94. },
  95. /**
  96. * 生命周期函数--监听页面初次渲染完成
  97. */
  98. onReady() {
  99. },
  100. loadMore(){
  101. },
  102. /**
  103. * 生命周期函数--监听页面显示
  104. */
  105. onShow() {
  106. this.onPullDownRefresh();
  107. },
  108. /**
  109. * 生命周期函数--监听页面隐藏
  110. */
  111. onHide() {
  112. },
  113. /**
  114. * 生命周期函数--监听页面卸载
  115. */
  116. onUnload() {
  117. },
  118. /**
  119. * 页面相关事件处理函数--监听用户下拉动作
  120. */
  121. onPullDownRefresh() {
  122. console.log("下拉刷新...");
  123. if (this.data.currentIndex == 1) {
  124. this.setData({ page2: 1 });
  125. if (this.data.isUser) {
  126. this.loadMyApplication();
  127. } else {
  128. this.loadComplete();
  129. }
  130. } else {
  131. if (!this.data.isUser) {
  132. this.setData({ page: 1 });
  133. this.loadTobeRepair();
  134. }
  135. }
  136. },
  137. /**
  138. * 页面上拉触底事件的处理函数
  139. */
  140. onReachBottom() {
  141. console.log("上拉加载....");
  142. if (this.data.currentIndex == 1) {
  143. if (this.data.total2 <= limit) {
  144. return;
  145. }
  146. if (this.data.list.length == this.data.total2 || this.data.completeList.length == this.data.total2) {
  147. return;
  148. }
  149. this.setData({ page2: this.data.page2+1 });
  150. if (this.data.isUser) {
  151. this.loadMyApplication(true);
  152. } else {
  153. this.loadComplete(true);
  154. }
  155. } else {
  156. if (this.data.total <= limit) {
  157. return;
  158. }
  159. if (this.data.repairList.length == this.data.total) {
  160. return;
  161. }
  162. if (!this.data.isUser) {
  163. this.setData({ page: this.data.page++ });
  164. this.loadTobeRepair(true);
  165. }
  166. }
  167. },
  168. /**
  169. * 点击切换标题
  170. * @param {*} e
  171. */
  172. titleClick(e) {
  173. this.setData({
  174. currentIndex: e.currentTarget.dataset.idx
  175. });
  176. if (this.data.isUser) {
  177. if (e.currentTarget.dataset.idx == 1) {
  178. this.loadMyApplication();
  179. }
  180. } else {
  181. if (e.currentTarget.dataset.idx == 1) {
  182. this.loadComplete();
  183. } else {
  184. this.loadTobeRepair();
  185. }
  186. }
  187. },
  188. /**
  189. * 切换swiper-item触发bindchange事件
  190. * @param {*} e
  191. */
  192. pagechange: function (e) {
  193. // console.info(e)
  194. // 通过touch判断,改变tab的下标值
  195. if ("touch" === e.detail.source) {
  196. // let currentPageIndex = this.data.currentIndex;
  197. // currentPageIndex = (currentPageIndex + 1) % 2;
  198. // 拿到当前索引并动态改变
  199. this.setData({
  200. currentIndex: e.detail.current
  201. });
  202. if (this.data.isUser) {
  203. if (e.detail.current == 1) {
  204. this.loadMyApplication();
  205. }
  206. } else {
  207. if (e.detail.current == 1) {
  208. this.loadComplete();
  209. } else {
  210. this.loadTobeRepair();
  211. }
  212. }
  213. }
  214. },
  215. /**
  216. * 报修类型选择
  217. * @param {*} e
  218. */
  219. typePickerChange(e) {
  220. this.setData({
  221. index: e.detail.value
  222. });
  223. },
  224. /**
  225. * 提交工单
  226. * @param {*} e
  227. */
  228. submitWorkOrder(e) {
  229. console.info(e);
  230. if (!e.detail.value.type) {
  231. wx.showToast({
  232. title: '请选择报修类型',
  233. icon: 'error'
  234. });
  235. return;
  236. }
  237. if (!e.detail.value.address) {
  238. wx.showToast({
  239. title: '请输入地址',
  240. icon: 'error'
  241. });
  242. return;
  243. }
  244. if (!e.detail.value.description) {
  245. wx.showToast({
  246. title: '请输入问题描述',
  247. icon: 'error'
  248. });
  249. return;
  250. }
  251. let tempList = this.data.tempFileList;
  252. if (!tempList || tempList.length == 0) {
  253. wx.showToast({
  254. title: '请上传报修图片',
  255. icon: 'error'
  256. });
  257. return;
  258. }
  259. if (!e.detail.value.phone) {
  260. wx.showToast({
  261. title: '请输入联系电话',
  262. icon: 'error'
  263. });
  264. return;
  265. }
  266. let phone = e.detail.value.phone;
  267. let reg = /^((0\d{2,3}(-)?\d{7,8})|(1[3-9]\d{9}))$/;
  268. if (!reg.test(phone)) {
  269. wx.showToast({
  270. title: '联系电话格式有误',
  271. icon: 'error'
  272. });
  273. return;
  274. }
  275. if (!e.detail.value.arriveTime) {
  276. wx.showToast({
  277. title: '请选择上门时间',
  278. icon: 'error'
  279. });
  280. return;
  281. }
  282. let arriveTime = e.detail.value.arriveTime;
  283. let t = new Date(arriveTime).getTime();
  284. let now = new Date().getTime();
  285. // 判断预约时间是否在两小时后
  286. if (t - now < twoHours) {
  287. wx.showModal({
  288. content: '您预约的上门时间过于紧急,确定继续预约?',
  289. complete: (res) => {
  290. if (res.cancel) {
  291. return;
  292. }
  293. if (res.confirm) {
  294. this.addOrder(e);
  295. }
  296. }
  297. });
  298. } else {
  299. this.addOrder(e);
  300. }
  301. },
  302. addOrder(e) {
  303. wx.showLoading({
  304. title: '加载中...',
  305. });
  306. let tempList = this.data.tempFileList;
  307. let formData = new FormData();
  308. formData.append('repairType', e.detail.value.type);
  309. formData.append('userDescription', e.detail.value.description);
  310. formData.append('phoneNum', e.detail.value.phone);
  311. formData.append('appointmentTime', e.detail.value.arriveTime);
  312. formData.append('maintenanceAddress', e.detail.value.address);
  313. formData.append('userId', wx.getStorageSync('userid'));
  314. for (let i in tempList) {
  315. formData.appendFile('files', tempList[i]);
  316. }
  317. let data = formData.getData();
  318. request({
  319. url: '/mini/worker/addOrder',
  320. method: 'POST',
  321. data: data.buffer,
  322. contentType: data.contentType
  323. }).then(res => {
  324. console.info(res)
  325. if (res.result) {
  326. wx.showToast({
  327. title: '提交成功',
  328. icon: 'success',
  329. mask: true,
  330. });
  331. setTimeout(() => {
  332. wx.navigateBack();
  333. }, 1500);
  334. } else {
  335. wx.showToast({
  336. title: '提交失败',
  337. icon: 'error',
  338. mask: true
  339. });
  340. }
  341. wx.hideLoading();
  342. });
  343. },
  344. /**
  345. * 上传报修图片
  346. */
  347. uploadImage() {
  348. let that = this;
  349. wx.chooseMedia({
  350. success(res) {
  351. console.info(res);
  352. if (res.errMsg == 'chooseMedia:ok') {
  353. let tempList = that.data.tempFileList;
  354. for (let i in res.tempFiles) {
  355. tempList.push(res.tempFiles[i].tempFilePath);
  356. }
  357. that.setData({
  358. tempFileList: tempList
  359. });
  360. }
  361. }
  362. });
  363. },
  364. changeDateTime(e) {
  365. this.setData({ dateTime: e.detail.value });
  366. },
  367. changeDateTimeColumn(e) {
  368. var arr = this.data.dateTime, dateArr = this.data.dateTimeArray;
  369. arr[e.detail.column] = e.detail.value;
  370. dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);
  371. this.setData({
  372. dateTimeArray: dateArr,
  373. dateTime: arr
  374. });
  375. },
  376. /**
  377. * 查看工单详情
  378. * @param {*} e
  379. */
  380. toDetail(e) {
  381. wx.navigateTo({
  382. url: '../work-order-detail/work-order-detail?id=' + e.currentTarget.dataset.id + '&isuser=' + e.currentTarget.dataset.isuser
  383. });
  384. },
  385. /**
  386. * 加载我的申请
  387. */
  388. loadMyApplication(ispullup) {
  389. wx.showLoading({
  390. title: '加载中...'
  391. });
  392. request({
  393. url: '/mini/worker/myOrder',
  394. method: 'GET',
  395. data: {
  396. page: this.data.page2,
  397. limit: limit,
  398. userId: wx.getStorageSync('userid'),
  399. isUser: true
  400. }
  401. }).then(res => {
  402. // console.info(res)
  403. let data = res.data;
  404. if (data && data.length > 0) {
  405. let typeList = this.data.typeList;
  406. for (let i in data) {
  407. if (data[i].status == 3) {
  408. data[i].image = 'status-complete.png';
  409. } else if (data[i].status == -1) {
  410. data[i].image = 'status-incomplete.png';
  411. } else {
  412. data[i].image = 'status-repair.png';
  413. }
  414. for (let j in typeList) {
  415. if (data[i].repairType == typeList[j].code) {
  416. data[i].typeName = typeList[j].codeValue;
  417. break;
  418. }
  419. }
  420. }
  421. if (ispullup) {
  422. data = this.data.list.concat(data);
  423. }
  424. this.setData({
  425. list: data,
  426. total2: res.count,
  427. nodata: false
  428. });
  429. } else {
  430. if (!ispullup) {
  431. this.setData({
  432. list: [],
  433. nodata: true
  434. });
  435. }
  436. }
  437. wx.hideLoading();
  438. });
  439. },
  440. /**
  441. * 加载待维修工单
  442. */
  443. loadTobeRepair(ispullup) {
  444. wx.showLoading({
  445. title: '加载中...'
  446. });
  447. request({
  448. url: '/mini/worker/myOrder',
  449. method: 'GET',
  450. data: {
  451. page: this.data.page,
  452. limit: limit,
  453. userId: wx.getStorageSync('userid'),
  454. todo: true
  455. }
  456. }).then(res => {
  457. // console.info(res)
  458. let data = res.data;
  459. if (data && data.length > 0) {
  460. let typeList = this.data.typeList;
  461. for (let i in data) {
  462. for (let j in typeList) {
  463. if (data[i].repairType == typeList[j].code) {
  464. data[i].typeName = typeList[j].codeValue;
  465. break;
  466. }
  467. }
  468. }
  469. if (ispullup) {
  470. data = this.data.repairList.concat(data);
  471. }
  472. this.setData({
  473. repairList: data,
  474. total: res.count,
  475. nodata: false
  476. });
  477. } else {
  478. if (!ispullup) {
  479. this.setData({
  480. repairList: [],
  481. nodata: true
  482. });
  483. }
  484. }
  485. wx.hideLoading();
  486. });
  487. },
  488. /**
  489. * 加载已完成工单
  490. */
  491. loadComplete(ispullup) {
  492. wx.showLoading({
  493. title: '加载中...'
  494. });
  495. request({
  496. url: '/mini/worker/myOrder',
  497. method: 'GET',
  498. data: {
  499. page: this.data.page2,
  500. limit: limit,
  501. userId: wx.getStorageSync('userid'),
  502. close: true
  503. }
  504. }).then(res => {
  505. // console.info(res)
  506. let data = res.data;
  507. if (data && data.length > 0) {
  508. let typeList = this.data.typeList;
  509. for (let i in data) {
  510. if (data[i].resultPic) {
  511. request({
  512. url: '/food/getFoodPicByPicId',
  513. method: 'GET',
  514. data: {
  515. 'picId': data[i].resultPic
  516. }
  517. }).then(re => {
  518. console.info(re)
  519. if (re.data && re.data.length > 0) {
  520. let arr = [];
  521. for (let j in re.data) {
  522. arr.push(baseUrl + '/' + re.data[j].path);
  523. }
  524. data[i].images = arr;
  525. }
  526. });
  527. }
  528. for (let j in typeList) {
  529. if (data[i].repairType == typeList[j].code) {
  530. data[i].typeName = typeList[j].codeValue;
  531. break;
  532. }
  533. }
  534. }
  535. if (ispullup) {
  536. data = this.data.completeList.concat(data);
  537. }
  538. setTimeout(() => {
  539. this.setData({
  540. completeList: data,
  541. total2: res.count,
  542. nodata2: false
  543. });
  544. wx.hideLoading();
  545. }, 1500);
  546. } else {
  547. if (!ispullup) {
  548. this.setData({
  549. completeList: [],
  550. nodata2: true
  551. });
  552. }
  553. wx.hideLoading();
  554. }
  555. });
  556. },
  557. /**
  558. * 设置报修类型
  559. */
  560. setRepairType() {
  561. request({
  562. url: '/mini/worker/getDictByType',
  563. method: 'GET',
  564. data: {
  565. types: 'repair_type',
  566. codes: ''
  567. }
  568. }).then(res => {
  569. // console.info(res)
  570. if (res.result) {
  571. let data = res.data;
  572. this.setData({
  573. typeList: data
  574. });
  575. }
  576. });
  577. },
  578. previewImg(e) {
  579. wx.previewImage({
  580. urls: this.data.tempFileList,
  581. current: e.currentTarget.dataset.src
  582. });
  583. }
  584. })