68 lines
2.3 KiB
Java
68 lines
2.3 KiB
Java
package com.zmzm.financial.common.controller;
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
import com.zmzm.financial.common.entity.User;
|
|
import com.zmzm.financial.common.entity.UserToken;
|
|
import com.zmzm.financial.common.service.IUserService;
|
|
import com.zmzm.financial.common.service.IUserTokenService;
|
|
import com.zmzm.financial.util.MD5Utils;
|
|
import com.zmzm.financial.util.R;
|
|
import com.zmzm.financial.util.ShiroUtils;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.web.bind.annotation.RequestBody;
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
import java.util.Date;
|
|
|
|
@Slf4j
|
|
@RestController("auth")
|
|
@RequestMapping("auth")
|
|
public class AuthController {
|
|
|
|
@Autowired
|
|
private IUserService userService;
|
|
@Autowired
|
|
private IUserTokenService userTokenService;
|
|
|
|
//登录
|
|
@RequestMapping("/login")
|
|
public R login(@RequestBody User user){
|
|
User userEntity = userService.getOne(new LambdaQueryWrapper<User>()
|
|
.eq(User::getAccount, user.getAccount()));
|
|
if(userEntity == null){
|
|
return R.error(500,"账号不存在");
|
|
}else {
|
|
if (MD5Utils.getSaltverifyMD5(user.getPassword(),userEntity.getPassword())){
|
|
UserToken userToken = userTokenService.createToken(userEntity.getId());
|
|
return R.ok("登录成功").put("userToken",userToken);
|
|
}else {
|
|
return R.error(500,"密码不正确,请重试");
|
|
}
|
|
}
|
|
}
|
|
|
|
//注册
|
|
@RequestMapping("/register")
|
|
public R register(@RequestBody User user){
|
|
long count = userService.count(new LambdaQueryWrapper<User>().eq(User::getAccount, user.getAccount()));
|
|
if(count>0){
|
|
return R.error(500,"账号已注册");
|
|
}
|
|
String saltMD5 = MD5Utils.getSaltMD5(user.getPassword());
|
|
user.setPassword(saltMD5);
|
|
userService.save(user);
|
|
return R.ok();
|
|
}
|
|
|
|
//退出
|
|
@RequestMapping("/logout")
|
|
public R logout(){
|
|
UserToken userToken = userTokenService.getById(ShiroUtils.getUserId());
|
|
userToken.setExpireTime(new Date());
|
|
userTokenService.updateById(userToken);
|
|
return R.ok();
|
|
}
|
|
|
|
}
|