// pages/cert/cert.js import { postApi, getApi } from "../../apis/api"; Page({ data: { shipName: "", mmsi: "", shipCerts: [], crewCerts: [], certs: [], contacts: [], introduce: {}, }, async getCerts() { let { data } = await getApi("/ship/cert/list"); console.log(data); if (data.status === 0) { // 处理船舶基本信息 const shipInfo = data.result; // 处理船舶证书信息 const formattedShipCerts = shipInfo.shipCerts.map((cert) => { // 获取第一个有效期(如果有多个,默认使用第一个) const expireDate = cert.certValidities && cert.certValidities.length > 0 ? cert.certValidities[0].endValidTime .split(" ")[0] .replace(/\//g, "-") : ""; // 处理所有的certFileViewUrls,确保它们都被正确格式化 const formattedViewUrls = cert.certFileViewUrls && cert.certFileViewUrls.length > 0 ? cert.certFileViewUrls.map((url) => url.trim().replace(/` /g, "").replace(/ `/g, "") ) : ["暂无"]; return { name: cert.certTypeName, photoUrls: formattedViewUrls, photoUrl: formattedViewUrls[0], // 保留单张图片的兼容性 expireDate: expireDate, }; }); // 处理船员证书信息 const formattedCrewCerts = shipInfo.shipCrewCerts.map((crew) => { // 处理船员的证书 const crewCertificates = crew.certs.map((cert) => { // 处理所有的viewUrls,确保它们都被正确格式化 const formattedViewUrls = cert.viewUrls && cert.viewUrls.length > 0 ? cert.viewUrls.map((url) => url.trim().replace(/` /g, "").replace(/ `/g, "") ) : ["暂无"]; return { name: cert.docTypeName, photoUrls: formattedViewUrls, photoUrl: formattedViewUrls[0], // 保留单张图片的兼容性 expireDate: cert.expiryAt ? cert.expiryAt.split(" ")[0].replace(/\//g, "-") : "", }; }); // 找到医疗报告和服务簿的URL const medicalReportCert = crew.certs.find((cert) => cert.docType === 2); const serviceBookCert = crew.certs.find((cert) => cert.docType === 3); // 处理医疗报告的所有URL const medicalReportUrls = medicalReportCert && medicalReportCert.viewUrls && medicalReportCert.viewUrls.length > 0 ? medicalReportCert.viewUrls.map((url) => url.trim().replace(/` /g, "").replace(/ `/g, "") ) : ["暂无"]; // 处理服务簿的所有URL const serviceBookUrls = serviceBookCert && serviceBookCert.viewUrls && serviceBookCert.viewUrls.length > 0 ? serviceBookCert.viewUrls.map((url) => url.trim().replace(/` /g, "").replace(/ `/g, "") ) : ["暂无"]; return { name: crew.fullName, medicalReportUrls: medicalReportUrls, medicalReport: medicalReportUrls[0], // 保留单张图片的兼容性 serviceBookUrls: serviceBookUrls, serviceBook: serviceBookUrls[0], // 保留单张图片的兼容性 certificates: crewCertificates, }; }); this.setData({ shipName: shipInfo.shipName, mmsi: shipInfo.mmsi, shipCerts: formattedShipCerts, crewCerts: formattedCrewCerts, certs: data.result, }); } else { this.setData({ shipCerts: [], crewCerts: [], certs: [], }); wx.showToast({ title: data.msg, icon: "none", }); } }, async getContacts() { let { data } = await getApi("/ship/cert/operation/contacts"); if (data.status === 0) { this.setData({ contacts: data.result, }); } else { this.setData({ contacts: [], }); wx.showToast({ title: data.msg, icon: "none", }); } }, async getIntroduce() { let { data } = await getApi("/ship/cert/operation/introduce"); if (data.status === 0) { this.setData({ introduce: data.result, }); } else { this.setData({ introduce: {}, }); wx.showToast({ title: data.msg, icon: "none", }); } }, makePhoneCall(e) { wx.makePhoneCall({ phoneNumber: e.currentTarget.dataset.phone, }); }, previewImage(e) { const url = e.currentTarget.dataset.url; const urls = e.currentTarget.dataset.urls; // 如果是暂无,则不预览 if (url === "暂无") { return; } // 如果提供了多个URL,则使用它们 if ( urls && Array.isArray(JSON.parse(urls)) && JSON.parse(urls).length > 0 ) { const urlsArray = JSON.parse(urls); wx.previewImage({ current: url, // 当前显示的图片 urls: urlsArray, }); } else { // 兼容旧版本,只有单张图片 wx.previewImage({ urls: [url], }); } }, copy(e) { wx.setClipboardData({ data: e.currentTarget.dataset.phone, success(res) { wx.showToast({ title: "复制成功", }); }, }); }, onLoad(options) { this.getCerts(); // this.getContacts(); // this.getIntroduce(); }, onShow() { if (typeof this.getTabBar === "function" && this.getTabBar()) { this.getTabBar().setData({ selected: 1, }); } }, });