functions.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. use app\service\system\SystemService;
  3. use support\Response;
  4. use think\Validate;
  5. if (!function_exists("getDateFull"))
  6. {
  7. /**
  8. * @param string $format
  9. * @param string $time
  10. * @return string
  11. */
  12. function getDateFull(string $format = "Y-m-d H:i:s",string $time = ""): string
  13. {
  14. if (empty($time)) $time = time();
  15. return date($format??'Y-m-d H:i:s',$time);
  16. }
  17. }
  18. if (!function_exists("success")) {
  19. /**
  20. * @param string $msg
  21. * @param array $data
  22. * @param int $code
  23. * @return Response
  24. */
  25. function success(string $msg = "", array $data = [], int $code = 1): Response
  26. {
  27. return json(compact('code', 'data', 'msg'));
  28. }
  29. }
  30. if (!function_exists("successTrans")) {
  31. /**
  32. * 消息返回
  33. * @param string $message
  34. * @param array $data
  35. * @param int $code
  36. * @return Response
  37. */
  38. function successTrans(string $message, array $data = [], int $code = 1): Response
  39. {
  40. $msg = trans($message);
  41. return json(compact("msg", "code", "data"));
  42. }
  43. }
  44. if (!function_exists("error")) {
  45. /**
  46. * @param $msg
  47. * @param array $data
  48. * @param int $code
  49. * @return Response
  50. */
  51. function error($msg, array $data = [], int $code = 0): Response
  52. {
  53. return json(compact('code', 'data', 'msg'));
  54. }
  55. }
  56. if (!function_exists("errorTrans")) {
  57. /**
  58. * 消息返回
  59. * @param string $message
  60. * @param array $data
  61. * @param int $code
  62. * @return Response
  63. */
  64. function errorTrans(string $message = "", array $data = [], int $code = 0): Response
  65. {
  66. $msg = trans($message);
  67. return json(compact("msg", "code", "data"));
  68. }
  69. }
  70. if (!function_exists('sConf')) {
  71. /**
  72. * 获取或配置系统参数
  73. * @param string $name 参数名称
  74. * @param string|null $value 参数内容
  75. */
  76. function sConf(string $name = '', string $value = null)
  77. {
  78. if (is_null($value) && is_string($name)) {
  79. return SystemService::get($name);
  80. } else {
  81. return SystemService::set($name, $value);
  82. }
  83. }
  84. }
  85. if (!function_exists("between_time"))
  86. {
  87. /**
  88. * 格式化起止时间(为了兼容前端RangePicker组件)
  89. * 2020-04-01T08:15:08.891Z => 1585670400
  90. * @param array $times
  91. * @param bool $isWithTime 是否包含时间
  92. * @return array
  93. */
  94. function between_time(array $times, bool $isWithTime = false): array
  95. {
  96. foreach ($times as &$time) {
  97. $time = trim($time, '&quot;');
  98. $time = str2date($time, $isWithTime);
  99. }
  100. return ['start_time' => current($times), 'end_time' => next($times)];
  101. }
  102. }
  103. if (!function_exists("str2date"))
  104. {
  105. /**
  106. * 日期转换时间戳
  107. * 例如: 2020-04-01 08:15:08 => 1585670400
  108. * @param string $date
  109. * @param bool $isWithTime 是否包含时间
  110. * @return int
  111. */
  112. function str2date(string $date, bool $isWithTime = false): int
  113. {
  114. if (!$isWithTime) {
  115. $date = date('Y-m-d', strtotime($date));
  116. }
  117. return strtotime($date);
  118. }
  119. }
  120. if (!function_exists("v_valid")) {
  121. function v_valid(array $rules, array|string $input = '', ?callable $callable = null)
  122. {
  123. if (is_string($input)) {
  124. $type = trim($input, '.') ?: 'get';
  125. $input = request()->$type();
  126. }
  127. [$data, $rule, $info] = [[], [], []];
  128. foreach ($rules as $name => $message) if (is_numeric($name)) {
  129. [$name, $alias] = explode('#', $message . '#');
  130. $data[$name] = $input[($alias ?: $name)] ?? null;
  131. } elseif (!str_contains($name, '.')) {
  132. $data[$name] = $message;
  133. } elseif (preg_match('|^(.*?)\.(.*?)#(.*?)#?$|', $name . '#', $matches)) {
  134. [, $_key, $_rule, $alias] = $matches;
  135. if (in_array($_rule, ['value', 'default'])) {
  136. if ($_rule === 'value') $data[$_key] = $message;
  137. elseif ($_rule === 'default') $data[$_key] = $input[($alias ?: $_key)] ?? $message;
  138. } else {
  139. $info[explode(':', $name)[0]] = $message;
  140. $data[$_key] = $data[$_key] ?? ($input[($alias ?: $_key)] ?? null);
  141. $rule[$_key] = isset($rule[$_key]) ? ($rule[$_key] . '|' . $_rule) : $_rule;
  142. }
  143. }
  144. $validate = new Validate();
  145. if ($validate->rule($rule)->message($info)->check($data)) {
  146. return $data;
  147. } elseif (is_callable($callable)) {
  148. return call_user_func($callable, $validate->getError(), $data);
  149. } else {
  150. return $validate->getError();
  151. }
  152. }
  153. }
  154. if (!function_exists("pageFormat")) {
  155. /**
  156. * @param $data
  157. * @param int $size
  158. * @return array {page:"",pageSize:"",rows:[],total:""}
  159. */
  160. function pageFormat($data, int $size = 10): array
  161. {
  162. if (empty($data)) return [];
  163. return [
  164. 'total' => $data->total(),
  165. 'page' => $data->currentPage(),
  166. 'pageSize' => $size,
  167. 'rows' => $data->items()
  168. ];
  169. }
  170. }