MyPic.vue 5.8 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 15 16
							<el-upload ref="upload" :limit="10" 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">
								<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 23 24 25 26
				<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">
27
							<img :src="props.row.picUrl" alt="图片" height="100px" @click="showImageDialog(props.row.picUrl, props.$index)" style="cursor: pointer" />
28 29 30 31 32 33 34 35
						</template>
					</el-table-column>
					<el-table-column label="创建时间" width="170">
						<template slot-scope="props">
							{{ props.row.createTime | dateFormat }}
						</template>
					</el-table-column>
				</el-table>
36
				<el-dialog :visible.sync="imageDialogVisible" width="30%">
37 38 39 40 41 42 43 44 45
					<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>
46
				</el-dialog>
47

48 49 50 51 52 53 54 55
				<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'
56
import ApiService from '../../api/ApiService'
57
export default {
58 59 60
	name: 'MyPic',
	data() {
		return {
61 62 63
			formInline: {
				picType: 0,
			},
64 65
			//待上传的图片
			fileList: [],
66
			fileName:[],
67 68 69 70 71 72 73 74
			// 用户列表数据
			picList: [],
			loading: false,
			elementui_page_component_key: 0,
			currentPage: 1,
			pageSize: 6,
			total: 0,
			imageDialogVisible: false,
75
			addPicVisible: false,
76
			enlargedImageUrl: '',
77
			imageIndex: 0, // 当前展示的图片索引
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 104 105 106 107
			options: [
				{
					value: 0,
					label: '宝宝照片',
				},
				{
					value: 1,
					label: '学习照片',
				},
				{
					value: 2,
					label: '风景照片',
				},
				{
					value: 3,
					label: '美女照片',
				},
				{
					value: 99,
					label: '其他照片',
				},
			],
			picType: 0,
		}
	},
	created() {
		// 调用请求数据的方法
		this.queryPic()
	},
	methods: {
108
		async submitUpload() {
109 110 111 112 113 114 115 116 117 118 119 120
			this.$refs.upload.submit()
			this.addPicVisible = false
		},
		handleRemove(file, fileList) {
			console.log(file, fileList)
		},
		handlePreview(file) {
			console.log(file)
		},
		prepareAddPic() {
			this.addPicVisible = true
		},
121 122
		handleUploadSuccess(response, file, fileList) {
			if (response.success) {
123 124 125
				setTimeout(() => {
					this.queryPic()
				}, 1000)
126 127 128 129
			} else {
				this.$message.error('Upload failed')
			}
		},
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
		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) {
147 148
			this.enlargedImageUrl = url
			this.imageDialogVisible = true
149
			this.imageIndex = index
150 151 152
		},
		async queryPic() {
			this.loading = true
153
			const { data: res } = await ApiService.queryPic(this.currentPage, this.pageSize, this.picType)
154 155 156 157 158 159 160 161 162 163 164
			if (res.code === 200) {
				this.picList = res.result.records
				this.total = res.result.total
			}
			this.loading = false
		},
		handleCurrentChange(currentPage) {
			this.currentPage = currentPage
			this.queryPic()
		},
	},
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
}
</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;
}
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202

.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; /* 添加透明度来表示禁用状态 */
}
203
</style>