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.

308 lines
8.8 KiB

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