MyPic.vue 10.8 KB
Newer Older
1 2 3 4
<template>
	<div>
		<el-container>
			<el-main>
5 6
				<el-form :inline="true" :model="formInline" class="demo-form-inline">
					<el-form-item>
7 8 9 10 11
						<el-input size="small" clearable v-model="formInline.picName" placeholder="请输入图片名称" @keydown.enter.native="queryPic"></el-input>
					</el-form-item>
					<el-form-item>
						<el-select size="small" clearable v-model="formInline.type" placeholder="请选择" @change="queryPic">
							<el-option v-for="item in options" :key="item" :label="item" :value="item"></el-option>
12 13
						</el-select>
					</el-form-item>
14 15 16
					<el-form-item>
						<el-button size="small" type="primary" @click="queryPic">查询</el-button>
					</el-form-item>
17 18 19
					<el-form-item>
						<el-button size="small" type="primary" @click="prepareAddPic">新增图片</el-button>
						<el-dialog title="新增图片" :visible.sync="addPicVisible" style="width: 100%">
20 21 22 23
							<el-select size="small" v-model="formInline.type" placeholder="请选择图片类型">
								<el-option v-for="item in options" :key="item" :label="item" :value="item"></el-option>
							</el-select>
							<el-upload ref="upload" :limit="30" accept=".jpg,.gif,.png,.jpeg,.txt,.pdf,.doc,.docx,.xls,.xlsx" name="files" multiple :action="getUploadAction()" :on-preview="handlePreview" :on-remove="handleRemove" :file-list="fileList" :auto-upload="false" :on-success="handleUploadSuccess">
24 25
								<el-button slot="trigger" size="small" type="primary">选择图片</el-button>
								<el-button style="margin-left: 10px" size="small" type="success" @click="submitUpload">上传到服务器</el-button>
26 27 28 29
							</el-upload>
						</el-dialog>
					</el-form-item>

30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
					<el-form-item>
						<el-button size="small" type="primary" @click="prepareBatchUpdatePic">批量修改</el-button>
						<el-dialog title="批量修改" :visible.sync="batchUpdatePicVisible" style="width: 100%">
							<el-form :model="form" class="custom-form">
								<el-form-item label="图片类型" :label-width="formLabelWidth" class="form-item">
									<el-select size="small" v-model="form.type" placeholder="请选择图片类型">
										<el-option v-for="item in options" :key="item" :label="item" :value="item"></el-option>
									</el-select>
								</el-form-item>
							</el-form>
							<div slot="footer" class="dialog-footer">
								<el-button @click="batchUpdatePicVisible = false">取 消</el-button>
								<el-button type="primary" @click="batchUpdatePic()">确 定</el-button>
							</div>
						</el-dialog>
					</el-form-item>
				</el-form>
				<el-table border ref="multipleTable" v-loading="loading" :data="picList" tooltip-effect="dark" style="width: 100%" @selection-change="handleSelectionChange">
					<el-table-column type="selection" width="55"></el-table-column>
49 50
					<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>
51
					<el-table-column prop="type" label="图片类型" align="center" show-overflow-tooltip></el-table-column>
52 53
					<el-table-column align="center">
						<template slot-scope="props">
54
							<img :src="props.row.picUrl" alt="图片" height="100px" @click="showImageDialog(props.row.picUrl, props.$index)" style="cursor: pointer" />
55 56
						</template>
					</el-table-column>
57

58
					<el-table-column label="创建时间" align="center">
59 60 61 62
						<template slot-scope="props">
							{{ props.row.createTime | dateFormat }}
						</template>
					</el-table-column>
63 64 65

					<el-table-column prop="操作" label="操作" width="100px" align="center">
						<template slot-scope="props">
66
							<el-button size="small" type="primary" @click.prevent="prePicEdit(props.row.id, props.row.picName, props.row.picUrl, props.row.type)">编辑</el-button>
67 68 69 70 71 72 73
							<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="请选择">
74
											<el-option v-for="item in options" :key="item" :label="item" :value="item"></el-option>
75 76
										</el-select>
									</el-form-item>
77 78 79
									<el-form-item>
										<el-input clearable v-model="form.picName" placeholder="请输入图片名称"></el-input>
									</el-form-item>
80 81 82 83 84 85 86 87
								</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>
88
				</el-table>
89
				<el-dialog :visible.sync="imageDialogVisible" width="30%">
90 91 92 93 94 95 96 97 98
					<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>
99
				</el-dialog>
100
				<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>
