ImageSelect.vue 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. <template>
  2. <div class="jp-upload" :class="{ 'jp-upload-round': round }" :style="style">
  3. <div
  4. v-if="file && file.status != 'success'"
  5. class="jp-upload__uploading"
  6. >
  7. <div class="jp-upload__progress">
  8. <el-progress
  9. :percentage="file.percentage"
  10. :text-inside="true"
  11. :stroke-width="16"
  12. />
  13. </div>
  14. <el-image class="image" :src="file.tempFile" fit="cover"></el-image>
  15. </div>
  16. <div v-if="file && file.status == 'success'" class="jp-upload__img">
  17. <el-image
  18. class="image"
  19. :src="file.url"
  20. :preview-src-list="[file.url]"
  21. fit="cover"
  22. hide-on-click-modal
  23. append-to-body
  24. :z-index="9999"
  25. >
  26. <template #placeholder>
  27. <div class="jp-upload__img-slot">Loading...</div>
  28. </template>
  29. </el-image>
  30. <div class="jp-upload__img-actions" v-if="!disabled">
  31. <span class="del" @click="handleRemove()"
  32. ><el-icon><el-icon-delete /></el-icon
  33. ></span>
  34. </div>
  35. </div>
  36. <el-upload
  37. v-if="!file"
  38. class="uploader"
  39. ref="uploader"
  40. :auto-upload="cropper ? false : autoUpload"
  41. :disabled="disabled"
  42. :show-file-list="showFileList"
  43. :action="action"
  44. :name="name"
  45. :data="data"
  46. :accept="accept"
  47. :limit="1"
  48. :http-request="request"
  49. :on-change="change"
  50. :before-upload="before"
  51. :on-success="success"
  52. :on-error="error"
  53. :on-exceed="handleExceed"
  54. >
  55. <slot>
  56. <div class="el-upload--picture-card">
  57. <div class="file-empty">
  58. <el-icon><component :is="icon" /></el-icon>
  59. <h4 v-if="title">{{ title }}</h4>
  60. </div>
  61. </div>
  62. </slot>
  63. </el-upload>
  64. <span style="display: none !important"
  65. ><el-input v-model="value"></el-input
  66. ></span>
  67. <el-dialog
  68. title="剪裁"
  69. draggable
  70. v-model="cropperDialogVisible"
  71. :width="580"
  72. @closed="cropperClosed"
  73. destroy-on-close
  74. >
  75. <cropper
  76. :src="cropperFile.tempCropperFile"
  77. :compress="compress"
  78. :aspectRatio="aspectRatio"
  79. ref="cropper"
  80. ></cropper>
  81. <template #footer>
  82. <el-button @click="cropperDialogVisible = false"
  83. >取 消</el-button
  84. >
  85. <el-button type="primary" @click="cropperSave">确 定</el-button>
  86. </template>
  87. </el-dialog>
  88. </div>
  89. </template>
  90. <script>
  91. import { defineAsyncComponent } from "vue";
  92. import { genFileId } from "element-plus";
  93. const cropper = defineAsyncComponent(() => import("@/components/cropper"));
  94. import config from "@/config/upload";
  95. export default {
  96. props: {
  97. modelValue: { type: String, default: "" },
  98. height: { type: Number, default: 148 },
  99. width: { type: Number, default: 148 },
  100. title: { type: String, default: "" },
  101. icon: { type: String, default: "el-icon-plus" },
  102. action: { type: String, default: "" },
  103. apiObj: { type: Object, default: () => {} },
  104. name: { type: String, default: config.filename },
  105. data: { type: Object, default: () => {} },
  106. accept: { type: String, default: "image/gif, image/jpeg, image/png" },
  107. maxSize: { type: Number, default: config.maxSize },
  108. limit: { type: Number, default: 1 },
  109. autoUpload: { type: Boolean, default: true },
  110. showFileList: { type: Boolean, default: false },
  111. disabled: { type: Boolean, default: false },
  112. round: { type: Boolean, default: false },
  113. onSuccess: {
  114. type: Function,
  115. default: () => {
  116. return true;
  117. },
  118. },
  119. cropper: { type: Boolean, default: false },
  120. compress: { type: Number, default: 1 },
  121. aspectRatio: { type: Number, default: NaN },
  122. },
  123. components: {
  124. cropper,
  125. },
  126. data() {
  127. return {
  128. value: "",
  129. file: null,
  130. style: {
  131. width: this.width + "px",
  132. height: this.height + "px",
  133. },
  134. cropperDialogVisible: false,
  135. cropperFile: {
  136. tempCropperFile: null,
  137. },
  138. };
  139. },
  140. watch: {
  141. modelValue(val) {
  142. this.value = val;
  143. this.newFile(val);
  144. },
  145. value(val) {
  146. this.$emit("update:modelValue", val);
  147. },
  148. },
  149. mounted() {
  150. this.value = this.modelValue;
  151. this.newFile(this.modelValue);
  152. },
  153. methods: {
  154. newFile(url) {
  155. if (url) {
  156. this.file = {
  157. status: "success",
  158. url: url,
  159. };
  160. } else {
  161. this.file = null;
  162. }
  163. },
  164. cropperSave() {
  165. this.$refs.cropper.getCropFile(
  166. (file) => {
  167. file.uid = this.cropperFile.uid;
  168. this.cropperFile.raw = file;
  169. this.file = this.cropperFile;
  170. this.file.tempFile = URL.createObjectURL(this.file.raw);
  171. this.$refs.uploader.submit();
  172. },
  173. this.cropperFile.name,
  174. this.cropperFile.type
  175. );
  176. this.cropperDialogVisible = false;
  177. },
  178. cropperClosed() {
  179. URL.revokeObjectURL(this.cropperFile.tempCropperFile);
  180. delete this.cropperFile.tempCropperFile;
  181. },
  182. handleRemove() {
  183. this.clearFiles();
  184. },
  185. clearFiles() {
  186. URL.revokeObjectURL(this.file.tempFile);
  187. this.value = "";
  188. this.file = null;
  189. this.$nextTick(() => {
  190. this.$refs.uploader.clearFiles();
  191. });
  192. },
  193. change(file) {
  194. if (this.cropper && file.status == "ready") {
  195. const acceptIncludes = [
  196. "image/gif",
  197. "image/jpeg",
  198. "image/png",
  199. ].includes(file.raw.type);
  200. if (!acceptIncludes) {
  201. this.$notify.warning({
  202. title: "上传文件警告",
  203. message: "选择的文件非图像类文件",
  204. });
  205. return false;
  206. }
  207. this.cropperFile = file;
  208. this.cropperFile.tempCropperFile = URL.createObjectURL(
  209. file.raw
  210. );
  211. this.cropperDialogVisible = true;
  212. return false;
  213. }
  214. this.file = file;
  215. if (file.status == "ready") {
  216. file.tempFile = URL.createObjectURL(file.raw);
  217. }
  218. },
  219. before(file) {
  220. const acceptIncludes = this.accept
  221. .replace(/\s/g, "")
  222. .split(",")
  223. .includes(file.type);
  224. if (!acceptIncludes) {
  225. this.$notify.warning({
  226. title: "上传文件警告",
  227. message: "选择的文件非图像类文件",
  228. });
  229. this.clearFiles();
  230. return false;
  231. }
  232. const maxSize = file.size / 1024 / 1024 < this.maxSize;
  233. if (!maxSize) {
  234. this.$message.warning(
  235. `上传文件大小不能超过 ${this.maxSize}MB!`
  236. );
  237. this.clearFiles();
  238. return false;
  239. }
  240. },
  241. handleExceed(files) {
  242. const file = files[0];
  243. file.uid = genFileId();
  244. this.$refs.uploader.handleStart(file);
  245. },
  246. success(res, file) {
  247. var response = config.parseData(res);
  248. file.url = response.src;
  249. this.value = file.url;
  250. //释放内存删除blob
  251. URL.revokeObjectURL(file.tempFile);
  252. delete file.tempFile;
  253. },
  254. error(err) {
  255. this.$nextTick(() => {
  256. this.clearFiles();
  257. });
  258. this.$notify.error({
  259. title: "上传文件未成功",
  260. message: err,
  261. });
  262. },
  263. request(param) {
  264. var apiObj = config.apiObj;
  265. if (this.apiObj) {
  266. apiObj = this.apiObj;
  267. }
  268. const data = new FormData();
  269. data.append(param.filename, param.file);
  270. for (const key in param.data) {
  271. data.append(key, param.data[key]);
  272. }
  273. apiObj
  274. .upload(data, {
  275. onUploadProgress: (e) => {
  276. const complete = parseInt(
  277. ((e.loaded / e.total) * 100) | 0,
  278. 10
  279. );
  280. param.onProgress({ percent: complete });
  281. },
  282. })
  283. .then((res) => {
  284. param.onSuccess(res);
  285. })
  286. .catch((err) => {
  287. param.onError(err);
  288. });
  289. },
  290. },
  291. };
  292. </script>
  293. <style scoped>
  294. @charset "utf-8";
  295. .el-form-item.is-error .jp-upload .el-upload--picture-card {
  296. border-color: var(--el-color-danger);
  297. }
  298. .jp-upload .el-upload--picture-card {
  299. border-radius: 0;
  300. }
  301. .jp-upload .uploader,
  302. .jp-upload:deep(.el-upload) {
  303. width: 100%;
  304. height: 100%;
  305. }
  306. .jp-upload__img {
  307. width: 100%;
  308. height: 100%;
  309. position: relative;
  310. }
  311. .jp-upload__img .image {
  312. width: 100%;
  313. height: 100%;
  314. }
  315. .jp-upload__img-actions {
  316. position: absolute;
  317. top: 0;
  318. right: 0;
  319. display: none;
  320. }
  321. .jp-upload__img-actions span {
  322. display: flex;
  323. justify-content: center;
  324. align-items: center;
  325. width: 25px;
  326. height: 25px;
  327. cursor: pointer;
  328. color: #fff;
  329. }
  330. .jp-upload__img-actions span i {
  331. font-size: 12px;
  332. }
  333. .jp-upload__img-actions .del {
  334. background: #f56c6c;
  335. }
  336. .jp-upload__img:hover .jp-upload__img-actions {
  337. display: block;
  338. }
  339. .jp-upload__img-slot {
  340. display: flex;
  341. justify-content: center;
  342. align-items: center;
  343. width: 100%;
  344. height: 100%;
  345. font-size: 12px;
  346. background-color: var(--el-fill-color-lighter);
  347. }
  348. .jp-upload__uploading {
  349. width: 100%;
  350. height: 100%;
  351. position: relative;
  352. }
  353. .jp-upload__progress {
  354. position: absolute;
  355. width: 100%;
  356. height: 100%;
  357. display: flex;
  358. justify-content: center;
  359. align-items: center;
  360. background-color: var(--el-overlay-color-lighter);
  361. z-index: 1;
  362. padding: 10px;
  363. }
  364. .jp-upload__progress .el-progress {
  365. width: 100%;
  366. }
  367. .jp-upload__uploading .image {
  368. width: 100%;
  369. height: 100%;
  370. }
  371. .jp-upload .file-empty {
  372. width: 100%;
  373. height: 100%;
  374. display: flex;
  375. justify-content: center;
  376. align-items: center;
  377. flex-direction: column;
  378. }
  379. .jp-upload .file-empty i {
  380. font-size: 28px;
  381. }
  382. .jp-upload .file-empty h4 {
  383. font-size: 12px;
  384. font-weight: normal;
  385. color: #8c939d;
  386. margin-top: 8px;
  387. }
  388. .jp-upload.jp-upload-round {
  389. border-radius: 50%;
  390. overflow: hidden;
  391. }
  392. .jp-upload.jp-upload-round .el-upload--picture-card {
  393. border-radius: 50%;
  394. }
  395. .jp-upload.jp-upload-round .jp-upload__img-actions {
  396. top: auto;
  397. left: 0;
  398. right: 0;
  399. bottom: 0;
  400. }
  401. .jp-upload.jp-upload-round .jp-upload__img-actions span {
  402. width: 100%;
  403. }
  404. </style>