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.

379 lines
11 KiB

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