index.vue 7.8 KB

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