singleUpload.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <template> 
  2. <div>
  3. <el-upload
  4. :headers="myHeaders"
  5. :action="minioUploadUrl"
  6. :data="useOss?dataObj:senddata"
  7. list-type="picture"
  8. :multiple="false" :show-file-list="showFileList"
  9. :file-list="fileList"
  10. :before-upload="beforeUpload"
  11. :on-remove="handleRemove"
  12. :on-success="handleUploadSuccess"
  13. :on-preview="handlePreview">
  14. <el-button size="small" type="primary">点击上传</el-button>
  15. <div slot="tip" class="el-upload__tip">只能上传jpg/png文件,且不超过10MB</div>
  16. </el-upload>
  17. <el-dialog :visible.sync="dialogVisible">
  18. <img width="100%" :src="fileList[0].url" alt="">
  19. </el-dialog>
  20. </div>
  21. </template>
  22. <script>
  23. import {policy} from '@/api/oss'
  24. import { getToken } from '@/utils/auth'
  25. let token = getToken()
  26. // console.log('token',token)
  27. export default {
  28. name: 'singleUpload',
  29. props: {
  30. value: String
  31. },
  32. computed: {
  33. imageUrl() {
  34. return this.value;
  35. },
  36. imageName() {
  37. if (this.value != null && this.value !== '') {
  38. return this.value.substr(this.value.lastIndexOf("/") + 1);
  39. } else {
  40. return null;
  41. }
  42. },
  43. fileList() {
  44. return [{
  45. name: this.imageName,
  46. url: this.imageUrl
  47. }]
  48. },
  49. showFileList: {
  50. get: function () {
  51. return this.value !== null && this.value !== ''&& this.value!==undefined;
  52. },
  53. set: function (newValue) {
  54. }
  55. }
  56. },
  57. data() {
  58. return {
  59. dataObj: {
  60. policy: '',
  61. signature: '',
  62. key: '',
  63. ossaccessKeyId: '',
  64. dir: '',
  65. host: '',
  66. // callback:'',
  67. },
  68. senddata:{type:1},
  69. myHeaders: {Authorization: token},
  70. dialogVisible: false,
  71. useOss:false, //使用oss->true;使用MinIO->false
  72. ossUploadUrl:'http://macro-oss.oss-cn-shenzhen.aliyuncs.com',
  73. minioUploadUrl:`${process.env.BASE_API}/upload/fileImg`,
  74. };
  75. },
  76. methods: {
  77. emitInput(val) {
  78. this.$emit('input', val)
  79. },
  80. handleRemove(file, fileList) {
  81. this.emitInput('');
  82. },
  83. handlePreview(file) {
  84. this.dialogVisible = true;
  85. },
  86. beforeUpload(file) {
  87. let _self = this;
  88. if(!this.useOss){
  89. //不使用oss不需要获取策略
  90. return true;
  91. }
  92. return new Promise((resolve, reject) => {
  93. policy().then(response => {
  94. _self.dataObj.policy = response.data.policy;
  95. _self.dataObj.signature = response.data.signature;
  96. _self.dataObj.ossaccessKeyId = response.data.accessKeyId;
  97. _self.dataObj.key = response.data.dir + '/${filename}';
  98. _self.dataObj.dir = response.data.dir;
  99. _self.dataObj.host = response.data.host;
  100. // _self.dataObj.callback = response.data.callback;
  101. resolve(true)
  102. }).catch(err => {
  103. console.log(err)
  104. reject(false)
  105. })
  106. })
  107. },
  108. handleUploadSuccess(res, file) {
  109. this.showFileList = true;
  110. this.fileList.pop();
  111. console.log('res',res);
  112. // let url = this.dataObj.host + '/' + this.dataObj.dir + '/' + file.name;
  113. let url = res.data;
  114. // if(!this.useOss){
  115. // //不使用oss直接获取图片路径
  116. // url = res.data.url;
  117. // }
  118. this.fileList.push({name: file.name, url: url});
  119. this.emitInput(this.fileList[0].url);
  120. }
  121. }
  122. }
  123. </script>
  124. <style>
  125. </style>