diff --git a/backend/src/main/java/com/teaching/backend/controller/cms/CmsEssayController.java b/backend/src/main/java/com/teaching/backend/controller/cms/CmsEssayController.java index 02fa668..1631a0b 100644 --- a/backend/src/main/java/com/teaching/backend/controller/cms/CmsEssayController.java +++ b/backend/src/main/java/com/teaching/backend/controller/cms/CmsEssayController.java @@ -87,4 +87,14 @@ public class CmsEssayController { public BaseResponse editStatus(@RequestBody CmsEssay cmsEssay){ return cmsEssayService.editStatus(cmsEssay); } + + /** + * 浏览量 + * @param id + * @return + */ + @PutMapping("/views/{id}") + public BaseResponse views(@PathVariable Integer id){ + return cmsEssayService.views(id); + } } diff --git a/backend/src/main/java/com/teaching/backend/service/cms/ICmsEssayService.java b/backend/src/main/java/com/teaching/backend/service/cms/ICmsEssayService.java index 2bdb32e..49d368e 100644 --- a/backend/src/main/java/com/teaching/backend/service/cms/ICmsEssayService.java +++ b/backend/src/main/java/com/teaching/backend/service/cms/ICmsEssayService.java @@ -28,4 +28,6 @@ public interface ICmsEssayService extends IService { BaseResponse editStatus(CmsEssay cmsEssay); BaseResponse queryEssayById(Integer essayId); + + BaseResponse views(Integer id); } diff --git a/backend/src/main/java/com/teaching/backend/service/impl/cms/CmsEssayServiceImpl.java b/backend/src/main/java/com/teaching/backend/service/impl/cms/CmsEssayServiceImpl.java index 5fbb48c..25b58c9 100644 --- a/backend/src/main/java/com/teaching/backend/service/impl/cms/CmsEssayServiceImpl.java +++ b/backend/src/main/java/com/teaching/backend/service/impl/cms/CmsEssayServiceImpl.java @@ -7,8 +7,10 @@ import com.teaching.backend.common.ResultUtils; import com.teaching.backend.mapper.cms.CmsEssayMapper; import com.teaching.backend.model.entity.cms.CmsEssay; import com.teaching.backend.service.cms.ICmsEssayService; +import com.teaching.backend.service.impl.umsAdmin.UmsUserServiceImpl; import org.springframework.stereotype.Service; +import javax.annotation.Resource; import java.time.LocalDate; import java.util.List; @@ -23,6 +25,9 @@ import java.util.List; @Service public class CmsEssayServiceImpl extends ServiceImpl implements ICmsEssayService { + @Resource + private UmsUserServiceImpl userService; + @Override public BaseResponse addEssay(CmsEssay cmsEssay) { //设置文章发布时间 @@ -30,9 +35,11 @@ public class CmsEssayServiceImpl extends ServiceImpl i //设置发布人姓名 // String nickName = userService.getById(cmsEssay.getUserId()).getNickName(); // cmsEssay.setUsername(nickName); + //将浏览次数设置为0 + cmsEssay.setReadingNumber(0); //如果数据库里没有文章,文章id设为2 Long count = query().count(); - if (count == 0){ + if (count == 0) { cmsEssay.setId(2); } save(cmsEssay); @@ -42,7 +49,7 @@ public class CmsEssayServiceImpl extends ServiceImpl i @Override public BaseResponse deleteEssay(List ids) { //如果要删除的文章中包含id为1的文章(就是包含关于我们)就删除失败 - if (ids.contains(1)){ + if (ids.contains(1)) { return ResultUtils.success("删除失败,不能删除关于我们!"); } removeByIds(ids); @@ -54,10 +61,10 @@ public class CmsEssayServiceImpl extends ServiceImpl i //获取要修改的文章id Integer id = cmsEssay.getId(); //判断文章id是否为1 - if (id == 1){ + if (id == 1) { Long count = query().eq("id", id).count(); //如果数据库里没id为的文章就先新增 - if (count == 0){ + if (count == 0) { save(cmsEssay); return ResultUtils.success("添加成功!"); } @@ -83,4 +90,10 @@ public class CmsEssayServiceImpl extends ServiceImpl i public BaseResponse queryEssayById(Integer essayId) { return ResultUtils.success(getById(essayId)); } -} + + @Override + public BaseResponse views(Integer id) { + boolean update = update().setSql("reading_number = reading_number + 1 where id = " + id).update(); + return update ? ResultUtils.success("浏览次数加1") : ResultUtils.success("error!!!"); + } +} \ No newline at end of file diff --git a/teaching_integration_platform_admin_template/.env.development b/teaching_integration_platform_admin_template/.env.development index bfb0e2d..7b3885a 100644 --- a/teaching_integration_platform_admin_template/.env.development +++ b/teaching_integration_platform_admin_template/.env.development @@ -1,6 +1,6 @@ # 变量必须以 VITE_ 为前缀才能暴露给外部读取 NODE_ENV = 'development' VITE_APP_TITLE = '无糖运营平台' - VITE_APP_BASE_API = 'http://127.0.0.1:8080' -# VITE_APP_BASE_API = 'http://39.106.16.162:8080' +# VITE_APP_BASE_API = 'http://127.0.0.1:8080' +VITE_APP_BASE_API = 'http://39.106.16.162:8080' diff --git a/teaching_integration_platform_admin_template/src/api/user/news.js b/teaching_integration_platform_admin_template/src/api/user/news.js index 9d00b05..4703077 100644 --- a/teaching_integration_platform_admin_template/src/api/user/news.js +++ b/teaching_integration_platform_admin_template/src/api/user/news.js @@ -53,4 +53,8 @@ export function editEssay(params) { //根据id查询关于我们文章 export function queryEssayById(essay_id) { return request.get(`/api/cms-essay/queryessaybyid?essay_id=${essay_id}`); +} +//阅读量增加 +export function viewsNumberAdd(id) { + return request.put(`/api/cms-essay/views/${id}`); } \ No newline at end of file diff --git a/teaching_integration_platform_admin_template/src/views/news/index.vue b/teaching_integration_platform_admin_template/src/views/news/index.vue index d4f6361..b132e30 100644 --- a/teaching_integration_platform_admin_template/src/views/news/index.vue +++ b/teaching_integration_platform_admin_template/src/views/news/index.vue @@ -16,7 +16,7 @@
  • - +

    {{listObject.title}}

    @@ -61,6 +61,7 @@ import { queryCategory, queryEssay, + viewsNumberAdd } from "@/api/user/news.js" import {ref,onMounted} from 'vue'; import {ElMessage} from 'element-plus' @@ -88,12 +89,30 @@ onMounted(() => { }) const essay = ref([]) const nowTimeNewsData = ref({}) - //新闻详细信息获取 const newViews = (listObject)=> { nowTimeNewsData.value = { ...listObject } console.log(nowTimeNewsData,'当前点击新闻详细内容') } +// 增加一个变量记录上一次请求的时间 +let lastRequestTime = 0; +const incrementViewsAndShow = async (listObject) => { + const currentTime = Date.now(); + // 检查是否在短时间内重复请求 + if (currentTime - lastRequestTime < 1000) { + return; + } + try { + // 调用API增加阅读量 + await viewsNumberAdd(listObject.id); + lastRequestTime = currentTime; // 更新最后请求的时间 + nowTimeNewsData.value = { ...listObject }; + console.log(nowTimeNewsData.value, '当前点击新闻详细内容'); + } catch (error) { + ElMessage.error('增加阅读量失败,请稍后再试!'); + } +}; + // 新闻文章查询 const EssayList = async (id) => { try { @@ -102,11 +121,11 @@ const EssayList = async (id) => { const filteredEssays = response.data.filter((item) => item.status === 1); essay.value = filteredEssays; // 如果过滤后的列表有内容,则显示第一条新闻 - if (essay.value.length > 0) { - newViews(essay.value[0]); // 显示第一条新闻 - } else { - ElMessage.warning('此栏目下没有可用的新闻!'); - } + // if (essay.value.length > 0) { + // newViews(essay.value[0]); // 显示第一条新闻 + // } else { + // ElMessage.warning('此栏目下没有可用的新闻!'); + // } } catch (error) { ElMessage.error('获取新闻列表失败,请稍后再试!'); } diff --git a/teaching_integration_platform_template/.env.development b/teaching_integration_platform_template/.env.development index 817502a..6692c9c 100644 --- a/teaching_integration_platform_template/.env.development +++ b/teaching_integration_platform_template/.env.development @@ -1,5 +1,5 @@ # 变量必须以 VITE_ 为前缀才能暴露给外部读取 NODE_ENV = 'development' VITE_APP_TITLE = '教学一体化平台' -VITE_APP_BASE_API = 'http://127.0.0.1:8080' -# VITE_APP_BASE_API = 'http://39.106.16.162:8080' \ No newline at end of file +# VITE_APP_BASE_API = 'http://127.0.0.1:8080' +VITE_APP_BASE_API = 'http://39.106.16.162:8080' \ No newline at end of file