Request.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Date: 2017/09/01
  5. * Time: 15:06
  6. */
  7. namespace common\components;
  8. use Yii;
  9. use yii\helpers\ArrayHelper;
  10. class Request extends \yii\web\Request {
  11. const DEVICE_PC = 'pc';
  12. const DEVICE_Mobile = 'mobile';
  13. const DEVICE_APP = 'app';
  14. private $_device = null;
  15. private $_deviceInfo = null;
  16. private $_suppressResponseCode = null;
  17. /**
  18. * 返回 post 和 get 的数据
  19. * @param null $name
  20. * @return array|mixed
  21. */
  22. public function request($name = null){
  23. $result = $this->post($name);
  24. if(!$result){
  25. $result = $this->get($name);
  26. }
  27. return $result;
  28. }
  29. /**
  30. * 合并获取参数
  31. *
  32. * @param $name
  33. * @param null $default
  34. * @return null
  35. */
  36. public function all($name = null ,$default = NULL){
  37. if(is_null($name)){
  38. return ArrayHelper::merge($this->getQueryParams(), $this->post());
  39. }
  40. $params = $this->getQueryParams();
  41. if(isset($params[$name])){
  42. return $params[$name];
  43. }
  44. $value = $this->post($name ,$default);
  45. return $value ? : $default;
  46. }
  47. /**
  48. * 校验完成Csrf之后清空csrf
  49. * @param null $clientSuppliedToken
  50. * @return bool
  51. */
  52. public function validateCsrfToken($clientSuppliedToken = null)
  53. {
  54. if(Yii::$app->controller->module->id == 'gii'){
  55. return true;
  56. }
  57. return parent::validateCsrfToken($clientSuppliedToken);
  58. }
  59. /**
  60. * 获取设备类型
  61. * @return mixed
  62. */
  63. public function getDevice(){
  64. if($this->_device === null){
  65. $header = Yii::$app->request->getHeaders();
  66. if(isset($header['device-type']) && ($header['device-type'] == self::DEVICE_PC || $header['device-type'] == self::DEVICE_APP)){
  67. $this->_device = $header['device-type'];
  68. } else {
  69. $this->_device = self::DEVICE_PC;
  70. }
  71. }
  72. return $this->_device;
  73. }
  74. /**
  75. * 获取设备信息
  76. * @return mixed|null|string
  77. */
  78. public function getDeviceInfo(){
  79. if($this->_deviceInfo === null){
  80. $header = Yii::$app->request->getHeaders();
  81. if(isset($header['device-info']) && $header['device-info']){
  82. // System,Version,NetworkType,UUID
  83. $pattern = '/System\:([\w]+)\;\s+Version\:([\w\.]+)\;\s+NetworkType\:([\w\.]+)\;\s+UUID\:([\-\w]+)/i';
  84. $infoArr = [];
  85. if(preg_match($pattern, $header['device-info'], $infoArr)){
  86. $this->_deviceInfo = [
  87. 'system' => $infoArr[1],
  88. 'version' => $infoArr[2],
  89. 'networkType' => $infoArr[3],
  90. 'uuid' => $infoArr[4],
  91. ];
  92. }
  93. } else {
  94. $this->_deviceInfo = [];
  95. }
  96. }
  97. return $this->_deviceInfo;
  98. }
  99. /**
  100. * 获取是否支持json响应代码
  101. * @return bool
  102. */
  103. public function getSuppressResponseCode(){
  104. if($this->_suppressResponseCode === null){
  105. $header = Yii::$app->request->getHeaders();
  106. if(isset($header['Suppress-Response-Code']) && $header['Suppress-Response-Code'] == 1){
  107. $this->_suppressResponseCode = true;
  108. }
  109. elseif (Yii::$app->request->get('suppress_response_code')){
  110. $this->_suppressResponseCode = true;
  111. }
  112. else {
  113. $this->_suppressResponseCode = false;
  114. }
  115. }
  116. return $this->_suppressResponseCode;
  117. }
  118. /**
  119. * 向query中添加数据
  120. * @param array $params
  121. * @return array|bool
  122. */
  123. public function appendQueryParams(array $params = []){
  124. if(!$params){
  125. return false;
  126. }
  127. $queryParams = \Yii::$app->request->getQueryParams();
  128. if($queryParams){
  129. $queryParams = ArrayHelper::merge($queryParams, $params);
  130. }else{
  131. $queryParams = $params;
  132. }
  133. \Yii::$app->request->setQueryParams($queryParams);
  134. return $queryParams;
  135. }
  136. }