BlogImage.vue 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <template>
  2. <el-container>
  3. <el-header height="220">
  4. <h3>图片列表</h3>
  5. <el-row style="margin-top: 10px">
  6. <el-button type="success" icon="el-icon-upload" style="margin-left: 5px" @click="handleUpload">添加</el-button>
  7. </el-row>
  8. </el-header>
  9. <el-main>
  10. <el-scrollbar style="width: 100%; height: 80vh;">
  11. <el-row v-if="dataList.length !== 0">
  12. <el-col
  13. v-for="(album, index) in dataList"
  14. :key="index"
  15. :md="6"
  16. :sm="12"
  17. :xs="12"
  18. style="padding: 5px"
  19. >
  20. <el-image
  21. lazy
  22. fit="cover"
  23. class="coverImg"
  24. :src="album.url"
  25. @click="showImages(index)"
  26. />
  27. </el-col>
  28. </el-row>
  29. </el-scrollbar>
  30. <el-pagination
  31. background
  32. :small="screenWidth <= 768"
  33. layout="prev, pager, next"
  34. :page-size="pageSize"
  35. :current-page="currentPage"
  36. :total="totalSize"
  37. @current-change="handleCurrentChange"
  38. @prev-click="handleCurrentChange"
  39. @next-click="handleCurrentChange"
  40. />
  41. <el-dialog
  42. title="上传图片"
  43. append-to-body
  44. :visible.sync="addDialog"
  45. center
  46. >
  47. <template>
  48. <el-upload
  49. class="avatar-uploader"
  50. :action="imgOssUrl"
  51. :headers="imgHeaders"
  52. :data="imgData"
  53. :with-credentials="true"
  54. :show-file-list="false"
  55. :before-upload="beforeAvatarUpload"
  56. :on-success="handleAvatarSuccess"
  57. >
  58. <i class="el-icon-plus avatar-uploader-icon" />
  59. </el-upload>
  60. </template>
  61. </el-dialog>
  62. </el-main>
  63. </el-container>
  64. </template>
  65. <script>
  66. import { getBlogImageList } from '@/api/blog'
  67. import { getAccessToken } from '@/utils/auth'
  68. export default {
  69. name: 'BlogImage',
  70. data() {
  71. return {
  72. imgOssUrl: '/api/file/upload',
  73. imgHeaders: {
  74. Authorization: ''
  75. },
  76. imgData: {
  77. channelCode: 0
  78. },
  79. queryInfo: {
  80. pn: 1
  81. },
  82. // 屏幕宽度, 为了控制分页条的大小
  83. screenWidth: document.body.clientWidth,
  84. currentPage: 1,
  85. pageSize: 12,
  86. totalSize: 0,
  87. dataList: [],
  88. addDialog: false
  89. }
  90. },
  91. created() {
  92. this.imgHeaders.Authorization = 'Bearer ' + getAccessToken()
  93. const pageNumber = this.$route.query.pn
  94. if (pageNumber !== undefined && pageNumber !== null) {
  95. this.currentPage = parseInt(pageNumber)
  96. this.queryInfo.pn = parseInt(pageNumber)
  97. }
  98. document.title = '图片列表'
  99. this.getData()
  100. },
  101. methods: {
  102. handleCurrentChange(pageNumber) {
  103. this.queryInfo.pn = pageNumber
  104. this.$router.push({
  105. path: '/bg/blog/image',
  106. query: this.queryInfo
  107. })
  108. this.getData()
  109. // 回到顶部
  110. scrollTo(0, 0)
  111. },
  112. getData() {
  113. getBlogImageList(this.queryInfo).then(resp => {
  114. if (resp.code === 0) {
  115. const respData = resp.data
  116. this.dataList = respData.list
  117. this.totalSize = respData.totalSize
  118. } else {
  119. this.$message.error(resp.msg)
  120. }
  121. }).catch(error => {
  122. this.$message.error(error.message)
  123. })
  124. },
  125. handleUpload() {
  126. this.addDialog = true
  127. },
  128. // ****************************************************************************************************************
  129. beforeAvatarUpload(file) {
  130. const isJPG = file.type.startsWith('image/')
  131. if (!isJPG) {
  132. this.$message.error('只能上传图片文件')
  133. }
  134. const isLt2M = file.size / 1024 / 1024 < 2
  135. if (!isLt2M) {
  136. this.$message.error('图片文件大小不能超过 2MB!')
  137. }
  138. return isJPG && isLt2M
  139. },
  140. handleAvatarSuccess(resp, file) {
  141. this.$message.info(resp.msg)
  142. },
  143. showImages(index) {
  144. const imageUrls = []
  145. for (const item of this.dataList) {
  146. imageUrls.push(item.url)
  147. }
  148. this.$viewerApi({
  149. images: imageUrls,
  150. options: {
  151. initialViewIndex: index,
  152. movable: true,
  153. fullscreen: false,
  154. keyboard: true
  155. }
  156. })
  157. }
  158. }
  159. }
  160. </script>
  161. <style scoped>
  162. /*处于手机屏幕时*/
  163. @media screen and (max-width: 768px){
  164. .coverImg {
  165. height: 120px !important;
  166. }
  167. }
  168. .uploader-example .uploader-btn {
  169. margin-right: 4px;
  170. }
  171. .uploader-example .uploader-list {
  172. max-height: 440px;
  173. overflow: auto;
  174. overflow-x: hidden;
  175. overflow-y: auto;
  176. }
  177. .avatar-uploader .el-upload {
  178. border: 1px dashed #d9d9d9;
  179. border-radius: 6px;
  180. cursor: pointer;
  181. position: relative;
  182. overflow: hidden;
  183. }
  184. .avatar-uploader .el-upload:hover {
  185. border-color: #409EFF;
  186. }
  187. .avatar-uploader-icon {
  188. font-size: 28px;
  189. color: #8c939d;
  190. width: 256px;
  191. height: 256px;
  192. line-height: 178px;
  193. text-align: center;
  194. }
  195. .coverImg {
  196. width: 100%;
  197. height: 320px;
  198. display: block;
  199. }
  200. </style>