| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 |
- <template>
- <el-container>
- <el-header height="220">
- <h3>图片列表</h3>
- <el-row style="margin-top: 10px">
- <el-button type="success" icon="el-icon-upload" style="margin-left: 5px" @click="handleUpload">添加</el-button>
- </el-row>
- </el-header>
- <el-main>
- <el-scrollbar style="width: 100%; height: 80vh;">
- <el-row v-if="dataList.length !== 0">
- <el-col
- v-for="(album, index) in dataList"
- :key="index"
- :md="6"
- :sm="12"
- :xs="12"
- style="padding: 5px"
- >
- <el-image
- lazy
- fit="cover"
- class="coverImg"
- :src="album.url"
- @click="showImages(index)"
- />
- </el-col>
- </el-row>
- </el-scrollbar>
- <el-pagination
- background
- :small="screenWidth <= 768"
- layout="prev, pager, next"
- :page-size="pageSize"
- :current-page="currentPage"
- :total="totalSize"
- @current-change="handleCurrentChange"
- @prev-click="handleCurrentChange"
- @next-click="handleCurrentChange"
- />
- <el-dialog
- title="上传图片"
- append-to-body
- :visible.sync="addDialog"
- center
- >
- <template>
- <el-upload
- class="avatar-uploader"
- :action="imgOssUrl"
- :headers="imgHeaders"
- :data="imgData"
- :with-credentials="true"
- :show-file-list="false"
- :before-upload="beforeAvatarUpload"
- :on-success="handleAvatarSuccess"
- >
- <i class="el-icon-plus avatar-uploader-icon" />
- </el-upload>
- </template>
- </el-dialog>
- </el-main>
- </el-container>
- </template>
- <script>
- import { getBlogImageList } from '@/api/blog'
- import { getAccessToken } from '@/utils/auth'
- export default {
- name: 'BlogImage',
- data() {
- return {
- imgOssUrl: '/api/file/upload',
- imgHeaders: {
- Authorization: ''
- },
- imgData: {
- channelCode: 0
- },
- queryInfo: {
- pn: 1
- },
- // 屏幕宽度, 为了控制分页条的大小
- screenWidth: document.body.clientWidth,
- currentPage: 1,
- pageSize: 12,
- totalSize: 0,
- dataList: [],
- addDialog: false
- }
- },
- created() {
- this.imgHeaders.Authorization = 'Bearer ' + getAccessToken()
- const pageNumber = this.$route.query.pn
- if (pageNumber !== undefined && pageNumber !== null) {
- this.currentPage = parseInt(pageNumber)
- this.queryInfo.pn = parseInt(pageNumber)
- }
- document.title = '图片列表'
- this.getData()
- },
- methods: {
- handleCurrentChange(pageNumber) {
- this.queryInfo.pn = pageNumber
- this.$router.push({
- path: '/bg/blog/image',
- query: this.queryInfo
- })
- this.getData()
- // 回到顶部
- scrollTo(0, 0)
- },
- getData() {
- getBlogImageList(this.queryInfo).then(resp => {
- if (resp.code === 0) {
- const respData = resp.data
- this.dataList = respData.list
- this.totalSize = respData.totalSize
- } else {
- this.$message.error(resp.msg)
- }
- }).catch(error => {
- this.$message.error(error.message)
- })
- },
- handleUpload() {
- this.addDialog = true
- },
- // ****************************************************************************************************************
- beforeAvatarUpload(file) {
- const isJPG = file.type.startsWith('image/')
- if (!isJPG) {
- this.$message.error('只能上传图片文件')
- }
- const isLt2M = file.size / 1024 / 1024 < 2
- if (!isLt2M) {
- this.$message.error('图片文件大小不能超过 2MB!')
- }
- return isJPG && isLt2M
- },
- handleAvatarSuccess(resp, file) {
- this.$message.info(resp.msg)
- },
- showImages(index) {
- const imageUrls = []
- for (const item of this.dataList) {
- imageUrls.push(item.url)
- }
- this.$viewerApi({
- images: imageUrls,
- options: {
- initialViewIndex: index,
- movable: true,
- fullscreen: false,
- keyboard: true
- }
- })
- }
- }
- }
- </script>
- <style scoped>
- /*处于手机屏幕时*/
- @media screen and (max-width: 768px){
- .coverImg {
- height: 120px !important;
- }
- }
- .uploader-example .uploader-btn {
- margin-right: 4px;
- }
- .uploader-example .uploader-list {
- max-height: 440px;
- overflow: auto;
- overflow-x: hidden;
- overflow-y: auto;
- }
- .avatar-uploader .el-upload {
- border: 1px dashed #d9d9d9;
- border-radius: 6px;
- cursor: pointer;
- position: relative;
- overflow: hidden;
- }
- .avatar-uploader .el-upload:hover {
- border-color: #409EFF;
- }
- .avatar-uploader-icon {
- font-size: 28px;
- color: #8c939d;
- width: 256px;
- height: 256px;
- line-height: 178px;
- text-align: center;
- }
- .coverImg {
- width: 100%;
- height: 320px;
- display: block;
- }
- </style>
|