image-libs.vue 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <template>
  2. <div>
  3. <el-drawer
  4. title="图片库"
  5. :visible.sync="dialogVisible"
  6. direction="ltr"
  7. custom-class="drawerClass">
  8. <el-tabs tab-position="top" stretch>
  9. <el-tab-pane label="历史图片">
  10. <!-- v-loading="loading" -->
  11. <div style="column-count: 3;column-gap: 0;">
  12. <div v-for="(item,index) in externalImagesList" :key="index" >
  13. <el-image
  14. style="height: 100%;"
  15. :src="item.url" @click="handleImageClick(item.url)"></el-image>
  16. </div>
  17. </div>
  18. </el-tab-pane>
  19. <!-- :tab-click="getMyImage()" -->
  20. <el-tab-pane label="我的图片" >
  21. <!-- todo 可调用外部图片 -->
  22. <!-- https://material-api.eqxiu.com/m/material/category/template/list?categoryId=898438&searchCode=94231&sortBy=0&fee=&pageNo=5&pageSize=20 -->
  23. <!--tmbPath https://res1.eqh5.com/store/bc7aeed3f426ab1d116139b834a7483b.png -->
  24. <div class="image-lib-inner">
  25. <div class="image-lib-btn">
  26. <el-upload
  27. action="/filemanage/upload"
  28. accept="jpg,png,gif"
  29. name="upload"
  30. :data="{
  31. type: 'upload'
  32. }"
  33. :show-file-list="false"
  34. :on-success="uploadSuccess"
  35. :on-error="uploadError"
  36. >
  37. <el-button size="small" type="primary">点击上传</el-button>
  38. <span slot="tip" class="el-upload__tip marginL20">只能上传jpg/png/gif文件,且不超过2M</span>
  39. </el-upload>
  40. </div>
  41. <el-scrollbar class="image-list-wrapper scroll-wrapper" v-if="imageList.length">
  42. <div class="image-item" v-for="(item, index) in imageList" :key="index" @click="handleImageClick(item.url)">
  43. <img :src="item.url" alt="">
  44. </div>
  45. </el-scrollbar>
  46. <div class="padding60 text-center gray" v-else>暂无数据</div>
  47. </div>
  48. </el-tab-pane>
  49. </el-tabs>
  50. </el-drawer>
  51. </div>
  52. </template>
  53. <script>
  54. import $bus from '@client/eventBus'
  55. export default {
  56. name: "image-libs",
  57. data() {
  58. return {
  59. dialogVisible: false,
  60. uploading: false,
  61. hasLoadData: false,
  62. imageList: [],
  63. selectId: '',
  64. externalImagesList:[],
  65. loading:true
  66. }
  67. },
  68. created() {
  69. $bus.$on('show-select-image', selectId => {
  70. this.dialogVisible = true;
  71. this.selectId = selectId;
  72. })
  73. this.$nextTick(() => {
  74. this.getExternalImages()//todo:这边还需要处理资源请求失败情况
  75. })
  76. },
  77. watch: {
  78. dialogVisible(val) {
  79. if (val && !this.uploading) {
  80. this.getMyImages()
  81. }
  82. }
  83. },
  84. methods: {
  85. getMyImage(){
  86. this.getMyImages()
  87. },
  88. uploadSuccess (res, file, fileList) {
  89. console.log(res, file, fileList)
  90. if (res.url) {
  91. this.uploadPsd(process.env.VUE_APP_UPLOAD_IMAGE_BASE + res.url)
  92. } else {
  93. this.uploadError('请检查接口')
  94. }
  95. },
  96. uploadError (err) {
  97. this.$message.error('上传失败', err)
  98. },
  99. beforeUpload(file) {
  100. if (file.size > 1 * 1024 * 1024) {
  101. this.$message.error('psd文件不能超过1M!')
  102. return;
  103. }
  104. let temp1 = file.name.split('.')
  105. let temp = temp1[temp1.length - 1]
  106. if (!['jpg', 'png', 'gif'].includes(temp)) {
  107. this.$message.error('请上传jpg/png/gif文件')
  108. return false;
  109. }
  110. this.uploadPsd(file);
  111. return false;
  112. },
  113. uploadPsd(file) {
  114. this.uploading = true;
  115. this.$API.uploadImage({
  116. fileUrl: file
  117. }).then(res => {
  118. this.uploading = false;
  119. this.imageList.splice(0, 0, res.body)
  120. }).catch(() => {
  121. this.uploading = true;
  122. })
  123. },
  124. getMyImages() {
  125. this.hasLoadData = true;
  126. this.$API.getMyImages().then(res => {
  127. this.imageList = res?.body || [];
  128. })
  129. },
  130. getExternalImages(){
  131. this.$API.getExternalImages().then(res => {
  132. if (res?.body?.data) {
  133. this.externalImagesList = res.body.data || []
  134. }
  135. })
  136. },
  137. /**
  138. * 点击图片
  139. * @param url
  140. */
  141. handleImageClick(url) {
  142. $bus.$emit('select-image', this.selectId, url)
  143. this.dialogVisible = false;
  144. }
  145. }
  146. }
  147. </script>
  148. <style lang="scss" scoped>
  149. .image-list-wrapper{
  150. height: 400px;
  151. padding-top: 20px;
  152. .image-item{
  153. width: 120px;
  154. height: 120px;
  155. float: left;
  156. background: #eee 50%/contain no-repeat;
  157. cursor: pointer;
  158. justify-content:center;
  159. align-items:center;
  160. display: flex;
  161. transition: all 0.28s;
  162. margin: 5px;
  163. &:hover{
  164. box-shadow: 0 0 16px 0 rgba(0,0,0,.16);
  165. transform: translate3d(0,-2px,0);
  166. }
  167. img{
  168. display: inline-block;
  169. max-width: 100%;
  170. max-height: 100%;
  171. }
  172. }
  173. }
  174. </style>
  175. <style lang="scss">
  176. .components-image-libs-wrapper{
  177. .el-dialog__body{
  178. padding: 0 20px 20px;
  179. }
  180. }
  181. .drawerClass{
  182. padding: 0 10px 0 15px;
  183. overflow: auto;
  184. /*更改滚动条样式*/
  185. &::-webkit-scrollbar {
  186. width: 7px;
  187. height: 7px;
  188. }
  189. &::-webkit-scrollbar-thumb {
  190. border-radius: 10px;
  191. background: rgba(152, 155, 163, 0.5);
  192. }
  193. .el-drawer__header{
  194. margin-bottom: 10px;
  195. text-align: center;
  196. font-weight: bold;
  197. }
  198. }
  199. </style>