代码示例,使用Element,Koa,SQLyog创建图片管理,包括增加,删除,图片列表分页的页面
时间: 2024-06-10 13:05:35 浏览: 166
图片管理工具代码
好的,以下是使用Element,Koa和SQLyog创建图片管理页面的代码示例:
1. 建立数据库表:
CREATE TABLE `images` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(100) NOT NULL DEFAULT '',
`url` varchar(100) NOT NULL DEFAULT '',
`description` varchar(255) DEFAULT NULL,
`created_time` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
2. 创建Koa应用,引入相关中间件:
const Koa = require('koa');
const koaBody = require('koa-body');
const koaStatic = require('koa-static');
const koaRouter = require('@koa/router');
const cors = require('@koa/cors');
const app = new Koa();
const router = new koaRouter();
app.use(cors()) // 跨域处理
.use(koaBody()) // 解析请求体
.use(koaStatic(__dirname + '/public')); // 加载静态资源
3. 编写路由接口,实现图片列表分页、添加、删除等功能:
const mysql = require('mysql');
const db = mysql.createConnection({
host: 'localhost',
port: 3306,
user: 'root',
password: 'password',
database: 'test'
});
// 图片列表
router.get('/api/images', async (ctx) => {
const { page, limit } = ctx.query;
const offset = (page - 1) * limit;
const [rows] = await db.query(`SELECT * FROM images ORDER BY created_time DESC LIMIT ?,?`, [offset, limit]);
const [count] = await db.query(`SELECT COUNT(*) AS count FROM images`);
ctx.body = {
code: 200,
data: {
images: rows,
total: count[0].count
}
}
});
// 添加图片
router.post('/api/images', async (ctx) => {
const { title, url, description } = ctx.request.body;
const now = new Date();
const result = await db.query(`INSERT INTO images (title,url,description,created_time) VALUES (?,?,?,?)`, [title, url, description, now]);
ctx.body = {
code: 200,
message: '添加成功!'
}
});
// 删除图片
router.delete('/api/images/:id', async (ctx) => {
const { id } = ctx.params;
await db.query(`DELETE FROM images WHERE id=?`, [id]);
ctx.body = {
code: 200,
message: '删除成功!'
}
});
4. 编写页面代码,使用Element组件库和axios实现界面和接口的交互:
<template>
<div>
<el-button @click="addImage">添加图片</el-button>
<el-table :data="tableData" stripe style="width: 100%">
<el-table-column prop="id" label="ID" width="80"></el-table-column>
<el-table-column prop="title" label="标题"></el-table-column>
<el-table-column prop="url" label="地址"></el-table-column>
<el-table-column prop="description" label="描述"></el-table-column>
<el-table-column prop="created_time" label="创建时间"></el-table-column>
<el-table-column label="操作" width="120">
<template slot-scope="scope">
<el-button type="danger" size="mini" @click="deleteImage(scope.row)">删除</el-button>
</template>
</el-table-column>
</el-table>
<el-pagination @current-change="handleCurrentChange" :current-page="currentPage" :page-size="pageSize" :total="total"></el-pagination>
</div>
</template>
<script>
import axios from 'axios';
export default {
name: 'ImageList',
data() {
return {
currentPage: 1,
pageSize: 10,
total: 0,
tableData: []
}
},
mounted() {
this.getImages();
},
methods: {
// 获取图片列表
async getImages() {
const { currentPage, pageSize } = this;
const response = await axios.get(`/api/images?page=${currentPage}&limit=${pageSize}`);
if (response.status === 200) {
this.tableData = response.data.data.images;
this.total = response.data.data.total;
}
},
// 添加图片
async addImage() {
const form = {
title: '图片标题',
url: 'http://example.com/image.jpg',
description: '图片描述'
};
const response = await axios.post('/api/images', form);
if (response.status === 200) {
this.$message.success('添加成功!');
this.getImages();
}
},
// 删除图片
async deleteImage(row) {
const response = await this.$confirm('确定删除该图片吗?', '提示', { type: 'warning' });
if (response === 'confirm') {
await axios.delete(`/api/images/${row.id}`);
this.$message.success('删除成功!');
this.getImages();
}
},
// 页面跳转
handleCurrentChange(page) {
this.currentPage = page;
this.getImages();
}
}
}
</script>
5. 在SQLyog中添加图片数据,运行应用程序,访问页面即可实现图片管理功能。
以上是示例代码,可以根据实际情况进行调整和修改,希望对你有所帮助!
阅读全文