SpringBoot和前端聯(lián)動(dòng)實(shí)現(xiàn)存儲(chǔ)瀏覽記錄功能
瀏覽課程歷史記錄表 Browsing history
id varchar(32) 非空 主鍵索引 瀏覽記錄主鍵id
course_id varchar(32) 非空 普通索引 課程id
course_name varchar(1024) 非空 課程名稱
user_id varchar(32) 非空 普通索引 用戶id
user_name varchar(1024) 可空 用戶名稱
last_viewing_time datetime 非空 最后觀看時(shí)間
在關(guān)閉(跳轉(zhuǎn)離開)該課程的時(shí)候,異步發(fā)請(qǐng)求,先使用 用戶id和課程id去查這個(gè)表,看看是否存在該條記錄,存在,則獲取并設(shè)置最新的時(shí)間,不存在,則設(shè)置最新時(shí)間,并插入該數(shù)據(jù)
離開當(dāng)前頁(yè)面出發(fā)異步請(qǐng)求(navigator.sendBeacon原理請(qǐng)搜索其他博客)
$(window).on('unload', function (e) {
// 使用navigator.sendBeacon發(fā)請(qǐng)求
const blob = new Blob([JSON.stringify({ 'courseId': courseId})], { type: 'application/json; charset=UTF-8'});
navigator.sendBeacon(url,blob)
console.log("離開了頁(yè)面");
});后端接受請(qǐng)求增加瀏覽記錄
controller.java
后端接受請(qǐng)求增加瀏覽記錄
@ApiOperation("添加該用戶的瀏覽記錄")
@PostMapping("/addBrowsingHistory")
public BaseResult<String> addBrowsingHistory(@RequestBody BrowsingHistory browsingHistory) {
if (browsingHistory.getCourseId().isEmpty()){
throw new RuntimeException("參數(shù)錯(cuò)誤");
}
String userId = super.getOperatorId();//用戶id
browsingHistory.setUserId(userId);
//根據(jù)用戶id和課程id查詢是否有該記錄
browsingHistoryService.addHistory(browsingHistory);
return getBaseResultSuccess("ok","記錄成功");
}
@ApiOperation("獲取該用戶的瀏覽記錄")
@PostMapping("/getBrowsingHistory")
public BaseResult<BaseQueryPageDTO<BrowsingHistory>> getBrowsingHistory(@RequestBody BrowsingHistory browsingHistory) {
String userId = super.getOperatorId();
browsingHistory.setUserId(userId);
BaseQueryPageDTO<BrowsingHistory> page = browsingHistoryService.getHistoryPage(browsingHistory);
return getBaseResultSuccess(page,"獲取瀏覽記錄成功");
}service.java
//添加記錄
public void addHistory(BrowsingHistory browsingHistory) {
String userId = browsingHistory.getUserId();//用戶id
String courseId = browsingHistory.getCourseId();//課程id
LambdaQueryWrapper<BrowsingHistory> eq = new LambdaQueryWrapper<BrowsingHistory>()
.eq(BrowsingHistory::getUserId, userId)
.eq(BrowsingHistory::getCourseId, courseId);
BrowsingHistory oldHistory = baseMapper.selectOne(eq);
Date date = new Date();
if (BeanUtil.isNotEmpty(oldHistory)){
//有記錄,更新
oldHistory.setLastViewingTime(date);
oldHistory.setUpdateTime(date);
baseMapper.updateById(oldHistory);
}else{
//無(wú)記錄,新增
//獲取用戶名,課程名,插入
LambdaQueryWrapper<Examiner> eq1 = new LambdaQueryWrapper<Examiner>().eq(Examiner::getId, userId).select(Examiner::getName);
String userName = examinerMapper.selectOne(eq1).getName();
LambdaQueryWrapper<CaseShare> eq2 = new LambdaQueryWrapper<CaseShare>().eq(CaseShare::getId, courseId).select(CaseShare::getCaseName);
String courseName = caseShareMapper.selectOne(eq2).getCaseName();
browsingHistory.setLastViewingTime(date);
browsingHistory.setUserName(userName);
browsingHistory.setCourseName(courseName);
browsingHistory.setCreateTime(date);
browsingHistory.setUpdateTime(date);
baseMapper.insert(browsingHistory);
}
}
//獲取瀏覽記錄,需要按照最后瀏覽時(shí)間查詢
public BaseQueryPageDTO<BrowsingHistory> getHistoryPage(BrowsingHistory browsingHistory) {
IPage<BrowsingHistory> page =
new Page<>(browsingHistory.getPageNum(), browsingHistory.getPageSize());
IPage<BrowsingHistory> iPage = baseMapper.getPage(page, BrowsingHistory);
return new BaseQueryPageDTO<>(
iPage.getCurrent(),
iPage.getSize(),
iPage.getPages(),
iPage.getTotal(),
iPage.getRecords()
);
}
public interface BrowsingHistoryMapper extends BaseMapper<BrowsingHistory> {
IPage<BrowsingHistory> getPage(IPage<BrowsingHistory> page,@Param("entity") BrowsingHistory browsingHistory);
}
<select id="getPage" resultType="com.xxx.entity.BrowsingHistory">
select
csbh.id as id,
csbh.course_id as courseId,
csbh.course_name as courseName,
csbh.user_id as userId,
csbh.user_name as userName,
csbh.last_viewing_time as lastViewingTime
from
case_share_browsing_history csbh
<where>
<if test="entity.userId != null and entity.userId !=''">
and csbh.user_id = #{entity.userId}
</if>
</where>
order by csbh.last_viewing_time desc
</select>到此這篇關(guān)于SpringBoot和前端聯(lián)動(dòng)實(shí)現(xiàn)存儲(chǔ)瀏覽記錄的文章就介紹到這了,更多相關(guān)SpringBoot存儲(chǔ)瀏覽記錄內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot2零基礎(chǔ)到精通之?dāng)?shù)據(jù)庫(kù)專項(xiàng)精講
SpringBoot是一種整合Spring技術(shù)棧的方式(或者說是框架),同時(shí)也是簡(jiǎn)化Spring的一種快速開發(fā)的腳手架,本篇我們來學(xué)習(xí)如何連接數(shù)據(jù)庫(kù)進(jìn)行操作2022-03-03
IDEA下因Lombok插件產(chǎn)生的Library source does not match the bytecode報(bào)
這篇文章主要介紹了IDEA下因Lombok插件產(chǎn)生的Library source does not match the bytecode報(bào)錯(cuò)問題及解決方法,親測(cè)試過好用,需要的朋友可以參考下2020-04-04
Java并發(fā)編程中的生產(chǎn)者與消費(fèi)者模型簡(jiǎn)述
這篇文章主要介紹了Java并發(fā)編程中的生產(chǎn)者與消費(fèi)者模型簡(jiǎn)述,多線程并發(fā)是Java編程中最終要的部分之一,需要的朋友可以參考下2015-07-07
springsecurity基于token的認(rèn)證方式
本文主要介紹了springsecurity基于token的認(rèn)證方式,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08

