work-order.js 17 KB

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