import { checkToken } from "../../utils/utils"; Page({ data: { notices: [], legalOrg: { name: "", photoUrl: "", introduction: "", }, aidRequest: "", currentPage: 1, pageSize: 10, totalPages: 0, isLoading: false, hasMore: true, isRefreshing: false, }, // 获取海事公告列表 getNoticeList(isRefresh = false) { if (this.data.isLoading || (!isRefresh && !this.data.hasMore)) return; this.setData({ isLoading: true }); const currentPage = isRefresh ? 1 : this.data.currentPage; // 导入API配置 const { api } = require("../../apis/apiConfig.js"); api( "/maritime/safety/notice/list", { currentPage: currentPage, size: this.data.pageSize, }, "POST" ) .then((res) => { if (res.data.status === 0) { const newNotices = res.data.result.map((item) => ({ id: item.id, title: item.fileName, date: item.createTime.split(" ")[0], unit: "", fileKey: item.fileKey, downloadUrl: item.downloadUrl, })); this.setData({ notices: isRefresh ? newNotices : [...this.data.notices, ...newNotices], currentPage: currentPage + 1, totalPages: Math.ceil(res.data.total / this.data.pageSize), hasMore: currentPage < Math.ceil(res.data.total / this.data.pageSize), }); } else { wx.showToast({ title: res.data.msg || "获取公告列表失败", icon: "none", }); } }) .catch(() => { wx.showToast({ title: "网络异常,请稍后重试", icon: "none", }); }) .finally(() => { this.setData({ isLoading: false }); wx.stopPullDownRefresh(); }); }, // 获取法律援助机构信息 getLegalAidInfo() { const { api } = require("../../apis/apiConfig.js"); api("/maritime/safety/legal/aid/introduce", {}, "GET") .then((res) => { if (res.data.status === 0 && res.data.result) { const { legalAidIntroduce, lawyerIntroduce } = res.data.result; this.setData({ legalOrg: { name: legalAidIntroduce.title || "法律援助机构", photoUrl: lawyerIntroduce.imageUrl || "../../images/new-version/pic.png", introduction: legalAidIntroduce.imageUrl || legalAidIntroduce.textContent || "", }, }); } }) .catch(() => { wx.showToast({ title: "获取法律援助信息失败", icon: "none", }); }); }, handleInputChange(e) { this.setData({ aidRequest: e.detail.value, }); }, submitAidRequest() { if (!checkToken("尚未登录")) return; if (!this.data.aidRequest.trim()) { wx.showToast({ title: "请输入法律援助需求", icon: "none", }); return; } const { api } = require("../../apis/apiConfig.js"); api( "/maritime/safety/legal/aid/request", { requestContent: this.data.aidRequest, }, "POST" ) .then((res) => { if (res.data && res.data.status === 0) { wx.showToast({ title: "申请提交成功", icon: "success", }); this.setData({ aidRequest: "", }); } else { wx.showToast({ title: res.data.msg || "申请提交失败", icon: "none", }); } }) .catch(() => { wx.showToast({ title: "网络异常,请稍后重试", icon: "none", }); }); }, onLoad() { this.getNoticeList(true); this.getLegalAidInfo(); }, onShow() { if (typeof this.getTabBar === "function" && this.getTabBar()) { this.getTabBar().setData({ selected: 2, }); } }, // 下拉刷新 onPullDownRefresh() { this.setData({ currentPage: 1, hasMore: true, isRefreshing: true, }); this.getNoticeList(true); }, // 处理scroll-view的滚动到底部事件 onScrollToLower() { if (this.data.hasMore && !this.data.isLoading) { this.getNoticeList(); } }, // 页面上拉触底事件(保留但不再使用,改为使用scroll-view的bindscrolltolower) onReachBottom() { // 页面的上拉触底事件已被scroll-view的bindscrolltolower替代 }, // 查看公告详情 viewNoticeDetail(e) { const id = e.currentTarget.dataset.id; const notice = this.data.notices.find((item) => item.id === id); if (notice && notice.downloadUrl) { wx.showLoading({ title: "加载中", }); // 获取API配置 const { apiUrl } = require("../../apis/apiConfig.js"); // 检查downloadUrl是否已包含完整URL // 打开文件预览 wx.downloadFile({ url: notice.downloadUrl, success: function (res) { const filePath = res.tempFilePath; console.log("文件路径:", res); wx.openDocument({ filePath: filePath, fileType: "pdf", success: function () { console.log("打开文档成功"); }, fail: function () { wx.showToast({ title: "无法打开文件", icon: "none", }); }, complete: function () { wx.hideLoading(); }, }); }, fail: function () { wx.hideLoading(); wx.showToast({ title: "文件下载失败", icon: "none", }); }, }); } else { wx.showToast({ title: "无法获取公告详情", icon: "none", }); } }, });