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.

372 lines
10 KiB

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