app-xiangsonghua/app-saas-src/script/systemOrder/systemOrderEdit.js
2024-12-26 17:00:06 +08:00

729 lines
24 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

apiready = function() {
var ctrl = {
init: {},
bind: {},
prefix: 'https://cdyfile.saas.dianwutong.com/',
employeeId: '',
prId: '',
orderData: {
pics: [],
serverPicUrls: [],
uploadPicNum: 0,
resultDesc: ''
},
tabType: 'order',
problemData: {
status: true, // true:完成了初始化false:当前存在未提交设备缺陷
desc: '', // 设备缺陷 文字描述
pics: [], // 设备缺陷 图片信息
serverPicUrls: [], // 上传完成,返回的图片 url
uploadPicNum: 0, // 已经上传完成的图片的数量
activeProblemItemDom: null, // 当前正在编辑的设备缺陷 DOM
},
};
ctrl.init = function() {
CommonModel.fitInStatusBar();
ctrl.orderId = api.pageParam.orderid;
ctrl.status = api.pageParam.status;
ctrl.employeeId = $api.getStorage('employeeId');
ctrl.startBtnDom = $('#start-btn');
ctrl.finishBtnDom = $('#finish-btn');
if (ctrl.status == 1) { // 未处理
ctrl.startBtnDom.show();
ctrl.finishBtnDom.hide();
$('.start-show').hide();
} else if (ctrl.status == 2) { // 处理中
ctrl.startBtnDom.hide();
ctrl.finishBtnDom.show();
$('.start-show').show();
};
ctrl.bind();
ctrl.initOrderDetail();
}
ctrl.bind = function() {
//点击返回按钮
$api.addEvt($api.dom("#back"), "touchend", function() {
api.closeWin();
});
//点击 “工单详情” tab按钮
$api.addEvt($api.dom("#order-tab"), "touchend", function () {
if (ctrl.tabType == 'order') {
return
}
$api.addCls(this, "active");
$api.removeCls($api.dom("#problem-tab"), "active");
$api.css($api.dom("#order-panel"), 'display:block');
$api.css($api.dom("#problem-panel"), 'display:none');
$api.css($api.dom('#inspectionitem-empty-tips'), "display:none");
ctrl.tabType = 'order'
}, false);
//点击 “设备缺陷” tab按钮
$api.addEvt($api.dom("#problem-tab"), "touchend", function () {
if (ctrl.tabType == 'problem') {
return
}
$api.addCls(this, "active");
$api.removeCls($api.dom("#order-tab"), "active");
$api.css($api.dom("#problem-panel"), 'display:block');
$api.css($api.dom("#order-panel"), 'display:none');
$api.css($api.dom('#inspectionitem-empty-tips'), "display:none");
ctrl.tabType = 'problem';
}, false);
/**
* =============================order-panel 工单详情 中的事件绑定===============================
*/
// 点击 添加 图片 +
// $("#pic-add-btn").on("touchend", function () {
// var resultPicContentDom = $('#result-pic-content');
// var params = {
// sourceType: 'camera',
// destinationType: 'base64'
// };
// api.getPicture(params, function(ret, err){
// if (ret && ret.data) {
// // 创建图片预览dom
// var picItemDom = '<span class="pic-item"><i></i><img class="pic" src="{{url}}" alt=""></span>';
// var dom = picItemDom.replace('{{url}}', ret.data);
// resultPicContentDom.prepend(dom);
// // 保存 图片 base64 和 url
// ctrl.orderData.pics.push({
// url: ret.data
// });
// }
// })
// });
// 点击 相册选择 添加 设备缺陷图片
$("#pic-add-btn").on("touchend", function () {
var picContentDom = $('#pic-content');
var params = {
sourceType: 'library',
// sourceType: 'camera',
// sourceType: 'album',
destinationType: 'base64'
}
api.getPicture(params, function(ret, err){
if (ret && ret.data) {
// 创建图片预览dom
var picItemDom = '<span class="pic-item"><i></i><img class="problem-pic-item" src="{{url}}" alt=""></span>'
var dom = picItemDom.replace('{{url}}', ret.data)
picContentDom.prepend(dom)
// 保存 图片 base64 和 url
ctrl.orderData.pics.push({
url: ret.data
});
}
})
});
// 点击 照相机 拍照添加 设备缺陷图片
$("#camera-btn").on("touchend", function () {
var picContentDom = $('#result-pic-content');
var params = {
// sourceType: 'library',
sourceType: 'camera',
// sourceType: 'album',
destinationType: 'base64'
}
api.getPicture(params, function(ret, err){
if (ret && ret.data) {
// 创建图片预览dom
var picItemDom = '<span class="pic-item"><i></i><img class="problem-pic-item" src="{{url}}" alt=""></span>'
var dom = picItemDom.replace('{{url}}', ret.data)
picContentDom.prepend(dom)
// 保存 图片 base64 和 url
ctrl.orderData.pics.push({
url: ret.data
});
}
})
});
// 图片 删除按钮
$("#result-pic-content").on("touchend", "i", function (e) {
e.stopPropagation();
var _this = this;
api.confirm({
title: '',
msg: '确定要删除吗',
buttons: ['确定', '取消']
}, function(ret, err) {
var index = ret.buttonIndex;
if (index == 1) { // 确定
var url = $($(_this).siblings('img')[0]).attr('src');
$(_this).parent('.pic-item').remove();
// 从 ctrl.orderData.pics 中删除对应url
ctrl.orderData.pics = ctrl.orderData.pics.filter(function (item) {
return item.url != url;
});
}
});
});
// 点击图片预览
$("#order-panel").on("touchend", ".pic", function (e) {
e.stopPropagation();
var src = $(this).attr('src');
$('#pic-review img').attr('src', src);
$('#pic-review').css('display', 'block');
});
// 点击 “开始处理” 按钮
$('#start-btn').on("touchend", function () {
api.confirm({
title: '',
msg: '确定开始处理吗',
buttons: ['确定', '取消']
}, function(ret, err) {
var index = ret.buttonIndex;
if (index == 1) { // 确定
ctrl.handleStart();
}
});
});
// 点击 “处理完成” 按钮
$('#finish-btn').on("touchend", function () {
api.confirm({
title: '',
msg: '确定提交处理结果吗',
buttons: ['确定', '取消']
}, function(ret, err) {
var index = ret.buttonIndex;
if (index == 1) { // 确定
// 判断是否有未提交的设备缺陷
if (!ctrl.problemData.status) {
ctrl.toast('存在未提交的设备缺陷!');
} else {
ctrl.handleFinish();
};
}
});
});
/**
* ===============================prolem-panel 设备缺陷 中的事件绑定================================
*/
// 点击 新增设备缺陷
$('#new-add-btn').on("touchend", function () {
// 判断当前工单是否是 “处理中” ,“未处理”工单不可以添加设备缺陷
if (ctrl.status == 1) {
ctrl.toast("工单未开始处理,不能添加设备缺陷");
return
};
// 判断是否有未提交的设备缺陷
if (ctrl.problemData.status) {
// 页面中添加一个 问题 li DOM
ctrl.addProblemItemDom();
ctrl.problemData.status = false;
} else {
ctrl.toast('存在未提交的设备缺陷!')
};
});
// 点击 添加设备缺陷图片 +
$("#problem-list-container").on("touchend", ".pic-add-btn", function () {
var picContentDom = $(this).parent('.pic-content');
var params = {
sourceType: 'camera',
destinationType: 'base64'
}
api.getPicture(params, function(ret, err){
if (ret && ret.data) {
// 创建图片预览dom
var picItemDom = '<span class="pic-item"><i></i><img class="problem-pic-item" src="{{url}}" alt=""></span>'
var dom = picItemDom.replace('{{url}}', ret.data)
picContentDom.prepend(dom)
// 保存 图片 base64 和 url
ctrl.problemData.pics.push({
url: ret.data
})
}
})
});
// 图片预览
$("#problem-list-container").on("touchend", ".problem-pic-item", function (e) {
e.stopPropagation();
var src = $(this).attr('src');
$('#pic-review img').attr('src', src);
$('#pic-review').css('display', 'block');
});
// 设备缺陷 图片 “删除”
$("#problem-list-container").on("touchend", "i", function (e) {
e.stopPropagation();
var _this = this;
api.confirm({
title: '',
msg: '确定要删除吗',
buttons: ['确定', '取消']
}, function(ret, err) {
var index = ret.buttonIndex;
if (index == 1) { // 确定
var url = $($(_this).siblings('img')[0]).attr('src');
$(_this).parent('.pic-item').remove();
// 从 ctrl.problemData.pics 中删除对应url
ctrl.problemData.pics = ctrl.problemData.pics.filter(function (item) {
return item.url != url;
});
}
});
});
// 点击 “删除” 设备缺陷
$('#problem-list-container').on("touchend", ".delete-btn", function () {
var _this = this;
api.confirm({
title: '',
msg: '确定要删除吗',
buttons: ['确定', '取消']
}, function(ret, err) {
ctrl.problemData.activeProblemItemDom = $(_this).parents('.problem-item');
var index = ret.buttonIndex;
if (index == 1) { // 确定
ctrl.handleDelete();
}
});
});
// 点击 “提交” 设备缺陷
$('#problem-list-container').on("touchend", ".submit-btn", function () {
ctrl.problemData.activeProblemItemDom = $(this).parents('.problem-item');
ctrl.problemData.desc = ctrl.problemData.activeProblemItemDom.find('.desc').val().trim();
api.confirm({
title: '',
msg: '确定要提交吗',
buttons: ['确定', '取消']
}, function(ret, err) {
var index = ret.buttonIndex;
if (index == 1) { // 确定
if (!ctrl.problemData.desc.trim()) {
ctrl.toast('缺陷描述不能为空!');
} else {
ctrl.handleSubmit();
};
}
});
});
// 点击预览图片 关闭预览
$('#pic-review').on("touchend", function (e) {
e.stopPropagation();
$(this).css('display', 'none');
});
};
/**
* 弹出提示框
*/
ctrl.toast = function (msg) {
api.toast({
msg: msg,
duration: 3000,
locaiton: 'top'
});
};
// 初始化 工单详情
ctrl.initOrderDetail = function () {
ctrl.getOrderDetail();
};
// 获取工单详情
ctrl.getOrderDetail = function () {
var url = '/test/gong-dan/queryById/' + ctrl.orderId;
api.showProgress({
title: '载入中...',
text: '请稍后',
modal: false
});
$api.get(url, function(res, err) {
api.hideProgress();
if (err) {
ctrl.toast("网络请求失败");
} else {
if (!res.code || res.code != "200" || !res.data) {
ctrl.toast("服务器响应错误" + (res.code ? (",错误码:" + res.code) : ""));
} else {
ctrl.prId = res.data.prId;
ctrl.renderOrderDetail(res.data);
}
}
});
};
// 渲染工单详情
ctrl.renderOrderDetail = function (data) {
$('#pr-name').text(data.prName);
$('#person').text(data.tiJiaoRenName);
$('#create-time').text(data.tiJiaoShiJian);
$('#desc').text(data.miaoShu);
// 渲染问题照片
var picContentDom = $('#pic-content');
if (data.zhaoPian) {
ctrl.renderPics(picContentDom, data.zhaoPian);
} else {
picContentDom.html('无');
};
};
// 渲染 图片
ctrl.renderPics = function(dom, pics) {
dom.empty();
var picsArr = pics.split(',');
var length = picsArr.length;
for (var i = 0; i < length; i++) {
var picUrl = ctrl.prefix + picsArr[i];
var picDom = '<span class="pic-item"><img class="pic" src="'+ picUrl +'" alt=""></span>';
dom.append(picDom);
}
};
// “开始处理”
ctrl.handleStart = function () {
api.showProgress();
var url = '/test/gong-dan/gongDanKaiShi';
var data = {
id: ctrl.orderId
};
$api.post(url, data, function (res, err) {
if (err) {
api.hideProgress();
ctrl.toast("开始失败,请稍后再试");
} else {
if (res.code == 200) {
ctrl.startBtnDom.hide();
ctrl.finishBtnDom.show();
$('.start-show').show();
api.hideProgress();
ctrl.toast("开始成功");
ctrl.status = 2;
} else {
api.hideProgress();
ctrl.toast("服务器响应错误" + (res.code ? (",错误码:" + res.code) : ""));
}
};
});
};
// “处理完成”
ctrl.handleFinish = function () {
// 数据校验
var flag = ctrl.checkData();
if (!flag) {
ctrl.toast("请填写完整,再提交");
return;
};
api.showProgress();
var pics = ctrl.orderData.pics;
var length = pics.length;
if (length > 0) { // 有图片
// 上传 图片
for (var i = 0; i < length; i++) {
var url = pics[i]['url'];
ctrl.uploadPic(url);
}
} else { // 没图片
ctrl.submit();
};
};
// 数据校验
ctrl.checkData = function () {
ctrl.orderData.resultDesc = $('#result-desc').val().trim();
if (ctrl.orderData.resultDesc) { // 验证处理结果
return true;
} else {
return false;
};
};
// 上传图片
ctrl.uploadPic = function (file) {
var url = "/ems/rest/common/file/pad_upload";
var data = file;
$api.uploadFile(url, data, function (res, err) {
if (err) {
ctrl.toast("网络请求失败,请稍后再试");
} else {
if (res.code == 200) {
// 将返回的图片路径保存
if (res.body && res.body.fileUrl) {
ctrl.orderData.serverPicUrls.push(res.body.fileUrl)
ctrl.orderData.uploadPicNum++;
if (ctrl.orderData.uploadPicNum == ctrl.orderData.pics.length) {// 代表全部图片上传完成
// 2、调用接口
ctrl.submit();
}
} else {
api.hideProgress();
ctrl.toast("服务器错误,未返回数据");
}
} else {
api.hideProgress();
ctrl.toast("服务器响应错误" + (res.code ? (",错误码:" + res.code) : ""));
}
}
})
};
// 提交处理结果
ctrl.submit = function () {
api.showProgress();
var chuLiZhaoPian = ctrl.orderData.serverPicUrls.join(',');
var url = '/test/gong-dan/gongDanWanCheng';
var data = {
id: ctrl.orderId,
chuLiMiaoShu: ctrl.orderData.resultDesc
};
if (chuLiZhaoPian) {
data.chuLiZhaoPian = chuLiZhaoPian;
};
$api.post(url, data, function (res, err) {
if (err) {
api.hideProgress();
ctrl.toast("提交结果失败,请稍后再试");
} else {
if (res.code == 200) {
// 返回 我的工单 页
api.sendEvent({
name: 'orderTaskChanged'
});
api.hideProgress();
ctrl.toast("提交成功");
api.closeWin();
} else {
api.hideProgress();
ctrl.toast("服务器响应错误" + (res.code ? (",错误码:" + res.code) : ""));
}
};
});
};
/**
* ============================= 设备缺陷 panel ============================
*/
// 添加一个 设备缺陷 dom
ctrl.addProblemItemDom = function() {
var index = $('#problem-list-container li').length + 1;
var itemDom = '';
itemDom += '<li class="problem-item">';
itemDom += '<div class="problem-index">';
itemDom += '<div class="problem-item-title">问题<span>{index}</span></div>';
itemDom += '<div class="btns">'
itemDom += '<div class="delete-btn">'
itemDom += '删除'
itemDom += '</div>'
itemDom += '<div class="submit-btn">';
itemDom += '提交';
itemDom += '</div>';
itemDom += '</div>'
itemDom += '<div class="finished">已提交</div>'
itemDom += '</div>';
itemDom += '<div class="problem-desc">';
itemDom += '<div class="problem-item-title"><b>*</b>问题描述:</div>';
itemDom += '<div>';
itemDom += '<textarea class="desc" name="" id="" cols="30" rows="10" placeholder="请输入问题描述..."></textarea>';
itemDom += '</div>';
itemDom += '</div>';
itemDom += '<div class="problem-pic">';
itemDom += '<div class="problem-item-title">现场照片:</div>';
itemDom += '<div class="pic-content" class="pics problem-item-content">';
itemDom += '<img class="pic-add-btn" src="../../image/add.png" alt="">';
itemDom += '</div>';
itemDom += '</div>';
itemDom += '</li>';
itemDom = itemDom.replace('{index}', index);
$('#problem-list-container').append(itemDom);
};
// 处理 删除 事件
ctrl.handleDelete = function () {
ctrl.problemData.activeProblemItemDom.remove();
ctrl.resetProblemData();
};
// 处理 问题 提交 事件
ctrl.handleSubmit = function() {
api.showProgress();
var pics = ctrl.problemData.pics;
var length = pics.length;
if (length > 0) { // 有图片
// 上传 图片
for (var i = 0; i < length; i++) {
var url = pics[i]['url']
ctrl.uploadProblemPic(url)
}
} else { // 没图片
ctrl.submitProblem();
};
};
// 重置 设备缺陷 数据表单
ctrl.resetProblemData = function () {
ctrl.problemData.status = true;
ctrl.problemData.desc = '';
ctrl.problemData.pics = [];
ctrl.problemData.serverPicUrls = [];
ctrl.problemData.uploadPicNum = 0;
ctrl.problemData.activeProblemItemDom = null;
};
// 上传 设备缺陷 图片
ctrl.uploadProblemPic = function (file) {
var url = "/ems/rest/common/file/pad_upload";
var data = file;
$api.uploadFile(url, data, function (res, err) {
if (err) {
api.hideProgress();
ctrl.toast("网络请求失败,请稍后再试");
} else {
if (res.code == 200) {
// 将返回的图片路径保存
if (res.body && res.body.fileUrl) {
ctrl.problemData.serverPicUrls.push(res.body.fileUrl)
ctrl.problemData.uploadPicNum++;
if (ctrl.problemData.uploadPicNum == ctrl.problemData.pics.length) {// 代表全部图片上传完成
// 2、调用提交 设备缺陷接口
ctrl.submitProblem();
}
} else {
api.hideProgress();
ctrl.toast("服务器错误,未返回数据");
}
} else {
api.hideProgress();
ctrl.toast("服务器响应错误" + (res.code ? (",错误码:" + res.code) : ""));
}
}
})
};
// 提交 设备缺陷
ctrl.submitProblem = function() {
api.showProgress();
var zhaoPian = ctrl.problemData.serverPicUrls.join(',');
var url = '/test/gong-dan/insert';
var data = {
tiJiaoRenId: ctrl.employeeId,
xunJianPrId: ctrl.id,
prId: ctrl.prId,
miaoShu: ctrl.problemData.desc,
isXjProblem: 1
};
if (zhaoPian) {
data.zhaoPian = zhaoPian;
};
$api.post(url, data, function (res, err) {
if (err) {
api.hideProgress();
ctrl.toast("提交设备缺陷失败,请稍后再试");
} else {
if (res.code == 200) {
// 隐藏当前设备缺陷的 删除 和 提交 按钮,隐藏 添加图片 删除图片 按钮
ctrl.problemData.activeProblemItemDom.find('.btns').css('display', 'none');
ctrl.problemData.activeProblemItemDom.find('.finished').css('display', 'block');
ctrl.problemData.activeProblemItemDom.find('.pic-add-btn').css('display', 'none');
ctrl.problemData.activeProblemItemDom.find('.pic-item i').css('display', 'none');
ctrl.problemData.activeProblemItemDom.find('.desc').attr('disabled', 'disabled');
// 初始化 problemData
ctrl.resetProblemData();
api.hideProgress();
ctrl.toast("提交成功");
} else {
api.hideProgress();
ctrl.toast("服务器响应错误" + (res.code ? (",错误码:" + res.code) : ""));
}
};
});
};
ctrl.init();
}