App_Controller_IndexController.proxy.php 2.1 KB

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