Index.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <?php
  2. namespace addons\geetest\controller;
  3. use addons\geetest\library\GeetestLib;
  4. use fast\Random;
  5. use think\addons\Controller;
  6. class Index extends Controller
  7. {
  8. public function index()
  9. {
  10. $this->error("当前插件暂无前台页面");
  11. }
  12. /**
  13. * 初始化验证码
  14. */
  15. public function start()
  16. {
  17. // 读取插件配置
  18. $config = get_addon_config('geetest');
  19. if (!$config['appid'] || !$config['appkey']) {
  20. $this->error('请先在后台中配置极验证的参数信息');
  21. }
  22. // 优先取网站的登录用户ID,没有的情况下取Session中的值,再没有的情况下随机生成
  23. $user_id = $this->auth->id ? $this->auth->id : (session('geetest_user_id') ? session('geetest_user_id') : Random::alnum(8));
  24. $gtSdk = new GeetestLib($config['appid'], $config['appkey']);
  25. $data = array(
  26. "user_id" => $user_id, # 网站用户id
  27. "client_type" => $this->request->isMobile() ? 'h5' : 'web', #web:电脑上的浏览器;h5:手机上的浏览器,包括移动应用内完全内置的web_view;native:通过原生SDK植入APP应用的方式
  28. "ip_address" => $this->request->ip() # 请在此处传输用户请求验证时所携带的IP
  29. );
  30. // 判断极验证服务器状态
  31. $status = $gtSdk->pre_process($data, 1);
  32. session('geetest_status', $status);
  33. session('geetest_user_id', $data['user_id']);
  34. $this->success('', null, $gtSdk->get_response());
  35. }
  36. }