package com.ruoyi.auth.controller; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import com.baomidou.mybatisplus.core.toolkit.ObjectUtils; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.ruoyi.account.api.feignClient.AppUserClient; import com.ruoyi.account.api.model.TAppUser; import com.ruoyi.common.core.constant.Constants; import com.ruoyi.common.core.constant.MsgConstants; import com.ruoyi.common.core.utils.HttpUtils; import com.ruoyi.common.core.utils.MsgUtil; import com.ruoyi.common.core.web.domain.AjaxResult; import com.ruoyi.common.redis.service.RedisService; import com.ruoyi.system.api.domain.SysRole; import com.ruoyi.system.api.domain.SysUser; import com.ruoyi.system.api.feignClient.SysUserClient; import io.swagger.annotations.ApiOperation; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.util.CollectionUtils; import org.springframework.web.bind.annotation.*; import com.ruoyi.auth.form.LoginBody; import com.ruoyi.auth.form.RegisterBody; import com.ruoyi.auth.service.SysLoginService; import com.ruoyi.common.core.domain.R; import com.ruoyi.common.core.utils.JwtUtils; import com.ruoyi.common.core.utils.StringUtils; import com.ruoyi.common.security.auth.AuthUtil; import com.ruoyi.common.security.service.TokenService; import com.ruoyi.common.security.utils.SecurityUtils; import com.ruoyi.system.api.model.LoginUser; import java.util.*; import java.util.concurrent.TimeUnit; /** * token 控制 * * @author ruoyi */ @Slf4j @RestController public class TokenController { @Autowired private TokenService tokenService; @Autowired private SysLoginService sysLoginService; @Resource private SysUserClient userClient; @Autowired private RedisService redisService; @Autowired private AppUserClient appUserClient; @PostMapping("login") public R login(@RequestBody LoginBody form, HttpServletRequest request) { // 用户登录 LoginUser userInfo = sysLoginService.login(form.getUsername(), form.getPassword(), request); HashMap map = new HashMap<>(); map.put("token",tokenService.createToken(userInfo)); List roles = userInfo.getSysUser().getRoles(); // if(CollectionUtils.isEmpty(roles)){ // return R.fail("请关联角色!"); // } // map.put("roleName",roles.get(0).getRoleName()); map.put("info",userInfo); // 修改用户最后登录时间 SysUser sysUser = new SysUser(); sysUser.setUserId(userInfo.getSysUser().getUserId()); sysUser.setLoginDate(new Date()); System.out.println("修改用户登录时间"+sysUser); userClient.updateSysUser(sysUser); return R.ok(map); } @DeleteMapping("logout") public R logout(HttpServletRequest request) { String token = SecurityUtils.getToken(request); if (StringUtils.isNotEmpty(token)) { String username = JwtUtils.getUserName(token); // 删除用户缓存记录 AuthUtil.logoutByToken(token); // 记录用户退出日志 sysLoginService.logout(username, request); } return R.ok(); } @DeleteMapping("logoutApplet") public R logoutApplet(HttpServletRequest request) { String token = SecurityUtils.getToken(request); if (StringUtils.isNotEmpty(token)) { // 删除用户缓存记录 AuthUtil.logoutByToken(token); } return R.ok(); } @PostMapping("refresh") public R refresh(HttpServletRequest request) { LoginUser loginUser = tokenService.getLoginUser(request); if (StringUtils.isNotNull(loginUser)) { // 刷新令牌有效期 tokenService.refreshToken(loginUser); return R.ok(); } return R.ok(); } @PostMapping("register") public R register(@RequestBody RegisterBody registerBody, HttpServletRequest request) { // 用户注册 sysLoginService.register(registerBody.getUsername(), registerBody.getPassword(), request); return R.ok(); } /** * 获取验证码 * * @param phone 手机号 * @return 结果 */ @ApiOperation(value = "获取验证码",notes = "获取验证码",tags = {"更换手机号获取验证码"}) @GetMapping("getCode") public AjaxResult getCode(@RequestParam("phone") String phone) { TAppUser appUser = appUserClient.selectByPhone(phone).getData(); if (Objects.nonNull(appUser)){ return AjaxResult.error("该手机号已绑定账号"); } String code = MsgUtil.createCode(); redisService.setCacheObject(phone+Constants.APPLET,code,5L, TimeUnit.MINUTES); String reqStr = MsgUtil.codeMsg(phone, code); String result = HttpUtils.post(MsgConstants.SEND_URL, reqStr); return AjaxResult.success(result); } }