十次方後端筆記五:用戶和短信微服務

用戶微服務

用戶微服務代碼生成(省略)

注意修改application 配置文件。

用戶註冊

需求:註冊賬號,用手機號註冊,填寫後發送短信驗證碼,填寫短信驗證碼正確方可註冊成功。

思路:在用戶微服務編寫API ,生成手機驗證碼,存入Redis併發送到RabbitMQ

準備工作

  1. 引入依賴

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-amqp</artifactId>
    </dependency>
    
  2. 配置application.yml

    spring: 
      redis:
        host: 192.168.136.104
        port: 6379
      rabbitmq:
        host: 192.168.136.104
        port: 5672
    

短信驗證碼發送

  1. UserController新增方法

        /**
         * 發送短信驗證碼
         *
         * @param mobile
         */
        @RequestMapping(value = "/sendsms/{mobile}", method = RequestMethod.POST)
        public Result sendsms(@PathVariable String mobile) {
            userService.sendSms(mobile);
            return new Result(true, StatusCode.OK, "發送成功");
        }
    
  2. UserService新增方法

        @Autowired
        private RedisTemplate redisTemplate;
    
        @Autowired
        private RabbitTemplate rabbitTemplate;
    
        /**
         * 發送短信驗證碼
         *
         * @param mobile 手機號
         */
        public void sendSms(String mobile) {
            //1.生成6位短信驗證碼
            Random random = new Random();
            int max = 999999;//最大數
            int min = 100000;//最小數
            int code = random.nextInt(max);//隨機生成
            if (code < min) {
                code = code + min;
            }
            System.out.println(mobile + "收到驗證碼是:" + code);
            //2.將驗證碼放入redis
            redisTemplate.opsForValue().set("smscode_" + mobile, code + "", 5,
                    TimeUnit.MINUTES);//五分鐘過期
            //3.將驗證碼和手機號發動到rabbitMQ中
            Map<String, String> map = new HashMap();
            map.put("mobile", mobile);
            map.put("code", code + "");
            rabbitTemplate.convertAndSend("sms", map);
        }
    

註冊

  1. UserController新增方法

        /**
         * 用戶註冊
         *
         * @param user
         */
        @RequestMapping(value = "/register/{code}", method = RequestMethod.POST)
        public Result register(@RequestBody User user, @PathVariable String
                code) {
            userService.add(user, code);
            return new Result(true, StatusCode.OK, "註冊成功");
        }
    
  2. UserService新增方法

        /**
         * 增加
         *
         * @param user 用戶
         * @param code 用戶填寫的驗證碼
         */
        public void add(User user, String code) {
            //判斷驗證碼是否正確
            String syscode = (String) redisTemplate.opsForValue().get("smscode_" + user.getMobile());
            //提取系統正確的驗證碼
            if (syscode == null) {
                throw new RuntimeException("請點擊獲取短信驗證碼");
            }
            if (!syscode.equals(code)) {
                throw new RuntimeException("驗證碼輸入不正確");
            }
            user.setId(idWorker.nextId() + "");
            user.setFollowcount(0);//關注數
            user.setFanscount(0);//粉絲數
            user.setOnline(0L);//在線時長
            user.setRegdate(new Date());//註冊日期
            user.setUpdatedate(new Date());//更新日期
            user.setLastdate(new Date());//最後登陸日期
            userDao.save(user);
        }
    

短信微服務

短信微服務參考我的另一篇文章:

鏈接:https://www.imxushuai.com/2001/12/31/樂優商城筆記八:短信微服務/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章