Files
Jiscuss/src/main/java/com/yaoyuan/jiscuss/controller/UserSystemController.java
T

317 lines
11 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package com.yaoyuan.jiscuss.controller;
import com.yaoyuan.jiscuss.entity.Discussion;
import com.yaoyuan.jiscuss.entity.Tag;
import com.yaoyuan.jiscuss.entity.User;
import com.yaoyuan.jiscuss.entity.UserInfo;
import com.yaoyuan.jiscuss.entity.custom.DiscussionCustom;
import com.yaoyuan.jiscuss.entity.custom.TagCustom;
import com.yaoyuan.jiscuss.repository.DiscussionsRepository;
import com.yaoyuan.jiscuss.service.IDiscussionsService;
import com.yaoyuan.jiscuss.service.ITagsService;
import com.yaoyuan.jiscuss.service.IUsersService;
import com.yaoyuan.jiscuss.util.DelTagsUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.view.RedirectView;
import jakarta.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
/**
* @author Chuyaoyuan
* 首页页面系统控制器
*/
@Controller
public class UserSystemController extends BaseController {
private static Logger logger = LoggerFactory.getLogger(UserSystemController.class);
@Autowired
private IUsersService usersService;
@Autowired
private IDiscussionsService discussionsService;
@Autowired
private ITagsService tagsService;
@Autowired
private BCryptPasswordEncoder passwordEncoder;
@Autowired
private DiscussionsRepository discussionsRepository;
/**
* 首页index
* @param tag
* @param type
* @param pageNum
* @param request
* @param map
* @return
*/
@RequestMapping({"/", "/main", "/index"})
public String home(@RequestParam(defaultValue = "all") String tag, @RequestParam(defaultValue = "all") String type,
@RequestParam(defaultValue = "1") Integer pageNum, HttpServletRequest request, ModelMap map) {
logger.info(">>> index");
logger.info(">>> tag:" + tag);
logger.info(">>> pageNum:" + pageNum);
int pageSiz = 10;
int pageNumNew = pageNum - 1;
Discussion discussion = new Discussion();
//分页获取主题帖子-默认
Page<Discussion> allDiscussionsPage = discussionsService.queryAllDiscussionsList(discussion, pageNumNew, pageSiz, tag, type);
List<Discussion> allDiscussions = allDiscussionsPage.getContent();
List<DiscussionCustom> newAllD = new ArrayList<>();
setTagAndUserList(newAllD, allDiscussions);
logger.info("全部主题==>{}", allDiscussions);
Long total = allDiscussionsPage.getTotalElements();
//获取主题帖子的分页数据(以后尝试去缓存中取)
List<String> pageNumList = getPageNumList(allDiscussionsPage.getTotalPages());
//获取所有标签(以后尝试去缓存中取)
List<Tag> allTags = tagsService.getAllList();
logger.info("全部标签==>{}", allTags);
if (!tag.equals("all")) {
//获取是否有子标签
List<Tag> allChildTags = tagsService.findByParentId(tag);
map.put("allChildTags", allChildTags);
}
map.put("data", "Jiscuss 用户");
map.put("allDiscussions", newAllD);
map.put("allUser", usersService.getAllList());
map.put("pageDiscussions", pageNumList);
map.put("allTags", allTags);
map.put("tag", tag);
map.put("type", type);
if (type.equals("all")) {
map.put("typeAll", "active");
}
if (type.equals("hot")) {
map.put("typeHot", "active");
}
if (type.equals("new")) {
map.put("typeNew", "active");
}
map.put("pageSize", pageSiz);
map.put("pageTotal", total);
map.put("pageNum", pageNum);
map.put("pageTotalPages", allDiscussionsPage.getTotalPages());
map.put("pageNumAll", allDiscussionsPage.getTotalPages());
map.put("topUsers", usersService.getTopActiveUsers(10));
UserInfo user = getUserInfo(request);
if (user != null) {
map.put("username", user.getUsername());
map.put("data", "Jiscuss 用户:" + user.getUsername());
}
return "index";
}
/**
* 组装DiscussionCustom
*
* @param newAllD
* @param allDiscussions
*/
private void setTagAndUserList(List<DiscussionCustom> newAllD, List<Discussion> allDiscussions) {
List<Integer> discussionIdLsit = new ArrayList<>();
for (Discussion dd : allDiscussions) {
discussionIdLsit.add(dd.getId());
}
List<TagCustom> TagCustomList = tagsService.findByDiscussionIdlistId(discussionIdLsit);
Map<Integer, List<TagCustom>> tagMap = new HashMap<>();
for (TagCustom tc : TagCustomList) {
List<TagCustom> tagCustomListTemp = new ArrayList<>();
if (tagMap.containsKey(tc.getDiscussionId())) {
tagCustomListTemp = tagMap.get(tc.getDiscussionId());
tagCustomListTemp.add(tc);
tagMap.put(tc.getDiscussionId(), tagCustomListTemp);
} else {
tagCustomListTemp.add(tc);
tagMap.put(tc.getDiscussionId(), tagCustomListTemp);
}
}
Set<Integer> userIds = new HashSet<>();
for (Discussion dd : allDiscussions) {
if (dd.getStartUserId() != null) {
userIds.add(dd.getStartUserId());
}
if (dd.getLastUserId() != null) {
userIds.add(dd.getLastUserId());
}
}
Map<Integer, User> userMap = usersService.findAllById(userIds).stream()
.collect(Collectors.toMap(User::getId, u -> u));
for (Discussion dd : allDiscussions) {
DiscussionCustom newdd = new DiscussionCustom();
BeanUtils.copyProperties(dd, newdd);
String newCon = "";
String content = DelTagsUtil.getTextFromHtml(newdd.getContent());
if (null != content && content.length() > 30) {
newCon = content.substring(0, 29);
} else {
newCon = content;
}
newdd.setContent(newCon);
if (null != newdd.getStartUserId()) {
User startUser = userMap.get(newdd.getStartUserId());
newdd.setAvatar(startUser != null && startUser.getAvatar() != null ? startUser.getAvatar() : "");
newdd.setRealname(startUser != null && startUser.getRealname() != null ? startUser.getRealname() : "");
newdd.setUsername(startUser != null && startUser.getUsername() != null ? startUser.getUsername() : "");
}
if (null != newdd.getLastUserId()) {
User lastUser = userMap.get(newdd.getLastUserId());
newdd.setAvatarLast(lastUser != null && lastUser.getAvatar() != null ? lastUser.getAvatar() : "");
newdd.setRealnameLast(lastUser != null && lastUser.getRealname() != null ? lastUser.getRealname() : "");
newdd.setUsernameLast(lastUser != null && lastUser.getUsername() != null ? lastUser.getUsername() : "");
}
// 组装tag
newdd.setTagList(tagMap.get(dd.getId()));
newAllD.add(newdd);
}
}
/**
* 分页信息
*
* @param size
* @return
*/
private List<String> getPageNumList(int size) {
List<String> pageNumList = new ArrayList<String>();
for (int i = 0; i < size; i++) {
pageNumList.add("" + (i + 1));
}
return pageNumList;
}
/**
* 登录页
*
* @param error
* @param logout
* @param map
* @return
*/
@GetMapping("/login")
public String login(@RequestParam(value = "error", required = false) String error,
@RequestParam(value = "logout", required = false) String logout, ModelMap map) {
if (error != null) {
map.put("msg", "您输入的用户名密码错误!");
return "login";
}
if (logout != null) {
map.put("msg", "退出成功!");
return "login";
}
return "login";
}
@GetMapping("/favicon.ico")
public RedirectView favicon() {
return new RedirectView("/static/assets/images/logo.png");
}
/**
* 注册提交
* @param username
* @param email
* @param password
* @param map
* @return
*/
@PostMapping("/registerDo")
public String registerDo(@RequestParam String username, @RequestParam String email,
@RequestParam String password, ModelMap map) {
//验证用户名是否存在
if (null != usersService.getByUsername(username)) {
map.put("msg", "用户已存在,请重试!");
return "register";
} else {
User user = new User();
user.setEmail(email);
user.setPassword(passwordEncoder.encode(password));
user.setUsername(username);
user.setRealname(username);
user.setJoinTime(new Date());
User userInsert = usersService.insert(user);
map.put("msg", "注册成功,请登陆!");
return "login";
}
}
/**
* 注册页面
*
* @return
*/
@RequestMapping("/register")
public String register() {
return "register";
}
/**
* 搜索页
*/
@GetMapping("/search")
public String search(@RequestParam(defaultValue = "") String q,
@RequestParam(defaultValue = "1") Integer pageNum,
HttpServletRequest request, ModelMap map) {
String keyword = q.trim();
int pageSize = 10;
int page = Math.max(pageNum - 1, 0);
Page<Discussion> resultPage = keyword.isEmpty()
? Page.empty()
: discussionsRepository.searchByKeyword(keyword,
org.springframework.data.domain.PageRequest.of(page, pageSize));
List<DiscussionCustom> results = new ArrayList<>();
if (!keyword.isEmpty()) {
setTagAndUserList(results, resultPage.getContent());
}
map.put("q", keyword);
map.put("results", results);
map.put("totalPages", resultPage.getTotalPages());
map.put("totalElements", resultPage.getTotalElements());
map.put("pageNum", pageNum);
UserInfo user = getUserInfo(request);
if (user != null) map.put("username", user.getUsername());
return "search";
}
//获取设置信息
//获取首页统计
}