App_Controller_IndexController.proxy.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. declare (strict_types=1);
  3. namespace App\Controller;
  4. use App\Tools\CommonService;
  5. use Hyperf\Di\Annotation\Inject;
  6. use Hyperf\HttpServer\Contract\RequestInterface;
  7. use Hyperf\Validation\Contract\ValidatorFactoryInterface;
  8. use App\Request\FooRequest;
  9. use App\Request\SceneRequest;
  10. class IndexController extends AbstractController
  11. {
  12. use \Hyperf\Di\Aop\ProxyTrait;
  13. use \Hyperf\Di\Aop\PropertyHandlerTrait;
  14. function __construct(\Hyperf\Logger\LoggerFactory $loggerFactory)
  15. {
  16. if (method_exists(parent::class, '__construct')) {
  17. parent::__construct(...func_get_args());
  18. }
  19. $this->__handlePropertyHandler(__CLASS__);
  20. }
  21. #[Inject]
  22. protected ValidatorFactoryInterface $validationFactory;
  23. public function index(RequestInterface $request)
  24. {
  25. $validator = $this->validationFactory->make($request->all(), ['foo' => 'required', 'bar' => 'required'], ['foo.required' => 'foo必填', 'bar.required' => 'bar必填']);
  26. if ($validator->fails()) {
  27. // Handle exception
  28. $errorMessage = $validator->errors()->first();
  29. var_dump($errorMessage);
  30. }
  31. }
  32. public function verifyCode()
  33. {
  34. $comm = new CommonService();
  35. $ip = $comm->userIp();
  36. $redis = $this->container->get(\Hyperf\Redis\Redis::class);
  37. $config = new \EasySwoole\VerifyCode\Config();
  38. $code = new \EasySwoole\VerifyCode\VerifyCode($config);
  39. $img_code = '';
  40. $characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
  41. $charLength = strlen($characters);
  42. for ($i = 0; $i < 4; $i++) {
  43. $img_code .= $characters[rand(0, $charLength - 1)];
  44. }
  45. //重写验证码
  46. $result = $code->DrawCode((string) $img_code);
  47. $img_code = $result->getImageCode();
  48. //写入缓存 用于其他方法验证 并且设置过期时间
  49. $redis->set('code' . $ip, $img_code, 60000);
  50. return $result->getImageBase64();
  51. }
  52. }