work-order.js 14 KB

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