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.

360 lines
10 KiB

  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:[Number,String],
  12. default:''
  13. },
  14. orderRef3:{
  15. type:String,
  16. default:''
  17. },
  18. rfqNo:{
  19. type:String,
  20. default:''
  21. },
  22. partNo:{
  23. type:String,
  24. default:''
  25. },
  26. partDesc:{
  27. type:String,
  28. default:''
  29. },
  30. columns:{
  31. type:Array,
  32. required:true
  33. },
  34. height:{
  35. type:[String,Number],
  36. default:'35vh'
  37. },
  38. title:{
  39. type:String,
  40. default:'附件信息'
  41. },
  42. label:{
  43. type:String,
  44. default:'单号'
  45. },
  46. disabled:{
  47. type:Boolean,
  48. default:false
  49. },
  50. },
  51. data(){
  52. return{
  53. dataList:[],
  54. queryLoading:false,
  55. uploadLoading:false,
  56. selectionDataList:[],
  57. ossVisible:false,
  58. ossForm:{
  59. orderRef2:'',
  60. orderRef3:'',
  61. rfqNo:'',
  62. partNo:'',
  63. partDesc:'',
  64. remark:''
  65. },
  66. fileList:[],
  67. }
  68. },
  69. methods:{
  70. handleSelectionChange(val){
  71. this.selectionDataList = val
  72. },
  73. handleQuery(){
  74. if (!this.params.orderRef1 && !this.params.orderRef2){
  75. return;
  76. }
  77. let params = {
  78. ...this.params,
  79. }
  80. this.queryLoading = true;
  81. queryOss(params).then(({data})=>{
  82. if (data && data.code === 0){
  83. this.dataList = data.rows;
  84. }else {
  85. this.$message.warning(data.msg);
  86. }
  87. this.queryLoading = false;
  88. }).catch((error)=>{
  89. this.$message.error(error);
  90. this.queryLoading = false;
  91. })
  92. },
  93. handleUpload(){
  94. this.$nextTick(()=>{
  95. if (this.$refs.upload){
  96. this.$refs.upload.clearFiles();
  97. }
  98. })
  99. this.fileList = [];
  100. this.ossForm.remark = '';
  101. this.ossVisible = true
  102. },
  103. onRemoveFile(file, fileList){
  104. this.fileList = fileList
  105. },
  106. onChangeFile(file, fileList){
  107. this.fileList = fileList
  108. },
  109. handleUploadFiles(){
  110. if (this.fileList.length === 0){
  111. this.$message.error('请选择文件');
  112. return;
  113. }
  114. let formData = new FormData();
  115. for (let i = 0; i < this.fileList.length; i++) {
  116. formData.append('file', this.fileList[i].raw);
  117. }
  118. formData.append('orderRef1', this.orderRef1);
  119. formData.append('orderRef2', this.ossForm.orderRef2);
  120. formData.append('orderRef3', this.orderRef3);
  121. formData.append('createdBy', this.$store.state.user.name);
  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. console.log(row)
  156. // 预览文件
  157. let type = '';
  158. let image = ['jpg', 'jpeg', 'png', 'gif', 'bmp'];
  159. if (image.includes(row.fileType.toLowerCase())){
  160. type = 'image/' + row.fileType;
  161. }
  162. let video = ['mp4', 'avi', 'mov', 'wmv', 'flv'];
  163. if (video.includes(row.fileType.toLowerCase())){
  164. type = 'video/' + row.fileType;
  165. }
  166. let txt = ['txt'];
  167. if (txt.includes(row.fileType.toLowerCase())){
  168. type = 'text/plain;charset=utf-8';
  169. }
  170. let office = ['doc', 'ppt', 'pptx'];
  171. if (office.includes(row.fileType.toLowerCase())){
  172. this.$message.warning(`该文件格式暂不支持预览`)
  173. return
  174. }
  175. let excel = ['xlsx','xls'];
  176. if (excel.includes(row.fileType.toLowerCase())){
  177. type = row.fileType.toLowerCase();
  178. }
  179. let word = ['docx'];
  180. if (word.includes(row.fileType.toLowerCase())){
  181. type = 'docx'
  182. }
  183. let pdf = ['pdf'];
  184. if (pdf.includes(row.fileType.toLowerCase())){
  185. type = 'application/pdf;charset-UTF-8';
  186. }
  187. if (type === ''){
  188. this.$message.warning(`该文件格式暂不支持预览`)
  189. return;
  190. }
  191. let params = {
  192. id:row.id,
  193. }
  194. previewOssFileById(params).then(({data}) => {
  195. const blob = new Blob([data], { type: type });
  196. // 创建URL来生成预览
  197. const fileURL = URL.createObjectURL(blob);
  198. if (type === 'xls' || type === 'docx' || type === 'xlsx'){
  199. const { href } = this.$router.resolve({
  200. name: 'pre',
  201. query:{
  202. src: fileURL,
  203. type:type
  204. }
  205. })
  206. window.open(href, '_blank')
  207. }else {
  208. // 在新标签页中打开文件预览
  209. const newTab = window.open(fileURL, '_blank');
  210. }
  211. });
  212. },
  213. handleDownload(){
  214. if (this.selectionDataList.length === 0){
  215. this.$message.warning('请选择要下载的附件');
  216. return;
  217. }
  218. for (let i = 0; i < this.selectionDataList.length; i++) {
  219. let params = {
  220. id:this.selectionDataList[i].id,
  221. }
  222. previewOssFileById(params).then((response) => {
  223. const blob = new Blob([response.data], { type: response.headers['content-type'] });
  224. const link = document.createElement('a');
  225. link.href = URL.createObjectURL(blob);
  226. link.setAttribute('download', this.selectionDataList[i].fileName);
  227. link.target = '_blank'; // 打开新窗口预览
  228. link.click();
  229. URL.revokeObjectURL(link.href);
  230. this.$refs.table.clearSelection();
  231. });
  232. }
  233. }
  234. },
  235. computed:{
  236. params:{
  237. get(){
  238. return{
  239. orderRef1:this.orderRef1,
  240. orderRef2:this.orderRef2,
  241. orderRef3:this.orderRef3,
  242. }
  243. }
  244. }
  245. },
  246. watch:{
  247. params:{
  248. handler(){
  249. this.ossForm.orderRef2 = this.orderRef2;
  250. this.ossForm.orderRef3 = this.orderRef3;
  251. this.ossForm.rfqNo = this.rfqNo;
  252. this.ossForm.partNo = this.partNo;
  253. this.ossForm.partDesc = this.partDesc;
  254. this.dataList = [];
  255. this.handleQuery();
  256. }
  257. }
  258. }
  259. }
  260. </script>
  261. <template>
  262. <div>
  263. <el-button type="primary" v-if="this.orderRef1 && this.orderRef2 && !disabled" @click="handleUpload">上传附件</el-button>
  264. <el-button type="primary" @click="handleDownload">下载</el-button>
  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="操作">
  295. <template slot-scope="{row,$index}">
  296. <el-link style="cursor:pointer;" v-if="!disabled" @click="handleRemove(row)">删除</el-link>
  297. <el-link style="cursor:pointer;" @click="previewFile(row)">预览</el-link>
  298. </template>
  299. </el-table-column>
  300. </el-table>
  301. <el-dialog :title="title" :visible.sync="ossVisible" v-drag width="400px" append-to-body :close-on-click-modal="false">
  302. <el-form ref="form" :model="ossForm" label-width="80px" label-position="top">
  303. <el-row :gutter="10">
  304. <el-col :span="8">
  305. <el-form-item label="询价单号">
  306. <el-input v-model="ossForm.rfqNo" readonly></el-input>
  307. </el-form-item>
  308. </el-col>
  309. <el-col :span="8">
  310. <el-form-item label="产品编码">
  311. <el-input v-model="ossForm.partNo" readonly></el-input>
  312. </el-form-item>
  313. </el-col>
  314. <el-col :span="8">
  315. <el-form-item label="产品描述">
  316. <el-input v-model="ossForm.partDesc" readonly></el-input>
  317. </el-form-item>
  318. </el-col>
  319. <slot></slot>
  320. <el-col :span="24">
  321. <el-form-item label=" " class="auto">
  322. <el-upload drag :file-list="fileList"
  323. action="#" ref="upload"
  324. :on-remove="onRemoveFile"
  325. :on-change="onChangeFile"
  326. multiple
  327. :auto-upload="false">
  328. <i class="el-icon-upload"></i>
  329. <div class="el-upload__text">将文件拖到此处<em>点击上传</em></div>
  330. </el-upload>
  331. </el-form-item>
  332. </el-col>
  333. <el-col :span="24">
  334. <el-form-item label="备注" class="auto">
  335. <el-input type="textarea" v-model="ossForm.remark" resize="none" :autosize="{minRows: 3, maxRows: 3}"></el-input>
  336. </el-form-item>
  337. </el-col>
  338. </el-row>
  339. </el-form>
  340. <span slot="footer" class="dialog-footer">
  341. <el-button type="primary" :loading="uploadLoading" @click="handleUploadFiles">确定</el-button>
  342. <el-button @click="ossVisible = false">关闭</el-button>
  343. </span>
  344. </el-dialog>
  345. </div>
  346. </template>
  347. <style scoped>
  348. .auto /deep/ .el-form-item__content{
  349. height: auto;
  350. line-height: 1.5;
  351. }
  352. </style>