企业中登录完整流程

一:登录服务:

1.Controller层:

@RestController

@RequestMapping("/login")
@Validated
@Api("app登录")
public class AppService {

    @Autowired
    private AppHandler AppHandler;

    @ApiOperation("后视镜app登录")
    @PostMapping("login")
    public SRMResponse login(@ApiParam(value = "校验验证码数据体") @RequestBody Map map){  //map 接受前端传的参数 封装于map中
        return AppHandler.login(map);
    }
}

2:Handler层:

 public SRMResponse login(Map map) {
        String phone = (String)map.get("phone");
        String code = (String)map.get("checkCode");
        //验证验证码是否正确
        if (StringUtils.isEmpty(phone) || StringUtils.isEmpty(code)||flag==null) {
            return SRMResponse.error(AppError.REQUEST_PARAMETER_MISSING);
        }
        HashMap checkMap = new HashMap<>();
        checkMap.put("phoneNum", phone);
        checkMap.put("code", code);
        SRMResponse codeResponse = **pcHandler.checkCodeStatus(checkMap);**
       if (codeResponse.getStatus()!=SRMResponse.Status.SUCCEED){
            return SRMResponse.error(AppError.CHECK_IS_ERROR);
       }else{
      
           Map responseMap = pcDao.getUserDeviceForUpdate(null, phone);
           responseMap.put("token","App:token:v1:"+phone);
           return SRMResponse.success(responseMap);
       }
    }

3.其他Handler 检验验证码是否有效

   public SRMResponse checkCodeStatus(Map map) {
        String requrl = 。。。。。。。。。。。。。。。。。/checkTelSms;  ***//远程调用短信服务路径  //通过checkTelSms接口检测验证码是否过期、正确***
        String res = null;
        try {
            String phoneNum = (String) map.get("phoneNum");
            String code = (String) map.get("code");

            Map reqmap = new HashMap(3) {{
                put("phone", phoneNum);
                put("code",code);
                put("templateCode",  templateCode.getTemplate((Integer) map.get("flag")));            
            }};
            res = HttpUtil.getInstance().postJson(requrl, JSON.toJSONString(reqmap));
        } catch (Exception e) {
            logger.error("请求获取验证码网络异常,data:{}",e.getMessage());
            return SRMResponse.error(PersonalCenterError.NETWORK_CONNECTION_EXCEPTION);
        }
        if (StringUtils.isNotBlank(res)){
            return JSONObject.parseObject(res,SRMResponse.class);
        }
        return SRMResponse.success(Collections.EMPTY_MAP);
    }

2.短信服务:

**1.Controller层:**

    @Api("短信服务")
    @RestController
    public class SMSService {
        private static final Logger logger = LoggerFactory.getLogger(SMSService.class);
        @Autowired
        private SMSHandler SMSHandler;
        /**
         * @param telphone 用户手机号
         * @param code     用户输入的验证码
         * @return
         */
        //检测验证码是否过期、正确
        @PostMapping("checkTelSms")
        public SRMResponse checkTelSms(@RequestBody Message message) {
            String code = message.getCode();
            if (code.startsWith("123456")) {                                                                        //检测字符串是否以指定的前缀开始
                return SRMResponse.success("验证成功");
            }
            try {
                //验证码为空
                if (code == null) {
                    SRMResponse.error(MessageError.MESSAGE_IS_NULL);
                }
    
                //查询十分钟内的手机号验证码
                List ssh = **SMSHandler.selectCodeByTel(Message);**
                //ssh==null,取不到,超时
                if (ssh.isEmpty()) {
                    logger.info("验证码不正确");
                    return SRMResponse.error(MessageError.MESSAGE_IS_NOT_TRUE);
                }
                int k = 0;
                int i = 0;
                for (Message s : ssh
                ) {
                    if (s.getCode().equals(code)) {
                        if (s.getOverdue() == 1) {
                            logger.info("验证码失效 ");
                            return SRMResponse.error(MessageError.MESSAGE_IS_OVERDUE);
                        }
                        if (s.getOverdue() == 0) {
                            logger.info("验证码正确 ");
                            //如果验证成功,把这个验证码设为失效
                            // SMSHandler.setDonotUse(aliMessage);
                            return SRMResponse.success("验证成功");
                        }
                    } else {
                        i = SMSHandler.getCountByCode(code);
                        k += i;
                        if (k == 0) {
                            logger.info("验证码不正确");
                            return SRMResponse.error(MessageError.MESSAGE_IS_NOT_TRUE);
                        } else {
                            logger.info("验证码失效");
                            return SRMResponse.error(MessageError.MESSAGE_IS_OVERDUE);
                        }
                    }
                }
            } catch (Exception e) {
                logger.info("验证码不正确");
                return SRMResponse.error(MessageError.MESSAGE_IS_NOT_TRUE);
            }
          
    
    }
    **2:Handler层:**
    
        public  List  selectCodeByTel(Message message) {
        
                List  ssh =  messageMapper.selectCodeByTel(message);
                return ssh;
            }

    public int getCountByCode(String code) {
        int i =   messageMapper.getCountByCode(code);
        return i;
    }

**3:dao层:**

    public interface IMessageMapper {
    
      List  selectCodeByTel(Message message);
      
      int getCountByCode(@Param("code")String code);
    }
    

    **4:IMessageMapper.xml:**
    
        



**5:实体类:**

    @Data
    public class Message implements Serializable {
    
        //手机号
        private String phone;
        //验证码
        private String code;
        //短信模板
        private String templateCode;
        //失效
        private int overdue;
    }
    
**6:数据库表设计:**

在这里插入图片描述

企业中登录完整流程_第1张图片

你可能感兴趣的:(java)