// pages/materialUpload/index.js const app = getApp(); const $$ = require('../../utils/util'); // 删除附件 function delfileApi(id) { return $$.request({ url: 'fileInfo/deleteById?id=' + id, type: 'get', service: 'sys', v1: true }); } Page({ /** * 页面的初始数据 */ previewImageVisible: false, // 判断是否进入查看图片 filesArr: [], data: { popup: {}, // 下拉弹出框数据 data: [], // 材料数据 activeMaterial: 0, // 当前选中的材料下标 person: [], // 身份证明材料中选择的人员数据 activePerson: 0, // 人员数据下标 formatData: { fileList: [], personFile: [], }, // 调整展示数据格式 caseId: 0, // 案件id fileUrl: $$.baseUrl + $$.url.fileShowUrl, }, // 展示弹出层 handleShowPopup(e) { let data = e.currentTarget.dataset.type === 'materialtype' ? this.data.data.map((x) => { delete x.fileList; x.label = x.title; return x; }) : this.data.person.map((x) => { x.label = x.trueName; return x; }); let index = 0; foreach: for (let i = 0; i <= data.length; i++) { if (data[i].label === e.currentTarget.dataset.value) { index = i; break foreach; } } this.setData({ popup: { visible: true, title: e.currentTarget.dataset.title, selectData: data, type: e.currentTarget.dataset.type, noPicker: true, activeIndex: index, }, }); }, // 关闭弹出层 handleClosePopup() { this.data.popup.visible = false; this.setData({ popup: this.data.popup }); }, // 下拉框确认选择 handleConfirmPicker({ detail }) { let type = this.data.popup.type; let index = detail.detail.index; this.data.popup.visible = false; if (type === 'materialtype') { this.data.activeMaterial = index; } else { this.data.activePerson = index; } this.setData({ popup: this.data.popup, activeMaterial: this.data.activeMaterial, activePerson: this.data.activePerson }, () => { this.formatData(); }); }, // 预览图片 handlePreviewImage(e) { let index = e.currentTarget.dataset.index; let personindex = e.currentTarget.dataset.personindex; let urls = []; let arr = this.data.activeMaterial !== 2 ? this.data.formatData.fileList : this.data.formatData.personFile[personindex].fileList; arr.forEach((x) => { urls.push(x.wxurl ? x.wxurl : `${this.data.fileUrl}${x.id}`); }); wx.previewImage({ current: arr[index].wxurl || `${this.data.fileUrl}${arr[index].id}`, urls: urls, }); this.previewImageVisible = true; }, // 删除图片 async handleDelImage(e) { let ownerType = e.currentTarget.dataset.ownertype; let ownerId = e.currentTarget.dataset.ownerid; let id = e.currentTarget.dataset.id; $$.showLoading(); const res = await delfileApi(id); if (res.type) { $$.showToast({ icon: 'success', title: '删除成功', duration: 500 }); let files = this.data.data[this.data.activeMaterial].files; let num = this.data.data[this.data.activeMaterial].num; let fileList = []; if (this.data.activeMaterial !== 2) { fileList = files.filter((item) => item.ownerType === ownerType)[0].fileList; } else { fileList = files.filter((item) => item.ownerId === ownerId)[0].fileList; } fileList.forEach((x, t) => { if (x.id === id) { fileList.splice(t, 1); num = num - 1; } }); this.data.data[this.data.activeMaterial].num = num; this.setData({ data: this.data.data }, () => { app.globalData.material.data = this.data.data; this.formatData(); }); } }, // 数据格式化显示 formatData() { let fileList = []; let personFile = []; if (this.data.activeMaterial !== 2) { fileList = this.data.data[this.data.activeMaterial].files[0]?.fileList || []; } else { let person = this.data.person; let files = this.data.data[this.data.activeMaterial].files; let personId = person[this.data.activePerson].id; let personFiles = files.filter((item) => item.ownerId === personId)[0]?.fileList || []; if (person[this.data.activePerson].agent) { // 代理人 personFile = [ { ownerId: personId, value: '22_00018-205', title: '身份证明材料', fileList: personFiles.filter((item) => item.ownerType === '22_00018-205'), text: '请上传“居民身份证”', }, { ownerId: personId, value: '22_00018-207', title: '授权委托书', fileList: personFiles.filter((item) => item.ownerType === '22_00018-207'), text: '请上传代理人的“授权委托书”', }, ]; } else { // 当事人 if (person[this.data.activePerson].perClass === '09_01001-1') { // 申请人 or 被申请人的人员类型:自然人 personFile = [ { ownerId: personId, value: '22_00018-202', title: '身份证明材料', fileList: personFiles.filter((item) => item.ownerType === '22_00018-202'), text: '请上传“居民身份证”', }, ]; } else if (person[this.data.activePerson].perClass === '09_01001-2' || person[this.data.activePerson].perClass === '09_01001-3') { // 申请人 or 被申请人的人员类型:法人,非法人组织 let name = person[this.data.activePerson].perClass === '09_01001-2' ? '法定' : '机构'; personFile = [ { ownerId: personId, value: '22_00018-204', title: name + '代表人身份证明', text: person[this.data.activePerson].perClass === '09_01001-2' ? '请上传“法定代表人居民身份证”和“法定代表人证明书”' : '请上传“机构代表人居民身份证”', fileList: personFiles.filter((item) => item.ownerType === '22_00018-204'), }, { ownerId: personId, value: '22_00018-203', title: name + '登记材料', text: '可上传“工商登记证明”或“组织机构代码证”', fileList: personFiles.filter((item) => item.ownerType === '22_00018-203'), }, ]; } } } this.setData({ formatData: { fileList, personFile } }); }, // 上传文件图片 async handleUploadFile(e) { let id = e.currentTarget.dataset.id; let type = e.currentTarget.dataset.type; let that = this; this.filesArr = []; wx.chooseMedia({ mediaType: ['image'], success(res2) { const tempFiles = res2.tempFiles; // 多个文件逐一上传 tempFiles.forEach((x, t) => { $$.showLoading('上传中...'); wx.uploadFile({ url: `${$$.baseUrl}${$$.url.sys}/api/v1/fileInfo/wechatUpload/${id}/${type}`, filePath: x.tempFilePath, name: 'file', header: { Authorization: app.globalData.token }, complete(res) { $$.hideLoading(); if (res.errMsg === 'uploadFile:ok') { const data = { ...JSON.parse(res.data), wxurl: x.tempFilePath }; that.filesArr.splice(t, 0, data); } else { that.filesArr.splice(t, 0, { code: '400' }); } if (tempFiles.length === that.filesArr.length) { let errNum = 0; let files = that.data.data[that.data.activeMaterial].files; let num = that.data.data[that.data.activeMaterial].num; that.filesArr.forEach((y) => { if (y.code == 0) { // 将文件数据回填展示 let file = y.data[0]; file.wxurl = y.tempFilePath; let fileList = []; // 判断是否是人员的身份材料上传 if (that.data.activeMaterial !== 2) { if (files.length === 0) { // 当无时添加默认 files[0] = { ownerType: type, fileList: [] }; } fileList = files.filter((item) => item.ownerType === type)[0].fileList; } else { let personFiles = files.filter((item) => item.ownerId === id); if (personFiles.length === 0) { // 当无时添加默认 files.push({ ownerId: id, ownerName: that.data.person[that.data.activePerson].trueName, fileList: [] }); } fileList = files.filter((item) => item.ownerId === id)[0].fileList; } num = num + 1; fileList.push(file); } else { errNum = errNum + 1; } }); if (errNum !== 0) { $$.showToast({ title: `已上传:${tempFiles.length - errNum},失败:${errNum}`, duration: 500 }); } else { $$.showToast({ icon: 'success', title: '上传成功', duration: 500 }); } app.globalData.material.uploadVisible = true; that.data.data[that.data.activeMaterial].num = num; that.setData({ data: that.data.data }, () => { app.globalData.material.data = that.data.data; that.formatData(); }); } }, }); }); }, }); }, /** * 生命周期函数--监听页面加载 */ onLoad(options) { if (!this.previewImageVisible) { this.setData( { data: app.globalData.material.data, activeMaterial: parseInt(options.activeMaterial), person: app.globalData.material.person, caseId: options.caseId, }, () => { this.formatData(); } ); } if (this.previewImageVisible) { this.previewImageVisible = false; } }, });