package com.izouma.jmrh.web;

import com.izouma.jmrh.config.Constants;
import com.izouma.jmrh.exception.BusinessException;
import com.izouma.jmrh.service.MailCodeService;
import com.izouma.jmrh.service.sms.SmsService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.regex.Pattern;

@RestController
@RequestMapping("/mailCode")
@AllArgsConstructor
public class MailCodeController extends BaseController {
    private MailCodeService mailCodeService;

    @GetMapping("/sendVerify")
    public void sendVerify(@RequestParam String mail) {
        if (!Pattern.matches(Constants.Regex.MAIL, mail)) {
            throw new BusinessException("请输入正确的邮箱");
        }
        mailCodeService.sendCode(mail);
    }

    @GetMapping("/verify")
    public void verify(@RequestParam String phone, @RequestParam String code) throws SmsService.SmsVerifyException {
        mailCodeService.verify(phone, code);
    }
}
