package com.example.mybatisplus.web.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.mybatisplus.common.JsonResponse;
import com.example.mybatisplus.common.utls.MD5Utils;
import com.example.mybatisplus.common.utls.SendCodeUtils;
import com.example.mybatisplus.common.utls.TokenUtils;
import com.example.mybatisplus.mapper.UserMapper;
import com.example.mybatisplus.model.domain.User;
import com.example.mybatisplus.service.MailService;
import com.example.mybatisplus.service.RedisService;
import com.example.mybatisplus.service.UserService;
import org.checkerframework.checker.units.qual.A;
import org.json.simple.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
*
* 前端控制器
*
*
* @author lgl
* @since 2022-03-01
* @version v1.0
*/
@Controller
@RequestMapping("/api/user")
@CrossOrigin
public class UserController {
private final Logger logger = LoggerFactory.getLogger( UserController.class );
@Autowired
private UserService userService;
@Autowired
private MailService mailService;
@Autowired
private RedisService redisService;
@Autowired
private UserMapper userMapper;
/**
* 描述:根据Id 查询
*
*/
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
@ResponseBody
public JsonResponse getById(@PathVariable("id") Long id)throws Exception {
User user = userService.getById(id);
return JsonResponse.success(user);
}
/**
* 描述:查询是否存在手机号
*
*/
@RequestMapping(value = "/phone",method = RequestMethod.POST)
@ResponseBody
public JsonResponse hasPhone(@RequestBody Map requestmap) throws Exception {
String phone = (String) requestmap.get("phone");
Boolean hasPhone = userService.hasPhone(phone);
System.out.println(111);
return JsonResponse.success(hasPhone);
}
/**
* 描述:查询是否存在邮箱
*
*/
@RequestMapping(value = "/email",method = RequestMethod.POST)
@ResponseBody
public JsonResponse hasEmail(@RequestBody Map requestmap) throws Exception {
String email = (String) requestmap.get("email");
Boolean hasEmail = userService.hasEmail(email);
Map<String,Boolean> map = new HashMap<>();
map.put("msg",hasEmail);
return JsonResponse.success(map);
}
/**
* 描述:查询是否存在用户名
*
*/
@RequestMapping(value = "/username",method = RequestMethod.POST)
@ResponseBody
public JsonResponse hasUserName(@RequestBody Map requestmap) throws Exception {
String username = (String) requestmap.get("username");
Boolean hasUserName = userService.hasUserName(username);
Map<String,Boolean> map = new HashMap<>();
map.put("msg",hasUserName);
return JsonResponse.success(map);
}
/**
* 描述:通过邮箱查找账号和密码
*
*/
@RequestMapping(value = "/find/vertificationByEmail",method = RequestMethod.POST)
@ResponseBody
public JsonResponse findByEmail(@RequestBody Map requestmap) throws Exception {
String email = (String) requestmap.get("email");
String email_code = (String) requestmap.get("email_code");
System.out.println(email_code);
System.out.println(redisService.get(email));
if (redisService.get(email).toString().equals(email_code)) {
Map<String,String> map = userService.findByEmail(email);
return JsonResponse.success(map);
} else {
return JsonResponse.success(null);
}
}
/**
* 描述:发送邮箱验证码
*
*/
@RequestMapping(value = "/find/getEmailCode",method = RequestMethod.POST)
@ResponseBody
public JsonResponse sendEmailCode( @RequestBody Map requestmap) throws Exception {
String email = (String) requestmap.get("email");
String msg= "";
String code = SendCodeUtils.randomCode();
String text= "您的验证码为:" + code +",两分钟内有效。请不要将验证码转发给他人,注意保护您的信息安全!";
mailService.sendTextMailMessage(email,"验证码",text);
//发送验证码,并存到session中,设置失效时间为120s
redisService.set(email,code);
msg += "邮箱验证码已发送";
return JsonResponse.success(msg);
}
/**
* 描述:先判断邮箱是否已经注册,再发送邮箱验证码
*
*/
@RequestMapping(value = "/find/EmailCode",method = RequestMethod.POST)
@ResponseBody
public JsonResponse emailCode( @RequestBody Map requestmap) throws Exception {
String email = (String) requestmap.get("email");
String msg= "";
if(!userService.hasEmail(email)) {
String code = SendCodeUtils.randomCode();
String text= "您的验证码为:" + code +",两分钟内有效。请不要将验证码转发给他人,注意保护您的信息安全!";
mailService.sendTextMailMessage(email,"验证码",text);
//发送验证码,并存到session中,设置失效时间为120s
redisService.set(email,code);
msg += "邮箱验证码已发送";
} else {
msg += "邮箱已注册";
}
Map<String,String> map = new HashMap<>();
map.put("msg",msg);
return JsonResponse.success(map);
}
/**
* 描述:修改头像、sex信息
*
*/
@RequestMapping(value = "/updateUserData",method = RequestMethod.POST)
@ResponseBody
public JsonResponse updateMessage(@RequestBody Map requestmap) {
String image = (String) requestmap.get("headimage");
String nickname = (String) requestmap.get("nickname");
String phone = (String) requestmap.get("phone");
short sex = ((Integer) requestmap.get("sex")).shortValue();
Long id = ((Integer) requestmap.get("id")).longValue();
String username = (String) requestmap.get("username");
LocalDateTime dateTime = LocalDateTime.now();
System.out.println(sex);
userService.updateMessage(image, sex, id,dateTime,nickname,phone,username);
return JsonResponse.success(null);
}
/**
* 描述:修改手机号信息,返回新的手机号信息
*
*/
@RequestMapping(value = "/update/phone",method = RequestMethod.POST)
@ResponseBody
public JsonResponse updatePhone(@RequestBody Map requestmap) {
String phone = (String) requestmap.get("phone");
Long id = ((Integer) requestmap.get("id")).longValue();
LocalDateTime dateTime = LocalDateTime.now();
userService.updatePhone(phone, id, dateTime);
User user = userService.getById(id);
return JsonResponse.success(user.getPhone());
}
/**
* 描述:修改邮箱信息
*
*/
@RequestMapping(value = "/update/email",method = RequestMethod.POST)
@ResponseBody
public JsonResponse updateEmail(@RequestBody Map requestmap) {
String email = (String) requestmap.get("email");
String emailCode = (String) requestmap.get("emailCode");
Map<String,String> map = new HashMap<>();
System.out.println(email +","+emailCode);
System.out.println(redisService.get(email));
Long id =((Integer) requestmap.get("id")).longValue();
if(redisService.get(email).toString().equals(emailCode)) {
LocalDateTime dateTime = LocalDateTime.now();
userService.updateEmail(email, id,dateTime);
System.out.pr