ZQN
2024-06-22 b48de0e35c54bd4715f8de2b86bac5a539c27275
project-framework/src/main/java/com/project/framework/web/service/SysLoginService.java
@@ -21,6 +21,7 @@
import com.project.common.exception.user.CaptchaException;
import com.project.common.exception.user.CaptchaExpireException;
import com.project.common.exception.user.UserPasswordNotMatchException;
import com.project.common.sms.YPSmsApi;
import com.project.common.utils.DateUtils;
import com.project.common.utils.MessageUtils;
import com.project.common.utils.ServletUtils;
@@ -113,6 +114,26 @@
        return tokenService.createToken(loginUser);
    }
    /**
     * 新注册获取验证码
     * @param phone 手机
     * @return  验证码
     */
    public Boolean getVerifyCodeNew(String phone) {
        // 生成4位随机数
        String code = "";
        Random ran = new Random();
        int randomNum = ran.nextInt(10000);
        code = String.format("%04d", randomNum);
        log.info("手机号:"+phone+"->验证码:"+code);
        boolean send = sendYp(phone, code);
        if (send){
            redisCache.setCacheObject(getCacheKey(phone), code, Constants.PHONE_EXPIRATION, TimeUnit.MINUTES);
            return true;
        }
        redisCache.setCacheObject(getCacheKey(phone), code, Constants.PHONE_EXPIRATION, TimeUnit.MINUTES);
        return false;
    }
    /**
     * 获取验证码
@@ -125,14 +146,19 @@
        if (user==null){
            throw new BaseException("您手机号尚未注册!");
        }
        return getVerifyCodeNew(phone);
    }
        // 生成4位随机数
        String code = "";
        Random ran = new Random();
        int randomNum = ran.nextInt(10000);
        code = String.format("%04d", randomNum);
        log.info("手机号:"+phone+"->验证码:"+code);
        Boolean flag = true;
    private boolean sendYp(String phone, String code ){
        String result = YPSmsApi.sendSms(phone, StringUtils.format(YPSmsApi.CODE_TMP, code, Constants.PHONE_EXPIRATION));
        if (result.contains("\"code\":0,\"msg\":\"OK\"")){
            log.info("发送成功 ->验证码:"+code);
            return true;
        }
        return false;
    }
    private boolean sendAl(String phone, String code ){
        DefaultProfile profile = DefaultProfile.getProfile("cn-beijing", AliyunSmsConstants.SMS_APPID, AliyunSmsConstants.SMS_SECRET);
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
@@ -149,15 +175,12 @@
            JSONObject jsonObject = JSON.parseObject(response.getData());
            if ("OK".equals(jsonObject.get("Code"))) {
                log.info("发送成功 ->验证码:"+code);
                redisCache.setCacheObject(getCacheKey(phone), code, Constants.PHONE_EXPIRATION, TimeUnit.MINUTES);
                return true;
            }
        } catch (ClientException e) {
            e.printStackTrace();
        }
        redisCache.setCacheObject(getCacheKey(phone), code, Constants.PHONE_EXPIRATION, TimeUnit.MINUTES);
        return flag;
        return false;
    }
    /**
@@ -172,9 +195,22 @@
        if (!verified){
            throw new BaseException("手机号验证码校验失败!");
        }
        SysUser user = userMapper.selectOne(new LambdaQueryWrapper<SysUser>().eq(SysUser::getPhonenumber,bo.getPhone()));
        SysUser user = null;
        if ("01".equals(bo.getUserType())){
            user = userMapper.selectOne(new LambdaQueryWrapper<SysUser>()
                    .eq(SysUser::getPhonenumber,bo.getPhone())
                    .and(wrapper->wrapper.eq(SysUser::getUserType,"00").or().eq(SysUser::getUserType, "01")));
        } else {
            user = userMapper.selectOne(new LambdaQueryWrapper<SysUser>()
                    .eq(SysUser::getPhonenumber,bo.getPhone())
                    .eq(SysUser::getUserType,bo.getUserType())
                    );
        }
        if (user==null){
            throw new BaseException("您手机号尚未注册!");
            throw new BaseException("您手机号尚未注册或您选择登录类型有误!");
        }
        if (!"0".equals(user.getStatus())){
            throw new BaseException("您账号已停用或待审批,请联系营商办管理人员!");
        }
        return this.login(user.getUserName(), user.getRecommendUser(), null, null);
    }
@@ -249,4 +285,6 @@
        return verify;
    }
}