plm前端
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.

354 lines
9.9 KiB

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