upload.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <template>
  2. <div>
  3. <div class="preview-container">
  4. <div class="preview-list">
  5. <div
  6. class="preview-item"
  7. v-for="(item, index) in tempFileUrl"
  8. :key="index"
  9. >
  10. <div class="preview-item-img" v-if="item.type == 'image'">
  11. <el-image :src="item.url" fit="cover" />
  12. </div>
  13. <div class="preview-item-img" v-if="item.type == 'file'">
  14. <i class="el-icon-document van-uploader__file-icon"></i>
  15. <span
  16. style="white-space: nowrap;overflow: hidden;text-overflow: ellipsis;width: 64px; font-size: 13px; padding-top: 5px;line-height: 1.3 "
  17. >{{ item.name }}</span
  18. >
  19. </div>
  20. <div class="preview-item-close">
  21. <i class="el-icon-close" @click="onDelete(item)"></i>
  22. </div>
  23. </div>
  24. </div>
  25. </div>
  26. <div class="upload_section">
  27. <el-upload
  28. :action="ossUploadUrl"
  29. :on-success="handleSuccess"
  30. :on-exceed="handleExceed"
  31. :before-upload="beforeUpload"
  32. :show-file-list="false"
  33. accept="image/*"
  34. :limit="5"
  35. :data="dataObj"
  36. >
  37. <el-button
  38. size="mini"
  39. :loading="uploadLoading"
  40. icon="el-icon-picture"
  41. round
  42. >上传图片</el-button
  43. >
  44. </el-upload>
  45. <el-upload
  46. :action="ossUploadUrl"
  47. style="margin-left: 8px"
  48. :on-success="handleSuccess"
  49. :on-exceed="handleExceedFile"
  50. :before-upload="beforeUploadFile"
  51. :show-file-list="false"
  52. :limit="3"
  53. :data="dataObj"
  54. accept="image/*, *.xlsx, *.xls,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel, application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document, *.txt, *.pdf"
  55. >
  56. <el-button
  57. size="mini"
  58. :loading="uploadFileLoading"
  59. icon="el-icon-document"
  60. round
  61. >上传附件</el-button
  62. >
  63. </el-upload>
  64. </div>
  65. </div>
  66. </template>
  67. <script>
  68. import { policy } from "@/api/user";
  69. export default {
  70. name: "FileUpload",
  71. props: ["fileUrl"],
  72. data() {
  73. return {
  74. tempFileUrl: this.fileUrl || [],
  75. type: "image",
  76. uploadLoading: false,
  77. uploadFileLoading: false,
  78. ossUploadUrl: "https://ks3-cn-beijing.ksyuncs.com/daya",
  79. dataObj: {
  80. policy: "",
  81. signature: "",
  82. key: "",
  83. KSSAccessKeyId: "",
  84. acl: "public-read",
  85. name: ""
  86. }
  87. };
  88. },
  89. watch: {
  90. fileUrl(val) {
  91. console.log(val);
  92. this.tempFileUrl = val;
  93. }
  94. },
  95. methods: {
  96. onClear() {
  97. this.tempFileUrl = [];
  98. this.$emit("update:fileUrl", []);
  99. },
  100. onDelete(item) {
  101. const index = this.tempFileUrl.indexOf(item);
  102. this.tempFileUrl.splice(index, 1);
  103. this.$emit("update:fileUrl", this.tempFileUrl);
  104. },
  105. async beforeUpload(file) {
  106. const isLt2M = file.size / 1024 / 1024 < 5;
  107. if (!isLt2M) {
  108. this.$message.error("上传图片大小不能超过 5MB!");
  109. }
  110. this.uploadLoading = true;
  111. this.type = "image";
  112. try {
  113. let filename = file.name.replaceAll(" ", "_");
  114. let key = new Date().getTime() + filename;
  115. let obj = {
  116. filename,
  117. bucketName: "daya",
  118. postData: {
  119. filename,
  120. acl: "public-read",
  121. key,
  122. unknowValueField: []
  123. }
  124. };
  125. const res = await policy(obj);
  126. this.dataObj = {
  127. policy: res.data.policy,
  128. signature: res.data.signature,
  129. key,
  130. KSSAccessKeyId: res.data.kssAccessKeyId,
  131. acl: "public-read",
  132. name: filename
  133. };
  134. console.log(res, "policy", this.dataObj);
  135. return isLt2M;
  136. } catch {
  137. return false;
  138. }
  139. },
  140. async beforeUploadFile(file) {
  141. const isLt2M = file.size / 1024 / 1024 < 5;
  142. if (!isLt2M) {
  143. this.$message.error("上传附件大小不能超过 5MB!");
  144. }
  145. this.uploadFileLoading = true;
  146. this.type = "file";
  147. try {
  148. let filename = file.name.replaceAll(" ", "_");
  149. let key = new Date().getTime();
  150. let obj = {
  151. filename,
  152. bucketName: "daya",
  153. postData: {
  154. filename,
  155. acl: "public-read",
  156. key,
  157. unknowValueField: []
  158. }
  159. };
  160. const res = await policy(obj);
  161. this.dataObj = {
  162. policy: res.data.policy,
  163. signature: res.data.signature,
  164. key,
  165. KSSAccessKeyId: res.data.kssAccessKeyId,
  166. acl: "public-read",
  167. name: filename
  168. };
  169. return isLt2M;
  170. } catch {
  171. return false;
  172. }
  173. },
  174. handleExceed(files, fileList) {
  175. this.$message.warning(`最多允许上传 5 张图片。`);
  176. },
  177. handleExceedFile(files, fileList) {
  178. this.$message.warning(`最多允许上传 3 个文件。`);
  179. },
  180. handleSuccess(response, file, fileList) {
  181. // console.log(response, file, fileList);
  182. this.uploadLoading = false;
  183. this.uploadFileLoading = false;
  184. let url = this.ossUploadUrl + "/" + this.dataObj.key;
  185. this.tempFileUrl.push({
  186. uid: file.uid,
  187. name: file.name,
  188. url,
  189. type: this.type,
  190. file: {
  191. name: file.name
  192. }
  193. });
  194. this.$emit("update:fileList", this.tempFileUrl);
  195. }
  196. }
  197. };
  198. </script>
  199. <style lang="scss" scoped>
  200. .upload_section {
  201. display: flex;
  202. }
  203. .preview-container::-webkit-scrollbar {
  204. display: none;
  205. }
  206. .preview-container {
  207. width: 100%;
  208. overflow: hidden;
  209. overflow-y: hidden;
  210. overflow-x: auto;
  211. }
  212. .preview-list {
  213. display: flex;
  214. }
  215. .preview-item {
  216. position: relative;
  217. margin: 0 8px 8px 0;
  218. border-radius: 4px;
  219. overflow: hidden;
  220. }
  221. .preview-item,
  222. .preview-item-img {
  223. background: #f7f8fa;
  224. width: 64px;
  225. height: 64px;
  226. display: flex;
  227. align-items: center;
  228. flex-direction: column;
  229. justify-content: center;
  230. }
  231. .preview-item-close {
  232. position: absolute;
  233. top: 0;
  234. right: 0;
  235. width: 14px;
  236. height: 14px;
  237. background-color: rgba(0, 0, 0, 0.7);
  238. border-radius: 0 0 0 12px;
  239. .el-icon-close {
  240. position: absolute;
  241. top: -2px;
  242. right: -2px;
  243. color: #fff;
  244. font-size: 16px;
  245. -webkit-transform: scale(0.5);
  246. transform: scale(0.5);
  247. cursor: pointer;
  248. }
  249. }
  250. </style>