WebController.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Controller;
  4. use App\JsonRpc\NewsServiceInterface;
  5. use App\JsonRpc\WebsiteServiceInterface;
  6. use App\Tools\PublicData;
  7. use Hyperf\Di\Annotation\Inject;
  8. use Hyperf\HttpServer\Annotation\AutoController;
  9. use App\Tools\Result;
  10. use Hyperf\Validation\Contract\ValidatorFactoryInterface;
  11. use App\Constants\ErrorCode;
  12. /**
  13. * Class WebController
  14. * @package App\Controller
  15. */
  16. class WebController extends AbstractController
  17. {
  18. #[Inject]
  19. protected ValidatorFactoryInterface $validationFactory;
  20. /**
  21. * @var NewsServiceInterface
  22. */
  23. #[Inject]
  24. private $newsServiceClient;
  25. /**
  26. * @var WebsiteServiceInterface
  27. */
  28. #[Inject]
  29. private $websiteServiceClient;
  30. protected $siteId;
  31. public function __construct(){
  32. $websiteInfo = $this->getWebsiteId();
  33. if($websiteInfo['code'] == 200){
  34. $this->siteId = $websiteInfo['data']['id'];
  35. }
  36. }
  37. /**
  38. * 获取栏目导航
  39. * @return array
  40. */
  41. public function getWebsiteCategory()
  42. {
  43. $data = [
  44. 'website_id'=>$this->siteId
  45. ];
  46. $result = $this->websiteServiceClient->getWebsiteCategory($data);
  47. if ($result['code'] != ErrorCode::SUCCESS) {
  48. return Result::error($result['message'],0,[]);
  49. }
  50. return Result::success($result['data']);
  51. }
  52. /**
  53. * @return array
  54. */
  55. public function getWebsiteId()
  56. {
  57. $header = $this->request->getHeaders();
  58. var_dump("获取header:",$header);
  59. if(!isset($header['origin'])){
  60. return Result::error("header请配置origin");
  61. }
  62. $origin = $header['origin'][0];
  63. $logindevice = explode("//", $origin);
  64. $data = [
  65. 'website_url'=>$logindevice[1]
  66. ];
  67. $result = $this->websiteServiceClient->getWebsiteId($data);
  68. if ($result['code'] != ErrorCode::SUCCESS) {
  69. return Result::error($result['message'],0,[]);
  70. }
  71. return Result::success($result['data']);
  72. }
  73. }