You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

636 lines
20 KiB

// package/inspectionInput/inspectionInput.js
import {
addPatrolRecord,
getPatrolTemplate,
getPatrolPlan
} from "../../utils/getApiUrl";
import {
Request
} from "../../common";
const moment = require("../../utils/moment");
Page({
/**
* 页面的初始数据
*/
data: {
dataList: '', // 当前巡检计划
itemData: '', // 点位
address: '', // 当前位置
imgUrl: getApp().globalData.imgUrl,
checkItems: [], // 检查项
inspectContentArr: [], // 巡检内容
inspectContentArrCopy:[],//保持源数据结构的巡检内容数组
isCommitting: false,
/*** 扫码巡检 ***/
planList: null, // 巡检计划列表
planListVisible: true,
scenePointId: null, // 当前点位id
devicesChecked: [], //设备复选框
pointDevices:[],
},
// 获取巡检计划
getPatrolPlan: function (scenePointId) {
let that = this;
wx.showLoading({
title: '加载中',
})
const userInfo = wx.getStorageSync('userInfo');
Request.get(getPatrolPlan(), {
userId: userInfo.id
}).then(res => {
wx.hideLoading();
let pointPlan = res.rows.filter(plan => {
for (const point of plan.points) {
if (point.id == scenePointId) {
return true;
}
}
return false;
}).map(p => ({
label: p.name,
value: p.name,
...p
}))
if (!pointPlan.length) {
wx.showModal({
title: '提示',
content: '巡检已完成,请更新计划以继续巡检',
showCancel: false,
success: function () {
wx.switchTab({
url: '/pages/index/index',
})
}
})
}
that.setData({
planList: pointPlan
})
})
},
// 获取巡检模板
getPatrolTemplate(templateId, pointDevices = []) {
Request.get(getPatrolTemplate(templateId)).then(res => {
const checkItems = res.rows[0].checkItems;
let inspectContentArr = [];
let inspectContentArrCopy = [];
// 有绑定设备的点位,每个设备都要检查各个检查项
if (pointDevices.length) {
//渲染数组
checkItems.forEach(c => {
inspectContentArr.push({
id: `${c.id}`,
name: c.name,
isNormal: true,
imgs: [],
msgInp: null,
level: null,
devices: pointDevices.map(device => ({
deviceName: device?.device?.name,
deviceId: device.deviceId,
id: `${device.deviceId}-${c.id}`
}))
})
})
pointDevices.forEach(device => {
inspectContentArrCopy.push({
deviceName: device?.device?.name,
deviceId: device.deviceId,
checkItems: checkItems.map(c => ({
id: `${device.deviceId}-${c.id}`,
name: c.name,
isNormal: true,
msgInp: null,
level: null,
imgs: [],
}))
})
});
// pointDevices.forEach(device => {
// inspectContentArr.push({
// deviceName: device?.device?.name,
// deviceId: device.deviceId,
// checkItems: checkItems.map(c => ({
// id: `${device.deviceId}-${c.id}`,
// name: c.name,
// isNormal: true,
// msgInp: null,
// level: null,
// imgs: [],
// }))
// })
// });
} else {
checkItems.forEach(c => {
inspectContentArr.push({
id: c.id,
name: c.name,
isNormal: true,
msgInp: null,
level: null,
imgs: [],
})})
}
this.setData({
checkItems,
inspectContentArr: inspectContentArr,
inspectContentArrCopy:inspectContentArrCopy,
pointDevices:pointDevices
})
})
},
onPickerChange(e) {
const {
key
} = e.currentTarget.dataset;
const {
value
} = e.detail;
this.setData({
[`${key}Visible`]: false,
[`${key}Value`]: value,
[`${key}Text`]: value.join(' '),
});
const curPlan = this.data.planList[e.detail.columns[0].index];
const nextItemData = curPlan.points.find(p => p.id == this.data.scenePointId)
this.setData({
dataList: curPlan,
itemData: nextItemData
});
this.getPatrolTemplate(curPlan.templateId, nextItemData.pointDevices);
},
onPickerCancel(e) {
const {
key
} = e.currentTarget.dataset;
this.setData({
[`${key}Visible`]: false,
});
},
onPlanListPicker() {
this.setData({
planListVisible: true
});
},
// 获取当前位置
selfLocation() {
const that = this
wx.showLoading({
title: '定位中',
mask: true,
});
wx.getLocation({
type: 'wgs84',
success: (res) => {
wx.request({
url: `https://apis.map.qq.com/ws/geocoder/v1/?location=${res.latitude},${res.longitude}&key=${getApp().globalData.key}`,
success: function (res) {
wx.hideLoading();
wx.chooseLocation({
success:function(res){
// 根据自己项目需求获取res内容
that.setData({
address: res.address
})
}
})
}
})
},
fail: (res) => {
wx.hideLoading();
if (res.errMsg === 'getLocation:fail auth deny') {
that.toSettingModal('请允许位置权限')
} else {
wx.showToast({
title: res.errMsg,
icon: 'none',
duration: 1000
});
}
}
});
},
toSettingModal() {
wx.showModal({
title: '提示',
confirmText: '去设置',
content: '请允许位置权限',
success: function (res) {
const {
confirm
} = res || {}
if (confirm) {
wx.openSetting()
}
}
})
},
handleChangeTwo(e) {
const {itemidx} = e.currentTarget.dataset
let nextInspectContentArr = this.data.inspectContentArr;
// nextInspectContentArr.forEach(e=>{
// const r=devicesChecked.find(i=>i?.join('-')==e.)
// e.isNormal=
// })
nextInspectContentArr[itemidx].isNormal = e.detail;
if (e.detail) { // 清除异常数据
nextInspectContentArr[itemidx].msgInp = null;
nextInspectContentArr[itemidx].level = null;
nextInspectContentArr[itemidx].imgs = [];
this.setData({
devicesChecked: []
})
}
this.setData({
inspectContentArr: nextInspectContentArr
})
},
handleChangeDevice(e) {
this.setData({
devicesChecked: e.detail
})
},
handleChangeThree(e) {
const { itemidx } = e.currentTarget.dataset;
let nextInspectContentArr = this.data.inspectContentArr
nextInspectContentArr[itemidx].level= e.detail
this.setData({
inspectContentArr: nextInspectContentArr
})
},
// 巡查详情
bindInput: function (e) {
const {deviceidx,itemidx} = e.currentTarget.dataset;
let nextInspectContentArr = this.data.inspectContentArr
nextInspectContentArr[itemidx].msgInp= e.detail.value
this.setData({
inspectContentArr: nextInspectContentArr
})
},
// 上传图片
chooseImg: function (e) { // 这里是选取图片的方法
const {
deviceidx,
itemidx
} = e.currentTarget.dataset;
const that = this;
let pics = [];
const detailPics = that.data.inspectContentArr[itemidx].imgs;
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, // 选取的图片的地址数组
}, deviceidx, itemidx);
},
})
},
//多张图片上传
uploadImg: function (data, deviceidx, itemidx) {
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.inspectContentArr[itemidx].imgs;
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) {
let nextInspectContentArr = that.data.inspectContentArr
nextInspectContentArr[itemidx].imgs = imgs;
that.setData({ inspectContentArr: nextInspectContentArr });
return false;
} else {
imgs.push(str);
let nextInspectContentArr = that.data.inspectContentArr
nextInspectContentArr[itemidx].imgs = imgs
that.setData({ inspectContentArr: nextInspectContentArr })
}
},
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, deviceidx, itemidx); // 递归,回调自己
}
}
});
},
// 删除图片
deleteImg: function (e) {
const {
itemidx,
index
} = e.currentTarget.dataset;
let imgs = this.data.inspectContentArr[itemidx].imgs;
imgs.splice(index, 1);
let nextInspectContentArr = this.data.inspectContentArr;
nextInspectContentArr[itemidx].imgs = imgs;
this.setData({
inspectContentArr: nextInspectContentArr
})
},
// 预览图片
previewImg: function (e) {
const {
deviceidx,
itemidx,
index
} = e.currentTarget.dataset;
// 所有图片
const imgs = this.data.inspectContentArr[itemidx].imgs;
const newImgs = imgs.map(i => this.data.imgUrl + i);
wx.previewImage({
// 当前显示图片
current: newImgs[index],
// 所有图片
urls: newImgs
})
},
bindCancel() {
if (this.data.scenePointId) {
wx.switchTab({
url: '/pages/index/index'
})
} else {
wx.navigateBack();
}
},
// 开始巡检录入
addPatrolRecord: function () {
const that = this;
if (that.data.isCommitting) {
return
}
let {itemData, inspectContentArrCopy, inspectContentArr, dataList,address,devicesChecked,pointDevices } = that.data;
let alarm = false;
// if (!address) {
// wx.showToast({
// title: '请获取当前位置',
// icon: 'none',
// duration: 1500
// })
// return;
// }
let reportArr =[]
if(pointDevices.length){
reportArr=inspectContentArrCopy.map(d => {
const r=devicesChecked.find(j=>j?.split('-')[0]==d.deviceId)
return {
...d,
checkItems:d.checkItems.map(i=>{
const cp=d.deviceId+'-'+i.id.split('-')[1]
const isExist=devicesChecked.some(l=>l==cp)
const rlst=inspectContentArr.find(q=>q.id==i.id.split('-')[1])
return {
isNormal:isExist?rlst?.isNormal:true,
level:isExist?rlst?.level:null,
msgInp:isExist?rlst?.msgInp:null,
imgs:isExist?rlst?.imgs:[],
name:i.name,
id:i.id
}
}),
alarm: r?true:false
}
})
}else{
reportArr = [{
checkItems:inspectContentArr,
alarm:false
}
]
}
for (const [index, checkItems] of inspectContentArr.entries()) {
if (checkItems.isNormal === null) {
wx.showToast({
title: '请填写完整',
icon: 'none',
duration: 1500
})
return;
}
if(checkItems.devices){
if(!checkItems.isNormal&&devicesChecked&&devicesChecked.length==0){
wx.showToast({
title: '选择了检查项异常,但未选择具体设备',
icon: 'none',
duration: 1500
})
return;
}
}
if ((!checkItems.isNormal) && (!checkItems.level || !checkItems.msgInp)) {
wx.showToast({
title: '异常项必须输入巡查详情和选择严重等级',
icon: 'none',
duration: 2000
})
return;
}
if (checkItems.isNormal === false) {
alarm = true; // 巡检记录异常
if(pointDevices.length){
reportArr[index].alarm = true; // 设备异常
}else{
//无设备
reportArr[index].alarm = true; //
}
}
}
const { id, name,departmentId, deptName } = wx.getStorageSync('userInfo');
let data = {
patrolPlanId: dataList.id,
pointId: itemData.id,
inspectionTime: moment().format('YYYY-MM-DD HH:mm:ss'),
points: { user: { id, name, department: { id: departmentId, name: deptName }},
project: dataList.project,
frequency: dataList.frequency,
itemData: itemData,
inspectContent: reportArr,
address: address
},
alarm,
projectId: dataList.project.id
}
wx.showLoading({
title: '提交中...'
});
that.setData({
isCommitting: true
});
Request.post(addPatrolRecord(), data).then(res => {
wx.hideLoading();
that.setData({
isCommitting: false
});
wx.showToast({
title: '提交成功',
icon: 'success'
})
setTimeout(() => {
that.bindCancel();
}, 1500)
})
},
/**
* 生命周期函数--监听页面加载
*/
onLoad(options) {
const that = this;
const scenePointId = options.scene;
if (scenePointId) { // 扫小程序码进入
const userInfo = wx.getStorageSync('userInfo');
if (!userInfo || !userInfo.id) { // 如果没登录,先登录
wx.showToast({
title: '请先登录'
})
wx.reLaunch({
url: `/pages/login/login?scene=${scenePointId}`
});
return;
}
that.setData({
scenePointId
});
that.getPatrolPlan(scenePointId);
} else { // 正常点击进入
const dataList = JSON.parse(decodeURIComponent(options.dataList));
const itemData = JSON.parse(decodeURIComponent(options.itemData));
that.setData({
dataList,
itemData
})
that.getPatrolTemplate(dataList.templateId, itemData.pointDevices);
}
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady() {
},
/**
* 生命周期函数--监听页面显示
*/
onShow() {
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide() {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload() {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh() {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom() {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage() {
}
})