Skip to content

Commit

Permalink
Try to dock the front end
Browse files Browse the repository at this point in the history
  • Loading branch information
HWZen committed May 27, 2022
1 parent 45318a0 commit 706f56f
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 13 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
.idea
lib
out
target
target
消息中心*
css
*.rar
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>RELEASE</version>
<version>7.5</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
22 changes: 19 additions & 3 deletions src/main/java/activate/LoginActivate.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,26 +45,42 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) t
String id = request.getParameter("name");
String pwd = request.getParameter("password");
String code = request.getParameter("code");
String cookie = id;
String cookie = null;
Cookie[] cookies = request.getCookies();
if(cookies == null) {
Cookie uid_cookie = new Cookie("uid", id);
if(cookies.length == 1) {
cookie = cookies[0].getValue();
Cookie uid_cookie = new Cookie("browser_uid", cookie);
uid_cookie.setMaxAge(24*60*60); //一天
response.addCookie(uid_cookie);
}else{
for (Cookie c : cookies) {
if(c.getName().equals("browser_uid")) {
cookie = c.getValue();
break;
}
}
if(cookie == null) {
throw new RuntimeException("no browser_uid");
}
}

if(!isCorrectCode(code)) {
System.out.println("code error");
request.setAttribute("loginMessage","验证码错误");
request.getRequestDispatcher("/WEB-INF/pages/login.jsp").forward(request, response);
return;
}
else if(!isCorrectUser(id,pwd,cookie)){
System.out.println("user error");
request.setAttribute("loginMessage","用户名/密码错误");
request.getRequestDispatcher("/WEB-INF/pages/login.jsp").forward(request, response);
return;
}
else {
System.out.println("login success");
request.setAttribute("userID", id);
request.setAttribute("userID",id);
request.setAttribute("firstLogin",true);
request.getRequestDispatcher("/message.jhtml").forward(request, response);
return;
}
Expand Down
43 changes: 41 additions & 2 deletions src/main/java/activate/MessageActivate.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

import entity.Msg;
import entity.ChatSession;
import server.CommandManager;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
Expand All @@ -26,16 +29,52 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) t
msgs.add(cur);
request.setAttribute("messages", msgs);
}
doPost(request,response);
// doPost(request,response);
System.out.println(topic);
request.setAttribute("messages", msgs);
request.setAttribute("TopicList", topics);
request.setAttribute("topicName",topic);
request.getRequestDispatcher("/WEB-INF/pages/chat.jsp").forward(request, response);
}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Boolean isFirst = (Boolean)request.getAttribute("firstLogin");
if(isFirst == null || isFirst){
Cookie[] cookies = request.getCookies();
String browser_uid = null;
for (Cookie c : cookies) {
if(c.getName().equals("browser_uid")) {
browser_uid = c.getValue();
break;
}
}
if(browser_uid == null) {
throw new ServletException("No browser_uid");
}
try {
topics = CommandManager.getChatSessions(browser_uid);
assert topics != null;
if(topics.size() > 0){
topic = topics.get(0).getSessionName();
if(topic.equals("")){

topic = topics.get(0).getSessionMemberIds().get(0) == request.getAttribute("userId") ? topics.get(0).getSessionMemberIds().get(1) : topics.get(0).getSessionMemberIds().get(0);
msgs = CommandManager.getSessionMsg(browser_uid, topics.get(0).getSessionName());
}
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
request.setAttribute("firstLogin", false);
}
System.out.println("message post");
System.out.println(topic);
request.setAttribute("messages", msgs);
request.setAttribute("TopicList", topics);
request.setAttribute("topicName",topic);
request.getRequestDispatcher("/WEB-INF/Pages/chat.jsp").forward(request, response);
request.getRequestDispatcher("/WEB-INF/pages/chat.jsp").forward(request, response);
}
}


12 changes: 12 additions & 0 deletions src/main/java/server/CommandManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import entity.*;
import entity.sql.ChatSessionInterImpl;
import entity.sql.MsgInterImpl;
import entity.sql.UserInterImpl;

