Request.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 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. use think\File;
  14. use think\file\UploadedFile;
  15. class Request extends \think\Request
  16. {
  17. use Macro;
  18. protected $cache = [];
  19. public function __construct()
  20. {
  21. parent::__construct();
  22. $this->filter[] = function ($val) {
  23. return is_string($val) ? trim($val) : $val;
  24. };
  25. }
  26. public function ip(): string
  27. {
  28. return $this->header('remote-host') ?? parent::ip();
  29. }
  30. public function isApp()
  31. {
  32. return $this->header('Form-type') === 'app';
  33. }
  34. /**
  35. * @param $db
  36. * @param $key
  37. * @return bool
  38. * @author xaboy
  39. * @day 2020/10/22
  40. */
  41. public function hasCache($db, $key)
  42. {
  43. return isset($this->cache[$db][$key]);
  44. }
  45. /**
  46. * @param $db
  47. * @param $key
  48. * @return array|mixed|string
  49. * @author xaboy
  50. * @day 2020/10/22
  51. */
  52. public function getCache($db, $key)
  53. {
  54. if (is_array($key)) {
  55. $data = [];
  56. foreach ($key as $v) {
  57. $data[$v] = $this->getCache($db, $v);
  58. }
  59. return $data;
  60. }
  61. return $this->cache[$db][$key] ?? '';
  62. }
  63. /**
  64. * @param $db
  65. * @param $key
  66. * @param null $value
  67. * @author xaboy
  68. * @day 2020/10/22
  69. */
  70. public function setCache($db, $key, $value = null)
  71. {
  72. if (!isset($this->cache[$db])) $this->cache[$db] = [];
  73. if (is_array($key)) {
  74. foreach ($key as $k => $v) {
  75. $this->setCache($db, $k, $v);
  76. }
  77. return;
  78. }
  79. $this->cache[$db][$key] = $value;
  80. }
  81. public function clearCache()
  82. {
  83. $this->cache = [];
  84. }
  85. public function params(array $names, $filter = '')
  86. {
  87. $data = [];
  88. $flag = false;
  89. if ($filter === true) {
  90. $filter = '';
  91. $flag = true;
  92. }
  93. foreach ($names as $name) {
  94. if (!is_array($name))
  95. $data[$name] = $this->param($name, '', $filter);
  96. else
  97. $data[$name[0]] = $this->param($name[0], $name[1], $filter);
  98. }
  99. return $flag ? array_values($data) : $data;
  100. }
  101. public function merId()
  102. {
  103. return intval($this->hasMacro('merchantId') ? $this->merchantId() : 0);
  104. }
  105. public function setOriginFile($name, $array)
  106. {
  107. $this->file[$name] = $array;
  108. }
  109. public function getOriginFile($name)
  110. {
  111. return $this->file[$name] ?? null;
  112. }
  113. protected function dealUploadFile(array $files, string $name): array
  114. {
  115. $array = [];
  116. foreach ($files as $key => $file) {
  117. if (is_array($file['name'])) {
  118. $item = [];
  119. $keys = array_keys($file);
  120. $count = count($file['name']);
  121. for ($i = 0; $i < $count; $i++) {
  122. if ($file['error'][$i] > 0) {
  123. if ($name == $key) {
  124. $this->throwUploadFileError($file['error'][$i]);
  125. } else {
  126. continue;
  127. }
  128. }
  129. $temp['key'] = $key;
  130. foreach ($keys as $_key) {
  131. $temp[$_key] = $file[$_key][$i];
  132. $name = explode('.',$temp['name']);
  133. $num = count($name);
  134. $suffix = strtolower($name[$num - 1]);
  135. array_pop($name);
  136. $temp['name'] = implode('.',$name).'.'.$suffix;
  137. }
  138. $item[] = new UploadedFile($temp['tmp_name'], $temp['name'], $temp['type'], $temp['error']);
  139. }
  140. $array[$key] = $item;
  141. } else {
  142. if ($file instanceof File) {
  143. $array[$key] = $file;
  144. } else {
  145. if ($file['error'] > 0) {
  146. if ($key == $name) {
  147. $this->throwUploadFileError($file['error']);
  148. } else {
  149. continue;
  150. }
  151. }
  152. $name = explode('.',$file['name']);
  153. $num = count($name);
  154. $suffix = strtolower($name[$num - 1]);
  155. array_pop($name);
  156. $file['name'] = implode('.',$name).'.'.$suffix;
  157. $array[$key] = new UploadedFile($file['tmp_name'], $file['name'], $file['type'], $file['error']);
  158. }
  159. }
  160. }
  161. return $array;
  162. }
  163. }