297 lines
11 KiB
Java
297 lines
11 KiB
Java
package com.peanut.modules.common.controller;
|
|
|
|
import com.alibaba.druid.util.StringUtils;
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
import com.peanut.common.utils.MD5Utils;
|
|
import com.peanut.common.utils.MailUtil;
|
|
import com.peanut.common.utils.R;
|
|
import com.peanut.modules.common.entity.MyUserEntity;
|
|
import com.peanut.modules.common.service.MyUserService;
|
|
import com.peanut.modules.sys.service.SysUserTokenService;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
|
import org.springframework.web.bind.annotation.RequestBody;
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
import org.springframework.web.bind.annotation.RequestParam;
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
import java.util.Map;
|
|
import java.util.Random;
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
/**
|
|
* 用户登陆注册验证码
|
|
*/
|
|
@Slf4j
|
|
@RestController("commonUser")
|
|
@RequestMapping("common/user")
|
|
public class UserController {
|
|
|
|
@Autowired
|
|
private MyUserService userService;
|
|
@Autowired
|
|
private StringRedisTemplate redisTemplate;
|
|
@Autowired
|
|
private SysUserTokenService sysUserTokenService;
|
|
|
|
/**
|
|
* 常规注册 发短信验证码
|
|
*/
|
|
@RequestMapping("/sms/sendcode")
|
|
public R registerSms(@RequestParam("phone") String phone, @RequestParam(required = false) String areaCode) throws Exception {
|
|
int areacode=0;
|
|
if(areaCode==null||areaCode.equals("")){
|
|
areacode=0;
|
|
}else{
|
|
areacode = Integer.valueOf(areaCode);
|
|
}
|
|
//验证一分钟内是否已经发过
|
|
String redisCode = redisTemplate.opsForValue().get("RegistCode" + phone);
|
|
if (!StringUtils.isEmpty(redisCode)) {
|
|
long l = Long.parseLong(redisCode.split("_")[1]);
|
|
if (System.currentTimeMillis() - l < 60000) {
|
|
//60s 内不能再发
|
|
return R.error(500,"短信验证码频率过高,请稍后再试!");
|
|
}
|
|
}
|
|
//生成随机五位数
|
|
Random random = new Random();
|
|
String i = random.nextInt(99999) + "";
|
|
StringBuffer sb = new StringBuffer();
|
|
for (int j = 0; j < 5 - i.length(); j++) {
|
|
sb.append("0");
|
|
}
|
|
i = sb.toString() + i;
|
|
String code = i + "_"+System.currentTimeMillis();
|
|
//redis 缓存验证码
|
|
redisTemplate.opsForValue().set("RegistCode"+phone,code,5, TimeUnit.MINUTES);
|
|
//发送
|
|
userService.sendCodeForRegister(phone,code,areacode);
|
|
return R.ok();
|
|
}
|
|
|
|
/**
|
|
* 常规注册 发送邮箱验证码
|
|
*/
|
|
@RequestMapping("/getMailCaptcha")
|
|
public R getMailCaptcha(String email){
|
|
//验证一分钟内是否已经发过
|
|
String redisCode = redisTemplate.opsForValue().get("RegistCode" + email);
|
|
if (!StringUtils.isEmpty(redisCode)) {
|
|
long l = Long.parseLong(redisCode.split("_")[1]);
|
|
if (System.currentTimeMillis() - l < 60000) {
|
|
//60s 内不能再发
|
|
return R.error(500,"验证码频率过高,请稍后再试!");
|
|
}
|
|
}
|
|
//生成随机五位数
|
|
Random random = new Random();
|
|
String code = random.nextInt(99999) + "";
|
|
String timeCode = code + "_"+System.currentTimeMillis();
|
|
//redis 缓存验证码
|
|
redisTemplate.opsForValue().set("RegistCode"+email,timeCode,5, TimeUnit.MINUTES);
|
|
//发送
|
|
return MailUtil.sendMail("疯子读书邮箱验证码",code,email);
|
|
}
|
|
|
|
/**
|
|
* 绑定电话号
|
|
* @return
|
|
*/
|
|
@RequestMapping("/updateUserTel")
|
|
public R updateUserTel(@RequestBody Map<String,Object> map){
|
|
String phone = map.get("phone").toString();
|
|
String code = map.get("code").toString();
|
|
Integer id = Integer.valueOf(map.get("id").toString());
|
|
//查询是否存在当前手机号
|
|
LambdaQueryWrapper<MyUserEntity> wrapper = new LambdaQueryWrapper();
|
|
wrapper.eq(MyUserEntity::getTel,phone);
|
|
MyUserEntity userEntity = userService.getOne(wrapper);
|
|
if (userEntity == null) {
|
|
String redisCode = redisTemplate.opsForValue().get("RegistCode"+phone);
|
|
if(StringUtils.isEmpty(redisCode)){
|
|
return R.error("验证码已过期,请重试");
|
|
}
|
|
String lcode = redisCode.split("_")[0];
|
|
if (!lcode.equals(code)) {
|
|
return R.error("短信验证码不符!");
|
|
}
|
|
MyUserEntity userInfo = userService.getById(id);
|
|
userInfo.setTel(phone);
|
|
userService.updateById(userInfo);
|
|
return R.ok();
|
|
}else {
|
|
return R.error("手机号已被绑定!");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 绑定邮箱
|
|
* @return
|
|
*/
|
|
@RequestMapping("/updateUserEmail")
|
|
public R updateUserEmail(@RequestBody Map<String,Object> map){
|
|
String email = map.get("email").toString();
|
|
String code = map.get("code").toString();
|
|
Integer id = Integer.valueOf(map.get("id").toString());
|
|
//查询是否存在当前邮箱
|
|
LambdaQueryWrapper<MyUserEntity> wrapper = new LambdaQueryWrapper();
|
|
wrapper.eq(MyUserEntity::getEmail,email);
|
|
MyUserEntity userEntity = userService.getOne(wrapper);
|
|
if (userEntity == null) {
|
|
String redisCode = redisTemplate.opsForValue().get("RegistCode"+email);
|
|
if(StringUtils.isEmpty(redisCode)){
|
|
return R.error("验证码已过期,请重试");
|
|
}
|
|
String lcode = redisCode.split("_")[0];
|
|
if (!lcode.equals(code)) {
|
|
return R.error("验证码不符!");
|
|
}
|
|
MyUserEntity userInfo = userService.getById(id);
|
|
userInfo.setEmail(email);
|
|
userService.updateById(userInfo);
|
|
return R.ok();
|
|
}else {
|
|
return R.error("邮箱已被绑定!");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 验证码注册或登录
|
|
*/
|
|
@RequestMapping("/registerOrLogin")
|
|
public R register(String tel, String code) {
|
|
String redisCode = redisTemplate.opsForValue().get("RegistCode" + tel);
|
|
System.out.println(redisCode);
|
|
if (StringUtils.isEmpty(redisCode)){
|
|
return R.error(500,"验证码已过期,请重试");
|
|
}
|
|
String lcode = redisCode.split("_")[0];
|
|
if (!lcode.equals(code)) {
|
|
return R.error(500,"验证码不符!");
|
|
}
|
|
//查询是否存在当前用户
|
|
LambdaQueryWrapper<MyUserEntity> wrapper = new LambdaQueryWrapper();
|
|
if (tel.contains("@")){
|
|
wrapper.eq(MyUserEntity::getEmail,tel);
|
|
}else {
|
|
wrapper.eq(MyUserEntity::getTel,tel);
|
|
}
|
|
MyUserEntity userEntity = userService.getOne(wrapper);
|
|
if (userEntity == null) {
|
|
// 用户不存在则创建用户 注册成功
|
|
MyUserEntity myUserEntity = new MyUserEntity();
|
|
if (tel.contains("@")){
|
|
myUserEntity.setEmail(tel);
|
|
}else {
|
|
myUserEntity.setTel(tel);
|
|
}
|
|
userService.save(myUserEntity);
|
|
R r = sysUserTokenService.createToken(myUserEntity.getId());
|
|
return R.ok("注册成功").put("userInfo",myUserEntity).put("token",r);
|
|
}else {
|
|
R r = sysUserTokenService.createToken(userEntity.getId());
|
|
return R.ok("登录成功!").put("userInfo",userEntity).put("token",r);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 账号密码登录
|
|
*/
|
|
@RequestMapping("/login")
|
|
public R login(@RequestParam("phone") String phone,
|
|
@RequestParam("password") String password) {
|
|
LambdaQueryWrapper<MyUserEntity> wrapper = new LambdaQueryWrapper();
|
|
if (phone.contains("@")) {
|
|
wrapper.eq(MyUserEntity::getEmail,phone);
|
|
}else {
|
|
wrapper.eq(MyUserEntity::getTel,phone);
|
|
}
|
|
//防止多账号报错
|
|
MyUserEntity userEntity = userService.getOne(wrapper);
|
|
if (userEntity == null) {
|
|
return R.error(500,"用户不存在!");
|
|
}else {
|
|
if (userEntity.getPassword() == null|| userEntity.getPassword().equals("")) {
|
|
return R.error(500,"当前未设置密码,请使用验证码登录!");
|
|
}else {
|
|
if (MD5Utils.getSaltverifyMD5(password,userEntity.getPassword())){
|
|
R r = sysUserTokenService.createToken(userEntity.getId());
|
|
return R.ok("登陆成功!").put("userInfo",userEntity).put("token",r);
|
|
}else {
|
|
return R.error(500,"密码不正确,请重试!");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 设置密码
|
|
*/
|
|
@RequestMapping("/setPassword")
|
|
public R setPassword(@RequestParam("phone") String phone,
|
|
@RequestParam("password") String password,
|
|
@RequestParam("code") String code) {
|
|
String redisCode = redisTemplate.opsForValue().get("RegistCode" + phone);
|
|
System.out.println(redisCode);
|
|
if (StringUtils.isEmpty(redisCode)){
|
|
return R.error(500,"验证码已过期,请重试");
|
|
}
|
|
String lcode = redisCode.split("_")[0];
|
|
if (!lcode.equals(code)) {
|
|
return R.error(500,"验证码不符!");
|
|
}
|
|
//查询是否存在当前用户
|
|
LambdaQueryWrapper<MyUserEntity> wrapper = new LambdaQueryWrapper<>();
|
|
if (phone.contains("@")){
|
|
wrapper.eq(MyUserEntity::getEmail, phone);
|
|
}else {
|
|
wrapper.eq(MyUserEntity::getTel, phone);
|
|
}
|
|
MyUserEntity userEntity = userService.getOne(wrapper);
|
|
if (userEntity == null) {
|
|
return R.error(500,"当前用户不存在!");
|
|
}
|
|
String saltMD5 = MD5Utils.getSaltMD5(password);
|
|
userEntity.setPassword(saltMD5);
|
|
userService.updateById(userEntity);
|
|
return R.ok("成功");
|
|
}
|
|
|
|
/**
|
|
* 手机号验证码密码注册 -未在apipost
|
|
*/
|
|
// @RequestMapping("/register")
|
|
// public R register(@RequestParam("tel") String tel,
|
|
// @RequestParam("code") String code,
|
|
// @RequestParam("password") String password){
|
|
// String redisCode = redisTemplate.opsForValue().get("RegistCode" + tel);
|
|
// if (StringUtils.isEmpty(redisCode)){
|
|
// return R.error(500,"短信验证码已过期,请重试");
|
|
// }
|
|
// String lcode = redisCode.split("_")[0];
|
|
// if (!lcode.equals(code)) {
|
|
// return R.error(500,"短信验证码不符!");
|
|
// }
|
|
// MyUserEntity user = userService.getBaseMapper().selectOne(new QueryWrapper<MyUserEntity>().eq("tel", tel));
|
|
// if(!ObjectUtil.isEmpty(user)){
|
|
// return R.error(500,"该手机号已经注册!");
|
|
// }
|
|
// String saltMD5 = MD5Utils.getSaltMD5(password);
|
|
// MyUserEntity myUserEntity = new MyUserEntity();
|
|
// myUserEntity.setTel(tel);
|
|
// myUserEntity.setPassword(saltMD5);
|
|
// userService.save(myUserEntity);
|
|
// R r = sysUserTokenService.createToken(myUserEntity.getId());
|
|
// return R.ok("注册成功").put("userInfo",myUserEntity).put("token",r);
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|