import java.sql.SQLException;
Expand All @@ -12,11 +13,13 @@ public class CommandManager {

private static UserInter userInter;
private static ChatSessionInter chatSessionInter;
private static MsgInter msgInter;

static {
try {
userInter = new UserInterImpl();
chatSessionInter = new ChatSessionInterImpl();
msgInter = new MsgInterImpl();
} catch (SQLException e) {
e.printStackTrace();
}
Expand Down Expand Up @@ -59,6 +62,13 @@ static public List<ChatSession> getChatSessions(String cookie) throws SQLExcepti
return chatSessionInter.getAllSessions(user.getId());
}

static public List<Msg> getSessionMsg(String cookie, String sessionId) throws SQLException {
User user = AuthorityManager.GetUser(new Cookie(cookie));
if(user == null)
return null;
return msgInter.bySessionId(sessionId);
}

static public ChatSession addFriend(String cookie, String friendId) throws SQLException {
User user = AuthorityManager.GetUser(new Cookie(cookie));
if(user == null)
Expand Down Expand Up @@ -152,4 +162,6 @@ static public boolean deleteGroup(String cookie, String groupId) throws SQLExcep
}




}
1 change: 1 addition & 0 deletions src/test/java/FriendTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class FriendTest {
public FriendTest() throws SQLException {
}

@Test
public void addAndDeleteFriendTest() throws SQLException {
System.out.println("addFriendTest");
User user = userInter.byId("[email protected]");
Expand Down
4 changes: 4 additions & 0 deletions src/test/java/GroupTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class GroupTest {
public GroupTest() throws SQLException {
}

@Test
public void createGroup() throws SQLException {
System.out.println("createGroup");
UserInter userInter = new UserInterImpl();
Expand Down Expand Up @@ -56,6 +57,7 @@ public void createGroup() throws SQLException {
assert chatSession.getOwnerId().equals(users[0].getId());
}

@Test
public void groupChatTest() throws SQLException {
int size = msgInter.bySessionId("-1").size();
Msg msg = new Msg("[email protected]", "-1", new Date(), "Hello, I'm test0");
Expand All @@ -71,6 +73,7 @@ public void groupChatTest() throws SQLException {

}

@Test
public void deleteGroup() throws SQLException {
System.out.println("deleteGroup");
ChatSessionInter chatSessionInter = new ChatSessionInterImpl();
Expand All @@ -89,6 +92,7 @@ public void deleteGroup() throws SQLException {

}

@Test
public void updateGroup() throws SQLException {
System.out.println("editGroup");
ChatSession cs = chatSessionInter.byId("-1");
Expand Down
2 changes: 2 additions & 0 deletions src/test/java/LoginTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
public class LoginTest {
static String cookie = "TEST_COOKIE";

@Test
public void logintest() throws SQLException {
UserInter userInter = new UserInterImpl();
User user = userInter.byId("[email protected]");
Expand All @@ -24,6 +25,7 @@ public void logintest() throws SQLException {

}

@Test
public void logouttest() throws SQLException {
UserInter userInter = new UserInterImpl();
User user = userInter.byId("[email protected]");
Expand Down
12 changes: 6 additions & 6 deletions web/WEB-INF/pages/chat.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@
<div data-v-9beceada="" class="list-container ps ps--active-y">
<div data-v-9beceada="" class="list">
<c:forEach items="${TopicList}" var="topic">
<c:if test="${topic.getTopicName() == topicName}">
<div data-v-42a3b689="" data-v-9beceada="" class="list-item active" id="${topic.getTopicName()}" onclick="topicSwitch(this.id)">
<c:if test="${topic.getSessionName() == topicName}">
<div data-v-42a3b689="" data-v-9beceada="" class="list-item active" id="${topic.getSessionName()}" onclick="topicSwitch(this.id)">
<div data-v-42a3b689="" class="name-box">
<div data-v-42a3b689="" class="name" title="topic-name">${topic.getTopicName()}</div>
<div data-v-42a3b689="" class="name" title="topic-name">${topic.getSessionName()}</div>
<div data-v-42a3b689="" title="" class="last-word"></div>
</div>
<div data-v-42a3b689="" class="close">
Expand All @@ -71,10 +71,10 @@
<!---->
</div>
</c:if>
<c:if test="${topic.getTopicName() != topicName}">
<div data-v-42a3b689="" data-v-9beceada="" class="list-item" id="${topic.getTopicName()}" onclick="topicSwitch(this.id)">
<c:if test="${topic.getSessionName() != topicName}">
<div data-v-42a3b689="" data-v-9beceada="" class="list-item" id="${topic.getSessionName()}" onclick="topicSwitch(this.id)">
<div data-v-42a3b689="" class="name-box">
<div data-v-42a3b689="" class="name" title="topic-name">${topic.getTopicName()}</div>
<div data-v-42a3b689="" class="name" title="topic-name">${topic.getSessionName()}</div>
<div data-v-42a3b689="" title="" class="last-word"></div>
</div>
<div data-v-42a3b689="" class="close">
Expand Down

0 comments on commit 706f56f

Please sign in to comment.