package cn.bestzuo.zuoforum.controller;
import cn.bestzuo.zuoforum.common.ForumResult;
import cn.bestzuo.zuoforum.common.LayuiFlowResult;
import cn.bestzuo.zuoforum.pojo.*;
import cn.bestzuo.zuoforum.pojo.vo.*;
import cn.bestzuo.zuoforum.service.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.util.StringUtils;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.regex.Pattern;
/**
* 用户主页Controller
*/
@Controller
public class UserIndexController {
@Autowired
private CollectionService collectionService;
@Autowired
private UserInfoService userInfoService;
@Autowired
private QuestionService questionService;
@Autowired
private CommentService commentService;
@Autowired
private FollowService followService;
@Autowired
private CommentLikeService commentLikeService;
@Autowired
private UserRateService userRateService;
/**
* 查询对应的用户名
*
* @param token
* @return
*/
@RequestMapping("/user/{token}")
public String userIndex(@PathVariable(value = "token") String token, Model model) {
if (token == null) {
return "404";
}
//根据用户ID查询对应的用户信息
int uid = Integer.parseInt(token);
//查询用户ID
UserInfo userInfo = userInfoService.selectUserInfoByUid(uid);
if (userInfo == null) {
return "404";
}
//保存用户信息
model.addAttribute("username", userInfo.getUsername());
return "user/user";
}
/**
* 获取首页用户信息
*
* @param username
* @return
*/
@RequestMapping("/getUserIndexInfo")
@ResponseBody
public ForumResult getMyIndexInfo(@RequestParam("username") String username) {
//后端校验数据
if (StringUtils.isEmpty(username)) {
return new ForumResult(400, "用户名不能为空", null);
}
//后台查询数据库信息
UserInfo userInfoByName = userInfoService.getUserInfoByName(username);
if (userInfoByName == null) {
return new ForumResult(400, "用户不存在", null);
}
//封装VO对象
UserIndexInfoVO vo = new UserIndexInfoVO();
vo.setId(userInfoByName.getUId());
vo.setUsername(username);
vo.setAvatar(userInfoByName.getAvatar());
vo.setSex(userInfoByName.getSex());
UserRate userRate = userRateService.selectRateById(userInfoByName.getUId());
vo.setRate(userRate == null ? 0 : userRate.getRate());
try {
//查询我关注的人数
List<Follow> follows = followService.selectFollowByUsername(username);
vo.setFollow(follows.size());
//查询关注我的人数
List<Follow> fans = followService.selectFansByUsername(username);
vo.setFans(fans.size());
//查询我发布的问题数
List<Question> questions = questionService.getAllQuestionsByPublisher(username);
vo.setQuestionNum(questions.size());
//查询我收获的点赞数
List<CommentLike> commentLikes = commentLikeService.selectCommentLikeByUsername(username);
vo.setLikeCount(commentLikes.size());
return new ForumResult(200, "查询成功", vo);
} catch (Exception e) {
e.printStackTrace();
return new ForumResult(500, "查询失败", null);
}
}
/**
* 获取我的问题信息
*
* @return
*/
@RequestMapping("/getMyQuestions")
@ResponseBody
public LayuiFlowResult getMyQuestions(@RequestParam("page") Integer page,
@RequestParam("username") String username) {
//后端校验数据
if (StringUtils.isEmpty(username)) {
return new LayuiFlowResult(400, "用户名不能为空", null,0);
}
//后台查询数据库信息
UserInfo userInfoByName = userInfoService.getUserInfoByName(username);
if (userInfoByName == null) {
return new LayuiFlowResult(400, "用户不存在", null,0);
}
//查询问题信息
PageHelper.startPage(page,5);
List<Question> questions = questionService.getAllQuestionsByPublisher(username);
PageInfo<Question> pageInfo = new PageInfo<>(questions);
if (pageInfo.getList().size() == 0) {
return new LayuiFlowResult(200, "查询成功", null,0);
} else {
List<UserIndexQuestionVO> res = new ArrayList<>();
for (Question question : pageInfo.getList()) {
res.add(convertQuestionToVO(question));
}
return new LayuiFlowResult(200, "查询成功", res,pageInfo.getPages());
}
}
/**
* 将Question信息转换成前端VO
*
* @param question
* @return
*/
private UserIndexQuestionVO convertQuestionToVO(Question question) {
UserIndexQuestionVO vo = new UserIndexQuestionVO();
vo.setId(question.getId());
vo.setTitle(question.getTitle());
vo.setCommentCount(question.getCommentCount());
vo.setLikeCount(question.getLikeCount());
vo.setGmtCreate(question.getGmtCreate());
vo.setViewCount(question.getViewCount());
String text = Html2Text(question.getDescription());
if (text.length() > 20) {
vo.setDescription(text.substring(0, 20) + "...");
} else
vo.setDescription(text);
return vo;
}
//从html中提取纯文本
private String Html2Text(String inputString) {
String htmlStr = inputString; // 含html标签的字符串
String textStr = "";
Pattern p_script;
java.util.regex.Matcher m_script;
Pattern p_style;
java.util.regex.Matcher m_style;
Pattern p_html;
java.util.regex.Matcher m_html;
try {
String regEx_script = "<[\\s]*?script[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?script[\\s]*?>"; // 定义script的正则表达式{或<script[^>]*?>[\\s\\S]*?<\\/script>
String regEx_style = "<[\\s]*?style[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?style[\\s]*?>"; // 定义style的正则表达式{或<style[^>]*?>[\\s\\S]*?<\\/style>
String regEx_html = "<[^>]+>"; // 定义HTML标签的正则表达式
p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE);
m_script = p_script.matcher(htmlStr);
htmlStr = m_script.replaceAll(""); // 过滤script标签
p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE);
m_style = p_style.matcher(htmlStr);
htmlStr = m_style.replaceAll(""); // 过滤style标签
p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
m_html = p_html.matcher(htmlStr);
htmlStr = m_html.replaceAll(""); // 过滤html标签
textStr = htmlStr;
} catch (Exception e) {
System.err.println("Html2Text: " + e.getMessage());
}
//剔除空格行
textStr = textStr.replaceAll("[ ]+", " ");
textStr = textStr.replaceAll("(?m)^\\s*$(\\n|\\r\\n)", "");
return textStr;// 返回文本字符串
}
/**
* 获取我的评论信息
*
* @return
*/
@RequestMapping("/getMyCommentInfo")
@ResponseBody
public LayuiFlowResult getMyComments(@RequestParam("page")Integer page,
@RequestParam("username") String username) {
//后端数据校验
if (StringUtils.isEmpty(username)) {
没有合适的资源?快使用搜索试试~ 我知道了~
基于java+springboot+mybatis+mysql的在线论坛管理系统
共934个文件
xml:177个
class:156个
java:156个
需积分: 0 7 下载量 118 浏览量
2023-07-20
14:41:50
上传
评论 2
收藏 52.34MB RAR 举报
温馨提示
一、项目功能:本论坛是一个功能基本全面的内部交流使用论坛平台,提供发布问题、个人资料展示、评论、回复、搜索、接收消息通知等基本功能。后端采用了 SpringBoot + MyBatis + MySQL进行构建,前端采用了 Thymeleaf + Layui 进行展示。界面风格简洁,尚未进行深度美化。 二、运行环境:idea/eclipse+jdk1.8+mysql 三、项目技术:springboot+mybatis+maven+html,B/S模式
资源推荐
资源详情
资源评论
收起资源包目录
基于java+springboot+mybatis+mysql的在线论坛管理系统 (934个子文件)
UserIndexController.class 15KB
NoticeController.class 14KB
QuestionServiceImpl.class 12KB
RedisUtil.class 11KB
QuestionController.class 10KB
AdminQuestionInfoService.class 10KB
UserInfo.class 10KB
QuestionVO.class 9KB
PostController.class 9KB
IndexController.class 8KB
UserInfoVO.class 7KB
UserLoginAndRegisterController.class 7KB
IpAddress.class 7KB
PulishController.class 7KB
CommentController.class 7KB
AdminUserInfoController.class 6KB
Question.class 6KB
IpUtil.class 6KB
UserIndexCommentsVO.class 6KB
EmailCheckController.class 6KB
AdminIndexController.class 6KB
AdminTypeController.class 6KB
CommentNoticeInfo.class 6KB
ForumResult.class 6KB
TagController.class 5KB
QuestionInfo.class 5KB
CommentVO.class 5KB
ImageUploadController.class 5KB
CommentServiceImpl.class 5KB
AdminTagController.class 5KB
CommentNoticeVO.class 5KB
AdminQuestionInfoController.class 5KB
SearchController.class 5KB
UserIndexInfoVO.class 5KB
CommentLikeController.class 5KB
CommentLike.class 5KB
UserInfoVO.class 5KB
FollowVO.class 5KB
CommentLikeVO.class 5KB
CollectionController.class 4KB
PraiseController.class 4KB
FollowController.class 4KB
IMailServiceImpl.class 4KB
UserIndexQuestionVO.class 4KB
CommentReply.class 4KB
UserServiceImpl.class 4KB
CommentReplyController.class 4KB
Collection.class 4KB
Praise.class 4KB
CollectionNoticeVO.class 4KB
QuestionReportVO.class 4KB
Follow.class 4KB
UserInfoController.class 4KB
PraiseVO.class 4KB
TagServiceImpl.class 4KB
CommentReplyServiceImpl.class 4KB
Comment.class 4KB
KaptchaController.class 4KB
LayuiFlowResult.class 4KB
CommentNoticeServiceImpl.class 3KB
TencentCOS.class 3KB
QuestionReport.class 3KB
TransactionAdviceConfig.class 3KB
FollowNoticeVO.class 3KB
EmailInfo.class 3KB
RedisConfig.class 3KB
UserInfoServiceImpl.class 3KB
QuestionReportServiceImpl.class 3KB
WebConfig.class 3KB
QiniuUtils.class 3KB
LayuiTableResult.class 3KB
Tags.class 3KB
TypeSeviceImpl.class 3KB
MD5Password.class 3KB
PageBean.class 3KB
UserVO.class 3KB
LayEditUploadImageResult.class 2KB
SafeController.class 2KB
QuestionMapperTest.class 2KB
UserRateVO.class 2KB
JsonUtils.class 2KB
VerifyCode.class 2KB
User.class 2KB
AdminLoginInterceptor.class 2KB
QiNiuController.class 2KB
LoginInterceptor.class 2KB
QuestionEdit.class 2KB
LoginInfo.class 2KB
IpInfo.class 2KB
EmailServiceImpl.class 2KB
CollectionServiceImpl.class 2KB
UserMapperTest.class 2KB
PraiseServiceImpl.class 2KB
ErrorPageConfig.class 2KB
QuestionService.class 2KB
WangEditorResult.class 2KB
CommentLikeServiceImpl.class 2KB
CommonUtils.class 2KB
UploadImage.class 2KB
FollowServiceImpl.class 2KB
共 934 条
- 1
- 2
- 3
- 4
- 5
- 6
- 10
资源评论
qq_3306428634
- 粉丝: 458
- 资源: 4
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功