Geetest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace addons\geetest;
  3. use addons\geetest\library\GeetestLib;
  4. use think\Addons;
  5. use think\Validate;
  6. /**
  7. * 极验证插件
  8. */
  9. class Geetest extends Addons
  10. {
  11. /**
  12. * 插件安装方法
  13. * @return bool
  14. */
  15. public function install()
  16. {
  17. return true;
  18. }
  19. /**
  20. * 插件卸载方法
  21. * @return bool
  22. */
  23. public function uninstall()
  24. {
  25. return true;
  26. }
  27. /**
  28. * 插件启用方法
  29. * @return bool
  30. */
  31. public function enable()
  32. {
  33. return true;
  34. }
  35. /**
  36. * 插件禁用方法
  37. * @return bool
  38. */
  39. public function disable()
  40. {
  41. return true;
  42. }
  43. /**
  44. * 自定义captcha验证事件
  45. */
  46. public function actionBegin()
  47. {
  48. Validate::extend('captcha', function ($value, $id = "") {
  49. $request = request();
  50. $geetest_challenge = $request->post('geetest_challenge');
  51. $geetest_validate = $request->post('geetest_validate');
  52. $geetest_seccode = $request->post('geetest_seccode');
  53. if (!$geetest_challenge || !$geetest_validate || !$geetest_seccode) {
  54. Validate::setTypeMsg('captcha', __('请先完成验证!'));
  55. return false;
  56. }
  57. $data = array(
  58. "user_id" => session('geetest_user_id'), # 网站用户id
  59. "client_type" => $request->isMobile() ? 'h5' : 'web', #web:电脑上的浏览器;h5:手机上的浏览器,包括移动应用内完全内置的web_view;native:通过原生SDK植入APP应用的方式
  60. "ip_address" => $request->ip() # 请在此处传输用户请求验证时所携带的IP
  61. );
  62. $config = get_addon_config('geetest');
  63. if (!$config['appid'] || !$config['appkey']) {
  64. Validate::setTypeMsg('captcha', '请先在后台中配置极验证的参数信息');
  65. return false;
  66. }
  67. $GtSdk = new GeetestLib($config['appid'], $config['appkey']);
  68. if (session('geetest_status') == 1) {
  69. //服务器正常
  70. $result = $GtSdk->success_validate($geetest_challenge, $geetest_validate, $geetest_seccode, $data);
  71. if (!$result) {
  72. Validate::setTypeMsg('captcha', '请先完成验证!');
  73. return false;
  74. }
  75. } else {
  76. //服务器宕机,走failback模式
  77. if (!$GtSdk->fail_validate($geetest_challenge, $geetest_validate, $geetest_seccode)) {
  78. Validate::setTypeMsg('captcha', '请先完成验证!');
  79. return false;
  80. }
  81. }
  82. return true;
  83. });
  84. }
  85. /**
  86. * 追加JS中配置
  87. * @param $params
  88. */
  89. public function configInit(&$params)
  90. {
  91. $config = get_addon_config('geetest');
  92. $params['geetest'] = ['product' => $config['product']];
  93. }
  94. }