Request.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app;
  12. use crmeb\traits\Macro;
  13. class Request extends \think\Request
  14. {
  15. use Macro;
  16. protected $cache = [];
  17. public function __construct()
  18. {
  19. parent::__construct();
  20. $this->filter[] = function ($val) {
  21. return is_string($val) ? trim($val) : $val;
  22. };
  23. }
  24. public function ip(): string
  25. {
  26. return $this->header('remote-host') ?? parent::ip();
  27. }
  28. /**
  29. * @param $db
  30. * @param $key
  31. * @return bool
  32. * @author xaboy
  33. * @day 2020/10/22
  34. */
  35. public function hasCache($db, $key)
  36. {
  37. return isset($this->cache[$db][$key]);
  38. }
  39. /**
  40. * @param $db
  41. * @param $key
  42. * @return array|mixed|string
  43. * @author xaboy
  44. * @day 2020/10/22
  45. */
  46. public function getCache($db, $key)
  47. {
  48. if (is_array($key)) {
  49. $data = [];
  50. foreach ($key as $v) {
  51. $data[$v] = $this->getCache($db, $v);
  52. }
  53. return $data;
  54. }
  55. return $this->cache[$db][$key] ?? '';
  56. }
  57. /**
  58. * @param $db
  59. * @param $key
  60. * @param null $value
  61. * @author xaboy
  62. * @day 2020/10/22
  63. */
  64. public function setCache($db, $key, $value = null)
  65. {
  66. if (!isset($this->cache[$db])) $this->cache[$db] = [];
  67. if (is_array($key)) {
  68. foreach ($key as $k => $v) {
  69. $this->setCache($db, $k, $v);
  70. }
  71. return;
  72. }
  73. $this->cache[$db][$key] = $value;
  74. }
  75. public function clearCache()
  76. {
  77. $this->cache = [];
  78. }
  79. public function params(array $names, $filter = '')
  80. {
  81. $data = [];
  82. $flag = false;
  83. if ($filter === true) {
  84. $filter = '';
  85. $flag = true;
  86. }
  87. foreach ($names as $name) {
  88. if (!is_array($name))
  89. $data[$name] = $this->param($name, '', $filter);
  90. else
  91. $data[$name[0]] = $this->param($name[0], $name[1], $filter);
  92. }
  93. return $flag ? array_values($data) : $data;
  94. }
  95. public function merId()
  96. {
  97. return intval($this->hasMacro('merchantId') ? $this->merchantId() : 0);
  98. }
  99. }