index.vue 11 KB

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