提交 7f394e90 编写于 作者: 7 7wc98#14

add function

上级 168c0318
......@@ -25,5 +25,6 @@ public class WebMVCConfig extends WebMvcConfigurerAdapter {
// registry.addViewController("/home").setViewName("page/Home");
registry.addViewController("/aboutLearn").setViewName("page/AboutLearn");
registry.addViewController("/publicChatRoom").setViewName("PublicChatRoom");
registry.addViewController("/chat").setViewName("page/PrivateChat");
}
}
......@@ -20,10 +20,11 @@ public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/endpointPublicChat").withSockJS();
registry.addEndpoint("/endPointChat").withSockJS();
}
@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
registry.enableSimpleBroker("/publicChat");
registry.enableSimpleBroker("/queue", "/publicChat");
}
}
......@@ -9,10 +9,16 @@ package com.pyc.campus.controller;
import com.pyc.campus.domain.PublishMessage;
import com.pyc.campus.domain.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.messaging.handler.annotation.DestinationVariable;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Controller;
import java.security.Principal;
@Controller
public class ChatController {
@MessageMapping("/publicChatRoom")
......@@ -21,4 +27,11 @@ public class ChatController {
Thread.sleep(500);
return new ResponseMessage(message.getName()+":"+message.getContent());
}
@Autowired
private SimpMessagingTemplate messagingTemplate;
@MessageMapping(value = "/chat/{name}")
public void handleChat(Principal principal, String msg, @DestinationVariable String name){
messagingTemplate.convertAndSendToUser(name,"/queue/notification",principal.getName()+"-send:"+msg);
}
}
......@@ -163,6 +163,68 @@ public class WebController {
model.addAttribute("fl",fl);
return "page/VerifyFriend";
}
@RequestMapping("/verifyFriend")
public String verifyFried(Model model, HttpSession session,
@Param("fromName")String fromName){
SecurityContextImpl securityContext = (SecurityContextImpl)session.getAttribute("SPRING_SECURITY_CONTEXT");
String currentStudentId = ((UserDetails) securityContext.getAuthentication().getPrincipal()).getUsername();
Student s = studentRepository.findNameByStudentID(currentStudentId);
int result = friendListRepository.setStatus(fromName,currentStudentId);
List<FriendList> fl = friendListRepository.toNameIsFalseByToName(currentStudentId);
int len = fl.size();
if(result!=0){
Msg msg = new Msg("待验证好友申请数量:", "一共"+len+"个","");
model.addAttribute("msg",msg);
model.addAttribute("curUse",s);
model.addAttribute("fl",fl);
return "page/VerifyFriend";
}
Msg msg = new Msg("错误:", "请检查输入的学号","");
model.addAttribute("msg",msg);
model.addAttribute("curUse",s);
model.addAttribute("fl",fl);
return "page/VerifyFriend";
}
@RequestMapping("/toPrivateChat")
public String toPrivateChat(Model model,HttpSession session,
@Param("toName")String toName){
SecurityContextImpl securityContext = (SecurityContextImpl)session.getAttribute("SPRING_SECURITY_CONTEXT");
String currentStudentId = ((UserDetails) securityContext.getAuthentication().getPrincipal()).getUsername();
boolean s;
FriendList s1 = friendListRepository.getStatus(currentStudentId,toName);
FriendList s2 = friendListRepository.getStatus(toName,currentStudentId);
if(s1!=null)
s=s1.getStatus();
else if (s2!=null)
s=s2.getStatus();
else
s=false;
model.addAttribute("friendStatus",s);
if(s)
{
boolean stu = studentRepository.getOnlineStatus(toName).isOnlineStatus();
model.addAttribute("onlineStatus",stu);
if(stu)
{
Msg msg = new Msg("提示:","对方在线,当前只提供临时聊天,不保存聊天记录,见谅","");
model.addAttribute("fromName",currentStudentId);
model.addAttribute("toName",toName);
model.addAttribute("msg",msg);
return "page/PrivateChat";
}
Msg msg = new Msg("提示:","对方不在线,你所发送的消息,可能无法被及时回复","");
model.addAttribute("fromName",currentStudentId);
model.addAttribute("toName",toName);
model.addAttribute("msg",msg);
return "page/PrivateChat";
}
Msg msg = new Msg("提示:","对方不是你的好友,无法进行私聊","");
model.addAttribute("fromName",currentStudentId);
model.addAttribute("toName",toName);
model.addAttribute("msg",msg);
return "page/PrivateChat";
}
@RequestMapping("/toChangePWD")
public String toChangePWD(Model model,HttpSession session){
SecurityContextImpl securityContext = (SecurityContextImpl)session.getAttribute("SPRING_SECURITY_CONTEXT");
......
......@@ -33,5 +33,10 @@ public interface FriendListRepository extends JpaRepository<FriendList,Long> {
@Transactional
@Query("select fl from FriendList fl where fl.toName=?1 and fl.status=true")
List<FriendList> findMyFriendsByToName(String toName);
@Modifying
@Transactional
@Query("update FriendList fl set fl.status=true where fl.fromName=?1 and fl.toName=?2")
int setStatus(String fromName, String toName);
@Query("select fl from FriendList fl where fl.fromName=?1 and fl.toName=?2")
FriendList getStatus(String fromName, String toName);
}
......@@ -31,6 +31,9 @@ public interface StudentRepository extends JpaRepository<Student,Long> {
@Transactional
@Query("update Student s set s.onlineStatus=?1 where s.studentID=?2")
void setOnlineStatus(Boolean onlineStatus, String studentID);
@Query("select s from Student s where s.studentID=?1")
Student getOnlineStatus(String studentID);
// 根据Student ID前缀查询
@Modifying
@Transactional
......
......@@ -150,10 +150,12 @@
<h3 class="panel-title">好友列表</h3>
</div>
<div class="panel-body">
<ul class="list-group" th:if="${not #lists.isEmpty(Fl) or #lists.isEmpty(Fl1)}">
<ul class="list-group" th:if="${not #lists.isEmpty(Fl)}">
<li class="list-group-item list-group-item-info" th:each="FL:${Fl}">
<span th:text="${FL.getToName()}"></span>
</li>
</ul>
<ul class="list-group" th:if="${not #lists.isEmpty(Fl1)}">
<li class="list-group-item list-group-item-info" th:each="FL1:${Fl1}">
<span th:text="${FL1.getFromName()}"></span>
</li>
......@@ -181,7 +183,7 @@
</li>
<li class="list-group-item-heading">请输入要发起私聊的好友学号:</li>
<li class="list-group-item-text">
<form name="chatFrom" method="post" action="#" onsubmit="check()">
<form name="chatFrom" method="post" action="/toPrivateChat" th:action="@{/toPrivateChat}" onsubmit="check()" target="_blank">
<label>
<input type="text" name="toName" placeholder="示例:201724073161">
</label>
......
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>私聊</title>
<script src="js/stomp.min.js"></script>
<script src="js/sockjs.min.js"></script>
<script src="js/jquery.js"></script>
<!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css"
integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<!-- 可选的 Bootstrap 主题文件(一般不用引入) -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap-theme.min.css"
integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
<!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js"
integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa"
crossorigin="anonymous"></script>
<script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
</head>
<body>
<div class="panel panel-primary">
<div class="page-header">
<h3 class="panel-title">注意</h3>
</div>
<div class="panel-body">
<div class="panel-heading" th:text="${msg.title}"></div>
<div class="panel-info" th:text="${msg.content}"></div>
</div>
</div>
<div th:if="${friendStatus}">
<p>聊天室</p>
<form id="chatForm">
<label for="fromName">我:</label>
<input type="text" id="fromName" name="fromName" th:value="${fromName}" readonly="readonly">
<br>
<label for="toName">对方:</label>
<input type="text" id="toName" name="toName" th:value="${toName}" readonly="readonly">
<br>
<label>
<textarea rows="4" cols="60" name="text"></textarea>
</label>
<br>
<input type="submit" value="发送">
</form>
<div class="col-md-8" id="output"></div>
</div>
<script th:inline="javascript">
$('#chatForm').submit(function (e) {
e.preventDefault();
var text = $('#chatForm').find('textarea[name="text"]').val();
var fromName = $('#chatForm').find('input[name="fromName"]').val();
var toName = $('#chatForm').find('input[name="toName"]').val();
sendSpittle(text,fromName,toName);
});
var sock = new SockJS("/endPointChat");
var stomp = Stomp.over(sock);
stomp.connect('guest','guest',function (frame) {
stomp.subscribe("/user/queue/notification",handleNotification);
});
function handleNotification(message) {
$('#output').append("</br>Received: "+message.body+"</b></br>")
}
function sendSpittle(text,fromName,toName) {
stomp.send("/chat/"+toName,{},text);
}
$('#stop').click(function () {
sock.close()
});
</script>
</body>
\ No newline at end of file
......@@ -138,6 +138,7 @@
<div class="panel-body">
<ul class="list-group" th:if="${not #lists.isEmpty(fl)}">
<li class="list-group-item list-group-item-info" th:each="TFL:${fl}">
<!--/*@thymesVar id="getFromName" type="com.pyc.campus.domain.FriendList"*/-->
<span th:text="${TFL.getFromName()}"></span>
</li>
</ul>
......@@ -145,10 +146,37 @@
</div>
</div>
</div>
<!--TODO:编辑通过好友申请的功能-->
<div class="col-md-8">
<div class="col-md-6">
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">好友申请确认</h3>
</div>
<div class="panel-body">
<ul class="list-group">
<li class="list-group-item-heading">请输入通过好友申请的学号:</li>
<li class="list-group-item-text">
<form name="browseFriendInfo" method="post" th:action="@{/verifyFriend}" action="/verifyFriend" onsubmit="return check(browseFriendInfo.toName.value)">
<label>
<input type="text" name="fromName" placeholder="示例:201724073161">
</label>
<input class="btn-primary" type="submit" value="确认">
</form>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
</div>
<script type="text/javascript">
function check(value) {
if(value!=="")
return true;
return false;
}
</script>
<div id="after_sale">
<div class="container">
<ul>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册