应用至商城
添加依赖
shop-parent的pom.xml
<!-- 腾讯云依赖 -->
<tencent.version>3.0.111</tencent.version>
<!-- 腾讯云依赖 -->
<dependency>
<groupId>com.tencentcloudapi</groupId>
<artifactId>tencentcloud-sdk-java</artifactId>
<version>${tencent.version}</version>
</dependency>
shop-portal的pom.xml
<!-- 腾讯云依赖 -->
<dependency>
<groupId>com.tencentcloudapi</groupId>
<artifactId>tencentcloud-sdk-java</artifactId>
</dependency>
前台系统编写Service
shop-portal的CaptchaService.java
package com.xxxx.portal.service;
import com.xxxx.common.result.BaseResult;
/**
* 腾讯行为验证service
*/
public interface CaptchaService {
/**
* 腾讯行为验证
* @return
*/
BaseResult captchaVerify(String ticket,String randStr);
}
shop-portal的CaptchaServiceImpl.java
package com.xxxx.portal.service.serviceImpl;
import com.xxxx.common.result.BaseResult;
import com.xxxx.portal.service.CaptchaService;
import com.tencentcloudapi.captcha.v20190722.CaptchaClient;
import com.tencentcloudapi.captcha.v20190722.models.DescribeCaptchaResultRequest;
import com.tencentcloudapi.captcha.v20190722.models.DescribeCaptchaResultResponse;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import org.springframework.stereotype.Service;
/**
* 腾讯行为验证Service实现类
*
* @author zhoubin
* @since 1.0.0
*/
@Service
public class CaptchaServiceImpl implements CaptchaService {
/**
* 腾讯行为验证
*
* @return
*/
@Override
public BaseResult captchaVerify(String ticket, String randStr) {
try {
//获取授权
Credential cred = new Credential("AKIDD69Ynx96AGRkCU8aSUThaSDqU6bm2TKW ",
"IPJBD7tWyzulPxX8Zpur9wZ4RemjbNwL");
//创建HttpProfile对象
HttpProfile httpProfile = new HttpProfile();
httpProfile.setEndpoint("captcha.tencentcloudapi.com");
//创建ClientProfile对象
ClientProfile clientProfile = new ClientProfile();
clientProfile.setHttpProfile(httpProfile);
//选择地区
CaptchaClient client = new CaptchaClient(cred, "ap-shanghai", clientProfile);
//拼接参数
String params =
"{\"CaptchaType\":9,\"Ticket\":\"" + ticket + "\",\"UserIp\":\"127.0.0.1\",\"Randstr\":\"" + randStr + "\"," +
"\"CaptchaAppId\":2093497282,\"AppSecretKey\":\"0vBtQT7uGriwdbJ_PDlIr6Q**\"}";
DescribeCaptchaResultRequest req = DescribeCaptchaResultRequest.fromJsonString(params,
DescribeCaptchaResultRequest.class);
//执行调用
DescribeCaptchaResultResponse resp = client.DescribeCaptchaResult(req);
System.out.println(DescribeCaptchaResultRequest.toJsonString(resp));
if ("OK".equalsIgnoreCase
(resp.getCaptchaMsg())) {
return BaseResult.success();
}
} catch (Exception e) {
System.out.println(e.toString());
}
return BaseResult.error();
}
}
前台系统编写Controller
shop-portal的CaptchaController.java
package com.xxxx.portal.controller;
import com.xxxx.common.result.BaseResult;
import com.xxxx.portal.service.CaptchaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* 腾讯行为验证Controller
*
* @author zhoubin
* @since 1.0.0
*/
@Controller
public class CaptchaController {
@Autowired
private CaptchaService captchaService;
/**
* 验证码判断
* @param ticket
* @param randStr
* @return
*/
@RequestMapping("getCaptcha")
@ResponseBody
public BaseResult getCaptcha(String ticket,String randStr){
return captchaService.captchaVerify(ticket, randStr);
}
}
前台系统页面处理
shop-portal的register.ftl
<div class="item">
<span class="label">验证码:</span>
<!--点击此元素会自动激活验证码-->
<!--id : 元素的 ID (必须)-->
<!--data-appid : AppID(必须)-->
<!--data-cbfn : 回调函数名(必须)-->
<!--data-biz-state : 业务自定义透传参数(可选)-->
<button id="TencentCaptcha"
data-appid="2093497282"
data-cbfn="callback"
type="button">验证
</button>
<span style="color: red" id="msg"></span>
</div>
<!--item end-->
<script src="https://ssl.captcha.qq.com/TCaptcha.js"></script>
<script type="text/javascript">
var check = false;
window.callback = function (res) {
console.log(res)
// res(用户主动关闭验证码)= {ret: 2, ticket: null}
// res(验证成功) = {ret: 0, ticket: "String", randstr: "String"}
if (res.ret === 0) {
// alert(res.ticket) // 票据
$.ajax({
type: "post",
url: "${ctx}/getCaptcha",
data: {
"ticket": res.ticket,
"randStr": res.randstr,
},
success: function (result) {
if (200 == result.code) {
check = true;
$("#msg").html("验证成功");
} else {
$("#msg").html("验证失败");
}
}
});
}
}
// 用户注册
$("#registsubmit").on("click", function () {
console.log(check);
if (check) {
$.ajax({
url: "${ctx}/user/register",
type: "POST",
data: {
email: $("#mail").val(),
userName: $("#username").val(),
password: $("#pwd").val()
},
dataType: "JSON",
success: function (result) {
if (200 == result.code) {
layer.alert("亲,注册成功,请登录!");
setTimeout(function () {
location.href = "${ctx}/login";
}, 5000);
} else {
layer.msg("亲,系统正在升级中,请稍后再试!");
}
},
error: function () {
layer.alert("亲,系统正在升级中,请稍后再试!");
}
});
} else {
$("#msg").html("请进行验证");
}
});
</script>