index.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. <template>
  2. <div class="component-upload-image img-accessory-1">
  3. <div class="img-box-1" :class="low && 'low'">
  4. <ul class="el-upload-list el-upload-list--picture-card">
  5. <li v-for="(item, index) in previewList" class="el-upload-list__item is-ready" style="float: left;">
  6. <auth-img v-if="!isPublic" :auth-src="item.thumbUrl" image-width="80px" image-height="78px"></auth-img>
  7. <el-image v-if="isPublic" :src="item.thumbUrl" fit="contain" style="width: 80px;height: 78px;border-radius: 4px;"></el-image>
  8. <label class="el-upload-list__item-status-label"><i class="el-icon-upload-success el-icon-check"></i></label>
  9. <span class="el-upload-list__item-actions">
  10. <span class="el-upload-list__item-preview"><i class="el-icon-zoom-in" @click="handlePreview(item)"></i></span>
  11. <span class="el-upload-list__item-delete"><i class="el-icon-delete" @click="handleDelete(item)"></i></span>
  12. </span>
  13. </li>
  14. </ul>
  15. <el-upload
  16. :action="fileSaveUrl"
  17. list-type="picture-card"
  18. :data="reqData"
  19. :on-success="fileUploadSuccess"
  20. :before-upload="handleBeforeUpload"
  21. :limit="limit"
  22. :on-error="handleUploadError"
  23. :on-exceed="handleExceed"
  24. name="file"
  25. multiple
  26. :on-remove="handleRemove"
  27. :show-file-list="false"
  28. :headers="headers"
  29. :http-request="reqUploadImage"
  30. :file-list="previewList"
  31. :on-preview="handlePictureCardPreview"
  32. :auto-upload="true"
  33. :class="{hide: this.fileList.length >= this.limit}">
  34. <i class="el-icon-plus"></i>
  35. </el-upload>
  36. </div>
  37. <!-- 上传提示 -->
  38. <!-- <div class="el-upload__tip" slot="tip" v-if="showTip">
  39. 请上传
  40. <template v-if="fileSize"> 大小不超过 <b style="color: #f56c6c">{{ fileSize }}MB</b> </template>
  41. <template v-if="fileType"> 格式为 <b style="color: #f56c6c">{{ fileType.join("/") }}</b> </template>
  42. 的文件
  43. </div> -->
  44. <el-dialog :visible.sync="dialogVisible" title="预览" width="800" append-to-body>
  45. <div style="display: block; max-width: 100%; margin: 0 auto;text-align: center;max-height: 435px;overflow: auto;">
  46. <auth-img v-if="!isPublic && dialogVisible" :auth-src="dialogImageUrl" image-width="435px" ></auth-img>
  47. <img v-if="isPublic" :src="dialogImageUrl" style="display: block; max-width: 100%; margin: 0 auto" />
  48. </div>
  49. </el-dialog>
  50. </div>
  51. </template>
  52. <script>
  53. import {
  54. getToken,
  55. getSign
  56. } from "@/utils/auth";
  57. import {
  58. randomStr20
  59. } from '@/utils/util'
  60. import {
  61. privateFileSaveUrl,
  62. publicFileSaveUrl,
  63. publicFileSaveAPI,
  64. privateFileSaveAPI,
  65. publicFileGetUrl,
  66. privateFileGetUrl
  67. } from '@/api/common'
  68. import AuthImg from "@/components/AuthImg/index.vue"
  69. import UploadImg from "@/components/UploadImg/index.vue"
  70. export default {
  71. components: {
  72. AuthImg,
  73. UploadImg
  74. },
  75. props: {
  76. // 已经上传的文件url列表
  77. value: [String, Object, Array],
  78. // 图片数量限制
  79. limit: {
  80. type: Number,
  81. default: 5,
  82. },
  83. // 大小限制(MB)
  84. fileSize: {
  85. type: Number,
  86. default: 5,
  87. },
  88. // 文件类型, 例如['png', 'jpg', 'jpeg']
  89. fileType: {
  90. type: Array,
  91. default: () => ["png", "jpg", "jpeg"],
  92. },
  93. // 是否显示提示
  94. isShowTip: {
  95. type: Boolean,
  96. default: true
  97. },
  98. // 是否显示提示
  99. isPublic: {
  100. type: Boolean,
  101. default: true
  102. },
  103. // 半高显示
  104. low: {
  105. type: Boolean,
  106. default: false
  107. }
  108. },
  109. data() {
  110. return {
  111. dialogImageUrl: "",
  112. dialogVisible: false,
  113. hideUpload: false,
  114. baseUrl: process.env.VUE_APP_BASE_API,
  115. uploadImgUrl: this.isPublic ? publicFileSaveUrl : privateFileSaveUrl, // 上传的图片服务器地址
  116. fileSaveUrl: '',
  117. headers: {
  118. Authorization: "Bearer " + getToken(),
  119. },
  120. fileList: [],
  121. previewList: [],
  122. reqData: {}
  123. };
  124. },
  125. watch: {
  126. value(val) {
  127. if (val) {
  128. this.initHandle()
  129. }
  130. }
  131. },
  132. computed: {
  133. // 是否显示提示
  134. showTip() {
  135. return this.isShowTip && (this.fileType || this.fileSize);
  136. }
  137. },
  138. created() {
  139. this.initHandle()
  140. },
  141. methods: {
  142. initHandle() {
  143. if (this.value) {
  144. // 首先将值转为数组
  145. const list = Array.isArray(this.value) ? this.value : this.value.split(',')
  146. // 然后将数组转为对象数组
  147. this.previewList = []
  148. this.fileList = list.map(item => {
  149. var pitem = {
  150. name: item.fileName,
  151. url: (this.isPublic ? publicFileGetUrl : (this.baseUrl + privateFileGetUrl)) + item.fileName,
  152. thumbUrl: (this.isPublic ? publicFileGetUrl : (this.baseUrl + privateFileGetUrl)) + item.fileName + "_s"
  153. };
  154. this.previewList.push(pitem)
  155. return item;
  156. });
  157. } else {
  158. this.previewList = []
  159. this.fileList = []
  160. }
  161. },
  162. handlePreview(item){
  163. this.dialogVisible = true;
  164. this.dialogImageUrl = item.url
  165. },
  166. handleDelete(item){
  167. const findex = this.fileList.map(f => f.fileName).indexOf(item.name);
  168. this.fileList.splice(findex, 1);
  169. this.previewList.splice(findex, 1);
  170. this.$emit("input", this.fileList);
  171. this.$emit('change')
  172. },
  173. // 删除图片
  174. handleRemove(file, fileList) {
  175. const findex = this.fileList.map(f => f.name).indexOf(file.name);
  176. this.fileList.splice(findex, 1);
  177. this.previewList.splice(findex, 1);
  178. this.$emit("input", this.fileList);
  179. this.$emit('change')
  180. },
  181. /**
  182. * 上传图片
  183. */
  184. fileUploadSuccess(response, file, fileList) {
  185. this.fileList.push({
  186. fileName: response.fileName,
  187. fileType: response.fileType
  188. });
  189. this.previewList.push({
  190. name: response.fileName,
  191. url: (this.isPublic ? publicFileGetUrl : (this.baseUrl + privateFileGetUrl)) + response.fileName,
  192. fileType: response.fileType
  193. });
  194. this.$emit("input", this.fileList);
  195. this.$emit('change')
  196. this.loading.close();
  197. },
  198. // 上传前loading加载
  199. handleBeforeUpload(file) {
  200. let isImg = false;
  201. if (this.fileType.length) {
  202. let fileExtension = "";
  203. if (file.name.lastIndexOf(".") > -1) {
  204. fileExtension = file.name.slice(file.name.lastIndexOf(".") + 1);
  205. }
  206. isImg = this.fileType.some(type => {
  207. if (file.type.indexOf(type) > -1) return true;
  208. if (fileExtension && fileExtension.indexOf(type) > -1) return true;
  209. return false;
  210. });
  211. } else {
  212. isImg = file.type.indexOf("image") > -1;
  213. }
  214. if (!isImg) {
  215. this.$message.error(
  216. `文件格式不正确, 请上传${this.fileType.join("/")}图片格式文件!`
  217. );
  218. return false;
  219. }
  220. if (this.fileSize) {
  221. const isLt = file.size / 1024 / 1024 < this.fileSize;
  222. if (!isLt) {
  223. this.$message.error(`上传头像图片大小不能超过 ${this.fileSize} MB!`);
  224. return false;
  225. }
  226. }
  227. this.getHttpHeader()
  228. this.loading = this.$loading({
  229. lock: true,
  230. text: "上传中",
  231. background: "rgba(0, 0, 0, 0.7)",
  232. });
  233. },
  234. // 自定义文件上传的实现
  235. reqUploadImage(param) {
  236. var data = this.reqData || {}
  237. var params = {
  238. file: param.file,
  239. ...data
  240. }
  241. const request = this.isPublic ? publicFileSaveAPI : privateFileSaveAPI
  242. request(params, this.headers).then(response => {
  243. var res = {
  244. fileName: response.data.fileName,
  245. fileType: response.data.fileType
  246. }
  247. this.fileList.push({
  248. fileName: response.data.fileName,
  249. fileType: response.data.fileType
  250. });
  251. this.previewList.push({
  252. name: response.data.fileName,
  253. url: (this.isPublic ? publicFileGetUrl : (this.baseUrl + privateFileGetUrl)) + response.data.fileName,
  254. fileType: response.fileType
  255. });
  256. this.$emit("input", this.fileList);
  257. this.$emit('change')
  258. this.loading.close();
  259. // 但是我们上传成功了图片, fileList 里面的值却没有改变,还好有on-change指令可以使用
  260. }).catch(response => {
  261. param.onError()
  262. })
  263. },
  264. // 请求头
  265. getHttpHeader() {
  266. let timestamp = parseInt(new Date().getTime()),
  267. nonce = randomStr20();
  268. var sign = getSign(this.reqData || {}, timestamp)
  269. let url = this.uploadImgUrl + '?sign=' + sign + '&nonce=' + nonce;
  270. this.fileSaveUrl = url
  271. var headers = {
  272. "Authorization": "Bearer " + getToken(),
  273. "x-zz-timestamp": timestamp
  274. }
  275. this.headers = headers
  276. },
  277. // 文件个数超出
  278. handleExceed() {
  279. this.$message.error(`上传文件数量不能超过 ${this.limit} 个!`);
  280. },
  281. // 上传失败
  282. handleUploadError() {
  283. // this.$message({
  284. // type: "error",
  285. // message: "上传失败",
  286. // });
  287. this.loading.close();
  288. },
  289. // 预览
  290. handlePictureCardPreview(file) {
  291. this.dialogImageUrl = file.url;
  292. this.dialogVisible = true;
  293. },
  294. // 对象转成指定字符串分隔
  295. listToString(list, separator) {
  296. let strs = "";
  297. separator = separator || ",";
  298. for (let i in list) {
  299. strs += list[i].url.replace(this.baseUrl, "") + separator;
  300. }
  301. return strs != '' ? strs.substr(0, strs.length - 1) : '';
  302. }
  303. }
  304. };
  305. </script>
  306. <style scoped lang="scss">
  307. // .el-upload--picture-card 控制加号部分
  308. ::v-deep.hide .el-upload--picture-card {
  309. display: none;
  310. }
  311. // 去掉动画效果
  312. ::v-deep .el-list-enter-active,
  313. ::v-deep .el-list-leave-active {
  314. transition: all 0s;
  315. }
  316. ::v-deep .el-list-enter,
  317. .el-list-leave-active {
  318. opacity: 0;
  319. transform: translateY(0);
  320. }
  321. .img-accessory-1 {
  322. padding: 0 1px;
  323. font-size: 12px;
  324. img {
  325. width: 16px;
  326. vertical-align: middle;
  327. }
  328. .img-box-1 ::v-deep .el-upload {
  329. width: 80px;
  330. height: 80px;
  331. line-height: 90px;
  332. }
  333. .img-box-1 ::v-deep .el-upload-list {
  334. .el-upload-list__item {
  335. width: 80px;
  336. height: 80px;
  337. }
  338. }
  339. .img-box-1 {
  340. position: relative;
  341. margin-top: 0px;
  342. .add-img {
  343. position: absolute;
  344. left: 0;
  345. top: -30px;
  346. height: 20px;
  347. line-height: 20px;
  348. margin-bottom: 10px;
  349. color: #2362FB;
  350. }
  351. }
  352. .add-accessory-1 {
  353. margin-top: 25px;
  354. margin-bottom: 20px;
  355. color: #2362FB;
  356. .wukong-file {
  357. font-size: 13px;
  358. }
  359. }
  360. }
  361. .el-upload-litem-actions{
  362. position: absolute;
  363. width: 80px;
  364. height: 78px;
  365. left: 0;
  366. top: 0;
  367. cursor: default;
  368. text-align: center;
  369. color: #fff;
  370. opacity: 0;
  371. font-size: 20px;
  372. background-color: rgba(0,0,0,.5);
  373. -webkit-transition: opacity .3s;
  374. transition: opacity .3s;
  375. border-radius: 4px;
  376. }
  377. .el-upload-item-actions span {
  378. display: none;
  379. cursor: pointer;
  380. }
  381. [class^=el-icon-], [class*=" el-icon-"] {
  382. font-family: "element-icons" !important;
  383. speak: none;
  384. font-style: normal;
  385. font-weight: normal;
  386. font-variant: normal;
  387. text-transform: none;
  388. line-height: 1;
  389. vertical-align: baseline;
  390. display: inline-block;
  391. -webkit-font-smoothing: antialiased;
  392. -moz-osx-font-smoothing: grayscale;
  393. }
  394. .el-upload-litem-actions .el-upload-item-delete {
  395. position: static;
  396. font-size: inherit;
  397. color: inherit;
  398. }
  399. .el-upload-litem-actions .el-upload-item-preview {
  400. position: static;
  401. font-size: inherit;
  402. color: inherit;
  403. }
  404. .el-upload-litem-actions:hover {
  405. opacity: 1;
  406. }
  407. .el-icon-zoom-in:before {
  408. content: "\E777";
  409. }
  410. .el-icon-delete:before {
  411. content: "\E6D7";
  412. }
  413. .el-upload-item-actions:hover span {
  414. display: inline-block;
  415. }
  416. .el-upload-item-actions:hover .el-upload-item-preview {
  417. position: absolute!important;
  418. left: 10px!important;
  419. top: 0!important;
  420. color: #666;
  421. display: block;
  422. }
  423. .el-upload-item-actions:hover .el-upload-item-delete {
  424. position: absolute!important;
  425. right: 10px!important;
  426. top: 0!important;
  427. color: #666;
  428. display: block;
  429. }
  430. </style>
  431. <style lang="scss">
  432. .component-upload-image {
  433. .low {
  434. height: 40px;
  435. .el-upload--picture-card {
  436. height: 40px !important;
  437. line-height: 50px !important;
  438. }
  439. .el-upload-list__item {
  440. margin: 0 !important;
  441. height: 40px !important;
  442. }
  443. .el-image {
  444. height: 38px !important;
  445. }
  446. .el-upload-list__item-actions {
  447. height: 38px !important;
  448. }
  449. }
  450. }
  451. </style>