You need to sign in or sign up before continuing.
MyPic.vue 9.3 KB
Newer Older
1 2 3 4
<template>
	<div>
		<el-container>
			<el-main>
5 6 7 8 9 10 11 12 13
				<el-form :inline="true" :model="formInline" class="demo-form-inline">
					<el-form-item>
						<el-select size="small" v-model="picType" placeholder="请选择" @change="queryPic">
							<el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value"></el-option>
						</el-select>
					</el-form-item>
					<el-form-item>
						<el-button size="small" type="primary" @click="prepareAddPic">新增图片</el-button>
						<el-dialog title="新增图片" :visible.sync="addPicVisible" style="width: 100%">
14
							<el-upload ref="upload" :limit="30" accept=".jpg,.gif,.png,.jpeg,.txt,.pdf,.doc,.docx,.xls,.xlsx" name="files" multiple action="http://43.139.90.182:8888/picInfo/upload" :on-preview="handlePreview" :on-remove="handleRemove" :file-list="fileList" :auto-upload="false" :on-success="handleUploadSuccess">
15 16
								<el-button slot="trigger" size="small" type="primary">选择图片</el-button>
								<el-button style="margin-left: 10px" size="small" type="success" @click="submitUpload">上传到服务器</el-button>
17 18 19 20 21
							</el-upload>
						</el-dialog>
					</el-form-item>
				</el-form>

22
				<el-table border :data="picList" v-loading="loading">
23 24
					<el-table-column prop="id" label="序号" align="center" sortable></el-table-column>
					<el-table-column prop="picName" label="图片名字" align="center" show-overflow-tooltip></el-table-column>
25 26
					<el-table-column align="center">
						<template slot-scope="props">
27
							<img :src="props.row.picUrl" alt="图片" height="100px" @click="showImageDialog(props.row.picUrl, props.$index)" style="cursor: pointer" />
28 29
						</template>
					</el-table-column>
30
					<el-table-column label="创建时间" align="center">
31 32 33 34
						<template slot-scope="props">
							{{ props.row.createTime | dateFormat }}
						</template>
					</el-table-column>
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57

					<el-table-column prop="操作" label="操作" width="100px" align="center">
						<template slot-scope="props">
							<el-button size="small" type="primary" @click.prevent="prePicEdit(props.row.id, props.row.picUrl, props.row.type)">编辑</el-button>
							<el-dialog title="编辑图片信息" :visible.sync="editPicVisible">
								<el-form :model="form" class="custom-form">
									<el-form-item align="center" class="form-item">
										<img :src="form.picUrl" alt="图片" height="100px" style="cursor: pointer" />
									</el-form-item>

									<el-form-item label="" class="form-item" align="middle">
										<el-select v-model="form.type" placeholder="请选择">
											<el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value"></el-option>
										</el-select>
									</el-form-item>
								</el-form>
								<div slot="footer" class="dialog-footer">
									<el-button @click="editPicVisible = false">取 消</el-button>
									<el-button type="primary" @click="editPicConfirm">确 定</el-button>
								</div>
							</el-dialog>
						</template>
					</el-table-column>
58
				</el-table>
59
				<el-dialog :visible.sync="imageDialogVisible" width="30%">
60 61 62 63 64 65 66 67 68
					<div style="display: flex; align-items: center; justify-content: space-between">
						<button class="arrow-button" :class="{ disabled: imageIndex === 0 }" @click="showBeforeImage" :disabled="imageIndex === 0">
							<el-icon class="custom-icon" name="arrow-left"></el-icon>
						</button>
						<img :src="enlargedImageUrl" alt="放大图片" style="width: 80%" />
						<button class="arrow-button" :class="{ disabled: imageIndex === picList.length - 1 }" @click="showNextImage" :disabled="imageIndex === picList.length - 1">
							<el-icon class="custom-icon" name="arrow-right"></el-icon>
						</button>
					</div>
69
				</el-dialog>
70
				<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage" :page-sizes="[8, 10, 50, 100, 200, 400]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total"></el-pagination>
71 72 73 74 75 76 77
			</el-main>
		</el-container>
	</div>
</template>

