| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- <?php
- use app\service\system\SystemService;
- use support\Response;
- use think\Validate;
- if (!function_exists("getDateFull"))
- {
- /**
- * @param string $format
- * @param string $time
- * @return string
- */
- function getDateFull(string $format = "Y-m-d H:i:s",string $time = ""): string
- {
- if (empty($time)) $time = time();
- return date($format??'Y-m-d H:i:s',$time);
- }
- }
- if (!function_exists("success")) {
- /**
- * @param string $msg
- * @param array $data
- * @param int $code
- * @return Response
- */
- function success(string $msg = "", array $data = [], int $code = 1): Response
- {
- return json(compact('code', 'data', 'msg'));
- }
- }
- if (!function_exists("successTrans")) {
- /**
- * 消息返回
- * @param string $message
- * @param array $data
- * @param int $code
- * @return Response
- */
- function successTrans(string $message, array $data = [], int $code = 1): Response
- {
- $msg = trans($message);
- return json(compact("msg", "code", "data"));
- }
- }
- if (!function_exists("error")) {
- /**
- * @param $msg
- * @param array $data
- * @param int $code
- * @return Response
- */
- function error($msg, array $data = [], int $code = 0): Response
- {
- return json(compact('code', 'data', 'msg'));
- }
- }
- if (!function_exists("errorTrans")) {
- /**
- * 消息返回
- * @param string $message
- * @param array $data
- * @param int $code
- * @return Response
- */
- function errorTrans(string $message = "", array $data = [], int $code = 0): Response
- {
- $msg = trans($message);
- return json(compact("msg", "code", "data"));
- }
- }
- if (!function_exists('sConf')) {
- /**
- * 获取或配置系统参数
- * @param string $name 参数名称
- * @param string|null $value 参数内容
- */
- function sConf(string $name = '', string $value = null)
- {
- if (is_null($value) && is_string($name)) {
- return SystemService::get($name);
- } else {
- return SystemService::set($name, $value);
- }
- }
- }
- if (!function_exists("between_time"))
- {
- /**
- * 格式化起止时间(为了兼容前端RangePicker组件)
- * 2020-04-01T08:15:08.891Z => 1585670400
- * @param array $times
- * @param bool $isWithTime 是否包含时间
- * @return array
- */
- function between_time(array $times, bool $isWithTime = false): array
- {
- foreach ($times as &$time) {
- $time = trim($time, '"');
- $time = str2date($time, $isWithTime);
- }
- return ['start_time' => current($times), 'end_time' => next($times)];
- }
- }
- if (!function_exists("str2date"))
- {
- /**
- * 日期转换时间戳
- * 例如: 2020-04-01 08:15:08 => 1585670400
- * @param string $date
- * @param bool $isWithTime 是否包含时间
- * @return int
- */
- function str2date(string $date, bool $isWithTime = false): int
- {
- if (!$isWithTime) {
- $date = date('Y-m-d', strtotime($date));
- }
- return strtotime($date);
- }
- }
- if (!function_exists("v_valid")) {
- function v_valid(array $rules, array|string $input = '', ?callable $callable = null)
- {
- if (is_string($input)) {
- $type = trim($input, '.') ?: 'get';
- $input = request()->$type();
- }
- [$data, $rule, $info] = [[], [], []];
- foreach ($rules as $name => $message) if (is_numeric($name)) {
- [$name, $alias] = explode('#', $message . '#');
- $data[$name] = $input[($alias ?: $name)] ?? null;
- } elseif (!str_contains($name, '.')) {
- $data[$name] = $message;
- } elseif (preg_match('|^(.*?)\.(.*?)#(.*?)#?$|', $name . '#', $matches)) {
- [, $_key, $_rule, $alias] = $matches;
- if (in_array($_rule, ['value', 'default'])) {
- if ($_rule === 'value') $data[$_key] = $message;
- elseif ($_rule === 'default') $data[$_key] = $input[($alias ?: $_key)] ?? $message;
- } else {
- $info[explode(':', $name)[0]] = $message;
- $data[$_key] = $data[$_key] ?? ($input[($alias ?: $_key)] ?? null);
- $rule[$_key] = isset($rule[$_key]) ? ($rule[$_key] . '|' . $_rule) : $_rule;
- }
- }
- $validate = new Validate();
- if ($validate->rule($rule)->message($info)->check($data)) {
- return $data;
- } elseif (is_callable($callable)) {
- return call_user_func($callable, $validate->getError(), $data);
- } else {
- return $validate->getError();
- }
- }
- }
- if (!function_exists("pageFormat")) {
- /**
- * @param $data
- * @param int $size
- * @return array {page:"",pageSize:"",rows:[],total:""}
- */
- function pageFormat($data, int $size = 10): array
- {
- if (empty($data)) return [];
- return [
- 'total' => $data->total(),
- 'page' => $data->currentPage(),
- 'pageSize' => $size,
- 'rows' => $data->items()
- ];
- }
- }
|