后端
定义预览数据模型 VO, ContentPreviewVO
/**
* @author BNTang
* @version S2.3.2Dev
* @program video_parent
* @date Created in 2021/4/11 12:50
* @description 预览数据模型 VO
**/
public class ContentPreviewVO {
private String title;
private String cover;
private Integer contentNum;
private String oneCategory;
private String twoCategory;
private String author;
/**
* 价格只用于显示所以为String
*/
private String price;
}
在 ContentController 当中定义接口方法
/**
* <b>
* 获取作品的预览信息
* </b>
*/
(value = "获取作品的预览信息")
("/getContentPreView/{id}")
public ResponseResult getContentPreView( String id) {
return ResponseResult.ok().data("item", contentService.getContentPreView(id));
}
修改 ContentService 添加对应的方法
/**
* <b>
* 获取作品的预览信息
* </b>
*
* @param id 作品ID
* @return 预览信息
*/
ContentPreviewVO getContentPreView(String id);
修改 ContentServiceImpl
public ContentPreviewVO getContentPreView(String id) {
return baseMapper.getContentPreviewVoById(id);
}
修改 ContentMapper
/**
* <b>
* 获取作品的预览信息
* </b>
*
* @param id 作品ID
* @return 预览信息
*/
ContentPreviewVO getContentPreviewVoById(String id);
由于是多表之间的查询,所以不能用 MP 实现,需要自己手写SQL,修改 ContentMapper.xml
<mapper namespace="top.it6666.service_video.mapper.ContentMapper">
<select id="getContentPreviewVoById" resultType="top.it6666.service_video.entity.vo.ContentPreviewVO">
SELECT c.title,
c.cover,
c.content_num AS contentNum,
CONVERT(c.price, DECIMAL(8, 2)) AS price,
c1.title AS oneCategory,
c2.title AS twoCategory,
AS author
FROM video_content c
LEFT JOIN video_author a ON c.author_id = a.id
LEFT JOIN video_category c1 ON c.category_parent_id = c1.id
LEFT JOIN video_category c2 ON c.category_id = c2.id
WHERE c.id = #{id}
</select>
</mapper>
修改 service_video
微服务的配置文件,配置 mapper.xml 文件地址如下
mapper-locations top/it6666/service_video/mapper/xml/*.xml
而且还要配置一步,配置 maven 打包时不拦截 xml 文件,xml 也需要打包,注意是修改最外层的父工程的 pom.xml
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
发布作品
/**
* <b>
* 发布作品
* </b>
*/
(value = "发布作品")
("/sendContent/{id}")
public ResponseResult sendContent( String id) {
contentService.sendContentWithId(id);
return ResponseResult.ok();
}
修改 ContentService
/**
* <b>
* 发布作品
* </b>
*
* @param id 作品ID
*/
void sendContentWithId(String id);
修改 ContentServiceImpl
public void sendContentWithId(String id) {
Content content = new Content();
content.setId(id);
content.setStatus("Normal");
baseMapper.updateById(content);
}
前端
前端界面
在 conent.js
当中定义 api 请求接口
// 获取课程预览信息
getContentPreview(id) {
return request({
url: `/service_video/content/getContentPreView/${id}`,
method: 'get'
});
},
// 发布作品
sendContent(id) {
return request({
url: `/service_video/content/sendContent/${id}`,
method: 'post'
});
}
在 send.vue 当中定义数据模型
data() {
return {
// 所属作品
contentId: '',
// 预览信息实体
contentSendObj: {}
};
},
导入 api
import content from "@/api/video/content/content";
接收路由参数
created() {
if (this.$route.params && this.$route.params.id) {
this.contentId = this.$route.params.id;
// 根据id获取作品基本信息
this.getPreviewInfo();
}
}
// 根据id获取作品基本信息
getPreviewInfo() {
content.getContentPreview(this.contentId).then(res => {
this.contentSendObj = res.data.item;
}).catch(error => {
this.$message.error(error.message);
});
}
定义界面,结构
<div class="contentInfo">
<img :src="contentSendObj.cover">
<div class="main">
<h2>{{ contentSendObj.title }}</h2>
<p class="gray"><span>共{{ contentSendObj.contentNum }}总数</span></p>
<p><span>所属分类:{{ contentSendObj.oneCategory }} — {{ contentSendObj.twoCategory }}</span></p>
<p>作者:{{ contentSendObj.author }}</p>
<h3 class="red">¥{{ contentSendObj.price }}</h3>
</div>
</div>
样式如下
<style scoped>
.contentInfo {
background: #f5f5f5;
padding: 20px;
overflow: hidden;
border: 1px dashed #DDD;
margin-bottom: 40px;
position: relative;
}
.contentInfo img {
background: #d6d6d6;
width: 500px;
height: 278px;
display: block;
float: left;
border: none;
}
.contentInfo .main {
margin-left: 520px;
}
.contentInfo .main h2 {
font-size: 28px;
margin-bottom: 30px;
line-height: 1;
font-weight: normal;
}
.contentInfo .main p {
margin-bottom: 10px;
word-wrap: break-word;
line-height: 24px;
max-height: 48px;
overflow: hidden;
}
.contentInfo .main p {
margin-bottom: 10px;
word-wrap: break-word;
line-height: 24px;
max-height: 48px;
overflow: hidden;
}
.contentInfo .main h3 {
left: 540px;
bottom: 20px;
line-height: 1;
font-size: 28px;
color: #d32f24;
font-weight: normal;
position: absolute;
}
</style>
发布按钮点击
send() {
content.sendContent(this.contentId).then(response => {
// 跳转到列表页
this.$router.push({path: '/content/list'});
}).catch(error => {
this.$message.error(error.message);
});
},