SpringBoot和前端聯(lián)動實現(xiàn)存儲瀏覽記錄功能
瀏覽課程歷史記錄表 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 非空 最后觀看時間
在關閉(跳轉(zhuǎn)離開)該課程的時候,異步發(fā)請求,先使用 用戶id和課程id去查這個表,看看是否存在該條記錄,存在,則獲取并設置最新的時間,不存在,則設置最新時間,并插入該數(shù)據(jù)
離開當前頁面出發(fā)異步請求(navigator.sendBeacon原理請搜索其他博客)
$(window).on('unload', function (e) { // 使用navigator.sendBeacon發(fā)請求 const blob = new Blob([JSON.stringify({ 'courseId': courseId})], { type: 'application/json; charset=UTF-8'}); navigator.sendBeacon(url,blob) console.log("離開了頁面"); });
后端接受請求增加瀏覽記錄
controller.java
后端接受請求增加瀏覽記錄 @ApiOperation("添加該用戶的瀏覽記錄") @PostMapping("/addBrowsingHistory") public BaseResult<String> addBrowsingHistory(@RequestBody BrowsingHistory browsingHistory) { if (browsingHistory.getCourseId().isEmpty()){ throw new RuntimeException("參數(shù)錯誤"); } 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{ //無記錄,新增 //獲取用戶名,課程名,插入 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); } } //獲取瀏覽記錄,需要按照最后瀏覽時間查詢 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>
到此這篇關于SpringBoot和前端聯(lián)動實現(xiàn)存儲瀏覽記錄的文章就介紹到這了,更多相關SpringBoot存儲瀏覽記錄內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
SpringBoot2零基礎到精通之數(shù)據(jù)庫專項精講
SpringBoot是一種整合Spring技術棧的方式(或者說是框架),同時也是簡化Spring的一種快速開發(fā)的腳手架,本篇我們來學習如何連接數(shù)據(jù)庫進行操作2022-03-03IDEA下因Lombok插件產(chǎn)生的Library source does not match the bytecode報
這篇文章主要介紹了IDEA下因Lombok插件產(chǎn)生的Library source does not match the bytecode報錯問題及解決方法,親測試過好用,需要的朋友可以參考下2020-04-04Java并發(fā)編程中的生產(chǎn)者與消費者模型簡述
這篇文章主要介紹了Java并發(fā)編程中的生產(chǎn)者與消費者模型簡述,多線程并發(fā)是Java編程中最終要的部分之一,需要的朋友可以參考下2015-07-07