ImgUpload.vue 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. <template>
  2. <div>
  3. <div v-if="type !== 'callback'" class="imgcomp-upload-list" v-for="item in uploadList">
  4. <template v-if="item.status === 'finished'">
  5. <div class="imgcomp-upload-img" v-bind:style="{ 'background-image': 'url(' + __thumb(item.thumb) + ')' }"></div>
  6. <div class="imgcomp-upload-list-cover">
  7. <Icon type="ios-eye-outline" @click.native="handleView(item)"></Icon>
  8. <Icon type="ios-trash-outline" @click.native="handleRemove(item)"></Icon>
  9. </div>
  10. </template>
  11. <template v-else>
  12. <Progress v-if="item.showProgress" :percent="item.percentage" hide-info></Progress>
  13. </template>
  14. </div>
  15. <div class="add-box" v-bind:class="{ 'callback-add-box': type === 'callback' }">
  16. <div class="add-box-icon">
  17. <Icon type="md-add" size="32"></Icon>
  18. </div>
  19. <div class="add-box-upload">
  20. <div class="add-box-item" @click="browsePicture">
  21. <span>{{$L('浏览')}}<em v-if="type === 'callback'">{{$L('图片')}}</em></span>
  22. </div>
  23. <div class="add-box-item">
  24. <Upload
  25. name="image"
  26. ref="upload"
  27. accept="image/*"
  28. :action="actionUrl"
  29. :data="uploadParams"
  30. :show-upload-list="false"
  31. :max-size="2048"
  32. :format="['jpg', 'jpeg', 'gif', 'png']"
  33. :default-file-list="defaultList"
  34. :on-success="handleSuccess"
  35. :on-format-error="handleFormatError"
  36. :on-exceeded-size="handleMaxSize"
  37. :before-upload="handleBeforeUpload"
  38. :multiple=multiple>
  39. <span>{{$L('上传')}}<em v-if="type === 'callback'">{{$L('图片')}}</em></span>
  40. </Upload>
  41. </div>
  42. </div>
  43. </div>
  44. <Modal :title="$L('浏览图片空间的图片')" v-model="browseVisible" class="img-upload-modal" width="710" :styles="{top: '35px',paddingBottom: '35px'}">
  45. <div class="browse-load" v-if="isLoading">{{$L('加载中...')}}</div>
  46. <div class="browse-list" :class="httpType==='input'?'browse-list-disabled':''" ref="browselistbox">
  47. <div class="browse-item" v-for="item in browseList" @click="browseItem(item)">
  48. <Icon v-if="item.active" class="browse-icon" type="ios-checkmark-circle"></Icon>
  49. <div class="browse-img" v-bind:style="{ 'background-image': 'url(' + item.thumb + ')' }"></div>
  50. <div class="browse-title">{{item.title}}</div>
  51. </div>
  52. </div>
  53. <div slot="footer" class="img-upload-foot">
  54. <div v-if="type !== 'callback' && http && httpType===''" class="img-upload-foot-input" @click="httpType='input'">
  55. <Icon type="ios-image" size="22"/>
  56. <div class="img-upload-foot-httptitle">{{$L('自定义图片地址')}}</div>
  57. </div>
  58. <div v-if="type !== 'callback' && http && httpType==='input'" class="img-upload-foot-input">
  59. <Input v-model="httpValue" :placeholder="$L('以“http://”或“https://”开头')" @on-search="httpEnter" search :enter-button="$L('确定')">
  60. <span slot="prepend" @click="httpType=''" style="cursor:pointer">{{$L('自定义地址')}}: </span>
  61. </Input>
  62. </div>
  63. <Button v-if="httpType===''" @click="browseVisible=false">{{$L('关闭')}}</Button>
  64. <Button v-if="httpType===''" type="primary" @click="handleCallback(true)">{{$L('完成')}}</Button>
  65. </div>
  66. </Modal>
  67. <Modal :title="$L('查看图片')" v-model="visible" class="img-upload-modal" draggable>
  68. <div style="max-height:480px;overflow:auto;">
  69. <a :href="imgVisible" target="_blank"><img :src="imgVisible" v-if="visible" style="max-width:100%;max-height:900px;display:block;margin:0 auto"></a>
  70. </div>
  71. </Modal>
  72. </div>
  73. </template>
  74. <style lang="scss">
  75. .img-upload-modal {
  76. .ivu-modal-mask {
  77. z-index: 1001;
  78. }
  79. .ivu-modal-no-mask {
  80. background-color: rgba(55,55,55,.2);
  81. }
  82. .ivu-modal-wrap {
  83. z-index: 1001;
  84. }
  85. }
  86. .imgcomp-upload-list{
  87. display: inline-block;
  88. width: 60px;
  89. height: 60px;
  90. text-align: center;
  91. line-height: 60px;
  92. border: 1px solid transparent;
  93. border-radius: 4px;
  94. overflow: hidden;
  95. background: #fff;
  96. position: relative;
  97. box-shadow: 0 1px 1px rgba(0,0,0,.2);
  98. margin-right: 4px;
  99. vertical-align: top;
  100. .imgcomp-upload-img {
  101. position: absolute;
  102. top: 0;
  103. left: 0;
  104. width: 100%;
  105. height: 100%;
  106. background-position: center;
  107. background-size: cover;
  108. }
  109. .imgcomp-upload-list-cover{
  110. display: none;
  111. position: absolute;
  112. top: 0;
  113. bottom: 0;
  114. left: 0;
  115. right: 0;
  116. background: rgba(0,0,0,.6);
  117. }
  118. .imgcomp-upload-list-cover i{
  119. color: #fff;
  120. font-size: 24px;
  121. cursor: pointer;
  122. vertical-align: middle;
  123. margin: 0;
  124. transition: all .2s;
  125. }
  126. .imgcomp-upload-list-cover i:hover{
  127. font-size: 28px;
  128. }
  129. .ivu-progress-outer {
  130. background-color: rgba(0, 0, 0, 0.68);
  131. .ivu-progress-inner{
  132. width: 88%;
  133. }
  134. }
  135. }
  136. .imgcomp-upload-list:hover .imgcomp-upload-list-cover{
  137. display: block;
  138. }
  139. .img-upload-foot {
  140. display: flex;
  141. align-items: center;
  142. justify-content: flex-end;
  143. .img-upload-foot-input {
  144. flex: 1;
  145. text-align: left;
  146. display: flex;
  147. align-items: center;
  148. justify-content: flex-end;
  149. .img-upload-foot-httptitle {
  150. cursor: pointer;
  151. padding-left: 3px;
  152. margin-right: 22px;
  153. }
  154. }
  155. }
  156. .add-box {
  157. width: 60px;
  158. height: 60px;
  159. line-height: 60px;
  160. display: inline-block;
  161. background: #fff;
  162. border: 1px dashed #dddee1;
  163. border-radius: 4px;
  164. text-align: center;
  165. position: relative;
  166. overflow: hidden;
  167. vertical-align: top;
  168. .add-box-icon {
  169. i {
  170. vertical-align:middle;
  171. padding-bottom: 2px;
  172. }
  173. }
  174. .add-box-upload {
  175. display: none;
  176. position: absolute;
  177. top: 0;
  178. left: 0;
  179. height: 100%;
  180. width: 100%;
  181. color: #ffffff;
  182. padding-top: 9px;
  183. background: rgba(0, 0, 0, 0.6);
  184. .add-box-item {
  185. height: 22px;
  186. line-height: 22px;
  187. cursor: pointer;
  188. .ivu-upload-drag,.ivu-upload-drag:hover {
  189. background:transparent;
  190. border:0;
  191. border-radius:0;
  192. }
  193. span {
  194. transition: all .2s;
  195. font-size: 12px;
  196. }
  197. }
  198. .add-box-item:hover {
  199. span {
  200. font-size: 14px;
  201. }
  202. }
  203. }
  204. em {
  205. font-style: normal;
  206. }
  207. }
  208. .add-box:hover {
  209. border-color: rgba(0,0,0,.6);
  210. .add-box-upload {
  211. display: block;
  212. }
  213. }
  214. .callback-add-box {
  215. display: block;
  216. width: auto;
  217. height: 25px;
  218. line-height: 25px;
  219. border: 0;
  220. background: transparent;
  221. .add-box-icon {
  222. display: none;
  223. }
  224. .add-box-upload {
  225. display: block;
  226. width: auto;
  227. background: transparent;
  228. color: #333;
  229. padding: 0;
  230. > div {
  231. display: inline-block;
  232. padding-right: 10px;
  233. }
  234. }
  235. }
  236. .browse-load {
  237. margin: 20px;
  238. text-align: center;
  239. }
  240. .browse-list {
  241. max-height: 540px;
  242. overflow: auto;
  243. .browse-item {
  244. margin: 10px 15px;
  245. display: inline-block;
  246. text-align: center;
  247. cursor: pointer;
  248. position: relative;
  249. .browse-img {
  250. width: 64px;
  251. height: 64px;
  252. background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAKoAAABxCAAAAABg5GeyAAACW0lEQVR4Ae3XVRLjMBAE0L3/rSwKM3OcSNPyLVYOLvM6UD0Bd03LVe9XH+RlhlRSSSWVVFJJJZVUUkkllVRSSSWVVFJJJZVUUkl9WyqppJJKKqmkkgpURP17xngOAR5NxW5wlJ9MaLQh83F4NHWmd/gZtdVBaOldfDB1bq5UpJFbFOC6LKnYrkRO209PAw+hIuzWB8Ep5es8HvYo4z4tE1X8UeRwlMM2D5Bzkc7kj6Bi3VTKDDwEeUcrMxrUvGDXTnHa6kK69SDN9sgq1clxKSbNHqqnYmdri81Q9QHf1JPt1Frncaib2XbiTKL2GkHaurnY9LOulMV0O7G6Kw+g9sw2ohhm62KezVJaaufjWC1TnOkr1exilJ7Ji0vxCCqO9V4UwV4PYr9+apouhGYLKfnahdpqegjmeoXOpXgANe70pKT6Zhu19qkY2nC0PZS527lQOyInqr8Uvc5jqfUb1X+PGh5IhW90S2quh3FQC2XRcF66TUkTXPcLKm5FtdR9RJq+2hWII7UpFtmsQLEyzsdJtkxxpr6gLotbUSlV9yeT0Trmzk2XPdUThLYarUbWOY9j04xXQ2u+pMZLYSumGmNUH3HbM9qOAwSHodN2Pks25F2j3aI7+IxqNsB+YLWb16ukSjiW4xNB0gMoMfApBS/XZQgi3p9/5RsiKNKZEOwYFVIF5VyTyD19sbyjIJiNJRZxpNbx2S8sGKvGZNHJBniBu9Wy5WxjGuQFqIAcBHiRGyt4ua5gSCWVVFJJJZVUUkkllVRSSSWVVFJJJZVUUkkllVRSSSWVVFI/AgO0SXIVYHeGAAAAAElFTkSuQmCC);
  253. background-position: center;
  254. background-repeat: no-repeat;
  255. background-size: cover;
  256. }
  257. .browse-title {
  258. display: block;
  259. width: 64px;
  260. margin-top: 5px;
  261. white-space: nowrap;
  262. overflow: hidden;
  263. text-overflow: ellipsis;
  264. }
  265. .browse-icon {
  266. position: absolute;
  267. top: 0;
  268. left: 0;
  269. width: 100%;
  270. height: 64px;
  271. font-size: 36px;
  272. padding-top: 15px;
  273. color: #ffffff;
  274. background-color: rgba(0, 0, 0, 0.5);
  275. }
  276. }
  277. }
  278. .browse-list-disabled {
  279. position: relative;
  280. }
  281. .browse-list-disabled:after {
  282. position: absolute;
  283. content: '';
  284. top: 0;
  285. left: 0;
  286. width: 100%;
  287. height: 100%;
  288. background-color: rgba(255, 255, 255, 0.9);
  289. z-index: 1;
  290. }
  291. </style>
  292. <script>
  293. export default {
  294. name: 'ImgUpload',
  295. props: {
  296. value: {
  297. },
  298. num: {
  299. },
  300. width: {
  301. },
  302. height: {
  303. },
  304. type: {
  305. },
  306. http: {
  307. type: Boolean,
  308. default: false
  309. },
  310. otherParams: {
  311. type: Object,
  312. default: () => {
  313. return {};
  314. }
  315. },
  316. },
  317. data () {
  318. return {
  319. actionUrl: $A.aUrl('imgupload'),
  320. params: {'token': $A.token(), 'width': this.width, 'height': this.height},
  321. multiple: this.num > 1,
  322. visible: false,
  323. browseVisible: false,
  324. isLoading: false,
  325. browseList: [],
  326. browseListNext: [],
  327. imgVisible: '',
  328. defaultList: this.initItems(this.value),
  329. uploadList: [],
  330. maxNum: Math.min(Math.max($A.runNum(this.num), 1), 99),
  331. httpValue: '',
  332. httpType: '',
  333. }
  334. },
  335. mounted () {
  336. this.uploadList = this.$refs.upload.fileList;
  337. this.$emit('input', this.uploadList);
  338. //
  339. let browseBox = $A(this.$refs.browselistbox);
  340. browseBox.scroll(()=>{
  341. let nHight = browseBox[0].scrollHeight;
  342. let nTop = browseBox[0].scrollTop;
  343. let boxHight = browseBox.height();
  344. if(nTop + boxHight >= nHight) {
  345. //到底了
  346. if (this.browseListNext.length > 0) {
  347. let tmpNext = this.browseListNext;
  348. this.browseListNext = [];
  349. this.browsePictureFor(tmpNext);
  350. }
  351. }
  352. });
  353. },
  354. watch: {
  355. value (val) {
  356. if (typeof val === 'string') {
  357. this.$emit('input', this.initItems(val));
  358. return;
  359. }
  360. if (val === this.$refs.upload.fileList) {
  361. return;
  362. }
  363. this.$refs.upload.fileList = this.initItems(val);
  364. this.uploadList = this.$refs.upload.fileList;
  365. },
  366. browseVisible() {
  367. this.httpType = '';
  368. this.httpValue = '';
  369. }
  370. },
  371. computed: {
  372. uploadParams() {
  373. if (Object.keys(this.otherParams).length > 0) {
  374. return Object.assign(this.params, this.otherParams);
  375. } else {
  376. return this.params;
  377. }
  378. }
  379. },
  380. methods: {
  381. handleCallback(file) {
  382. if (this.type === 'callback') {
  383. if (file === true) {
  384. this.$emit('on-callback', this.uploadList);
  385. this.$refs.upload.fileList = [];
  386. this.uploadList = this.$refs.upload.fileList;
  387. }else if (typeof file === "object") {
  388. this.$emit('on-callback', [file]);
  389. }
  390. }
  391. this.browseVisible = false;
  392. },
  393. initItems(items) {
  394. //数据初始化
  395. if (typeof items === 'string') {
  396. items = [{'url': items}];
  397. }
  398. let lists = [];
  399. $A.each(items, (index, item)=>{
  400. if (typeof item === 'string') item = {'url': item};
  401. if (item.url) {
  402. item.active = true;
  403. item.status = 'finished';
  404. if (typeof item.path === 'undefined') item.path = item.url;
  405. if (typeof item.thumb === 'undefined') item.thumb = item.url;
  406. lists.push(item);
  407. }
  408. });
  409. return lists;
  410. },
  411. handleView (item) {
  412. //查看
  413. this.visible = true;
  414. this.imgVisible = item.url;
  415. },
  416. handleRemove (item) {
  417. //删除
  418. let fileList = this.$refs.upload.fileList;
  419. this.$refs.upload.fileList.splice(fileList.indexOf(item), 1);
  420. this.$emit('input', this.$refs.upload.fileList);
  421. },
  422. handleSuccess (res, file) {
  423. //上传完成
  424. if (res.ret === 1) {
  425. file.url = res.data.url;
  426. file.path = res.data.path;
  427. file.thumb = res.data.thumb;
  428. this.handleCallback(file);
  429. }else{
  430. this.$Modal.warning({
  431. title: this.$L('上传失败'),
  432. content: this.$L('文件 % 上传失败 %', file.name, res.msg)
  433. });
  434. this.$refs.upload.fileList.pop();
  435. }
  436. this.$emit('input', this.$refs.upload.fileList);
  437. },
  438. handleFormatError (file) {
  439. //上传类型错误
  440. this.$Modal.warning({
  441. title: this.$L('文件格式不正确'),
  442. content: this.$L('文件 % 格式不正确,请上传 jpg、jpeg、gif、png 格式的图片。', file.name)
  443. });
  444. },
  445. handleMaxSize (file) {
  446. //上传大小错误
  447. this.$Modal.warning({
  448. title: this.$L('超出文件大小限制'),
  449. content: this.$L('文件 % 太大,不能超过2M。', file.name)
  450. });
  451. },
  452. handleBeforeUpload () {
  453. //上传前判断
  454. let check = this.uploadList.length < this.maxNum;
  455. if (!check && this.uploadList.length == 1) {
  456. this.handleRemove(this.uploadList[0]);
  457. check = this.uploadList.length < this.maxNum;
  458. }
  459. if (!check) {
  460. this.$Modal.warning({
  461. title: this.$L('温馨提示'),
  462. content: this.$L('最多只能上传 % 张图片。', this.maxNum)
  463. });
  464. }
  465. return check;
  466. },
  467. handleClick() {
  468. //手动上传
  469. if (this.handleBeforeUpload()) {
  470. this.$refs.upload.handleClick()
  471. }
  472. },
  473. browsePicture(path) {
  474. //获取图片空间
  475. this.browseVisible = true;
  476. this.browseList = [];
  477. this.browseListNext = [];
  478. this.isLoading = true;
  479. $A.aAjax({
  480. url: 'imgview',
  481. data: { path: path?path:'' },
  482. beforeSend: true,
  483. complete: true,
  484. error: true,
  485. success: (res) => {
  486. this.isLoading = false;
  487. if (res.ret === 1) {
  488. let dirs = res.data['dirs'];
  489. for (let i = 0; i < dirs.length; i++) {
  490. this.browseList.push(dirs[i]);
  491. }
  492. this.browsePictureFor(res.data['files']);
  493. }else if (res.ret === -2) {
  494. this.browseVisible = false;
  495. this.$Modal.warning({ title: this.$L('温馨提示'), content: res.msg });
  496. }
  497. }
  498. });
  499. },
  500. browsePictureFor(files) {
  501. for (let o = 0; o < files.length; o++) {
  502. for (let j = 0; j < this.uploadList.length; j++) {
  503. if (this.uploadList[j]['url'] === files[o]['url']
  504. || this.uploadList[j]['url'] === files[o]['path']) {
  505. files[o]['active'] = true;
  506. break;
  507. }
  508. }
  509. if (o < 100) {
  510. this.browseList.push(files[o]);
  511. }else{
  512. this.browseListNext.push(files[o]);
  513. }
  514. }
  515. },
  516. browseItem(item) {
  517. //点击选择图片
  518. if (item.type === 'dir') {
  519. //目录
  520. this.browsePicture(item.path);
  521. }else if (item.type === 'file') {
  522. //文件
  523. if (item.active) {
  524. let fileList = this.$refs.upload.fileList;
  525. this.$refs.upload.fileList.splice(fileList.indexOf(item), 1);
  526. item.active = false;
  527. }else{
  528. if (this.maxNum === 1) {
  529. for (let i = 0; i < this.browseList.length; i++) {
  530. this.browseList[i].active = false;
  531. }
  532. this.$refs.upload.fileList = [];
  533. this.uploadList = this.$refs.upload.fileList;
  534. }
  535. let check = this.uploadList.length < this.maxNum;
  536. if (!check) {
  537. this.$Modal.warning({ title: this.$L('温馨提示'), content: this.$L('最多只能选择 % 张图片。', this.maxNum) });
  538. return;
  539. }
  540. item.active = true;
  541. item.status = 'finished';
  542. this.$refs.upload.fileList.push(item);
  543. this.uploadList = this.$refs.upload.fileList;
  544. }
  545. this.$emit('input', this.$refs.upload.fileList);
  546. }
  547. },
  548. __thumb(url) {
  549. if ($A.strExists(url, "?", false)) {
  550. return url + "&__thumb=true";
  551. }else{
  552. return url + "?__thumb=true";
  553. }
  554. },
  555. httpEnter() {
  556. this.$emit('input', this.initItems(this.httpValue));
  557. this.browseVisible = false;
  558. }
  559. }
  560. }
  561. </script>