101 102 103 104 105 106 107
			</el-main>
		</el-container>
	</div>
</template>

<script>
import axios from 'axios'
108
import ApiService from '../../api/ApiService'
109
export default {
110 111 112
	name: 'MyPic',
	data() {
		return {
113
			fileUrl: 'http://localhost:80/picInfo/upload',
114
			form: {
115
				picName: '',
116
				picUrl: '',
117
				type: '',
118 119
			},
			formInline: {
120 121
				type: '',
				picName: '',
122 123 124
			},
			//待上传的图片
			fileList: [],
125
			multipleSelection: [],
126 127 128 129 130 131 132
			// 用户列表数据
			picList: [],
			loading: false,
			elementui_page_component_key: 0,
			currentPage: 1,
			pageSize: 6,
			total: 0,
133
			formLabelWidth: '120px',
134
			imageDialogVisible: false,
135
			batchUpdatePicVisible: false,
136 137 138 139 140
			addPicVisible: false,
			editPicVisible: false,
			enlargedImageUrl: '',
			currentRowId: null,
			imageIndex: 0, // 当前展示的图片索引
141
			options: ['全部', '宝宝', '学习', '风景', '美女', '猫咪', '素材', '动漫', '其他'],
142 143
		}
	},
144 145 146 147 148 149 150 151 152 153 154 155 156 157
	watch: {
		formInline: {
			handler(newVal, oldVal) {
				this.currentPage = 1
				localStorage.setItem('picPage', this.currentPage)
				this.queryPic()
			},
			deep: true,
		},
		// 监听currentPage的变化,将新值保存到localStorage中
		currentPage(newPage) {
			localStorage.setItem('picPage', newPage.toString())
		},
	},
158 159 160 161 162
	created() {
		// 调用请求数据的方法
		this.queryPic()
	},
	methods: {
163 164 165 166 167 168 169
		prepareBatchUpdatePic() {
			this.form.type = ''
			this.batchUpdatePicVisible = true
		},
		getUploadAction() {
			return this.fileUrl + '?type=' + this.formInline.type
		},
170 171 172 173 174 175 176 177 178 179 180 181 182
		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
		},
183
		prePicEdit(id, picName, picUrl, type) {
184
			this.currentRowId = id
185
			this.form.picName = picName
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
			this.form.picUrl = picUrl
			this.form.type = type
			this.editPicVisible = true
		},
		handleUploadSuccess(response, file, fileList) {
			if (response.success) {
				setTimeout(() => {
					this.queryPic()
				}, 1000)
				this.fileList = []
			} 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
223
			const { data: res } = await ApiService.queryPic(this.currentPage, this.pageSize, this.formInline)
224 225 226 227 228 229 230 231 232 233
			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
234
				const { data: res } = await ApiService.editPicConfirm(this.currentRowId, this.form.type, this.form.picName)
235
				if (res.code === 200) {
236 237
					this.form.picName = ''
					this.form.type = ''
238 239 240 241 242 243 244 245 246 247 248 249 250
					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
			}
		},
251

252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
		async batchUpdatePic() {
			this.loading = true
			this.batchUpdatePicVisible = false
			if (this.multipleSelection && this.multipleSelection.length === 0) {
				this.loading = false
				this.errorMsg('请选择需要处理项')
			} else {
				let ids = this.multipleSelection.map((item) => item.id)
				const { data: res } = await ApiService.batchUpdatePic(ids, this.form.type)
				if (res.code === 200) {
					this.multipleSelection = []
					this.form.type = ''
					this.queryPic()
				} else {
					console.error('Received non-200 status code', res)
				}
			}
			this.loading = false
		},

272 273 274 275 276 277 278 279
		handleCurrentChange(currentPage) {
			this.currentPage = currentPage
			this.queryPic()
		},
		handleSizeChange(currentSize) {
			this.pageSize = currentSize
			this.queryPic()
		},
280 281 282 283 284 285 286 287 288 289
		handleSelectionChange(val) {
			this.multipleSelection = val
		},
		errorMsg(msg) {
			this.$message({
				showClose: true,
				message: msg,
				type: 'error',
			})
		},
290
	},
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
}
</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;
}
309 310 311 312 313 314 315 316 317 318 319

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

.custom-icon {
320 321 322
	font-size: 24px;
	color: #ff9900;
	margin-right: 6px;
323 324 325
}

.arrow-button.disabled {
326 327
	pointer-events: none;
	opacity: 0.1;
328
}
329
</style>