Files
nuttyreading-java/src/main/java/com/peanut/modules/common/controller/UserController.java
2025-06-19 09:35:09 +08:00

337 lines
13 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.common.utils.ShiroUtils;
import com.peanut.modules.common.entity.MyUserEntity;
import com.peanut.modules.common.entity.UserInviteRegister;
import com.peanut.modules.common.entity.UserVip;
import com.peanut.modules.common.service.MyUserService;
import com.peanut.modules.common.service.UserInviteRegisterService;
import com.peanut.modules.common.service.UserVipService;
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.transaction.annotation.Transactional;
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.List;
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;
@Autowired
private UserVipService userVipService;
@Autowired
private UserInviteRegisterService inviteRegisterService;
/**
* 常规注册 发短信验证码
*/
@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(999999) + "";
StringBuffer sb = new StringBuffer();
for (int j = 0; j < 6 - 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);
//发送
return userService.sendCodeForRegister(phone,code,areacode);
}
/**
* 常规注册 发送邮箱验证码
*/
@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(999999) + "";
StringBuffer sb = new StringBuffer();
for (int j = 0; j < 6 - code.length(); j++) {
sb.append("0");
}
code = sb.toString() + code;
String timeCode = code + "_"+System.currentTimeMillis();
//redis 缓存验证码
redisTemplate.opsForValue().set("RegistCode"+email,timeCode,5, TimeUnit.MINUTES);
//发送
return MailUtil.sendMail("疯子读书邮箱验证码",code,email);
}
@RequestMapping("/getUserInfo")
public R getUserInfo(){
int uid = ShiroUtils.getUId();
MyUserEntity userEntity = userService.getById(uid);
List<UserVip> userVips = userVipService.list(new LambdaQueryWrapper<UserVip>()
.eq(UserVip::getUserId, uid)
.eq(UserVip::getState,0));
userEntity.setUserVips(userVips);
return R.ok().put("result",userEntity);
}
/**
* 绑定电话号
* @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("/updateUser")
public R updateUser(@RequestBody MyUserEntity userEntity){
userService.updateById(userEntity);
return R.ok();
}
/**
* 验证码注册或登录
*/
@RequestMapping("/registerOrLogin")
@Transactional
public R register(String tel, String code,String inviteCode) {
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,"验证码不符!");
}
//校验邀请码
MyUserEntity inviteUser = null;
if (!StringUtils.isEmpty(inviteCode)) {
inviteUser = userService.getOne(new LambdaQueryWrapper<MyUserEntity>()
.eq(MyUserEntity::getInviteCode, inviteCode));
if (inviteUser == null) {
return R.error("邀请码有误");
}
}
//查询是否存在当前用户
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);
//添加邀请记录
if (!StringUtils.isEmpty(inviteCode)){
myUserEntity.setInviteCode(inviteCode);
userService.updateById(myUserEntity);
UserInviteRegister inviteRegister = new UserInviteRegister();
inviteRegister.setUserId(inviteUser.getId());
inviteRegister.setInvitedUserId(myUserEntity.getId());
inviteRegisterService.save(inviteRegister);
inviteRegisterService.checkInviteRegisterCount(inviteUser.getId());
}
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("/setPasswordByCode")
public R setPassword(@RequestParam("phone") String phone,
@RequestParam("password") String password,
@RequestParam("code") String code) {
String redisCode = redisTemplate.opsForValue().get("RegistCode" + phone);
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("成功");
}
/**
* 设置密码不验证
*/
@RequestMapping("/setPasswordById")
public R setPassword(@RequestParam("id") String id,@RequestParam("password") String password) {
MyUserEntity userEntity = userService.getById(id);
if (userEntity == null) {
return R.error(500,"当前用户不存在!");
}
String saltMD5 = MD5Utils.getSaltMD5(password);
userEntity.setPassword(saltMD5);
userService.updateById(userEntity);
return R.ok("成功");
}
}