package com.css.txw.sso.controller.verify;

import cn.hutool.core.util.StrUtil;
import com.css.txw.sso.service.verify.VerifyService;
import com.css.ggzc.framework.common.pojo.CommonResult;
import com.css.ggzc.framework.common.util.servlet.ServletUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.servlet.http.HttpServletRequest;

@Tag(name = "验证码")
@RestController
@RequestMapping("/verify")
public class VerifyController {

    @Resource
    private VerifyService verifyService;


    @Operation(summary = "获取验证码")
    @PermitAll
    @PostMapping("/get")
    public CommonResult<String> check(HttpServletRequest request){
        final String remoteId = getRemoteId(request);
        return verifyService.getVerifyToken(remoteId);
    }

    public static String getRemoteId(HttpServletRequest request) {
        String ip = ServletUtils.getClientIP(request);
        String ua = request.getHeader("user-agent");
        if (StrUtil.isNotBlank(ip)) {
            return ip + ua;
        }
        return request.getRemoteAddr() + ua;
    }


}
