index.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. <template>
  2. <div class="component-upload-image">
  3. <el-upload
  4. v-if="type === 'more'"
  5. multiple
  6. action="#"
  7. list-type="picture-card"
  8. :on-success="handleUploadSuccess"
  9. :before-upload="handleBeforeUpload"
  10. :limit="limit"
  11. :on-error="handleUploadError"
  12. :on-exceed="handleExceed"
  13. ref="imageUpload"
  14. :on-remove="handleDelete"
  15. :show-file-list="true"
  16. :headers="headers"
  17. :file-list="fileList"
  18. :on-preview="handlePictureCardPreview"
  19. :http-request="uploadImage"
  20. :class="{ hide: fileList.length >= limit }"
  21. >
  22. <i class="el-icon-plus"></i>
  23. <!-- 上传提示 -->
  24. <div class="el-upload__tip" slot="tip" v-if="showTip">
  25. 请上传
  26. <template v-if="fileSize">
  27. 大小不超过 <b style="color: #f56c6c">{{ fileSize }}MB</b>
  28. </template>
  29. <template v-if="fileType">
  30. 格式为 <b style="color: #f56c6c">{{ fileType.join("/") }}</b>
  31. </template>
  32. 的文件
  33. </div>
  34. </el-upload>
  35. <el-upload
  36. v-if="type === 'radioIcon'"
  37. action="#"
  38. list-type="picture-card"
  39. :on-success="handleUploadSuccess"
  40. :before-upload="handleBeforeUpload"
  41. :limit="1"
  42. :on-error="handleUploadError"
  43. :on-exceed="handleExceed"
  44. ref="imageUpload"
  45. :on-remove="handleDelete"
  46. :show-file-list="true"
  47. :headers="headers"
  48. :file-list="fileList"
  49. :on-preview="handlePictureCardPreview"
  50. :http-request="uploadImage"
  51. :class="{ hide: fileList.length >0 }"
  52. >
  53. <i class="el-icon-plus"></i>
  54. <!-- 上传提示 -->
  55. <div class="el-upload__tip" slot="tip" >
  56. 请上传
  57. <template v-if="fileSize">
  58. 大小不超过 <b style="color: #f56c6c">{{ fileSize }}MB</b>
  59. </template>
  60. <template v-if="fileType">
  61. 格式为 <b style="color: #f56c6c">{{ fileType.join("/") }}</b>
  62. </template>
  63. 的文件
  64. </div>
  65. </el-upload>
  66. <el-upload
  67. v-if="type === 'alone'"
  68. drag
  69. action="#"
  70. :before-upload="handleBeforeUpload"
  71. :limit="2"
  72. :on-error="handleUploadError"
  73. ref="imageUpload"
  74. :show-file-list="false"
  75. :file-list="fileList"
  76. :http-request="uploadImage"
  77. >
  78. <div v-if="fileList.length > 0" class="img-box">
  79. <img style="width: 100%; height: 100%" :src="fileList[0].url" alt="" />
  80. <div class="img-model" @click.stop="clickImg">
  81. <span>预览</span>
  82. </div>
  83. </div>
  84. <div v-else>
  85. <i class="el-icon-upload"></i>
  86. <div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
  87. <div class="el-upload__tip" slot="tip" v-if="showTip">
  88. 请上传
  89. <template v-if="fileSize">
  90. 大小不超过 <b style="color: #f56c6c">{{ fileSize }}MB</b>
  91. </template>
  92. <template v-if="fileType">
  93. 格式为 <b style="color: #f56c6c">{{ fileType.join("/") }}</b>
  94. </template>
  95. 的文件
  96. </div>
  97. <!-- <div class="el-upload__tip" slot="tip">只能上传jpg/png/jpeg文件,且不超过500kb</div>-->
  98. </div>
  99. </el-upload>
  100. <el-dialog
  101. :visible.sync="dialogVisible"
  102. title="预览"
  103. width="800"
  104. append-to-body
  105. >
  106. <img
  107. :src="dialogImageUrl"
  108. style="display: block; max-width: 100%; margin: 0 auto"
  109. />
  110. </el-dialog>
  111. </div>
  112. </template>
  113. <script>
  114. import { getToken } from "@/utils/auth";
  115. import { upload } from "@/api/system/public";
  116. export default {
  117. props: {
  118. value: [String, Object, Array],
  119. //两种模式:more(多选)、alone(单选)
  120. type: {
  121. type: String,
  122. default: "more",
  123. },
  124. // 图片数量限制(单选模式下失效)
  125. limit: {
  126. type: Number,
  127. default: 5,
  128. },
  129. // 大小限制(MB)
  130. fileSize: {
  131. type: Number,
  132. default: 5,
  133. },
  134. // 文件类型, 例如['png', 'jpg', 'jpeg']
  135. fileType: {
  136. type: Array,
  137. default: () => ["png", "jpg", "jpeg"],
  138. },
  139. // 是否显示提示
  140. isShowTip: {
  141. type: Boolean,
  142. default: true,
  143. },
  144. },
  145. data() {
  146. return {
  147. number: 0,
  148. uploadList: [],
  149. dialogImageUrl: "",
  150. dialogVisible: false,
  151. hideUpload: false,
  152. uploadImgUrl:
  153. process.env.NODE_ENV === "development"
  154. ? "/dev-api" + "/file/file/upload"
  155. : process.env.VUE_APP_BASE_API + "/file/file/upload", // 上传的图片服务器地址
  156. headers: {
  157. Authorization: "Bearer " + getToken(),
  158. },
  159. fileList: [],
  160. file: null,
  161. };
  162. },
  163. watch: {
  164. value: {
  165. handler(val) {
  166. if (val) {
  167. // 首先将值转为数组
  168. const list = Array.isArray(val) ? val : this.value.split(",");
  169. // 然后将数组转为对象数组
  170. this.fileList = list.map((item) => {
  171. if (typeof item === "string") {
  172. item = { name: item, url: item };
  173. }
  174. return item;
  175. });
  176. } else {
  177. this.fileList = [];
  178. return [];
  179. }
  180. },
  181. deep: true,
  182. immediate: true,
  183. },
  184. },
  185. computed: {
  186. // 是否显示提示
  187. showTip() {
  188. return this.isShowTip && (this.fileType || this.fileSize);
  189. },
  190. },
  191. methods: {
  192. //单传模式下的预览
  193. clickImg() {
  194. this.dialogImageUrl = this.fileList[0].url;
  195. this.dialogVisible = true;
  196. },
  197. //自定义上传方式(自带的成功回调及失败回调会失效)
  198. uploadImage(fileObj) {
  199. console.log(window.origin, "URL");
  200. let formData = new FormData();
  201. formData.append("file", fileObj.file);
  202. upload(formData, "image")
  203. .then((res) => {
  204. /*上传成功*/
  205. this.$modal.closeLoading();
  206. let imgUrl =
  207. process.env.NODE_ENV === "development"
  208. ? process.env.VUE_APP_BASE_API + res.data.url
  209. : window.origin + res.data.url;
  210. let arr = [];
  211. arr.push({ name: res.data.name, url: imgUrl });
  212. if(this.type=='more'){
  213. this.fileList = this.fileList.concat(arr);
  214. }
  215. else{
  216. this.fileList = arr;
  217. }
  218. //emit完整图片URL路径
  219. this.$emit("input", this.listToString(this.fileList));
  220. // //非完整图片URL路径
  221. // this.$emit("imgUrl", res.data.url);
  222. })
  223. .catch((err) => {
  224. /*上传失败*/
  225. this.$modal.closeLoading();
  226. //this.$modal.msgError(res.msg);
  227. this.$refs.imageUpload.handleRemove(fileObj.file);
  228. //this.uploadedSuccessfully();
  229. });
  230. },
  231. // 上传前loading加载
  232. handleBeforeUpload(file) {
  233. let isImg = false;
  234. if (this.fileType.length) {
  235. let fileExtension = "";
  236. if (file.name.lastIndexOf(".") > -1) {
  237. fileExtension = file.name.slice(file.name.lastIndexOf(".") + 1);
  238. }
  239. isImg = this.fileType.some((type) => {
  240. if (file.type.indexOf(type) > -1) return true;
  241. if (fileExtension && fileExtension.indexOf(type) > -1) return true;
  242. return false;
  243. });
  244. } else {
  245. isImg = file.type.indexOf("image") > -1;
  246. }
  247. if (!isImg) {
  248. this.$modal.msgError(
  249. `文件格式不正确, 请上传${this.fileType.join("/")}图片格式文件!`
  250. );
  251. return false;
  252. }
  253. if (this.fileSize) {
  254. const isLt = file.size / 1024 / 1024 < this.fileSize;
  255. if (!isLt) {
  256. this.$modal.msgError(`上传图片大小不能超过 ${this.fileSize} MB!`);
  257. return false;
  258. }
  259. }
  260. this.$modal.loading("正在上传图片,请稍候...");
  261. this.number++;
  262. this.file = file;
  263. },
  264. // 文件个数超出
  265. handleExceed() {
  266. this.$modal.msgError(`上传文件数量不能超过 ${this.limit} 个!`);
  267. },
  268. // 上传成功回调
  269. handleUploadSuccess(res, file,fileList) {
  270. console.log("handleUploadSuccess",res,file,fileList);
  271. if (res.code === 200) {
  272. let imgUrl = process.env.VUE_APP_BASE_API + res.data.url;
  273. this.uploadList.push({ name: res.data.name, url: imgUrl });
  274. this.uploadedSuccessfully();
  275. // let str = res.data.code;
  276. // let blob = new Blob([str],{type:'image/jpeg'});
  277. // let imgUrl = window.URL.createObjectURL(blob);
  278. // debugger
  279. } else {
  280. this.number--;
  281. this.$modal.closeLoading();
  282. this.$modal.msgError(res.msg);
  283. this.$refs.imageUpload.handleRemove(file);
  284. this.uploadedSuccessfully();
  285. }
  286. },
  287. // 删除图片
  288. handleDelete(file) {
  289. const findex = this.fileList.map((f) => f.name).indexOf(file.name);
  290. if (findex > -1) {
  291. this.fileList.splice(findex, 1);
  292. this.$emit("input", this.listToString(this.fileList));
  293. }
  294. console.log(this.listToString(this.fileList), "删除图片");
  295. },
  296. // 上传失败
  297. handleUploadError() {
  298. this.$modal.msgError("上传图片失败,请重试");
  299. this.$modal.closeLoading();
  300. },
  301. // 上传结束处理
  302. uploadedSuccessfully() {
  303. if (this.number > 0 && this.uploadList.length === this.number) {
  304. debugger
  305. this.fileList = this.fileList.concat(this.uploadList);
  306. this.uploadList = [];
  307. this.number = 0;
  308. this.$emit("input", this.listToString(this.fileList));
  309. this.$modal.closeLoading();
  310. }
  311. },
  312. // 预览
  313. handlePictureCardPreview(file) {
  314. this.dialogImageUrl = file.url;
  315. this.dialogVisible = true;
  316. },
  317. // 对象转成指定字符串分隔
  318. listToString(list, separator) {
  319. let strs = "";
  320. separator = separator || ",";
  321. for (let i in list) {
  322. if (list[i].url) {
  323. strs += list[i].url.replace(this.baseUrl, "") + separator;
  324. }
  325. }
  326. return strs != "" ? strs.substr(0, strs.length - 1) : "";
  327. },
  328. },
  329. };
  330. </script>
  331. <style scoped lang="scss">
  332. // .el-upload--picture-card 控制加号部分
  333. ::v-deep .hide .el-upload--picture-card {
  334. display: none !important;
  335. }
  336. // 去掉动画效果
  337. ::v-deep .el-list-enter-active,
  338. ::v-deep .el-list-leave-active {
  339. transition: all 0s;
  340. }
  341. ::v-deep .el-list-enter,
  342. .el-list-leave-active {
  343. opacity: 0;
  344. transform: translateY(0);
  345. }
  346. .img-box {
  347. width: 100%;
  348. height: 100%;
  349. position: relative;
  350. .img-model {
  351. width: 100%;
  352. display: none;
  353. position: absolute;
  354. left: 0;
  355. top: 0;
  356. align-items: center;
  357. > span {
  358. background-color: rgba(30, 30, 30, 0.3);
  359. display: block;
  360. padding: 5px;
  361. color: #fff;
  362. text-shadow: 0 0 2px #1e1e1e;
  363. }
  364. }
  365. &:hover {
  366. .img-model {
  367. display: block;
  368. }
  369. }
  370. }
  371. </style>