index.vue 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <template>
  2. <!-- :before-upload="beforeUpload" :headers="headers" action="/api-web/uploadFile"-->
  3. <div>
  4. <el-upload
  5. :action="ossUploadUrl"
  6. :data="dataObj"
  7. :on-preview="handlePictureCardPreview"
  8. :show-file-list="false"
  9. multiple
  10. accept=".png, .jpg, .jpeg, .gif"
  11. :before-upload="beforeUpload"
  12. :on-success="successed"
  13. :on-remove="handleRemove"
  14. :on-exceed="handError"
  15. :limit="max"
  16. >
  17. <el-button :loading="uploading" type="primary">上传图片</el-button>
  18. <div v-if="max" class="el-upload__tip" slot="tip">
  19. 最多只能上传{{ max }}张图片
  20. </div>
  21. </el-upload>
  22. <div class="img-container">
  23. <div class="list" v-if="uploaded.length > 0">
  24. <div v-for="(item, index) in uploaded" :key="item.url" class="item">
  25. <div class="ctrl-bar">
  26. <i class="el-icon-circle-close" @click="remove(index)"></i>
  27. </div>
  28. <el-image
  29. :src="item.url"
  30. :preview-src-list="uploaded.map(item => item.url)"
  31. class="img"
  32. >
  33. </el-image>
  34. <!-- <el-input
  35. v-model="item.name"
  36. placeholder="请输入图片名称"
  37. clearable
  38. /> -->
  39. </div>
  40. </div>
  41. <!-- <empty v-else /> -->
  42. </div>
  43. </div>
  44. </template>
  45. <script>
  46. import copy from "copy-to-clipboard";
  47. import { getToken } from "@/utils/auth";
  48. import load from "@/utils/loading";
  49. import { policy } from "@/api/appTenant";
  50. export default {
  51. name: "uploadImageList",
  52. props: {
  53. buttonText: {
  54. type: String,
  55. default: "点击上传"
  56. },
  57. tips: {
  58. type: String,
  59. default: ""
  60. },
  61. uploaded: {
  62. type: Array,
  63. default: () => []
  64. },
  65. accept: {
  66. type: String,
  67. default: ""
  68. },
  69. max: {
  70. type: Number,
  71. default: 5
  72. },
  73. bucket_name: {
  74. type: String,
  75. default: "daya"
  76. }
  77. },
  78. watch: {
  79. value: {
  80. handler() {
  81. if (this.value) {
  82. this.filelist = [
  83. {
  84. name: this.value,
  85. url: this.value
  86. }
  87. ];
  88. } else {
  89. this.remove();
  90. }
  91. },
  92. immediate: true
  93. }
  94. },
  95. data() {
  96. return {
  97. filelist: [],
  98. headers: {
  99. Authorization: getToken()
  100. },
  101. // ossUploadUrl: "https://ks3-cn-beijing.ksyuncs.com/" + this.bucket_name,
  102. ossUploadUrl: `https://${this.bucket_name}.ks3-cn-beijing.ksyuncs.com`,
  103. dataObj: {
  104. policy: "",
  105. signature: "",
  106. key: "",
  107. KSSAccessKeyId: "",
  108. // dir: "",
  109. acl: "public-read",
  110. name: ""
  111. // bucket_name: props.bucket_name
  112. },
  113. uploading: false
  114. };
  115. },
  116. methods: {
  117. async beforeUpload(file) {
  118. console.log(file);
  119. this.uploading = true;
  120. try {
  121. let fileName = file.name.replaceAll(" ", "_");
  122. let key = new Date().getTime() + fileName;
  123. file.key = key;
  124. let obj = {
  125. filename: fileName,
  126. bucketName: this.bucket_name,
  127. postData: {
  128. filename: fileName,
  129. acl: "public-read",
  130. key: key,
  131. unknowValueField: []
  132. }
  133. };
  134. const res = await policy(obj);
  135. this.dataObj = {
  136. policy: res.data.policy,
  137. signature: res.data.signature,
  138. key: key,
  139. KSSAccessKeyId: res.data.kssAccessKeyId,
  140. // dir: "",
  141. acl: "public-read",
  142. name: fileName
  143. // bucket_name: props.bucket_name
  144. };
  145. } catch (e) {
  146. console.log(e);
  147. }
  148. return true;
  149. },
  150. successed(response, file, fileList) {
  151. this.uploading = false;
  152. let url = this.ossUploadUrl + "/" + file.raw.key;
  153. if (url) {
  154. this.uploaded.push({
  155. url,
  156. name: file.name.split(".").shift(),
  157. clientShow: "YES"
  158. });
  159. } else {
  160. this.$message.error(res.data?.message || res.msg || "上传失败");
  161. }
  162. console.log(response, file, fileList);
  163. },
  164. handleRemove(file, fileList) {
  165. console.log(file, fileList);
  166. },
  167. handlePictureCardPreview(file) {
  168. this.dialogImageUrl = file.url;
  169. this.dialogVisible = true;
  170. },
  171. remove(index) {
  172. this.uploaded.splice(index, 1);
  173. },
  174. handError(files, fileList) {
  175. this.$message.error(
  176. `当前限制选择 ${this.max} 个文件,本次选择了 ${
  177. files.length
  178. } 个文件,共选择了 ${files.length + fileList.length} 个文件`
  179. );
  180. }
  181. }
  182. };
  183. </script>
  184. <style lang="less" scoped>
  185. .upload-text {
  186. display: inline-block;
  187. width: 100%;
  188. overflow: hidden;
  189. white-space: nowrap;
  190. text-overflow: ellipsis;
  191. }
  192. .dialog-footer {
  193. text-align: right;
  194. }
  195. .img-container {
  196. margin: 10px auto;
  197. }
  198. .item {
  199. width: 150px;
  200. margin-top: 10px;
  201. margin-right: 10px;
  202. display: inline-block;
  203. position: relative;
  204. }
  205. .img {
  206. width: 150px;
  207. height: 150px;
  208. }
  209. .ctrl-bar {
  210. background-color: rgba(0, 0, 0, 0.45);
  211. height: 30px;
  212. position: absolute;
  213. top: 0;
  214. width: 100%;
  215. z-index: 1;
  216. display: flex;
  217. justify-content: flex-end;
  218. align-items: center;
  219. padding: 0 15px;
  220. i {
  221. color: #fff;
  222. cursor: pointer;
  223. }
  224. }
  225. </style>