SmsService.java 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package com.izouma.nineth.service.sms;
  2. import com.izouma.nineth.config.Constants;
  3. import com.izouma.nineth.exception.BusinessException;
  4. import io.jsonwebtoken.Claims;
  5. import io.jsonwebtoken.Jwts;
  6. import java.util.Date;
  7. import java.util.function.Supplier;
  8. public interface SmsService {
  9. String sendVerify(String phone);
  10. String verify(String phone, String code);
  11. void sellOut(String phone);
  12. default String verifyToken(String token) {
  13. try {
  14. Claims claims = Jwts.parser()
  15. .setSigningKey(Constants.SMS_TOKEN_SECRET)
  16. .parseClaimsJws(token)
  17. .getBody();
  18. if (claims.getExpiration().before(new Date())) {
  19. throw new BusinessException("验证码已过期");
  20. }
  21. return claims.getSubject();
  22. } catch (Exception e) {
  23. throw new BusinessException("验证码无效");
  24. }
  25. }
  26. class SmsVerifyException extends Exception implements Supplier<SmsVerifyException> {
  27. public SmsVerifyException(String msg) {
  28. super(msg);
  29. }
  30. @Override
  31. public SmsVerifyException get() {
  32. return this;
  33. }
  34. }
  35. }