package com.ruoyi.web.controller.common;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.ValidGroup;
import com.ruoyi.system.domain.bo.VerificationCodeDTO;
import com.ruoyi.system.domain.enums.verificationCode.CodeBusinessType;
import com.ruoyi.system.service.IVerificationCodeService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
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 static com.ruoyi.common.core.domain.AjaxResult.success;

/**
 * @author wjh
 * 2024/10/11
 */
@RestController
@RequestMapping("/verification")
public class VerificationController extends BaseController {

    @Autowired
    private IVerificationCodeService verificationCodeService;

    @ApiOperation("获取短信验证码")
    @GetMapping("/mobile")
    @Anonymous
    public AjaxResult getVerificationMobileCode(@Validated({ValidGroup.FrontCreate.class}) VerificationCodeDTO bo) {
        return success(verificationCodeService.sendMobileCode(bo.getMobile(), CodeBusinessType.parse(bo.getType())));
    }

    @ApiOperation("判断验证码是否正确")
    @GetMapping("/check")
    @Anonymous
    public AjaxResult checkCode(@RequestParam @ApiParam("手机号或邮箱") String key,
                                @RequestParam @ApiParam("验证码") String code,
                                @RequestParam @ApiParam("验证码类型") String type
    ) {
        return success(verificationCodeService.checkCode(key, code, CodeBusinessType.parse(type), false));
    }

}
