快速接入
1. 前端引入 SDK
<button id="captcha-btn">点击验证</button>
<script src="https://captcha.yinxh.fun/js/sdk.js"></script>
<script>
OneCaptcha.init({
el: '#captcha-btn',
server: 'https://captcha.yinxh.fun',
onSuccess: function(token) {
console.log('验证通过, token:', token);
},
onFail: function() {
console.log('验证失败');
},
onClose: function() {
console.log('用户关闭');
}
});
</script>
2. 后端校验 Token
用户验证通过后,前端收到 token,将其发送到你的后端,再调用验证服务校验:
POST https://captcha.yinxh.fun/api/validate-token
Content-Type: application/json
{ "token": "用户提交的 token" }
// 响应
{ "valid": true }
3. Go 后端示例
func verifyCaptcha(token string) bool {
body, _ := json.Marshal(map[string]string{"token": token})
resp, err := http.Post(
"https://captcha.yinxh.fun/api/validate-token",
"application/json",
bytes.NewReader(body),
)
if err != nil {
return false
}
defer resp.Body.Close()
var result struct{ Valid bool `json:"valid"` }
json.NewDecoder(resp.Body).Decode(&result)
return result.Valid
}