/* eslint-disable class-methods-use-this */ import _axios, { get, put, _delete } from '@/lin/plugin/axios' // 我们通过 class 这样的语法糖使模型这个概念更加具象化,其优点:耦合性低、可维护性。 class ProjectInfo { // constructor() {} // 类中的方法可以代表一个用户行为 async createProjectInfo(data) { return _axios({ method: 'post', url: 'v1/projectInfo', data, }) } // 在这里通过 async await 语法糖让代码同步执行 // 1. await 一定要搭配 async 来使用 // 2. await 后面跟的是一个 Promise 对象 async getProjectInfo(id) { const res = await get(`v1/projectInfo/${id}`) return res } async getProjectInfoDetail(id) { const res = await get(`v1/projectInfo/detail/${id}`) return res } async editProjectInfo(id, info) { const res = await put(`v1/projectInfo/${id}`, info) return res } async deleteProjectInfo(id) { const res = await _delete(`v1/projectInfo/${id}`) return res } async getProjectInfoList(params) { return _axios({ method: 'get', url: 'v1/projectInfo/list', // params:{keyword:keyword}, params, handleError: true, }) } // async getProjectInfoPage(params) { // return _axios({ // method: 'get', // url: 'v1/projectInfo/page', // // params:{keyword:queryForm.keyword.value,status:queryForm.status.value,stage:queryForm.stage.value,type:queryForm.type.value}, // params, // handleError: true, // }) // } async getProjectInfoPage(data) { return _axios({ method: 'post', url: 'v1/projectInfo/page', // params:{keyword:queryForm.keyword.value,status:queryForm.status.value,stage:queryForm.stage.value,type:queryForm.type.value}, data, handleError: true, }) } async getProjectInfoReportList(data) { return _axios({ method: 'post', url: 'v1/projectInfo/report/list', // params:{keyword:queryForm.keyword.value,status:queryForm.status.value,stage:queryForm.stage.value,type:queryForm.type.value}, data, handleError: true, }) } // async exportProjectInfoList(data) { // return _axios({ // method: 'post', // url: 'v1/projectInfo/report/list/export', // // params:{keyword:queryForm.keyword.value,status:queryForm.status.value,stage:queryForm.stage.value,type:queryForm.type.value}, // data, // responseType: 'blob', // 关键点:告诉浏览器这是一份二进制流 // handleError: true, // }) // } async exportProjectInfoList(data) { try { const response = await _axios({ method: 'post', url: 'v1/projectInfo/report/list/export', data, responseType: 'blob', // 确保返回二进制数据 handleError: true, }); // 判断返回数据类型 if (response && response.data) { const contentDisposition = response.headers['content-disposition']; // 从 Content-Disposition 头部中提取文件名并进行 URL 解码 let fileName = '项目报表.xlsx'; // 默认文件名 if (contentDisposition) { // 使用正则匹配文件名(支持 UTF-8 编码格式) const matches = contentDisposition.match(/filename\*?=UTF-8''([^;]+)/); if (matches && matches[1]) { // 解码并处理文件名 fileName = decodeURIComponent(matches[1]); } } // 创建一个 Blob 对象,传入文件的二进制数据 const blob = new Blob([response.data], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' }); // 创建一个临时的下载链接 const link = document.createElement('a'); link.href = URL.createObjectURL(blob); // 生成对象 URL link.download = fileName; // 设置下载文件名 link.click(); // 触发下载 } } catch (error) { console.error('文件下载失败', error); } } async getProjectStatistics(params) { return _axios({ method: 'get', url: 'v1/projectInfo/hourStatistics', // params:{keyword:queryForm.keyword.value,status:queryForm.status.value,stage:queryForm.stage.value,type:queryForm.type.value}, params, handleError: true, }) } async getProjectStatisticsUnconfirm(params) { return _axios({ method: 'get', url: 'v1/projectInfo/all/hourStatistics', // params:{keyword:queryForm.keyword.value,status:queryForm.status.value,stage:queryForm.stage.value,type:queryForm.type.value}, params, handleError: true, }) } // 项目立项 async editProjectSetUp(id, info) { const res = await put(`v1/projectInfo/projectSetUp/${id}`, info) return res } // 项目启动 async editProjectInfoStart(id, info) { const res = await put(`v1/projectInfo/projectStart/${id}`, info) return res } // 项目完成 async editProjectInfoComplete(id, info) { const res = await put(`v1/projectInfo/projectComplete/${id}`, info) return res } // 项目完成 async editProjectInfoTermination(id, info) { const res = await put(`v1/projectInfo/projectTermination/${id}`, info) return res } // 项目完成 async editProjectHours(id, info) { const res = await put(`v1/projectInfo/projectHours/${id}`, info) return res } getProjectBoard(params) { return _axios({ method: 'post', url: 'v1/projectInfo/board', // params:{keyword:queryForm.keyword.value,status:queryForm.status.value,stage:queryForm.stage.value,type:queryForm.type.value}, data: params, handleError: true, }) } editProjectProBusDate(id, info) { const res = put(`v1/projectInfo/projectProBusDate/${id}`, info) return res } /** * * @param {*} params * @returns */ getProjectBoardByMonth(params) { return _axios({ method: 'post', url: 'v1/projectInfo/board/month', // params:{keyword:queryForm.keyword.value,status:queryForm.status.value,stage:queryForm.stage.value,type:queryForm.type.value}, data: params, handleError: true, }) } } export default new ProjectInfo()