<script>
import axios from 'axios'
78
import ApiService from '../../api/ApiService'
79
export default {
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
    name: 'MyPic',
    data() {
        return {
            form: {
                picUrl: '',
                type: 0,
            },
            formInline: {
                picType: 0,
            },
            //待上传的图片
            fileList: [],
            // 用户列表数据
            picList: [],
            loading: false,
            elementui_page_component_key: 0,
            currentPage: 1,
            pageSize: 6,
            total: 0,
            imageDialogVisible: false,
            addPicVisible: false,
            editPicVisible: false,
            enlargedImageUrl: '',
            currentRowId: null,
            imageIndex: 0, // 当前展示的图片索引
            options: [
                {
                    value: 0,
                    label: '宝宝',
                },
                {
                    value: 1,
                    label: '学习',
                },
                {
                    value: 2,
                    label: '风景',
                },
                {
                    value: 3,
                    label: '美女',
                },
                {
                    value: 4,
                    label: '猫咪',
                },
                {
                    value: 5,
                    label: '素材',
                },
                {
                    value: 6,
                    label: '动漫',
                },
                {
                    value: 99,
                    label: '其他',
                },
            ],
            picType: 0,
        }
    },
    created() {
        // 调用请求数据的方法
        this.queryPic()
    },
    methods: {
        async submitUpload() {
            this.$refs.upload.submit()
            this.addPicVisible = false
        },
        handleRemove(file, fileList) {
            console.log(file, fileList)
        },
        handlePreview(file) {
            console.log(file)
        },
        prepareAddPic() {
            this.addPicVisible = true
        },
        prePicEdit(id, picUrl, type) {
            this.currentRowId = id
            this.form.picUrl = picUrl
            this.form.type = type
            this.editPicVisible = true
        },
        handleUploadSuccess(response, file, fileList) {
            if (response.success) {
                setTimeout(() => {
                    this.queryPic()
                }, 1000)
171
                this.fileList = []
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
            } else {
                this.$message.error('Upload failed')
            }
        },
        showBeforeImage() {
            if (this.imageIndex > 0) {
                // 增加索引以显示下一张图片
                this.imageIndex--
                this.enlargedImageUrl = this.picList[this.imageIndex].picUrl
                this.imageDialogVisible = true // 打开对话框展示图片
            }
        },
        showNextImage() {
            if (this.imageIndex < this.picList.length - 1) {
                // 增加索引以显示下一张图片
                this.imageIndex++
                this.enlargedImageUrl = this.picList[this.imageIndex].picUrl
                this.imageDialogVisible = true
            }
        },
        showImageDialog(url, index) {
            this.enlargedImageUrl = url
            this.imageDialogVisible = true
            this.imageIndex = index
        },
        async queryPic() {
            this.loading = true
            const { data: res } = await ApiService.queryPic(this.currentPage, this.pageSize, this.picType)
            if (res.code === 200) {
                this.picList = res.result.records
                this.total = res.result.total
            }
            this.loading = false
        },
        async editPicConfirm() {
            try {
                this.loading = true
                this.editPicVisible = false
                const { data: res } = await ApiService.editPicConfirm(this.currentRowId, this.form.type)
                if (res.code === 200) {
                    this.form.type = 0
                    this.form.picUrl = null
                    this.currentRowId = null
                    this.queryPic()
                } else {
                    console.error('Received non-200 status code', res)
                }
            } catch (error) {
                console.error('An error occurred while adding the questionName:', error)
                // 异常处理逻辑
            } finally {
                this.loading = false
            }
        },

        handleCurrentChange(currentPage) {
            this.currentPage = currentPage
            this.queryPic()
        },
        handleSizeChange(currentSize) {
            this.pageSize = currentSize
            this.queryPic()
        },
    },
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
}
</script>

<style lang="less" scoped>
.el-header {
	background-color: #b3c0d1;
	color: #333;
	line-height: 60px;
}

.el-aside {
	color: #333;
}

.pagination {
	margin-top: 16px;
	text-align: right;
}
254 255 256 257 258 259 260 261 262 263 264

.arrow-button {
	display: flex;
	align-items: center;
	justify-content: center;
	border: none;
	background-color: transparent;
	cursor: pointer;
}

.custom-icon {
265 266 267
	font-size: 24px;
	color: #ff9900;
	margin-right: 6px;
268 269 270
}

.arrow-button.disabled {
271 272
	pointer-events: none;
	opacity: 0.1;
273
}
274
</style>