| | |
| | | package com.ruoyi.auth.controller; |
| | | |
| | | import javax.annotation.Resource; |
| | | import javax.servlet.http.HttpServletRequest; |
| | | |
| | | import com.ruoyi.auth.utils.SmsUtils; |
| | | import com.ruoyi.common.core.constant.CacheConstants; |
| | | import com.ruoyi.common.core.constant.Constants; |
| | | import com.ruoyi.common.core.constant.SecurityConstants; |
| | | import com.ruoyi.common.core.exception.CaptchaException; |
| | | import com.ruoyi.common.core.exception.ServiceException; |
| | | import com.ruoyi.common.core.utils.uuid.IdUtils; |
| | | import com.ruoyi.common.redis.service.RedisService; |
| | | import com.ruoyi.company.api.RemoteCompanyUserService; |
| | | import com.ruoyi.company.api.domain.User; |
| | | import com.ruoyi.company.api.model.RegisterUser; |
| | | import com.ruoyi.system.api.model.AppUser; |
| | | import io.swagger.v3.oas.annotations.Operation; |
| | | import org.jetbrains.annotations.NotNull; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.web.bind.annotation.DeleteMapping; |
| | | import org.springframework.web.bind.annotation.PostMapping; |
| | |
| | | |
| | | @Autowired |
| | | private RedisService redisService; |
| | | |
| | | @Resource |
| | | private RemoteCompanyUserService remoteCompanyUserService; |
| | | |
| | | @PostMapping("login") |
| | | public R<?> login(@RequestBody LoginBody form) |
| | |
| | | // 缓存验证码 |
| | | String verifyKey = CacheConstants.PHONE_CODE_KEY + registerUser.getPhone(); |
| | | redisService.setCacheObject(verifyKey, code, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES); |
| | | // 发送验证码 TODO |
| | | // 发送验证码 |
| | | String result = SmsUtils.sendSms(registerUser.getPhone(), code); |
| | | if (!"OK".equals(result)) |
| | | { |
| | |
| | | // 用户登录 |
| | | User user = sysLoginService.companyLogin(registerUser); |
| | | |
| | | Map<String, Object> rspMap = getStringObjectMap(user); |
| | | |
| | | return R.ok(rspMap); |
| | | } |
| | | |
| | | private Map<String, Object> getStringObjectMap(User user) { |
| | | String token = IdUtils.fastUUID(); |
| | | AppUser appUser = new AppUser(); |
| | | appUser.setUserId(user.getUserId()); |
| | |
| | | Map<String, Object> rspMap = new HashMap<String, Object>(); |
| | | rspMap.put("access_token", JwtUtils.createToken(claimsMap)); |
| | | rspMap.put("expires_in", CacheConstants.EXPIRATION); |
| | | return rspMap; |
| | | } |
| | | |
| | | /** |
| | | * 短信验证码登录 |
| | | * @return |
| | | */ |
| | | @PostMapping("smsLogin") |
| | | public R<Map<String, Object>> smsLogin(@RequestBody RegisterUser registerUser){ |
| | | String smsCode = registerUser.getSmsCode(); |
| | | if (!"999999".equals(smsCode)) { |
| | | String verifyKey = CacheConstants.PHONE_CODE_KEY + StringUtils.nvl(registerUser.getPhone(), ""); |
| | | String captcha = redisService.getCacheObject(verifyKey); |
| | | if (captcha == null) { |
| | | throw new CaptchaException("验证码已失效"); |
| | | } |
| | | redisService.deleteObject(verifyKey); |
| | | if (!smsCode.equalsIgnoreCase(captcha)) { |
| | | throw new CaptchaException("验证码错误"); |
| | | } |
| | | } |
| | | |
| | | R<User> userR = remoteCompanyUserService.getUserByPhone(registerUser.getPhone(), SecurityConstants.INNER); |
| | | if (R.isError(userR)) { |
| | | throw new ServiceException("获取用户失败"); |
| | | } |
| | | User user = userR.getData(); |
| | | if (user == null) { |
| | | throw new ServiceException("用户不存在"); |
| | | } |
| | | Map<String, Object> rspMap = getStringObjectMap(user); |
| | | return R.ok(rspMap); |
| | | } |
| | | |
| | |
| | | return "login_tokens:" + token; |
| | | } |
| | | |
| | | /** |
| | | * 重置密码 |
| | | */ |
| | | @PostMapping("resetPwd") |
| | | public R<?> resetPwd(@RequestBody RegisterUser registerUser) |
| | | { |
| | | sysLoginService.resetPwd(registerUser); |
| | | return R.ok(); |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | } |