You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

341 lines
9.9 KiB

9 months ago
  1. <script>
  2. import {ossUpload, previewOssFileById, previewOssFileById2, removeOss} from "../../../api/oss/oss";
  3. import {projectIncomeUpload,projectIncomeQueryOss} from "../../../api/machine/machineProjectIncome";
  4. import XLSX from 'xlsx';
  5. import jsPDF from 'jspdf';
  6. import * as mammoth from 'mammoth'; // Ensure it's correctly imported
  7. export default {
  8. name: "ossProjectComponents",
  9. props:{
  10. orderRef1:{
  11. type:String,
  12. default:''
  13. },
  14. orderRef2:{
  15. type:[Number,String],
  16. default:''
  17. },
  18. rfqNo:{
  19. type:String,
  20. default:''
  21. },
  22. columns:{
  23. type:Array,
  24. required:true
  25. },
  26. height:{
  27. type:[String,Number],
  28. default:'35vh'
  29. },
  30. title:{
  31. type:String,
  32. default:'附件信息'
  33. },
  34. label:{
  35. type:String,
  36. default:'单号'
  37. },
  38. disabled:{
  39. type:Boolean,
  40. default:false
  41. },
  42. },
  43. data(){
  44. return{
  45. dataList:[],
  46. queryLoading:false,
  47. uploadLoading:false,
  48. selectionDataList:[],
  49. ossVisible:false,
  50. ossForm:{
  51. orderRef2:'',
  52. rfqNo:'',
  53. remark:''
  54. },
  55. fileList:[],
  56. }
  57. },
  58. methods:{
  59. handleSelectionChange(val){
  60. this.selectionDataList = val
  61. },
  62. handleQuery(){
  63. if (!this.params.orderRef1 && !this.params.orderRef2){
  64. return;
  65. }
  66. let params = {
  67. ...this.params,
  68. }
  69. this.queryLoading = true;
  70. projectIncomeQueryOss(params).then(({data})=>{
  71. if (data && data.code === 0){
  72. this.dataList = data.rows;
  73. }else {
  74. this.$message.warning(data.msg);
  75. }
  76. this.queryLoading = false;
  77. }).catch((error)=>{
  78. this.$message.error(error);
  79. this.queryLoading = false;
  80. })
  81. },
  82. handleUpload(){
  83. this.ossForm.rfqNo = this.rfqNo
  84. this.$nextTick(()=>{
  85. if (this.$refs.upload){
  86. this.$refs.upload.clearFiles();
  87. }
  88. })
  89. this.fileList = [];
  90. this.ossForm.remark = '';
  91. this.ossVisible = true
  92. },
  93. onRemoveFile(file, fileList){
  94. this.fileList = fileList
  95. },
  96. onChangeFile(file, fileList){
  97. this.fileList = fileList
  98. },
  99. handleUploadFiles(){
  100. if (this.fileList.length === 0){
  101. this.$message.error('请选择文件');
  102. return;
  103. }
  104. let formData = new FormData();
  105. for (let i = 0; i < this.fileList.length; i++) {
  106. formData.append('file', this.fileList[i].raw);
  107. }
  108. formData.append('orderRef1', this.orderRef1);
  109. formData.append('orderRef2', this.orderRef2);
  110. formData.append('orderRef3', this.rfqNo);
  111. formData.append('createdBy', this.$store.state.user.name);
  112. formData.append('cAdditionalInfo', this.ossForm.remark);
  113. this.uploadLoading = true;
  114. projectIncomeUpload(formData).then(({data})=>{
  115. if (data && data.code === 0){
  116. this.$message.success(data.msg);
  117. this.handleQuery();
  118. this.ossVisible = false;
  119. }else {
  120. this.$message.warning(data.msg);
  121. }
  122. this.uploadLoading = false;
  123. }).catch((error)=>{
  124. this.$message.error(error);
  125. this.uploadLoading = false;
  126. })
  127. },
  128. handleRemove(row){
  129. this.$confirm('确认删除吗?', '提示').then(() => {
  130. let ids = [row.id]
  131. removeOss(ids).then(({data})=>{
  132. if (data && data.code === 0){
  133. this.$message.success(data.msg);
  134. this.handleQuery();
  135. }else {
  136. this.$message.warning(data.msg);
  137. }
  138. }).catch((error)=>{
  139. this.$message.error(error);
  140. })
  141. }).catch(()=>{
  142. })
  143. },
  144. previewFile(row){
  145. // 预览文件
  146. let type = '';
  147. let image = ['jpg', 'jpeg', 'png', 'gif', 'bmp'];
  148. if (image.includes(row.fileType.toLowerCase())){
  149. type = 'image/' + row.fileType;
  150. }
  151. let video = ['mp4', 'avi', 'mov', 'wmv', 'flv'];
  152. if (video.includes(row.fileType.toLowerCase())){
  153. type = 'video/' + row.fileType;
  154. }
  155. let txt = ['txt'];
  156. if (txt.includes(row.fileType.toLowerCase())){
  157. type = 'text/plain;charset=utf-8';
  158. }
  159. let office = ['doc', 'ppt', 'pptx'];
  160. if (office.includes(row.fileType.toLowerCase())){
  161. this.$message.warning(`该文件格式暂不支持预览`)
  162. return
  163. }
  164. let excel = ['xlsx','xls'];
  165. if (excel.includes(row.fileType.toLowerCase())){
  166. type = 'excel';
  167. }
  168. let word = ['docx'];
  169. if (word.includes(row.fileType.toLowerCase())){
  170. type = 'word'
  171. }
  172. let pdf = ['pdf'];
  173. if (pdf.includes(row.fileType.toLowerCase())){
  174. type = 'application/pdf;charset-UTF-8';
  175. }
  176. if (type === ''){
  177. this.$message.warning(`该文件格式暂不支持预览`)
  178. return;
  179. }
  180. let params = {
  181. id:row.id,
  182. fileType:type
  183. }
  184. previewOssFileById2(params).then(({data}) => {
  185. if (type === 'excel' || type === 'word'){
  186. type = 'application/pdf;charset=UTF-8'
  187. }
  188. const blob = new Blob([data], { type: type });
  189. // 创建URL来生成预览
  190. const fileURL = URL.createObjectURL(blob);
  191. if (type === 'xls' || type === 'docx' || type === 'xlsx'){
  192. const { href } = this.$router.resolve({
  193. name: 'pre',
  194. query:{
  195. src: fileURL,
  196. type: 'pdf' // 直接传递 pdf 类型
  197. }
  198. })
  199. window.open(href, '_blank')
  200. }else {
  201. // 在新标签页中打开文件预览
  202. const newTab = window.open(fileURL, '_blank');
  203. }
  204. });
  205. },
  206. handleDownload(){
  207. if (this.selectionDataList.length === 0){
  208. this.$message.warning('请选择要下载的附件');
  209. return;
  210. }
  211. for (let i = 0; i < this.selectionDataList.length; i++) {
  212. let params = {
  213. id:this.selectionDataList[i].id,
  214. }
  215. previewOssFileById(params).then((response) => {
  216. const blob = new Blob([response.data], { type: response.headers['content-type'] });
  217. const link = document.createElement('a');
  218. link.href = URL.createObjectURL(blob);
  219. link.setAttribute('download', this.selectionDataList[i].fileName);
  220. link.target = '_blank'; // 打开新窗口预览
  221. link.click();
  222. URL.revokeObjectURL(link.href);
  223. this.$refs.table.clearSelection();
  224. });
  225. }
  226. }
  227. },
  228. computed:{
  229. params:{
  230. get(){
  231. return{
  232. orderRef1:this.orderRef1,
  233. orderRef2:this.orderRef2,
  234. orderRef3:this.rfqNo,
  235. }
  236. }
  237. }
  238. },
  239. watch:{
  240. params:{
  241. handler(){
  242. this.ossForm.orderRef2 = this.orderRef2;
  243. this.ossForm.rfqNo = this.rfqNo;
  244. this.dataList = [];
  245. this.handleQuery();
  246. }
  247. }
  248. }
  249. }
  250. </script>
  251. <template>
  252. <div>
  253. <el-button type="primary" v-if="this.orderRef1 && this.orderRef2 && !disabled" @click="handleUpload">上传附件</el-button>
  254. <el-button type="primary" @click="handleDownload">下载</el-button>
  255. <el-table
  256. :height="height"
  257. :data="dataList"
  258. ref="table"
  259. v-loading="queryLoading"
  260. border
  261. @selection-change="handleSelectionChange"
  262. style="width: 100%;margin-top: 5px">
  263. <el-table-column type="selection" label="序号" align="center"/>
  264. <el-table-column
  265. v-for="(item,index) in columns" :key="index"
  266. :sortable="item.columnSortable"
  267. :prop="item.columnProp"
  268. :header-align="item.headerAlign"
  269. :show-overflow-tooltip="item.showOverflowTooltip"
  270. :align="item.align"
  271. :fixed="item.fixed===''?false:item.fixed"
  272. :min-width="item.columnWidth"
  273. :label="item.columnLabel">
  274. <template slot-scope="scope">
  275. <span v-if="!item.columnHidden">{{scope.row[item.columnProp]}}</span>
  276. <span v-if="item.columnImage"><img :src="scope.row[item.columnProp]" style="width: 100px; height: 80px"/></span>
  277. </template>
  278. </el-table-column>
  279. <el-table-column
  280. fixed="right"
  281. header-align="center"
  282. align="center"
  283. width="120"
  284. label="操作">
  285. <template slot-scope="{row,$index}">
  286. <el-link style="cursor:pointer;" v-if="!disabled" @click="handleRemove(row)">删除</el-link>
  287. <el-link style="cursor:pointer;" @click="previewFile(row)">预览</el-link>
  288. </template>
  289. </el-table-column>
  290. </el-table>
  291. <el-dialog :title="title" :visible.sync="ossVisible" v-drag width="400px" append-to-body :close-on-click-modal="false">
  292. <el-form ref="form" :model="ossForm" label-width="80px" label-position="top">
  293. <el-row :gutter="10">
  294. <el-col :span="8">
  295. <el-form-item label="项目单号">
  296. <el-input v-model="ossForm.rfqNo" readonly></el-input>
  297. </el-form-item>
  298. </el-col>
  299. <slot></slot>
  300. <el-col :span="24">
  301. <el-form-item label=" " class="auto">
  302. <el-upload drag :file-list="fileList"
  303. action="#" ref="upload"
  304. :on-remove="onRemoveFile"
  305. :on-change="onChangeFile"
  306. multiple
  307. :auto-upload="false">
  308. <i class="el-icon-upload"></i>
  309. <div class="el-upload__text">将文件拖到此处<em>点击上传</em></div>
  310. </el-upload>
  311. </el-form-item>
  312. </el-col>
  313. <el-col :span="24">
  314. <el-form-item label="备注" class="auto">
  315. <el-input type="textarea" v-model="ossForm.remark" resize="none" :autosize="{minRows: 3, maxRows: 3}"></el-input>
  316. </el-form-item>
  317. </el-col>
  318. </el-row>
  319. </el-form>
  320. <span slot="footer" class="dialog-footer">
  321. <el-button type="primary" :loading="uploadLoading" @click="handleUploadFiles">确定</el-button>
  322. <el-button @click="ossVisible = false">关闭</el-button>
  323. </span>
  324. </el-dialog>
  325. </div>
  326. </template>
  327. <style scoped>
  328. .auto /deep/ .el-form-item__content{
  329. height: auto;
  330. line-height: 1.5;
  331. }
  332. </style>