231 lines
8.4 KiB
Java
231 lines
8.4 KiB
Java
package com.lovenav.controller;
|
||
|
||
import com.alibaba.fastjson2.JSONObject;
|
||
import com.lovenav.entity.LoginLogs;
|
||
import com.lovenav.entity.UrlList;
|
||
import com.lovenav.entity.User;
|
||
import com.lovenav.service.ConfigService;
|
||
import com.lovenav.service.LoginLogsService;
|
||
import com.lovenav.service.UserService;
|
||
import com.lovenav.utils.*;
|
||
import org.apache.commons.lang.StringUtils;
|
||
import org.springframework.beans.factory.annotation.Autowired;
|
||
import org.springframework.boot.autoconfigure.web.ServerProperties;
|
||
import org.springframework.http.HttpHeaders;
|
||
import org.springframework.http.HttpRequest;
|
||
import org.springframework.mail.javamail.JavaMailSenderImpl;
|
||
import org.springframework.stereotype.Controller;
|
||
import org.springframework.ui.Model;
|
||
import org.springframework.web.bind.annotation.*;
|
||
|
||
import javax.servlet.*;
|
||
import javax.servlet.http.HttpServletRequest;
|
||
import javax.servlet.http.HttpServletResponse;
|
||
import javax.servlet.http.HttpSession;
|
||
import javax.websocket.Session;
|
||
import java.io.IOException;
|
||
import java.io.PrintWriter;
|
||
import java.util.HashMap;
|
||
import java.util.Map;
|
||
import java.util.concurrent.Executors;
|
||
import java.util.concurrent.ScheduledExecutorService;
|
||
import java.util.concurrent.TimeUnit;
|
||
|
||
@RestController
|
||
public class UserController {
|
||
@Autowired
|
||
UserService userService;
|
||
@Autowired
|
||
TokenUtils tokenUtils;
|
||
@Autowired
|
||
LoginLogsService loginLogsService;
|
||
@Autowired
|
||
ConfigService configService;
|
||
@Autowired
|
||
IPutils iPutils;
|
||
//发送邮箱验证码
|
||
ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(5);
|
||
//这个是我用户Service实现类可以自行替换
|
||
|
||
//这个是邮件类,必须要导入哦
|
||
|
||
@GetMapping("/sendActiveCode")
|
||
public String sendActiveCode(HttpSession session, User user){
|
||
HashMap<String,String> configMap=new HashMap<>();
|
||
configMap=configService.selectEmailConfig();
|
||
String activecode=userService.sendEmailActivecode(user,configMap);
|
||
if (activecode=="配置文件有错误"){
|
||
return "邮箱配置文件有错误,请重新确认!";
|
||
} else if (activecode=="该邮箱不存在") {
|
||
return "将要发送的邮箱不存在!";
|
||
}
|
||
session.setAttribute(user.getUserEmail(),activecode);
|
||
scheduledExecutorService.schedule(new Runnable() {
|
||
@Override
|
||
public void run() {
|
||
session.removeAttribute(user.getUserEmail());
|
||
System.out.println(session.getAttribute(user.getUserEmail()));
|
||
}
|
||
},60, TimeUnit.SECONDS);
|
||
return "发送验证码成功!";
|
||
}
|
||
@RequestMapping("/register")
|
||
public String userRegister(HttpSession session,User user){
|
||
if (session.getAttribute(user.getUserEmail())==null){
|
||
return "验证码过期";
|
||
}
|
||
// 比较验证码
|
||
if (!user.getActiveCode().equals((String) session.getAttribute(user.getUserEmail()))) {
|
||
return "验证码不正确";
|
||
}
|
||
System.out.println(user.getUserLogin());
|
||
System.out.println(user.getUserEmail());
|
||
// 用户注册之前根据用户名称查询该用户是否存在如果不存在的情况下才可以注册 如果存在的话就无法注册
|
||
User user1=userService.selectUserAlreadyExist(user);
|
||
if (user1 != null) {
|
||
return "用户名: " + user1.getUserEmail() + "已被注册!";
|
||
}
|
||
//用户数据注册
|
||
|
||
int register = userService.UserRegister(user);
|
||
if (register <= 0) {
|
||
// 注册失败了 //转发到错误页面
|
||
return "注册失败!";
|
||
}
|
||
return "注册成功!";
|
||
}
|
||
@RequestMapping(value = "/login",produces = {"application/json;charset=UTF-8"})
|
||
public Map<String,Object>login(User user, String code, HttpSession session, HttpServletRequest request){
|
||
LoginLogs loginLogs=new LoginLogs();
|
||
Map<String,Object> result=new HashMap<>();
|
||
Map<String,Object> map=new HashMap<>();
|
||
|
||
String sessionCode = (String) session.getAttribute(RandomValidateCode.RANDOMVALIDATECODE);
|
||
if (sessionCode==null){
|
||
result.put("msg","验证码空的");
|
||
return result;
|
||
}
|
||
if (!sessionCode.equals(code)){
|
||
result.put("msg","验证码错误");
|
||
return result;
|
||
}
|
||
|
||
if(StringUtils.isEmpty(user.getUserLogin())||StringUtils.isEmpty(user.getUserPassword())){
|
||
result.put("msg","用户或密码为空!");
|
||
return result;
|
||
}
|
||
|
||
|
||
String ip=IPutils.getIpAddress(request);
|
||
System.out.println(ip);
|
||
|
||
String locat= String.valueOf(IPutils.getLocation(ip));
|
||
User user1 = userService.userLogin(user);
|
||
|
||
if(user1!=null){
|
||
loginLogs.setUserId(user1.getId());
|
||
loginLogs.setLoginIp(ip);
|
||
loginLogs.setLocation(locat);
|
||
loginLogsService.addLoginLogs(loginLogs);
|
||
result.put("code",200);
|
||
map.put("userEmail",user1.getUserEmail());
|
||
map.put("userLogin",user1.getUserLogin());
|
||
String token= TokenUtils.createJWT(map,10000000L);
|
||
result.put("user",user1.getUserEmail());
|
||
result.put("userStatus",user1.getUserStatus());
|
||
result.put("Id",user1.getId());
|
||
result.put("nickname",user1.getNickname());
|
||
result.put("roleId",user1.getRoleId());
|
||
result.put("userRegistered",user1.getUserRegistered());
|
||
result.put("token",token);
|
||
}else {
|
||
result.put("msg","用户名或密码错误!");
|
||
}
|
||
return result;
|
||
}
|
||
/*图片验证码*/
|
||
@RequestMapping("/verifyCode")
|
||
public void verifyCode(HttpServletRequest request,HttpServletResponse response){
|
||
response.setContentType("image/jpeg");//设置相应类型,告诉浏览器输出的内容为图片
|
||
response.setHeader("Pragma", "No-cache");//设置响应头信息,告诉浏览器不要缓存此内容
|
||
response.setDateHeader("Expire", 0);
|
||
RandomValidateCode randomValidateCode = new RandomValidateCode();
|
||
try {
|
||
randomValidateCode.getRandcode( request, response);//输出图片方法
|
||
} catch (Exception e) {
|
||
e.printStackTrace();
|
||
}
|
||
|
||
}
|
||
//找回妈咪
|
||
@RequestMapping("/findThePassword")
|
||
public Map<String,Object> findThePassword(User user,HttpSession session){
|
||
Map<String,Object> map=new HashMap<>();
|
||
|
||
User user1=userService.selectUserAlreadyExist(user);
|
||
System.out.println(user1);
|
||
if (user1==null){
|
||
map.put("msg","无此邮箱");
|
||
return map;
|
||
}
|
||
// 比较验证码
|
||
if (!user.getActiveCode().equals((String) session.getAttribute(user.getUserEmail()))) {
|
||
map.put("msg","验证码不正确") ;
|
||
return map;
|
||
}
|
||
|
||
|
||
if (user1 != null) {
|
||
int result=userService.updatePassword(user1);
|
||
if(result==1){
|
||
map.put("Data",user1.getUserLogin());
|
||
map.put("Data",user1.getUserEmail());
|
||
map.put("msg","重置成功");
|
||
return map;
|
||
}else {
|
||
map.put("msg","重置失败");
|
||
return map;
|
||
}
|
||
}
|
||
|
||
return map;
|
||
|
||
}
|
||
@RequestMapping("/getAllUsers")
|
||
public HashMap<Integer,Object> getAllUsers(){
|
||
return userService.getAllUsers();
|
||
}
|
||
|
||
@RequestMapping("/updateUserDetails")
|
||
public HashMap<String,Object> updateUserDetails(User user){
|
||
|
||
HashMap<String,Object> map=new HashMap<>();
|
||
if (user==null){
|
||
map.put("msg","用户不能为空!");
|
||
return map;
|
||
}
|
||
map.put("code",200);
|
||
map.put("msg",userService.updateUserMessage(user));
|
||
|
||
map.put("user",userService.selectByUserId(user));
|
||
return map;
|
||
|
||
}
|
||
|
||
@RequestMapping("/deleteUser")
|
||
public String deleteUser(User user){
|
||
if (user==null){
|
||
return "请输入删除的用户的信息ID!";
|
||
}
|
||
return userService.deleteUser(user);
|
||
|
||
}
|
||
// @RequestMapping("/updateStatus")
|
||
// public String updateStatus(User user){
|
||
// if (user==null){
|
||
// return "用户不能为空!";
|
||
// }
|
||
// return userService.updateUserMessage(user);
|
||
// }
|
||
}
|