package gaf3.core.services.verify

import gaf3.core.data.ErrorResult
import gaf3.core.exception.BusinessError
import gaf3.core.services.verify.service.VerifyCodeService
import org.springframework.http.MediaType
import org.springframework.web.bind.annotation.*

@RestController
class VerifyCodeController(val service: VerifyCodeService) {

    @GetMapping(path = ["/gaf/verify/{state}.jpg"], produces = [MediaType.IMAGE_JPEG_VALUE])
    @ResponseBody
    fun create(@PathVariable("state") state: String): ByteArray {
        return service.create(state)
    }

    @PostMapping(path = ["/gaf/verify/image"])
    fun verify(state: String, code: String): ErrorResult {
        return if(service.verify(state, code))
            ErrorResult(0,"success")
        else
            ErrorResult(BusinessError.ERR_VERIFYCODE_INVALID, "验证码无效")
    }
}