upload.vue 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. <template>
  2. <div class="uploader">
  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. <van-image :src="item.url" fit="cover" />
  12. </div>
  13. <div class="preview-item-img" v-if="item.type == 'file'">
  14. <i
  15. class="van-icon van-icon-description van-uploader__file-icon"
  16. ></i>
  17. <span
  18. style="
  19. white-space: nowrap;
  20. overflow: hidden;
  21. text-overflow: ellipsis;
  22. width: 64px;
  23. font-size: 13px;
  24. padding-top: 5px;
  25. "
  26. >{{ item.name }}</span
  27. >
  28. </div>
  29. <div class="preview-item-close">
  30. <van-icon name="cross" @click="onDelete(item)" />
  31. </div>
  32. </div>
  33. </div>
  34. </div>
  35. <div class="upload_section">
  36. <van-uploader
  37. :before-read="beforeRead"
  38. :after-read="afterRead"
  39. :disabled="false"
  40. >
  41. <div class="upload-file">
  42. <img src="../images/icon_upload_img.png" />
  43. 上传图片
  44. </div>
  45. </van-uploader>
  46. <van-uploader
  47. style="margin-left: 4%"
  48. :before-read="beforeReadFile"
  49. :after-read="afterRead"
  50. :disabled="false"
  51. accept="image/*, *.xlsx, *.xls,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel, application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document, *.txt, *.pdf"
  52. >
  53. <div class="upload-file">
  54. <img src="../images/icon_upload_file.png" />
  55. 上传附件
  56. </div>
  57. </van-uploader>
  58. </div>
  59. </div>
  60. </template>
  61. <script>
  62. // import setLoading from "@/common/loading";
  63. // import { uploadFile } from "../api";
  64. import { policy } from "@/views/massMessage/api";
  65. import axios from "axios";
  66. export default {
  67. name: "upload",
  68. props: ["fileUrl"],
  69. data() {
  70. return {
  71. tempFileUrl: this.fileUrl || [],
  72. type: "image",
  73. ossUploadUrl: "https://ks3-cn-beijing.ksyuncs.com/daya",
  74. dataObj: {
  75. policy: "",
  76. signature: "",
  77. key: "",
  78. KSSAccessKeyId: "",
  79. acl: "public-read",
  80. name: "",
  81. },
  82. };
  83. },
  84. watch: {
  85. fileUrl(val) {
  86. this.tempFileUrl = val;
  87. },
  88. },
  89. methods: {
  90. onDelete(item) {
  91. const index = this.tempFileUrl.indexOf(item);
  92. this.tempFileUrl.splice(index, 1);
  93. this.$emit("fileUrl", this.tempFileUrl);
  94. },
  95. beforeRead(file) {
  96. const isLt2M = file.size / 1024 / 1024 < 5;
  97. if (!isLt2M) {
  98. this.$toast("上传图片大小不能超过 5MB");
  99. return false;
  100. }
  101. const fileList = this.tempFileUrl.map((item) => {
  102. if (item.type == "image") {
  103. return item;
  104. }
  105. });
  106. if (fileList.length >= 5) {
  107. this.$toast("最多上传5张图片");
  108. return false;
  109. }
  110. this.type = "image";
  111. return true;
  112. },
  113. beforeReadFile(file) {
  114. const isLt2M = file.size / 1024 / 1024 < 20;
  115. if (!isLt2M) {
  116. this.$toast("上传文件大小不能超过 20MB");
  117. return false;
  118. }
  119. const fileList = this.tempFileUrl.map((item) => {
  120. if (item.type == "file") {
  121. return item;
  122. }
  123. });
  124. if (fileList.length >= 3) {
  125. this.$toast("最多上传3个附件");
  126. return false;
  127. }
  128. this.type = "file";
  129. return true;
  130. },
  131. async afterRead(file) {
  132. // 上传头像
  133. // 上传头像
  134. try {
  135. file.status = "uploading";
  136. file.message = "上传中...";
  137. let tempName = file.file.name || "";
  138. const fileName = tempName && tempName.replace(/ /gi, "_");
  139. let key = new Date().getTime() + fileName;
  140. let objTemp = {
  141. filename: fileName,
  142. bucketName: this.bucket_name,
  143. postData: {
  144. filename: fileName,
  145. acl: "public-read",
  146. key: key,
  147. unknowValueField: [],
  148. },
  149. };
  150. const res = await policy(objTemp);
  151. const obj = {
  152. policy: res.data.policy,
  153. signature: res.data.signature,
  154. key: key,
  155. KSSAccessKeyId: res.data.kssAccessKeyId,
  156. acl: "public-read",
  157. name: fileName,
  158. };
  159. let formData = new FormData();
  160. for (let key in obj) {
  161. formData.append(key, obj[key]);
  162. }
  163. formData.append("file", file.file);
  164. await axios({
  165. method: "post",
  166. url: this.ossUploadUrl,
  167. data: formData,
  168. });
  169. const uploadUrl = this.ossUploadUrl + "/" + key;
  170. file.status = "done";
  171. this.tempFileUrl.push({
  172. url: uploadUrl,
  173. name: file.file.name,
  174. type: this.type,
  175. file: {
  176. // 为了处理pc端显示文件名称
  177. name: file.file.name,
  178. },
  179. });
  180. this.$emit("update:fileUrl", this.tempFileUrl);
  181. } catch (e) {
  182. //
  183. file.status = "failed";
  184. file.message = "上传失败";
  185. return false;
  186. }
  187. // try {
  188. // setLoading(true);
  189. // file.status = "uploading";
  190. // file.message = "上传中...";
  191. // let formData = new FormData();
  192. // formData.append("file", file.file);
  193. // let res = await uploadFile(formData);
  194. // let result = res.data;
  195. // if (result.code == 200) {
  196. // file.status = "done";
  197. // this.tempFileUrl.push({
  198. // url: result.data.url,
  199. // name: file.file.name,
  200. // type: this.type,
  201. // file: {
  202. // // 为了处理pc端显示文件名称
  203. // name: file.file.name,
  204. // },
  205. // });
  206. // this.$emit("update:fileUrl", this.tempFileUrl);
  207. // } else {
  208. // file.status = "failed";
  209. // file.message = "上传失败";
  210. // this.$toast(result.msg);
  211. // return false;
  212. // }
  213. // setLoading(false);
  214. // } catch (err) {
  215. // setLoading(false);
  216. // return false;
  217. // }
  218. },
  219. },
  220. };
  221. </script>
  222. <style lang="less" scoped>
  223. .preview-container::-webkit-scrollbar {
  224. display: none;
  225. }
  226. .preview-container {
  227. width: 100%;
  228. overflow: hidden;
  229. overflow-y: hidden;
  230. overflow-x: auto;
  231. }
  232. .preview-list {
  233. display: flex;
  234. }
  235. .preview-item {
  236. position: relative;
  237. margin: 0 8px 8px 0;
  238. border-radius: 4px;
  239. overflow: hidden;
  240. }
  241. .preview-item,
  242. .preview-item-img {
  243. background: #f7f8fa;
  244. width: 64px;
  245. height: 64px;
  246. display: flex;
  247. align-items: center;
  248. flex-direction: column;
  249. justify-content: center;
  250. /deep/.van-image {
  251. width: 100%;
  252. height: 100%;
  253. }
  254. }
  255. .preview-item-close {
  256. position: absolute;
  257. top: 0;
  258. right: 0;
  259. width: 14px;
  260. height: 14px;
  261. background-color: rgba(0, 0, 0, 0.7);
  262. border-radius: 0 0 0 12px;
  263. /deep/.van-icon {
  264. position: absolute;
  265. top: -2px;
  266. right: -2px;
  267. color: #fff;
  268. font-size: 16px;
  269. -webkit-transform: scale(0.5);
  270. transform: scale(0.5);
  271. }
  272. }
  273. .upload_section {
  274. display: flex;
  275. align-content: center;
  276. /deep/.van-uploader {
  277. width: 48%;
  278. }
  279. /deep/.van-uploader__wrapper,
  280. /deep/.van-uploader__input-wrapper {
  281. width: 100%;
  282. }
  283. }
  284. .upload-file {
  285. background: #f7f8f9;
  286. border-radius: 9px;
  287. padding: 0.12rem 0.14rem;
  288. display: flex;
  289. align-content: center;
  290. justify-content: center;
  291. font-size: 0.14rem;
  292. font-weight: 500;
  293. color: #01c1b5;
  294. img {
  295. height: 22px;
  296. margin-right: 0.19rem;
  297. }
  298. }
  299. </style>