work-order.js 17 KB

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