// package/troubleshooting/shootingForm/index.js import { getPatrolRecordIssueHandleById, getStructuresUsers, editPatrolRecordIssueHandle } from "../../../utils/getApiUrl"; import { Request } from "../../../common"; const moment = require("../../../utils/moment"); Page({ /** * 页面的初始数据 */ data: { imgServer: getApp().globalData.imgUrl, userInfo: wx.getStorageSync("userInfo"), data: {}, strucFocusUser: {}, focusPerson: [], tabIndex: '', shootingid: '', pointItemCollapseActiveNames: undefined, maintenancePersonIndex: undefined, maintenancePersonId: null, maintenancePersonDepartmentShow: '', maintenanceRequirement: '', qualityPersonIndex: undefined, planStartTimePopupShow: false, planStartTime: '', planStartTimeShow: '', planEndTimePopupShow: false, planEndTime: '', planEndTimeShow: '', planApproval: '', planApprovalPerson: '', planApprovalTime: '', repair: '', repairImgs: [], checkDesc: '', cost: '', checkImgs: [], // 表单控制 isPlanState: false, isApprove: false, isRepair: false, isCheck: false, }, /** * 生命周期函数--监听页面加载 */ onLoad (options) { const { shootingid, tabIndex } = options if (shootingid) { this.setData({ shootingid, tabIndex }) Promise.all( [ Request.get(getPatrolRecordIssueHandleById(shootingid), {}), Request.get(getStructuresUsers(), {}) ] ).then(res => { const [issue, strucUser] = res const nextData = { ...issue, PatrolRecord: { ...issue.PatrolRecord, inspectionTime: moment(issue.PatrolRecord.inspectionTime).format('YYYY-MM-DD HH:mm:ss'), points: { ...issue.PatrolRecord.points, inspectContent: (() => { let inspectContent = [] for (let k in issue.PatrolRecord.points.inspectContent) { inspectContent.push({ ...issue.PatrolRecord.points.inspectContent[k], itemName: k }) } return inspectContent })() } } } this.setData({ data: nextData, isPlanState: tabIndex == 0 && (issue.state == 1 || issue.state == 3), isApprove: tabIndex == 0 && issue.state == 2, isRepair: tabIndex == 0 && (issue.state == 4 || issue.state == 7), isCheck: tabIndex == 0 && issue.state == 5, }) this.setData({ strucFocusUser: { ...strucUser, } }) const focusPerson = (strucUser.find(s => s.id == issue.PatrolRecord.points.project.id) || {}).users || [] this.setData({ focusPerson: focusPerson }) if (issue.state > 1) { let maintenancePersonIndex = focusPerson.findIndex(f => f.id == issue.repairPerson.id) this.setData({ maintenancePersonIndex: maintenancePersonIndex, maintenancePersonDepartmentShow: maintenancePersonIndex >= 0 ? focusPerson[maintenancePersonIndex].department.name : '', qualityPersonIndex: focusPerson.findIndex(f => f.id == issue.checkPerson.id), planStartTime: moment(issue.startTime).unix(), planStartTimeShow: moment(issue.startTime).format('YYYY-MM-DD'), planEndTimeShow: moment(issue.endTime).format('YYYY-MM-DD'), maintenanceRequirement: issue.repairAsk }) } if (issue.state > 2) { this.setData({ planApproval: issue.approveOpinion, planApprovalPerson: issue.approvePerson ? issue.approvePerson.name : '', planApprovalTime: issue.approveDate ? moment(issue.approveDate).format('YYYY-MM-DD HH:mm:ss') : '', }) } if (issue.state > 3) { this.setData({ repair: issue.repairDesc, repairImgs: issue.repairImage || [], }) } if (issue.state > 4) { this.setData({ checkDesc: issue.checkOpinion, cost: issue.cost, checkImgs: issue.checkImage || [], }) } }) } else { } }, onPointItemCollapseActiveChange (event) { this.setData({ pointItemCollapseActiveNames: event.detail, }); }, onMaintenancePersonPopupChange (e) { if (e.target.dataset.type == 'zhijian') { this.setData({ qualityPersonIndex: e.detail.value }) } else { let selectedUser = this.data.focusPerson[e.detail.value] this.setData({ maintenancePersonIndex: e.detail.value, maintenancePersonDepartmentShow: selectedUser ? selectedUser.department.name : '' }) } }, showPlanStartTimePopup () { if (this.data.isPlanState) { this.setData({ planStartTimePopupShow: true }) } }, closePlanStartTimePopup (e) { this.setData({ planStartTimePopupShow: false }) if (e.target.dataset.option == 'cancel') { // this.setData({ planStartTime: '' }) } else if (e.target.dataset.option == 'confirmed') { this.setData({ planStartTimeShow: this.data.planStartTime ? moment(this.data.planStartTime).format('YYYY-MM-DD') : '' }) } }, onPlanStartTimeChange (event) { this.setData({ planStartTime: event.detail, }) }, showPlanEndTimePopup () { if (this.data.isPlanState) { this.setData({ planEndTimePopupShow: true }) } }, closePlanEndTimePopup (e) { this.setData({ planEndTimePopupShow: false }) if (e.target.dataset.option == 'confirmed') { this.setData({ planEndTimeShow: this.data.planEndTime ? moment(this.data.planEndTime).format('YYYY-MM-DD') : '' }) } }, onPlanEndTimeChange (event) { this.setData({ planEndTime: event.detail, }) }, onInputChange (e) { let value = e.detail.value if (e.target.dataset.type === 'cost' && !/^(\d?)+(\.\d{0,2})?$/.test(e.detail.value)) { wx.showToast({ title: '只能输入两位小数', icon: 'none' }) value = value.substring(0, value.length - 1); } this.setData({ [e.target.dataset.type]: value }) }, // 预览图片 previewImg: function (e) { const { index, itemindex, type } = e.currentTarget.dataset const imgs = type == 'point' ? this.data.data.PatrolRecord.points.inspectContent[itemindex].imgs : this.data[type]; const newImgs = imgs.map(i => this.data.imgServer + i); wx.previewImage({ current: newImgs[index], urls: newImgs }) }, // 上传图片 chooseImg: function (e) { // 这里是选取图片的方法 const that = this; const { type } = e.currentTarget.dataset let pics = []; const detailPics = that.data[type] if (detailPics.length >= 20) { wx.showToast({ title: '最多选择20张图片上传', icon: 'none' }); return; } wx.chooseMedia({ count: 20, // 基础库2.25.0前,最多可支持9个文件,2.25.0及以后最多可支持20个文件 mediaType: ['image'], // 文件类型 sizeType: ['original', 'compressed'], // original 原图,compressed 压缩图,默认二者都有 sourceType: ['album', 'camera'], // album 从相册选图,camera 使用相机,默认二者都有 success: function (res) { const imgs = res.tempFiles; for (let i = 0; i < imgs.length; i++) { if (res.tempFiles[i].size > 15728640) { wx.showToast({ title: '图片大于15M,不可上传', icon: 'none' }); return; } const fileNameArr = res.tempFiles[i].tempFilePath.split('.'); const extension = res.tempFiles[i].tempFilePath.split('.')[fileNameArr.length - 1]; if (extension !== 'jpg' && extension !== 'png' && extension !== 'jpeg') { wx.showToast({ title: '只能上传jpg、jpeg、png格式的图片', icon: 'none' }); return; } pics.push(imgs[i].tempFilePath) } that.uploadimg({ url: getApp().globalData.webUrl + '_upload/attachments/project', // 图片上传的接口 path: pics, // 选取的图片的地址数组 }, type); }, }) }, //多张图片上传 uploadimg: function (data, itemName) { wx.showLoading({ title: '上传中...', mask: true, }) let that = this, i = data.i ? data.i : 0, success = data.success ? data.success : 0, fail = data.fail ? data.fail : 0; let imgs = that.data[itemName]; wx.uploadFile({ url: data.url, filePath: data.path[i], name: 'file', success: (resp) => { wx.hideLoading(); success++; let str = JSON.parse(resp.data) // 返回的结果,可能不同项目结果不一样 str = str.uploaded if (imgs.length >= 20) { return false; } else { let imgs_ = JSON.parse(JSON.stringify(imgs)) imgs_.push(str); that.setData({ [itemName]: imgs_, }) } }, fail: (res) => { fail++; console.log('fail:' + i + "fail:" + fail); }, complete: () => { i++; if (i == data.path.length) { // 当图片传完时,停止调用 console.log('执行完毕'); console.log('成功:' + success + " 失败:" + fail); } else { // 若图片还没有传完,则继续调用函数 data.i = i; data.success = success; data.fail = fail; that.uploadimg(data, itemName); // 递归,回调自己 } } }); }, // 删除图片 deleteImg: function (e) { const { type, index } = e.currentTarget.dataset const imgs = this.data[type] imgs.splice(index, 1); this.setData({ [type]: imgs }); }, confirm (e) { const { approve } = e.target.dataset const { state } = this.data.data const { shootingid, focusPerson, maintenancePersonIndex, maintenancePersonDepartmentShow, qualityPersonIndex, planStartTime, planEndTime, planStartTimeShow, planEndTimeShow, maintenanceRequirement, userInfo, planApproval, repair, repairImgs, checkDesc, cost, checkImgs } = this.data let nextState = '' let confirmData = {} let successMsg = '' if (state == 1 || state == 3) { nextState = 2 confirmData = { ...confirmData, repairPerson: focusPerson[maintenancePersonIndex], repairUnit: maintenancePersonDepartmentShow, checkPerson: focusPerson[qualityPersonIndex], startTime: moment(planStartTime).format(), endTime: moment(planEndTime).format(), repairAsk: maintenanceRequirement, } if (!this.mustInput({ repairPerson: focusPerson[maintenancePersonIndex], checkPerson: focusPerson[qualityPersonIndex], startTime: planStartTimeShow, endTime: planStartTimeShow, })) { return } successMsg = '制定完成' } else if (state == 2) { confirmData = { ...confirmData, approveOpinion: planApproval, approvePerson: userInfo, approveDate: moment().format(), } if (approve == 'agree') { nextState = 4 } else { nextState = 3 } successMsg = '审核完成' } else if (state == 4 || state == 7) { confirmData = { ...confirmData, repairDesc: repair, repairImage: repairImgs, } nextState = 5 successMsg = '上报成功' } else if (state == 5) { confirmData = { ...confirmData, checkOpinion: checkDesc, cost, checkImage: checkImgs, // checkPerson: { id: userInfo.id, name: userInfo.name } } if (approve == 'agree') { nextState = 6 } else { nextState = 7 } successMsg = '验收完成' } Request.put(editPatrolRecordIssueHandle(shootingid), { ...confirmData, state: nextState, }).then(res => { wx.redirectTo({ url: '/package/troubleshooting/index', complete: () => { wx.showToast({ title: successMsg, icon: 'success' }) } }) }) }, mustInput (field) { let fieldMap = { repairPerson: '维修人', checkPerson: '质检人', startTime: '计划开始时间', endTime: '计划结束时间', } let fieldKeys = Object.keys(field) for (let k of fieldKeys) { if (!field[k]) { wx.showToast({ title: `请填写/选择 ${fieldMap[k]}`, icon: 'none' }) return false } } return true }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady () { }, /** * 生命周期函数--监听页面显示 */ onShow () { }, /** * 生命周期函数--监听页面隐藏 */ onHide () { }, /** * 生命周期函数--监听页面卸载 */ onUnload () { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh () { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom () { }, /** * 用户点击右上角分享 */ onShareAppMessage () { } })