1
luodangjia
2025-01-22 c4469d269bab585a02e02e7f28abd18064067fbd
ruoyi-auth/src/main/java/com/ruoyi/auth/controller/TokenController.java
@@ -17,7 +17,9 @@
import com.ruoyi.common.security.auth.AuthUtil;
import com.ruoyi.common.security.service.TokenService;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.company.api.RemoteCompanyService;
import com.ruoyi.company.api.RemoteCompanyUserService;
import com.ruoyi.company.api.domain.Company;
import com.ruoyi.company.api.domain.User;
import com.ruoyi.company.api.domain.dto.MgtCompanyDTO;
import com.ruoyi.company.api.model.RegisterUser;
@@ -27,7 +29,6 @@
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@@ -35,7 +36,10 @@
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
/**
@@ -59,6 +63,9 @@
    @Resource
    private RemoteCompanyUserService remoteCompanyUserService;
    @Resource
    private RemoteCompanyService remoteCompanyService;
    @Operation(summary = "管理端-登录")
    @PostMapping("login")
    public R<?> login(@RequestBody LoginBody form)
@@ -127,11 +134,14 @@
    {
        // 验证码生成
        String code = String.valueOf(Math.random()).substring(2, 6);
        // 发送验证码
        String result = SmsUtils.sendSms(registerUser.getPhone(), code);
        code += ":"+System.currentTimeMillis();
        // 缓存验证码
        String verifyKey = CacheConstants.PHONE_CODE_KEY + registerUser.getPhone();
        redisService.setCacheObject(verifyKey, code, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);
        // 发送验证码
        String result = SmsUtils.sendSms(registerUser.getPhone(), code);
        if (!"OK".equals(result))
        {
            return R.fail("验证码发送失败");
@@ -185,14 +195,20 @@
    @PostMapping("smsLogin")
    @Operation(summary = "验证码登录",description = "验证码登录")
    public R<Map<String, Object>> smsLogin(@RequestBody RegisterUser registerUser){
        String smsCode = registerUser.getSmsCode();
        String smsCode = registerUser.getCode();
        if (!"999999".equals(smsCode)) {
            String verifyKey = CacheConstants.PHONE_CODE_KEY + StringUtils.nvl(registerUser.getPhone(), "");
            String captcha = redisService.getCacheObject(verifyKey);
            if (captcha == null) {
            if (captcha == null){
                throw new CaptchaException("验证码错误");
            }
            String[] split = captcha.split(":");
            long l = Long.parseLong(split[1]);
            long l1 = System.currentTimeMillis();
            // 判断是否大于两分钟
            if (l1 - l > 2 * 60 * 1000) {
                throw new CaptchaException("验证码已失效");
            }
            redisService.deleteObject(verifyKey);
            if (!smsCode.equalsIgnoreCase(captcha)) {
                throw new CaptchaException("验证码错误");
            }
@@ -216,6 +232,23 @@
        return "login_tokens:" + token;
    }
    /**
     * 公司校验
     */
    @PostMapping("check")
    @Operation(summary = "公司校验",description = "公司校验")
    public R<?> check(@RequestBody RegisterUser registerUser)
    {
        User check = sysLoginService.check(registerUser);
        if (check == null) {
            return R.fail("账号不存在!");
        }
        return R.ok();
    }
    /**
     * 重置密码
     */