helper.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2015 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: yunwuxin <448901948@qq.com>
  10. // +----------------------------------------------------------------------
  11. \think\Route::get('captcha/[:id]', "\\think\\captcha\\CaptchaController@index");
  12. \think\Validate::extend('captcha', function ($value, $id = "")
  13. {
  14. return captcha_check($value, $id, (array) \think\Config::get('captcha'));
  15. });
  16. \think\Validate::setTypeMsg('captcha', '验证码错误!');
  17. /**
  18. * @param string $id
  19. * @param array $config
  20. * @return \think\Response
  21. */
  22. function captcha($id = "", $config = [])
  23. {
  24. $captcha = new \think\captcha\Captcha($config);
  25. return $captcha->entry($id);
  26. }
  27. /**
  28. * @param $id
  29. * @return string
  30. */
  31. function captcha_src($id = "")
  32. {
  33. return \think\Url::build('/captcha' . ($id ? "/{$id}" : ''));
  34. }
  35. /**
  36. * @param $id
  37. * @return mixed
  38. */
  39. function captcha_img($id = "")
  40. {
  41. return '<img src="' . captcha_src($id) . '" alt="captcha" />';
  42. }
  43. /**
  44. * @param $value
  45. * @param string $id
  46. * @param array $config
  47. * @return bool
  48. */
  49. function captcha_check($value, $id = "", $config = [])
  50. {
  51. $captcha = new \think\captcha\Captcha($config);
  52. return $captcha->check($value, $id);
  53. }