Request.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. namespace app;
  3. use ln\traits\Macro;
  4. class Request extends \think\Request
  5. {
  6. use Macro;
  7. protected $cache = [];
  8. public function __construct()
  9. {
  10. parent::__construct();
  11. $this->filter[] = function ($val) {
  12. return is_string($val) ? trim($val) : $val;
  13. };
  14. }
  15. public function ip(): string
  16. {
  17. return $this->header('remote-host') ?? parent::ip();
  18. }
  19. public function isApp()
  20. {
  21. return $this->header('Form-type') === 'app';
  22. }
  23. /**
  24. * @param $db
  25. * @param $key
  26. * @return bool
  27. * @author xaboy
  28. * @day 2020/10/22
  29. */
  30. public function hasCache($db, $key)
  31. {
  32. return isset($this->cache[$db][$key]);
  33. }
  34. /**
  35. * @param $db
  36. * @param $key
  37. * @return array|mixed|string
  38. * @author xaboy
  39. * @day 2020/10/22
  40. */
  41. public function getCache($db, $key)
  42. {
  43. if (is_array($key)) {
  44. $data = [];
  45. foreach ($key as $v) {
  46. $data[$v] = $this->getCache($db, $v);
  47. }
  48. return $data;
  49. }
  50. return $this->cache[$db][$key] ?? '';
  51. }
  52. /**
  53. * @param $db
  54. * @param $key
  55. * @param null $value
  56. * @author xaboy
  57. * @day 2020/10/22
  58. */
  59. public function setCache($db, $key, $value = null)
  60. {
  61. if (!isset($this->cache[$db])) $this->cache[$db] = [];
  62. if (is_array($key)) {
  63. foreach ($key as $k => $v) {
  64. $this->setCache($db, $k, $v);
  65. }
  66. return;
  67. }
  68. $this->cache[$db][$key] = $value;
  69. }
  70. public function clearCache()
  71. {
  72. $this->cache = [];
  73. }
  74. public function params(array $names, $filter = '')
  75. {
  76. $data = [];
  77. $flag = false;
  78. if ($filter === true) {
  79. $filter = '';
  80. $flag = true;
  81. }
  82. foreach ($names as $name) {
  83. if (!is_array($name))
  84. $data[$name] = $this->param($name, '', $filter);
  85. else
  86. $data[$name[0]] = $this->param($name[0], $name[1], $filter);
  87. }
  88. return $flag ? array_values($data) : $data;
  89. }
  90. public function merId()
  91. {
  92. return intval($this->hasMacro('merchantId') ? $this->merchantId() : 0);
  93. }
  94. }