MyPic.vue 6.0 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 class="upload-demo" ref="upload" 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 17 18 19 20 21 22
								<el-button slot="trigger" size="small" type="primary">Select File</el-button>
								<el-button style="margin-left: 10px" size="small" type="success" @click="submitUpload">Upload to Server</el-button>
								<div slot="tip" class="el-upload__tip">Only jpg/png files are allowed, and they should not exceed 500kb.</div>
							</el-upload>
						</el-dialog>
					</el-form-item>
				</el-form>

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

49 50 51 52 53 54 55 56 57
				<el-pagination class="pagination" background :key="elementui_page_component_key" :current-page.sync="currentPage" :page-size="pageSize" :total="total" @current-change="handleCurrentChange"></el-pagination>
			</el-main>
		</el-container>
	</div>
</template>

<script>
import axios from 'axios'
export default {
58 59 60
	name: 'MyPic',
	data() {
		return {
61 62
			//待上传的图片
			fileList: [],
63 64 65 66 67 68 69 70
			// 用户列表数据
			picList: [],
			loading: false,
			elementui_page_component_key: 0,
			currentPage: 1,
			pageSize: 6,
			total: 0,
			imageDialogVisible: false,
71
			addPicVisible: false,
72
			enlargedImageUrl: '',
73
			imageIndex: 0, // 当前展示的图片索引
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
			options: [
				{
					value: 0,
					label: '宝宝照片',
				},
				{
					value: 1,
					label: '学习照片',
				},
				{
					value: 2,
					label: '风景照片',
				},
				{
					value: 3,
					label: '美女照片',
				},
				{
					value: 99,
					label: '其他照片',
				},
			],
			picType: 0,
		}
	},
	created() {
		// 调用请求数据的方法
		this.queryPic()
	},
	methods: {
104 105 106 107 108 109 110 111 112 113 114 115 116
		submitUpload() {
			this.$refs.upload.submit()
			this.addPicVisible = false
		},
		handleRemove(file, fileList) {
			console.log(file, fileList)
		},
		handlePreview(file) {
			console.log(file)
		},
		prepareAddPic() {
			this.addPicVisible = true
		},
117 118 119 120 121 122 123 124 125 126 127 128
		handleUploadSuccess(response, file, fileList) {
			// Handle the upload success logic here
			// Assuming the response contains the necessary information
			// to determine whether the upload was successful
			if (response.success) {
				// Call the queryPic interface here
				this.queryPic()
			} else {
				// Handle the case where the upload was not successful
				this.$message.error('Upload failed')
			}
		},
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
		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) {
146 147
			this.enlargedImageUrl = url
			this.imageDialogVisible = true
148
			this.imageIndex = index
149 150 151
		},
		async queryPic() {
			this.loading = true
152
			const { data: res } = await axios.get('http://43.139.90.182:8888/picInfo/page', {
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
				params: {
					page: this.currentPage,
					pageSize: this.pageSize,
					picType: this.picType,
				},
			})
			if (res.code === 200) {
				this.picList = res.result.records
				this.total = res.result.total
			}
			this.loading = false
		},
		handleCurrentChange(currentPage) {
			this.currentPage = currentPage
			this.queryPic()
		},
	},
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
}
</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;
}
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207

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

.custom-icon {
	font-size: 24px; /* 调整图标大小 */
	color: #ff9900; /* 调整图标颜色 */
	margin-right: 6px; /* 调整图标与文字之间的间距 */
}

.arrow-button.disabled {
	pointer-events: none; /* 禁用点击事件 */
	opacity: 0.1; /* 添加透明度来表示禁用状态 */
}
208
</style>