cyx-ament-wechat/miniprogram/pages/workReport/workReport.js

411 lines
11 KiB
JavaScript
Raw Normal View History

2023-08-31 17:11:28 +08:00
// pages/workReport/workReport.js
const {API: $api} = require("../../utils/api");
import {base_url} from '../../utils/http';
Page({
/**
* 页面的初始数据
*/
data: {
searchForm:{
type:'',
target:'',
targetId:''
},
//搜索
typeOpt:[
{text:'请选择类型',value:''},
{text:'党务及意识形态',value:'党务及意识形态'},
{text:'党风廉政建设',value:'党风廉政建设'},
{text:'日常考核指标',value:'日常考核指标'},
2023-09-06 15:30:30 +08:00
{text:'季度考核指标',value:'季度考核指标'},
2023-08-31 17:11:28 +08:00
],
typeShow:false,
targetOpt:[],
targetShow:false,
mainActiveIndex:0,
activeId:null,
detailData:null,
fileList:[],
isRadio:false
},
/**
* 生命周期函数--监听页面加载
*/
onLoad(options) {
if(options && JSON.stringify(options) !== '{}'){
this.setData({
"searchForm.type":options.type,
isRadio:options.isRadio?options.isRadio:false
})
if(this.data.isRadio){
2023-10-09 10:57:16 +08:00
this.loadDetailData({
pageNum:1,
pageSize:10,
type:options.type,
id:options.id
});
2023-08-31 17:11:28 +08:00
}else{
this.loadTargetData({type:options.type});
}
}
},
//类型搜索
handleTypeOpen(){
this.setData({
typeShow:true
})
},
handleTypeClose(){
this.setData({
typeShow:false
})
},
handleTypeChange(e){
this.setData({
"searchForm.type": e.detail,
typeShow:false,
targetOpt:[],
mainActiveIndex:0,
activeId:null,
detailData:null,
"searchForm.target":""
});
this.loadTargetData({type:this.data.searchForm.type});
},
handleTypeClick(event) {
const { name } = event.currentTarget.dataset;
this.setData({
"searchForm.type": name,
typeShow:false,
targetOpt:[],
mainActiveIndex:0,
activeId:null,
detailData:null,
"searchForm.target":""
});
this.loadTargetData({type:this.data.searchForm.type});
},
//指标搜索
handleTargetOpen(){
this.setData({
targetShow:true
})
},
handleTargetClose(){
this.setData({
targetShow:false
})
},
handleTargetNav(e){
this.setData({
mainActiveIndex:e.detail.index
})
},
handleTargettem(e){
this.setData({
"searchForm.target":e.detail.text,
"searchForm.targetId":e.detail.id,
2023-09-06 15:30:30 +08:00
"searchForm.shotName":e.detail.shotName,
2023-08-31 17:11:28 +08:00
activeId:e.detail.id,
targetShow:false,
})
2023-10-09 10:57:16 +08:00
this.loadDetailData({
pageNum:1,
pageSize:10,
type:this.data.searchForm.type,
id:this.data.searchForm.targetId
});
2023-08-31 17:11:28 +08:00
},
//上传之前
handleBeforeUpload(e){
const { file, callback } = e.detail;
2023-10-17 09:42:53 +08:00
if(file.size > 52428800){
wx.showToast({
title:'文件大小不能大于50MB',
duration:2000,
icon:'none'
})
callback(false);
}else if(file.url.indexOf('.jpg') > -1 || file.url.indexOf('.png') > -1 || file.url.indexOf('.pdf') > -1 ||
2023-08-31 17:11:28 +08:00
file.url.indexOf('.jpg') > -1 || file.url.indexOf(".doc")>-1 || file.url.indexOf(".docx")>-1 || file.url.indexOf(".xlsx")>-1 ||
file.url.indexOf(".xls")>-1){
callback(true);
}else{
wx.showToast({
title:'支持jpg,png,pdf,doc,docx,xlsx,xls格式的文件',
duration:2000,
icon:'none'
})
callback(false);
}
},
2023-09-20 13:47:58 +08:00
//上传文件数据
2023-08-31 17:11:28 +08:00
handleUploadFile(e){
const { file } = e.detail;
let _fileList = this.data.fileList;
_fileList.push({
name:file.name,
url:file.url,
index:_fileList.length
});
this.setData({
fileList:_fileList
})
},
2023-09-20 13:47:58 +08:00
//上传图片数据
handleUploadImg(){
let _this = this;
wx.chooseMedia({
count:3,
mediaType:['image','video'],
sourceType:['album', 'camera'],
maxDuration:60, //视频拍摄时长3s-60s
success(res){
let _isUpload = false;
res.tempFiles.forEach(item => {
2023-10-17 09:42:53 +08:00
if(item.size > 52428800){
2023-09-20 13:47:58 +08:00
_isUpload = true;
}
});
if(_isUpload){
wx.showToast({
2023-10-17 09:42:53 +08:00
title:'图片或视频不能大于50MB',
2023-09-20 13:47:58 +08:00
icon:'none',
duration:1500
})
return false;
}
let _filesArr = res.tempFiles;
let _fileList = _this.data.fileList;
_filesArr.forEach(item => {
let _lastNum = item.tempFilePath.lastIndexOf('/')+1;
let _name =item.tempFilePath.substr(_lastNum,item.tempFilePath.length);
_fileList.push({
name:_name,
url:item.tempFilePath,
index:_fileList.length
})
})
_this.setData({
fileList:_fileList
})
}
})
},
2023-08-31 17:11:28 +08:00
//预览文件
handlePreView(e){
let _url = e.currentTarget.dataset.url;
if(_url.indexOf('.jpg') > -1 || _url.indexOf('.png') > -1){ //预览图片
wx.previewImage({
urls:[_url],
current:_url
})
}else{ //下载文件
wx.downloadFile({
url: _url, //仅为示例,并非真实的资源
success (res) {
if(res.statusCode === 200){
wx.openDocument({
filePath: res.tempFilePath,
success: function (res) {
console.log(res,'打开文档成功')
}
})
}
}
})
}
},
//删除文件
handleDelFile(e){
let _index = e.currentTarget.dataset.index;
let _fileList = this.data.fileList;
_fileList.forEach((val,idx) => {
if(val.index === _index){
_fileList.splice(idx,1);
}
});
_fileList.forEach((val,idx) => {
val.index = idx;
})
this.setData({
fileList:_fileList
})
},
//封装单个上传的方法
uploadFile(params){
return new Promise((resolve, reject) => {
wx.uploadFile({
url: base_url+"/common/upload",
filePath: params.url,
name: 'file',
success: function(data){
resolve(data);
},
fail(data) {
reject(data);
}
})
})
},
//批量上传多个文件的方法
uploadFiles(fileList){
let promises = fileList.map(item => this.uploadFile(item));
return Promise.all(promises);
},
//保存数据
handleSubmit(){
let _this = this;
if(!_this.data.detailData){
wx.showToast({
title:'请先查询上方汇报数据!',
duration:1500,
icon:'none'
})
return false;
}
if(_this.data.fileList.length === 0){
wx.showToast({
title:'请上传附件!',
duration:1500,
icon:'none'
})
return false;
}
let _data = _this.data.detailData;
let _files = [];
let _fileList = _this.data.fileList;
_this.uploadFiles(_fileList).then(obj => {
obj.forEach(item => {
let _data = JSON.parse(item.data);
_files.push(_data.fileName);
})
let _submitForm = {
header:_data.header,
deptName:_data.deptName,
shotName:_data.shotName,
remark:_data.remark,
id:_data.id,
files:_files.join(',')
};
$api.commit(_submitForm).then(res => {
if(res.code === 0){
wx.showToast({
title:'保存成功',
duration:1500
});
setTimeout(function () {
2023-09-20 13:47:58 +08:00
if(_this.data.isRadio){
wx.navigateBack();
}else{
_this.setData({
"searchForm.target":'',
"searchForm.targetId":'',
targetOpt:[],
mainActiveIndex:0,
activeId:null,
detailData:null,
fileList:[]
})
_this.loadTargetData({type:_this.data.searchForm.type});
}
2023-08-31 17:11:28 +08:00
},1500)
}else{
wx.showToast({
title:res.msg,
duration:1500,
icon:'none'
});
}
})
})
},
//加载指标
loadTargetData(param){
let _this = this;
$api.queryTree(param).then(res => {
if(res.code === 0){
res.data.forEach((item,index) => {
item.index = index;
})
_this.setData({
targetOpt:res.data
})
}
})
},
//加载详情数据
2023-10-09 10:57:16 +08:00
loadDetailData(param){
2023-08-31 17:11:28 +08:00
let _this = this;
2023-10-09 10:57:16 +08:00
$api.queryList(param).then(res => {
2023-08-31 17:11:28 +08:00
if(res.code === 0){
let _data = null;
if(res.rows.length>0){
_data = res.rows[0];
}
_this.setData({
detailData:_data
})
}
})
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady() {
},
/**
* 生命周期函数--监听页面显示
*/
onShow() {
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide() {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload() {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh() {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom() {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage() {
}
})