index.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <template>
  2. <!-- :before-upload="beforeUpload" :headers="headers" action="/api-web/uploadFile"-->
  3. <el-upload
  4. :action="ossUploadUrl"
  5. :data="dataObj"
  6. :on-success="success"
  7. :on-remove="remove"
  8. :on-progress="progress"
  9. :on-error="error"
  10. :file-list="filelist"
  11. :before-upload="beforeUpload"
  12. :accept="accept"
  13. >
  14. <el-button type="primary" plain>{{ buttonText }}</el-button>
  15. <div slot="tip" v-if="tips" class="el-upload__tip">{{ tips }}</div>
  16. <div slot="file" slot-scope="{ file }">
  17. <div
  18. style="
  19. display: flex;
  20. align-items: center;
  21. flex: 1 auto;
  22. justify-content: space-between;
  23. "
  24. >
  25. <div style="display: flex; align-items: center; overflow: hidden">
  26. <i
  27. v-if="!!file.url"
  28. @click.stop="copyText(file.url)"
  29. title="复制"
  30. style="padding-right: 5px"
  31. class="el-icon-document-copy"
  32. ></i>
  33. <span class="upload-text" :title="file.url">{{ file.url }}</span>
  34. </div>
  35. <i v-if="!!file.url" class="el-icon-delete" @click="remove"></i>
  36. </div>
  37. </div>
  38. </el-upload>
  39. </template>
  40. <script>
  41. import copy from "copy-to-clipboard";
  42. import { getToken } from "@/utils/auth";
  43. import load from "@/utils/loading";
  44. import { policy } from "@/api/appTenant";
  45. export default {
  46. name: "singe-file-upload",
  47. props: {
  48. buttonText: {
  49. type: String,
  50. default: "点击上传",
  51. },
  52. tips: {
  53. type: String,
  54. default: "",
  55. },
  56. value: {
  57. type: String,
  58. default: "",
  59. },
  60. accept: {
  61. type: String,
  62. default: "",
  63. },
  64. bucket_name: {
  65. type: String,
  66. default: "daya",
  67. },
  68. },
  69. watch: {
  70. value: {
  71. handler() {
  72. if (this.value) {
  73. this.filelist = [
  74. {
  75. name: this.value,
  76. url: this.value,
  77. },
  78. ];
  79. } else {
  80. this.remove();
  81. }
  82. },
  83. immediate: true,
  84. },
  85. },
  86. data() {
  87. return {
  88. filelist: [],
  89. headers: {
  90. Authorization: getToken(),
  91. },
  92. ossUploadUrl: "https://ks3-cn-beijing.ksyuncs.com/" + this.bucket_name,
  93. dataObj: {
  94. policy: "",
  95. signature: "",
  96. key: "",
  97. KSSAccessKeyId: "",
  98. // dir: "",
  99. acl: "public-read",
  100. name: "",
  101. // bucket_name: props.bucket_name
  102. },
  103. };
  104. },
  105. methods: {
  106. remove() {
  107. this.filelist = [];
  108. this.$emit("update:value", "");
  109. this.$emit("input", "");
  110. },
  111. error() {
  112. this.remove();
  113. load.endLoading();
  114. },
  115. // beforeUpload(file) {
  116. // console.log(file)
  117. // return false
  118. // },
  119. progress(file) {
  120. load.startLoading();
  121. },
  122. success(res, file) {
  123. load.endLoading();
  124. let url = this.ossUploadUrl + '/' + this.dataObj.key
  125. if (url) {
  126. this.filelist = [
  127. {
  128. name: url,
  129. url: url,
  130. },
  131. ];
  132. this.$emit("update:value",url);
  133. this.$emit("input", url);
  134. this.$emit("inputFile", file);
  135. this.$emit('success')
  136. // val.data.name
  137. let res= {
  138. data:{
  139. name:this.dataObj.name
  140. }
  141. }
  142. this.$emit("getName", res);
  143. } else {
  144. this.remove();
  145. this.$message.error(res.msg || "上传失败");
  146. }
  147. },
  148. copyText(text) {
  149. if (text) {
  150. copy(text);
  151. this.$message.success("复制成功");
  152. }
  153. },
  154. async beforeUpload(file) {
  155. // let fileName = file.name.replaceAll(" ", "_");
  156. // let key = new Date().getTime() + fileName;
  157. // let obj = {
  158. // filename: fileName,
  159. // bucketName: props.bucket_name,
  160. // postData: {
  161. // filename: fileName,
  162. // acl: "public-read",
  163. // key: key,
  164. // unknowValueField: [],
  165. // },
  166. // };
  167. try {
  168. let fileName = file.name.replaceAll(" ", "_");
  169. let key = new Date().getTime() + fileName;
  170. let obj = {
  171. filename: fileName,
  172. bucketName: this.bucket_name,
  173. postData: {
  174. filename: fileName,
  175. acl: "public-read",
  176. key: key,
  177. unknowValueField: [],
  178. },
  179. };
  180. const res = await policy(obj);
  181. this.dataObj = {
  182. policy: res.data.policy,
  183. signature: res.data.signature,
  184. key: key,
  185. KSSAccessKeyId: res.data.kssAccessKeyId,
  186. // dir: "",
  187. acl: "public-read",
  188. name: fileName,
  189. // bucket_name: props.bucket_name
  190. };
  191. } catch (e) {
  192. console.log(e)
  193. }
  194. },
  195. },
  196. };
  197. </script>
  198. <style lang="less" scoped>
  199. .upload-text {
  200. display: inline-block;
  201. width: 100%;
  202. overflow: hidden;
  203. white-space: nowrap;
  204. text-overflow: ellipsis;
  205. }
  206. </style>