UtilService.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | [ UtilService ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2018-2020 rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Author: TABLE ME
  8. // +----------------------------------------------------------------------
  9. // | Date: 2020-08-25 17:30
  10. // +----------------------------------------------------------------------
  11. namespace library\services;
  12. use library\exceptions\UtilException;
  13. class UtilService
  14. {
  15. private $data;
  16. public function getData()
  17. {
  18. return $this->data;
  19. }
  20. public function setData($k, $v)
  21. {
  22. $this->data[$k] = $v;
  23. return $this;
  24. }
  25. public function __set($_name,$_val){
  26. $this->data[$_name] = $_val;
  27. }
  28. /**
  29. * 参数判断模型
  30. * @param $params [
  31. * [ 'parm1' => 'post|get',
  32. * 'parm2' => '',//默认值
  33. * 'parm3' => 'empty',//函数
  34. * 'parm4' => '错误信息',
  35. * ]
  36. * ]
  37. * @param null $request
  38. * @param bool $suffix
  39. */
  40. public static function getMore($params, $request = null, $suffix = false)
  41. {
  42. $fiAr = ['empty', 'is_null', 'isset', 'is_numeric', 'is_moblie'];
  43. if ($request === null) {
  44. $request = request();
  45. }
  46. $i = 0;
  47. foreach ($params as $param) {
  48. if (!is_array($param)) {
  49. $p[$suffix == true ? $i++ : $param] = $request->param($param);
  50. } else {
  51. if (!isset($param[1])) $param[1] = null;
  52. if (!isset($param[2])) $param[2] = '';
  53. if (!isset($param[3])) $param[3] = '';
  54. if (is_array($param[0])) {
  55. $name = $param[0][0];
  56. $keyName = $param[0][0];
  57. foreach ($param[0] as $k => $v) {
  58. $name = $k;
  59. $keyName = $v;
  60. }
  61. } else {
  62. $name = $param[0];
  63. $keyName = $param[0];
  64. }
  65. $value = $request->param($name, $param[1]);
  66. $p[$suffix == true ? $i++ : $keyName] = $value;
  67. if (in_array($param[2], $fiAr)) {
  68. if ($param[2] == 'empty') {
  69. if (empty($value)) {
  70. throw new UtilException($param[3]);
  71. }
  72. }
  73. if ($param[2] == 'is_null') {
  74. if (is_null($value)) {
  75. throw new UtilException($param[3]);
  76. }
  77. }
  78. if ($param[2] == 'isset') {
  79. if (!isset($value)) {
  80. throw new UtilException($param[3]);
  81. }
  82. }
  83. if ($param[2] == 'is_numeric') {
  84. if (!is_numeric($value)) {
  85. throw new UtilException($param[3]);
  86. }
  87. }
  88. if ($param[2] == 'is_moblie') {
  89. if (!is_moblie($value)) {
  90. throw new UtilException($param[3]);
  91. }
  92. }
  93. } else {
  94. $parmValue = $request->param($name, $param[1]);
  95. //大于
  96. if (strrpos($param[2], "gt|") === 0) {
  97. $number = str_replace("gt|", "", $param[2]);
  98. if (!is_numeric($number) || floatval($parmValue) < floatval($number)) {
  99. throw new UtilException($param[3]);
  100. }
  101. continue;
  102. }
  103. //小于
  104. if (strrpos($param[2], "lt|") === 0) {
  105. $number = str_replace("lt|", "", $param[2]);
  106. if (!is_numeric($number) || floatval($parmValue) > floatval($number)) {
  107. throw new UtilException($param[3]);
  108. }
  109. continue;
  110. }
  111. //函数
  112. if (!empty($param[2]) && is_string($param[2]) && !function_exists($param[2])) {
  113. throw new UtilException('方法' . $param[2] . '不存在');
  114. }
  115. if (!empty($param[2]) && !$param[2]($request->param($name, $param[1]))) {
  116. throw new UtilException($param[3]);
  117. }
  118. }
  119. }
  120. }
  121. return $p;
  122. }
  123. /**
  124. * 参数解析 | 返回
  125. * @param $params
  126. * @param $data
  127. */
  128. public static function getParam($params, $data,$isAll = false)
  129. {
  130. $result = [];
  131. foreach ($data as $k => $vv) {
  132. $d = [];
  133. foreach ($params as $v) {
  134. if (is_array($v)) {
  135. $alias = empty($v[0]) ? $v[1] : $v[0];
  136. $key = $v[1];
  137. $callStr = empty($v[2]) ? '' : $v[2];
  138. $val = $vv[$key];
  139. if ($callStr instanceof \Closure) {
  140. if($isAll){
  141. $_val = $callStr($val,$vv);
  142. }else{
  143. $_val = $callStr($val);
  144. }
  145. if ($_val instanceof UtilService) {
  146. foreach ($_val->getData() as $mk => $kv) {
  147. $d[$mk] = $kv;
  148. }
  149. } else {
  150. $d[$alias] = $_val;
  151. }
  152. } else if (!empty($callStr)) {
  153. $e = null;
  154. $callStr = str_replace('$1', '$val', $callStr);
  155. eval('$e =' . $callStr . ';');
  156. $d[$alias] = $e;
  157. } else {
  158. $d[$alias] = $val;
  159. }
  160. } else {
  161. $d[$v] = $vv[$v];
  162. }
  163. }
  164. $result[] = $d;
  165. }
  166. return $result;
  167. }
  168. }