Request.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: shenyang
  5. * Date: 2018/7/9
  6. * Time: 下午9:40
  7. */
  8. namespace app\framework\Http;
  9. use app\common\facades\SiteSetting;
  10. use Illuminate\Database\QueryException;
  11. use Illuminate\Support\Str;
  12. class Request extends \Illuminate\Http\Request
  13. {
  14. private $route;
  15. /**
  16. * @var bool
  17. */
  18. private $isBackend;
  19. public function isBackend()
  20. {
  21. if (!isset($this->isBackend)) {
  22. if (config('app.framework') == 'platform') {
  23. $this->isBackend = strpos(request()->getRequestUri(), config('app.isWeb')) !== false ? true : false;
  24. } else {
  25. $this->isBackend = strpos($_SERVER['PHP_SELF'], '/web/index.php') !== false ? true : false;
  26. }
  27. }
  28. return $this->isBackend;
  29. }
  30. public function isShop()
  31. {
  32. return !$this->isPlugins();
  33. }
  34. public function isFrontend()
  35. {
  36. if (strpos(request()->getRequestUri(), '/addons/') !== false
  37. && strpos(request()->getRequestUri(), '/api.php') !== false
  38. ) {
  39. return true;
  40. }
  41. return false;
  42. }
  43. public function isPayment()
  44. {
  45. return strpos($_SERVER['PHP_SELF'], '/payment/') > 0 ? true : false;
  46. }
  47. public function isPlugins()
  48. {
  49. return Str::startsWith(request('route'), 'plugin.');
  50. }
  51. public function isCron()
  52. {
  53. return strpos(request()->getRequestUri(), '/addons/') !== false &&
  54. strpos(request()->getRequestUri(), '/cron.php') !== false;
  55. }
  56. /**
  57. * 前后端强制https跳转app
  58. *
  59. * @return string
  60. */
  61. public function getScheme()
  62. {
  63. try {
  64. if ((config('app.framework') == 'platform' && file_exists(base_path().'/bootstrap/install.lock'))
  65. || config('app.framework') != 'platform') {
  66. if (SiteSetting::get('base.https')) {
  67. return 'https';
  68. }
  69. }
  70. } catch (QueryException $exception) {
  71. \Log::error('getScheme error', $exception->getMessage());
  72. }
  73. return parent::getScheme();
  74. }
  75. public function getHost()
  76. {
  77. try {
  78. if ((config('app.framework') == 'platform' && file_exists(base_path().'/bootstrap/install.lock'))
  79. || config('app.framework') != 'platform') {
  80. if (parent::getHost() == 'localhost' && SiteSetting::get('base.host')) {
  81. return SiteSetting::get('base.host');
  82. }
  83. }
  84. } catch (QueryException $exception) {
  85. \Log::error('getHost error', $exception->getMessage());
  86. }
  87. return parent::getHost(); // TODO: Change the autogenerated stub
  88. }
  89. public function setRoute($route)
  90. {
  91. $this->route = $route;
  92. }
  93. public function getRoute()
  94. {
  95. return $this->route;
  96. }
  97. }