2.简阅注册功能

1.下载redis准备

  • 运行redis-server:redis-server.exe redis.windows.conf
  • 再开一个命令行窗口,进入redis目录运行redis-cli.exe命令

2.注册阿里云服务(并申请一个模板和签名写程序有用,发送验证码要收钱往自己账户里充点钱)

3.添加依赖


    com.aliyun
    aliyun-java-sdk-core
    4.0.3


    org.springframework.boot
    spring-boot-starter-data-redis

  • 获取六位随机数验证码
public static String getVerifyCode(){
        Random random=new Random();
        StringBuilder stringBuilder=new StringBuilder();
        for(int i=0;i<6;i++){
            stringBuilder.append(String.valueOf(random.nextInt(10)));
        }
        return stringBuilder.toString();
    }
  • SMSUtil
package com.spring.jianyueapi.util;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;

public class SMSUtil {
    public static String send(String mobile){
        DefaultProfile profile=DefaultProfile.getProfile("cn-hangzhou","LTAIL4bn0wGEtnsG","KMwPoHxc95LnRAfmaIL6sAVJ4M9PGc");
        IAcsClient client=new DefaultAcsClient(profile);
        CommonRequest request=new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", mobile);
        request.putQueryParameter("SignName", "jianyuesms");
        request.putQueryParameter("TemplateCode", "SMS_162732247");
        String verifyCode = StringUtil.getVerifyCode();
        request.putQueryParameter("TemplateParam","{\"code\":"+verifyCode+"}");
        CommonResponse response= null;
        try {
            response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return verifyCode;
    }

}
  • Service接口增加注册方法
  void signUp(UserDTO userDTO);
  • signUp实现方法
 @Override
    public void signUp(UserDTO userDTO) {
        User user1=new User();
        user1.setMobile(userDTO.getMobile());
        user1.setPassword(StringUtil.getBase64Encoder(userDTO.getPassword()));
        user1.setNickname("新用户");
        user1.setAvatar("http://ppeto2k90.bkt.clouddn.com/avatar/default.png");
        user1.setRegtime(new Date());
        user1.setStatus((short)1);
        userMapper.insert(user1);
    }
  • 单元测试
  • 获取/验证验证码接口
  @PostMapping(value = "/verify")
    public ResponseResult getVerifyCode(@RequestParam("mobile") String mobile){
        User user=userService.getUserByMobile(mobile);
        if (user!=null){
            return ResponseResult.error(StatusConst.MOBILE_EXIST,MsgConst.MOBILE_EXIST);
        }else{
            //SMSUtil.send(mobile);
            String verifyCode= SMSUtil.send(mobile);
            System.out.println(verifyCode);
            redisService.set(mobile,verifyCode);
            return ResponseResult.success();
        }
    }

    @PostMapping(value = "/check")
    public ResponseResult checkVerifyCode(@RequestParam("mobile") String mobile,@RequestParam("verifyCode") String verifyCode){
        String code=redisService.get(mobile).toString();
        System.out.println(code+"---");
        System.out.println(verifyCode);
        if(code.equals(verifyCode)){
            return ResponseResult.success();
        }else {
            return ResponseResult.error(StatusConst.VERIFYCODE_ERROR,MsgConst.VERIFYCODE_ERROR);
        }
    }
  • 注册接口
  @PostMapping(value = "/sign_up")
    public ResponseResult signUp(@RequestBody UserDTO userDTO){
        userService.signUp(userDTO);
        return ResponseResult.success();
    }
  • 启动主类用Swagger测试

前端

  • 注册页面






你可能感兴趣的:(2.简阅注册功能)