index.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. <DialogCom
  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. </DialogCom>
  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. baseUrl:process.env.NODE_ENV === "development"
  162. ? process.env.VUE_APP_BASE_API
  163. : window.origin
  164. };
  165. },
  166. watch: {
  167. value: {
  168. handler(val) {
  169. if (val) {
  170. // 首先将值转为数组
  171. const list = Array.isArray(val) ? val : this.value.split(",");
  172. // 然后将数组转为对象数组
  173. this.fileList = list.map((item) => {
  174. if (typeof item === "string") {
  175. item = { name: item, url: item };
  176. }
  177. return item;
  178. });
  179. } else {
  180. this.fileList = [];
  181. return [];
  182. }
  183. },
  184. deep: true,
  185. immediate: true,
  186. },
  187. },
  188. computed: {
  189. // 是否显示提示
  190. showTip() {
  191. return this.isShowTip && (this.fileType || this.fileSize);
  192. },
  193. },
  194. methods: {
  195. //单传模式下的预览
  196. clickImg() {
  197. this.dialogImageUrl = this.fileList[0].url;
  198. this.dialogVisible = true;
  199. },
  200. //自定义上传方式(自带的成功回调及失败回调会失效)
  201. uploadImage(fileObj) {
  202. console.log(window.origin, "URL");
  203. let formData = new FormData();
  204. formData.append("file", fileObj.file);
  205. upload(formData, "image")
  206. .then((res) => {
  207. /*上传成功*/
  208. this.$modal.closeLoading();
  209. let imgUrl =
  210. process.env.NODE_ENV === "development"
  211. ? process.env.VUE_APP_BASE_API + res.data.url
  212. : window.origin + res.data.url;
  213. let arr = [];
  214. arr.push({ name: res.data.name, url: imgUrl });
  215. if(this.type=='more'){
  216. this.fileList = this.fileList.concat(arr);
  217. }
  218. else{
  219. this.fileList = arr;
  220. }
  221. //emit完整图片URL路径
  222. this.$emit("input", this.listToString(this.fileList));
  223. // //非完整图片URL路径
  224. // this.$emit("imgUrl", res.data.url);
  225. })
  226. .catch((err) => {
  227. /*上传失败*/
  228. this.$modal.closeLoading();
  229. //this.$modal.msgError(res.msg);
  230. this.$refs.imageUpload.handleRemove(fileObj.file);
  231. //this.uploadedSuccessfully();
  232. });
  233. },
  234. // 上传前loading加载
  235. handleBeforeUpload(file) {
  236. let isImg = false;
  237. if (this.fileType.length) {
  238. let fileExtension = "";
  239. if (file.name.lastIndexOf(".") > -1) {
  240. fileExtension = file.name.slice(file.name.lastIndexOf(".") + 1);
  241. }
  242. isImg = this.fileType.some((type) => {
  243. if (file.type.indexOf(type) > -1) return true;
  244. if (fileExtension && fileExtension.indexOf(type) > -1) return true;
  245. return false;
  246. });
  247. } else {
  248. isImg = file.type.indexOf("image") > -1;
  249. }
  250. if (!isImg) {
  251. this.$modal.msgError(
  252. `文件格式不正确, 请上传${this.fileType.join("/")}图片格式文件!`
  253. );
  254. return false;
  255. }
  256. if (this.fileSize) {
  257. const isLt = file.size / 1024 / 1024 < this.fileSize;
  258. if (!isLt) {
  259. this.$modal.msgError(`上传图片大小不能超过 ${this.fileSize} MB!`);
  260. return false;
  261. }
  262. }
  263. this.$modal.loading("正在上传图片,请稍候...");
  264. this.number++;
  265. this.file = file;
  266. },
  267. // 文件个数超出
  268. handleExceed() {
  269. this.$modal.msgError(`上传文件数量不能超过 ${this.limit} 个!`);
  270. },
  271. // 上传成功回调
  272. handleUploadSuccess(res, file,fileList) {
  273. console.log("handleUploadSuccess",res,file,fileList);
  274. if (res.code === 200) {
  275. let imgUrl = process.env.VUE_APP_BASE_API + res.data.url;
  276. this.uploadList.push({ name: res.data.name, url: imgUrl });
  277. this.uploadedSuccessfully();
  278. // let str = res.data.code;
  279. // let blob = new Blob([str],{type:'image/jpeg'});
  280. // let imgUrl = window.URL.createObjectURL(blob);
  281. // debugger
  282. } else {
  283. this.number--;
  284. this.$modal.closeLoading();
  285. this.$modal.msgError(res.msg);
  286. this.$refs.imageUpload.handleRemove(file);
  287. this.uploadedSuccessfully();
  288. }
  289. },
  290. // 删除图片
  291. handleDelete(file) {
  292. const findex = this.fileList.map((f) => f.name).indexOf(file.name);
  293. if (findex > -1) {
  294. this.fileList.splice(findex, 1);
  295. this.$emit("input", this.listToString(this.fileList));
  296. }
  297. console.log(this.listToString(this.fileList), "删除图片");
  298. },
  299. // 上传失败
  300. handleUploadError() {
  301. this.$modal.msgError("上传图片失败,请重试");
  302. this.$modal.closeLoading();
  303. },
  304. // 上传结束处理
  305. uploadedSuccessfully() {
  306. if (this.number > 0 && this.uploadList.length === this.number) {
  307. debugger
  308. this.fileList = this.fileList.concat(this.uploadList);
  309. this.uploadList = [];
  310. this.number = 0;
  311. this.$emit("input", this.listToString(this.fileList));
  312. this.$modal.closeLoading();
  313. }
  314. },
  315. // 预览
  316. handlePictureCardPreview(file) {
  317. this.dialogImageUrl = file.url;
  318. this.dialogVisible = true;
  319. },
  320. // 对象转成指定字符串分隔
  321. listToString(list, separator) {
  322. let strs = "";
  323. separator = separator || ",";
  324. for (let i in list) {
  325. if (list[i].url) {
  326. strs += list[i].url.replace(this.baseUrl, "") + separator;
  327. }
  328. }
  329. return strs != "" ? strs.substr(0, strs.length - 1) : "";
  330. },
  331. },
  332. };
  333. </script>
  334. <style scoped lang="scss">
  335. // .el-upload--picture-card 控制加号部分
  336. ::v-deep .hide .el-upload--picture-card {
  337. display: none !important;
  338. }
  339. // 去掉动画效果
  340. ::v-deep .el-list-enter-active,
  341. ::v-deep .el-list-leave-active {
  342. transition: all 0s;
  343. }
  344. ::v-deep .el-list-enter,
  345. .el-list-leave-active {
  346. opacity: 0;
  347. transform: translateY(0);
  348. }
  349. .img-box {
  350. width: 100%;
  351. height: 100%;
  352. position: relative;
  353. .img-model {
  354. width: 100%;
  355. display: none;
  356. position: absolute;
  357. left: 0;
  358. top: 0;
  359. align-items: center;
  360. > span {
  361. background-color: rgba(30, 30, 30, 0.3);
  362. display: block;
  363. padding: 5px;
  364. color: #fff;
  365. text-shadow: 0 0 2px #1e1e1e;
  366. }
  367. }
  368. &:hover {
  369. .img-model {
  370. display: block;
  371. }
  372. }
  373. }
  374. </style>