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.

376 lines
10 KiB

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