helper.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. Route::get('captcha/[:id]', "\\think\\captcha\\CaptchaController@index");
  12. Validate::extend('captcha', function ($value, $id = '') {
  13. return captcha_check($value, $id);
  14. });
  15. Validate::setTypeMsg('captcha', ':attribute错误!');
  16. /**
  17. * @param string $id
  18. * @param array $config
  19. * @return \think\Response
  20. */
  21. function captcha($id = '', $config = [])
  22. {
  23. $captcha = new \think\captcha\Captcha($config);
  24. return $captcha->entry($id);
  25. }
  26. /**
  27. * @param $id
  28. * @return string
  29. */
  30. function captcha_src($id = '')
  31. {
  32. return Url::build('/captcha' . ($id ? "/{$id}" : ''));
  33. }
  34. /**
  35. * @param $id
  36. * @return mixed
  37. */
  38. function captcha_img($id = '')
  39. {
  40. return '<img src="' . captcha_src($id) . '" alt="captcha" />';
  41. }
  42. /**
  43. * @param $value
  44. * @param string $id
  45. * @param array $config
  46. * @return bool
  47. */
  48. function captcha_check($value, $id = '')
  49. {
  50. $captcha = new \think\captcha\Captcha((array) Config::pull('captcha'));
  51. return $captcha->check($value, $id);
  52. }