// pagesPublic/pages/work-order/work-order.js var dateTimePicker = require('../../../utils/dateTimePicker'); const FormData = require('../../pages/menu/formData.js'); const twoHours = 7200000; const limit = 10; import { baseUrl, request } from "../../../pages/api/canteen-request.js" Page({ /** * 页面的初始数据 */ data: { page: 1, page2: 1, isUser: true, currentIndex: 0, date: '2023-01-01', time: '12:00', dateTimeArray: null, dateTime: null, startYear: 2000, endYear: 2250, typeList: [], list: [], repairList: [], completeList: [], tempFileList: [], nodata: false, nodata2: false, total: 0, total2: 0, scroll_height:"" }, /** * 生命周期函数--监听页面加载 */ onLoad(options) { var windowWidth = wx.getSystemInfoSync().windowWidth; var windowHeight = wx.getSystemInfoSync().windowHeight; //rpx与px单位之间的换算 : 750/windowWidth = 屏幕的高度(rpx)/windowHeight var scroll_height = 750*windowHeight/windowWidth; this.setData({ scroll_height:scroll_height }) // 查询用户所属角色 request({ url: '/mini/worker/getUserType', method: 'GET', data: { userId: wx.getStorageSync('userid') } }).then(res => { if (res.result) { let userType = res.data; if ("0" == userType) { // 普通用户 this.setData({ isUser: true }); // 获取完整的年月日 时分秒,以及默认显示的数组 let obj = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear); // 精确到分的处理,将数组的秒去掉 let lastArray = obj.dateTimeArray.pop(); let lastTime = obj.dateTime.pop(); this.setData({ dateTime: obj.dateTime, dateTimeArray: obj.dateTimeArray }); // 设置所有报修类型 this.setRepairType(); } else if ('2' == userType) { this.setData({ isUser: false }); // 设置所有报修类型 this.setRepairType(); // 初始化待维修工单 this.loadTobeRepair(); // 初始化已完成工单 // this.loadComplete(); } else { wx.navigateBack(); } } else { wx.navigateBack(); } }); }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady() { }, loadMore(){ }, /** * 生命周期函数--监听页面显示 */ onShow() { this.onPullDownRefresh(); }, /** * 生命周期函数--监听页面隐藏 */ onHide() { }, /** * 生命周期函数--监听页面卸载 */ onUnload() { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh() { console.log("下拉刷新..."); if (this.data.currentIndex == 1) { this.setData({ page2: 1 }); if (this.data.isUser) { this.loadMyApplication(); } else { this.loadComplete(); } } else { if (!this.data.isUser) { this.setData({ page: 1 }); this.loadTobeRepair(); } } }, /** * 页面上拉触底事件的处理函数 */ onReachBottom() { console.log("上拉加载...."); if (this.data.currentIndex == 1) { if (this.data.total2 <= limit) { return; } if (this.data.list.length == this.data.total2 || this.data.completeList.length == this.data.total2) { return; } this.setData({ page2: this.data.page2+1 }); if (this.data.isUser) { this.loadMyApplication(true); } else { this.loadComplete(true); } } else { if (this.data.total <= limit) { return; } if (this.data.repairList.length == this.data.total) { return; } if (!this.data.isUser) { this.setData({ page: this.data.page++ }); this.loadTobeRepair(true); } } }, /** * 点击切换标题 * @param {*} e */ titleClick(e) { this.setData({ currentIndex: e.currentTarget.dataset.idx }); if (this.data.isUser) { if (e.currentTarget.dataset.idx == 1) { this.loadMyApplication(); } } else { if (e.currentTarget.dataset.idx == 1) { this.loadComplete(); } else { this.loadTobeRepair(); } } }, /** * 切换swiper-item触发bindchange事件 * @param {*} e */ pagechange: function (e) { // console.info(e) // 通过touch判断,改变tab的下标值 if ("touch" === e.detail.source) { // let currentPageIndex = this.data.currentIndex; // currentPageIndex = (currentPageIndex + 1) % 2; // 拿到当前索引并动态改变 this.setData({ currentIndex: e.detail.current }); if (this.data.isUser) { if (e.detail.current == 1) { this.loadMyApplication(); } } else { if (e.detail.current == 1) { this.loadComplete(); } else { this.loadTobeRepair(); } } } }, /** * 报修类型选择 * @param {*} e */ typePickerChange(e) { this.setData({ index: e.detail.value }); }, /** * 提交工单 * @param {*} e */ submitWorkOrder(e) { console.info(e); if (!e.detail.value.type) { wx.showToast({ title: '请选择报修类型', icon: 'error' }); return; } if (!e.detail.value.address) { wx.showToast({ title: '请输入地址', icon: 'error' }); return; } if (!e.detail.value.description) { wx.showToast({ title: '请输入问题描述', icon: 'error' }); return; } let tempList = this.data.tempFileList; if (!tempList || tempList.length == 0) { wx.showToast({ title: '请上传报修图片', icon: 'error' }); return; } if (!e.detail.value.phone) { wx.showToast({ title: '请输入联系电话', icon: 'error' }); return; } let phone = e.detail.value.phone; let reg = /^((0\d{2,3}(-)?\d{7,8})|(1[3-9]\d{9}))$/; if (!reg.test(phone)) { wx.showToast({ title: '联系电话格式有误', icon: 'error' }); return; } if (!e.detail.value.arriveTime) { wx.showToast({ title: '请选择上门时间', icon: 'error' }); return; } let arriveTime = e.detail.value.arriveTime; let t = new Date(arriveTime).getTime(); let now = new Date().getTime(); // 判断预约时间是否在两小时后 if (t - now < twoHours) { wx.showModal({ content: '您预约的上门时间过于紧急,确定继续预约?', complete: (res) => { if (res.cancel) { return; } if (res.confirm) { this.addOrder(e); } } }); } else { this.addOrder(e); } }, addOrder(e) { wx.showLoading({ title: '加载中...', }); let tempList = this.data.tempFileList; let formData = new FormData(); formData.append('repairType', e.detail.value.type); formData.append('userDescription', e.detail.value.description); formData.append('phoneNum', e.detail.value.phone); formData.append('appointmentTime', e.detail.value.arriveTime); formData.append('maintenanceAddress', e.detail.value.address); formData.append('userId', wx.getStorageSync('userid')); for (let i in tempList) { formData.appendFile('files', tempList[i]); } let data = formData.getData(); request({ url: '/mini/worker/addOrder', method: 'POST', data: data.buffer, contentType: data.contentType }).then(res => { console.info(res) if (res.result) { wx.showToast({ title: '提交成功', icon: 'success', mask: true, }); setTimeout(() => { wx.navigateBack(); }, 1500); } else { wx.showToast({ title: '提交失败', icon: 'error', mask: true }); } wx.hideLoading(); }); }, /** * 上传报修图片 */ uploadImage() { let that = this; wx.chooseMedia({ success(res) { console.info(res); if (res.errMsg == 'chooseMedia:ok') { let tempList = that.data.tempFileList; for (let i in res.tempFiles) { tempList.push(res.tempFiles[i].tempFilePath); } that.setData({ tempFileList: tempList }); } } }); }, changeDateTime(e) { this.setData({ dateTime: e.detail.value }); }, changeDateTimeColumn(e) { var arr = this.data.dateTime, dateArr = this.data.dateTimeArray; arr[e.detail.column] = e.detail.value; dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]); this.setData({ dateTimeArray: dateArr, dateTime: arr }); }, /** * 查看工单详情 * @param {*} e */ toDetail(e) { wx.navigateTo({ url: '../work-order-detail/work-order-detail?id=' + e.currentTarget.dataset.id + '&isuser=' + e.currentTarget.dataset.isuser }); }, /** * 加载我的申请 */ loadMyApplication(ispullup) { wx.showLoading({ title: '加载中...' }); request({ url: '/mini/worker/myOrder', method: 'GET', data: { page: this.data.page2, limit: limit, userId: wx.getStorageSync('userid'), isUser: true } }).then(res => { // console.info(res) let data = res.data; if (data && data.length > 0) { let typeList = this.data.typeList; for (let i in data) { if (data[i].status == 3) { data[i].image = 'status-complete.png'; } else if (data[i].status == -1) { data[i].image = 'status-incomplete.png'; } else { data[i].image = 'status-repair.png'; } for (let j in typeList) { if (data[i].repairType == typeList[j].code) { data[i].typeName = typeList[j].codeValue; break; } } } if (ispullup) { data = this.data.list.concat(data); } this.setData({ list: data, total2: res.count, nodata: false }); } else { if (!ispullup) { this.setData({ list: [], nodata: true }); } } wx.hideLoading(); }); }, /** * 加载待维修工单 */ loadTobeRepair(ispullup) { wx.showLoading({ title: '加载中...' }); request({ url: '/mini/worker/myOrder', method: 'GET', data: { page: this.data.page, limit: limit, userId: wx.getStorageSync('userid'), todo: true } }).then(res => { // console.info(res) let data = res.data; if (data && data.length > 0) { let typeList = this.data.typeList; for (let i in data) { for (let j in typeList) { if (data[i].repairType == typeList[j].code) { data[i].typeName = typeList[j].codeValue; break; } } } if (ispullup) { data = this.data.repairList.concat(data); } this.setData({ repairList: data, total: res.count, nodata: false }); } else { if (!ispullup) { this.setData({ repairList: [], nodata: true }); } } wx.hideLoading(); }); }, /** * 加载已完成工单 */ loadComplete(ispullup) { wx.showLoading({ title: '加载中...' }); request({ url: '/mini/worker/myOrder', method: 'GET', data: { page: this.data.page2, limit: limit, userId: wx.getStorageSync('userid'), close: true } }).then(res => { // console.info(res) let data = res.data; if (data && data.length > 0) { let typeList = this.data.typeList; for (let i in data) { if (data[i].resultPic) { request({ url: '/food/getFoodPicByPicId', method: 'GET', data: { 'picId': data[i].resultPic } }).then(re => { console.info(re) if (re.data && re.data.length > 0) { let arr = []; for (let j in re.data) { arr.push(baseUrl + '/' + re.data[j].path); } data[i].images = arr; } }); } for (let j in typeList) { if (data[i].repairType == typeList[j].code) { data[i].typeName = typeList[j].codeValue; break; } } } if (ispullup) { data = this.data.completeList.concat(data); } setTimeout(() => { this.setData({ completeList: data, total2: res.count, nodata2: false }); wx.hideLoading(); }, 1500); } else { if (!ispullup) { this.setData({ completeList: [], nodata2: true }); } wx.hideLoading(); } }); }, /** * 设置报修类型 */ setRepairType() { request({ url: '/mini/worker/getDictByType', method: 'GET', data: { types: 'repair_type', codes: '' } }).then(res => { // console.info(res) if (res.result) { let data = res.data; this.setData({ typeList: data }); } }); }, previewImg(e) { wx.previewImage({ urls: this.data.tempFileList, current: e.currentTarget.dataset.src }); } })