11. 开发论坛帖子管理功能
SQL
CREATE TABLE `article` (
`id` int NOT NULL AUTO_INCREMENT COMMENT 'ID',
`img` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '封面',
`title` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '标题',
`description` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '简介',
`content` longtext COLLATE utf8mb4_unicode_ci COMMENT '内容',
`time` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '发布时间',
`user_id` int DEFAULT NULL COMMENT '发布人ID',
`status` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '审核状态',
`reason` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '审核理由',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='帖子信息';
开发后端接口
Article.java
package com.example.entity;
public class Article {
/**ID */
private Integer id;
/**封面 */
private String img;
/**标题 */
private String title;
/**简介 */
private String description;
/**内容 */
private String content;
/**发布时间 */
private String time;
/**发布人ID */
private Integer userId;
private String userName;
/**审核状态 */
private String status;
/**审核理由 */
private String reason;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getImg() {
return img;
}
public void setImg(String img) {
this.img = img;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getReason() {
return reason;
}
public void setReason(String reason) {
this.reason = reason;
}
}
ArticleMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.mapper.ArticleMapper">
<select id="selectAll" resultType="com.example.entity.Article">
select article.*, user.name as userName from `article`
left join user on article.user_id = user.id
<where>
<if test="title != null"> and article.title like concat('%', #{title}, '%')</if>
<if test="userId != null"> and article.user_id = #{userId}</if>
</where>
order by article.id desc
</select>
<insert id="insert" parameterType="com.example.entity.Article" useGeneratedKeys="true">
insert into `article`
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">id,</if>
<if test="img != null">img,</if>
<if test="title != null">title,</if>
<if test="description != null">description,</if>
<if test="content != null">content,</if>
<if test="time != null">time,</if>
<if test="userId != null">user_id,</if>
<if test="status != null">status,</if>
<if test="reason != null">reason,</if>
</trim>
values
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">#{id},</if>
<if test="img != null">#{img},</if>
<if test="title != null">#{title},</if>
<if test="description != null">#{description},</if>
<if test="content != null">#{content},</if>
<if test="time != null">#{time},</if>
<if test="userId != null">#{userId},</if>
<if test="status != null">#{status},</if>
<if test="reason != null">#{reason},</if>
</trim>
</insert>
<update id="updateById" parameterType="com.example.entity.Article">
update `article`
<set>
<if test="id != null">
id = #{id},
</if>
<if test="img != null">
img = #{img},
</if>
<if test="title != null">
title = #{title},
</if>
<if test="description != null">
description = #{description},
</if>
<if test="content != null">
content = #{content},
</if>
<if test="time != null">
time = #{time},
</if>
<if test="userId != null">
user_id = #{userId},
</if>
<if test="status != null">
status = #{status},
</if>
<if test="reason != null">
reason = #{reason},
</if>
</set>
where id = #{id}
</update>
</mapper>
开发前端页面
Article.vue
<template>
<div>
<div class="card" style="margin-bottom: 5px;">
<el-input v-model="data.title" style="width: 300px; margin-right: 10px" placeholder="请输入标题查询"></el-input>
<el-button type="primary" @click="load">查询</el-button>
<el-button type="info" style="margin: 0 10px" @click="reset">重置</el-button>
</div>
<div class="card" style="margin-bottom: 5px">
<div style="margin-bottom: 10px" v-if="data.user.role === '普通用户'">
<el-button type="primary" @click="handleAdd">发布新帖</el-button>
</div>
<el-table :data="data.tableData" stripe>
<el-table-column prop="img" label="封面">
<template #default="scope">
<el-image style="width: 80px; height: 60px; display: block; border-radius: 5px" :src="scope.row.img" :preview-src-list="[scope.row.img]" preview-teleported></el-image>
</template>
</el-table-column>
<el-table-column prop="title" label="标题" show-overflow-tooltip></el-table-column>
<el-table-column prop="description" label="简介" show-overflow-tooltip></el-table-column>
<el-table-column label="内容">
<template #default="scope">
<el-button type="primary" @click="view(scope.row.content)">查看内容</el-button>
</template>
</el-table-column>
<el-table-column prop="time" label="发布时间"></el-table-column>
<el-table-column prop="userName" label="发布人"></el-table-column>
<el-table-column prop="status" label="审核状态">
<template #default="scope">
<span style="color: orange" v-if="scope.row.status === '待审核'">待审核</span>
<b style="color: #01a601" v-if="scope.row.status === '通过'">通过</b>
<b style="color: red" v-if="scope.row.status === '拒绝'">拒绝</b>
</template>
</el-table-column>
<el-table-column prop="reason" label="审核理由"></el-table-column>
<el-table-column label="操作" align="center" width="160">
<template #default="scope">
<el-button type="primary" @click="handleEdit(scope.row)" v-if="data.user.role === '管理员'">审核</el-button>
<el-button type="primary" @click="handleEdit(scope.row)" v-if="data.user.role === '普通用户'">编辑</el-button>
<el-button type="danger" @click="handleDelete(scope.row.id)">删除</el-button>
</template>
</el-table-column>
</el-table>
</div>
<div class="card">
<el-pagination @current-change="load" background layout="total, prev, pager, next" v-model:page-size="data.pageSize" v-model:current-page="data.pageNum" :total="data.total"/>
</div>
<el-dialog title="内容" v-model="data.viewVisible" width="50%" :close-on-click-modal="false" destroy-on-close>
<div class="editor-content-view" style="padding: 20px" v-html="data.content"></div>
<template #footer>
<span class="dialog-footer">
<el-button @click="data.viewVisible = false">关 闭</el-button>
</span>
</template>
</el-dialog>
<el-dialog title="帖子信息" width="40%" v-model="data.formVisible" :close-on-click-modal="false" destroy-on-close>
<el-form ref="formRef" :model="data.form" :rules="data.rules" label-width="100px" style="padding-right: 50px">
<div v-if="data.user.role === '普通用户'">
<el-form-item label="封面" prop="img">
<el-upload
:action="baseUrl + '/files/upload'"
:headers="{ 'token': data.user.token }"
:on-success="handleFileUpload"
list-type="picture"
:show-file-list="false"
>
<img v-if="data.form.img" style="width: 200px; height: 100px; border-radius: 5px; " :src="data.form.img" alt="">
<div v-else style="width: 200px; height: 150px; border-radius: 5px; border: 1px dashed #ccc; display: flex; align-items: center; justify-content: center">上传图片</div>
</el-upload>
</el-form-item>
<el-form-item label="标题" prop="title">
<el-input placeholder="请输入标题" v-model="data.form.title" autocomplete="off" />
</el-form-item>
<el-form-item label="简介" prop="description">
<el-input type="textarea" :rows="3" maxlength="200" placeholder="请输入简介" v-model="data.form.description" autocomplete="off" />
</el-form-item>
<el-form-item label="内容" prop="content">
<div style="border: 1px solid #ccc; width: 100%">
<Toolbar
style="border-bottom: 1px solid #ccc"
:editor="editorRef"
:mode="mode"
/>
<Editor
style="height: 500px; overflow-y: hidden;"
v-model="data.form.content"
:mode="mode"
:defaultConfig="editorConfig"
@onCreated="handleCreated"
/>
</div>
</el-form-item>
</div>
<div v-if="data.user.role === '管理员'">
<el-form-item label="审核状态" prop="status">
<el-radio-group v-model="data.form.status">
<el-radio-button label="通过" value="通过"></el-radio-button>
<el-radio-button label="拒绝" value="拒绝"></el-radio-button>
</el-radio-group>
</el-form-item>
<el-form-item label="审核理由" prop="reason">
<el-input type="textarea" v-model="data.form.reason" placeholder="请输入审核理由"></el-input>
</el-form-item>
</div>
</el-form>
<template #footer>
<span class="dialog-footer">
<el-button @click="data.formVisible = false">取 消</el-button>
<el-button type="primary" @click="save">保 存</el-button>
</span>
</template>
</el-dialog>
</div>
</template>
<script setup>
import request from "@/utils/request";
import {reactive, ref} from "vue";
import {ElMessageBox, ElMessage} from "element-plus";
import '@wangeditor/editor/dist/css/style.css' // 引入 css
import {onBeforeUnmount, shallowRef} from "vue";
import { Editor, Toolbar } from '@wangeditor/editor-for-vue'
const formRef = ref()
const data = reactive({
user: JSON.parse(localStorage.getItem('system-user') || '{}'),
pageNum: 1,
pageSize: 10,
total: 0,
formVisible: false,
form: {},
tableData: [],
title: null,
viewVisible: false,
content: null,
rules: {
img: [
{ required: true, message: '请上传图片', trigger: 'blur' }
],
title: [
{ required: true, message: '请输入标题', trigger: 'blur' }
],
description: [
{ required: true, message: '请输入简介', trigger: 'blur' }
],
content: [
{ required: true, message: '请输入内容', trigger: 'blur' }
],
}
})
const view = (content) => {
data.content = content
data.viewVisible = true
}
/* wangEditor5 初始化开始 */
const baseUrl = import.meta.env.VITE_BASE_URL
const editorRef = shallowRef() // 编辑器实例,必须用 shallowRef
const mode = 'default'
const editorConfig = { MENU_CONF: {} }
// 图片上传配置
editorConfig.MENU_CONF['uploadImage'] = {
server: baseUrl + '/files/wang/upload', // 服务端图片上传接口
fieldName: 'file' // 服务端图片上传接口参数
}
// 组件销毁时,也及时销毁编辑器,否则可能会造成内存泄漏
onBeforeUnmount(() => {
const editor = editorRef.value
if (editor == null) return
editor.destroy()
})
// 记录 editor 实例,重要!
const handleCreated = (editor) => {
editorRef.value = editor
}
/* wangEditor5 初始化结束 */
const handleFileUpload = (res) => {
data.form.img = res.data
}
// 分页查询
const load = () => {
request.get('/article/selectPage', {
params: {
pageNum: data.pageNum,
pageSize: data.pageSize,
title: data.title,
userId: data.user.role === '管理员' ? null : data.user.id
}
}).then(res => {
data.tableData = res.data?.list
data.total = res.data?.total
})
}
// 新增
const handleAdd = () => {
data.form = {}
data.formVisible = true
}
// 编辑
const handleEdit = (row) => {
data.form = JSON.parse(JSON.stringify(row))
data.formVisible = true
}
// 新增保存
const add = () => {
data.form.userId = data.user.id
request.post('/article/add', data.form).then(res => {
if (res.code === '200') {
load()
ElMessage.success('操作成功')
data.formVisible = false
} else {
ElMessage.error(res.msg)
}
})
}
// 编辑保存
const update = () => {
request.put('/article/update', data.form).then(res => {
if (res.code === '200') {
load()
ElMessage.success('操作成功')
data.formVisible = false
} else {
ElMessage.error(res.msg)
}
})
}
// 弹窗保存
const save = () => {
formRef.value.validate(valid => {
if (valid) {
// data.form有id就是更新,没有就是新增
data.form.id ? update() : add()
}
})
}
// 删除
const handleDelete = (id) => {
ElMessageBox.confirm('删除后数据无法恢复,您确定删除吗?', '删除确认', { type: 'warning' }).then(res => {
request.delete('/article/delete/' + id).then(res => {
if (res.code === '200') {
load()
ElMessage.success('操作成功')
} else {
ElMessage.error(res.msg)
}
})
}).catch(err => {})
}
// 重置
const reset = () => {
data.title = null
load()
}
load()
</script>