package com.ruoyi.web.controller.commonapi;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.commonapi.cache.limit.annotation.LimitPoint;
import com.ruoyi.commonapi.enums.ResultCode;
import com.ruoyi.commonapi.enums.ResultUtil;
import com.ruoyi.commonapi.service.SmsUtil;
import com.ruoyi.commonapi.entity.enums.VerificationEnums;
import com.ruoyi.commonapi.service.VerificationService;
import com.ruoyi.commonapi.vo.ResultMessage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;

/**
 * 短信验证码接口
 *
 * @author zq
 * @since 2020/11/26 15:41
 */
@RestController
@Api(tags = "短信验证码接口")
@RequestMapping("/common/sms")
public class SmsController {

    @Resource
    private SmsUtil smsUtil;

    @Resource
    private VerificationService verificationService;

    @LimitPoint(name = "sms_send", key = "sms")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "String", name = "mobile", value = "手机号"),
            @ApiImplicitParam(paramType = "header", dataType = "String", name = "uuid", value = "uuid"),
    })
    @GetMapping("/{verificationEnums}/{mobile}")
    @ApiOperation(value = "发送短信验证码,一分钟同一个ip请求1次")
    public AjaxResult getSmsCode(
            @RequestHeader String uuid,
            @PathVariable String mobile,
            @PathVariable VerificationEnums verificationEnums) {
        verificationService.check(uuid, verificationEnums);
        smsUtil.sendSmsCode(mobile, verificationEnums, uuid);
        return AjaxResult.success(ResultCode.VERIFICATION_SEND_SUCCESS);
    }
}
