work-order.js 17 KB

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