functions.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. <?php
  2. use app\service\system\SystemService;
  3. use support\Response;
  4. use Webman\Event\Event;
  5. if (!function_exists("between_time"))
  6. {
  7. /**
  8. * 格式化起止时间(为了兼容前端RangePicker组件)
  9. * 2020-04-01T08:15:08.891Z => 1585670400
  10. * @param array $times
  11. * @param bool $isWithTime 是否包含时间
  12. * @return array
  13. */
  14. function between_time(array $times, bool $isWithTime = false): array
  15. {
  16. foreach ($times as &$time) {
  17. $time = trim($time, '&quot;');
  18. $time = str2date($time, $isWithTime);
  19. }
  20. return ['start_time' => current($times), 'end_time' => next($times)];
  21. }
  22. }
  23. if (!function_exists("str2date"))
  24. {
  25. /**
  26. * 日期转换时间戳
  27. * 例如: 2020-04-01 08:15:08 => 1585670400
  28. * @param string $date
  29. * @param bool $isWithTime 是否包含时间
  30. * @return int
  31. */
  32. function str2date(string $date, bool $isWithTime = false): int
  33. {
  34. if (!$isWithTime) {
  35. $date = date('Y-m-d', strtotime($date));
  36. }
  37. return strtotime($date);
  38. }
  39. }
  40. if(!function_exists("hide_mobile")){
  41. /**
  42. * 手机号码脱敏
  43. * @param string $mobile
  44. * @param int $len 4 末尾4位 6 末尾2位
  45. * @return string
  46. */
  47. function hide_mobile(string $mobile,int $len = 6): string
  48. {
  49. return substr_replace($mobile, ($len==4 ? '****' : '******'), 3, $len);
  50. }
  51. }
  52. if (!function_exists('hide_str')) {
  53. /**
  54. * 将一个字符串部分字符用*替代隐藏
  55. * @param string $string 待转换的字符串
  56. * @param int $begin 起始位置,从0开始计数,当$type=4时,表示左侧保留长度
  57. * @param int $len 需要转换成*的字符个数,当$type=4时,表示右侧保留长度
  58. * @param int $type 转换类型:0,从左向右隐藏;1,从右向左隐藏;2,从指定字符位置分割前由右向左隐藏;3,从指定字符位置分割后由左向右隐藏;4,保留首末指定字符串中间用***代替
  59. * @param string $glue 分割符
  60. * @return string 处理后的字符串
  61. */
  62. function hide_str(string $string, int $begin = 3, int $len = 4, int $type = 0, string $glue = "@"): string
  63. {
  64. $array = array();
  65. if ($type == 0 || $type == 1 || $type == 4) {
  66. $strlen = $length = mb_strlen($string);
  67. while ($strlen) {
  68. $array[] = mb_substr($string, 0, 1, "utf8");
  69. $string = mb_substr($string, 1, $strlen, "utf8");
  70. $strlen = mb_strlen($string);
  71. }
  72. }
  73. if ($type == 0) {
  74. for ($i = $begin; $i < ($begin + $len); $i++) {
  75. if (isset($array[$i])) {
  76. $array[$i] = "*";
  77. }
  78. }
  79. $string = implode("", $array);
  80. } elseif ($type == 1) {
  81. $array = array_reverse($array);
  82. for ($i = $begin; $i < ($begin + $len); $i++) {
  83. if (isset($array[$i])) {
  84. $array[$i] = "*";
  85. }
  86. }
  87. $string = implode("", array_reverse($array));
  88. } elseif ($type == 2) {
  89. $array = explode($glue, $string);
  90. if (isset($array[0])) {
  91. $array[0] = hide_str($array[0], $begin, $len, 1);
  92. }
  93. $string = implode($glue, $array);
  94. } elseif ($type == 3) {
  95. $array = explode($glue, $string);
  96. if (isset($array[1])) {
  97. $array[1] = hide_str($array[1], $begin, $len, 0);
  98. }
  99. $string = implode($glue, $array);
  100. } elseif ($type == 4) {
  101. $left = $begin;
  102. $right = $len;
  103. $tem = array();
  104. for ($i = 0; $i < ($length - $right); $i++) {
  105. if (isset($array[$i])) {
  106. $tem[] = $i >= $left ? "" : $array[$i];
  107. }
  108. }
  109. $tem[] = '*****';
  110. $array = array_chunk(array_reverse($array), $right);
  111. $array = array_reverse($array[0]);
  112. for ($i = 0; $i < $right; $i++) {
  113. if (isset($array[$i])) {
  114. $tem[] = $array[$i];
  115. }
  116. }
  117. $string = implode("", $tem);
  118. }
  119. return $string;
  120. }
  121. }
  122. if (!function_exists('list_sort_by')) {
  123. /**
  124. *----------------------------------------------------------
  125. * 对查询结果集进行排序
  126. *----------------------------------------------------------
  127. * @access public
  128. *----------------------------------------------------------
  129. * @param array $list 查询结果
  130. * @param string $field 排序的字段名
  131. * @param string $sortBy 排序类型
  132. * @switch string asc正向排序 desc逆向排序 nat自然排序
  133. *----------------------------------------------------------
  134. * @return array
  135. *----------------------------------------------------------
  136. */
  137. function list_sort_by(array $list, string $field, string $sortBy = 'asc'): array
  138. {
  139. if (!empty($list)) {
  140. $refer = $resultSet = array();
  141. foreach ($list as $i => $data)
  142. $refer[$i] = &$data[$field];
  143. switch ($sortBy) {
  144. case 'asc': // 正向排序
  145. asort($refer);
  146. break;
  147. case 'desc':// 逆向排序
  148. arsort($refer);
  149. break;
  150. case 'nat': // 自然排序
  151. natcasesort($refer);
  152. break;
  153. }
  154. foreach ($refer as $key => $val)
  155. $resultSet[] = &$list[$key];
  156. return $resultSet;
  157. }
  158. return [];
  159. }
  160. }
  161. if (!function_exists('supplement_id')) {
  162. /**
  163. * 用户ID风格
  164. * @param string $id
  165. * @return string
  166. */
  167. function supplement_id(string $id): string
  168. {
  169. $len = strlen($id);
  170. $buf = '000000';
  171. return $len < 6 ? substr($buf, 0, (6 - $len)) . $id : $id;
  172. }
  173. }
  174. if (!function_exists('createOrderId')) {
  175. /**
  176. * 生成订单号
  177. * @param string $letter
  178. * @param int $length
  179. * @return string
  180. */
  181. function createOrderId(string $letter = '', int $length = 3): string
  182. {
  183. $gradual = 0;
  184. $orderId = date('YmdHis') . mt_rand(10000000, 99999999);
  185. $lengths = strlen($orderId);
  186. // 循环处理随机数
  187. for ($i = 0; $i < $lengths; $i++) {
  188. $gradual += (int)(substr($orderId, $i, 1));
  189. }
  190. if (empty($letter)) {
  191. $letter = get_order_letter($length);
  192. }
  193. $code = (100 - $gradual % 100) % 100;
  194. return $letter . $orderId . str_pad((string)$code, 2, '0', STR_PAD_LEFT);
  195. }
  196. }
  197. if (!function_exists('get_order_letter')) {
  198. /**
  199. * 生成订单短ID
  200. * @param int $length
  201. * @return string
  202. */
  203. function get_order_letter(int $length = 2): string
  204. {
  205. $letter_all = range('A', 'Z');
  206. shuffle($letter_all);
  207. $letter_array = array_diff($letter_all, ['I', 'O']);
  208. $letter = array_rand(array_flip($letter_array), $length);
  209. return implode('', $letter);
  210. }
  211. }
  212. /**
  213. * 过滤emoji表情
  214. * @param string $str
  215. * @return string
  216. */
  217. if (!function_exists('removeEmoji')) {
  218. function removeEmoji(string $str = '') : string
  219. {
  220. $str = preg_replace('/[\x{1F600}-\x{1F64F}]/u', '', $str);
  221. $str = preg_replace('/[\x{1F300}-\x{1F5FF}]/u', '', $str);
  222. $str = preg_replace('/[\x{1F680}-\x{1F6FF}]/u', '', $str);
  223. $str = preg_replace('/[\x{2600}-\x{26FF}]/u', '', $str);
  224. return preg_replace('/[\x{2700}-\x{27BF}]/u', '', $str);
  225. }
  226. }
  227. if (!function_exists('object_array')) {
  228. /**
  229. * @param $array
  230. * @return array
  231. */
  232. function object_array($array): array
  233. {
  234. if(is_object($array)) {
  235. $array = (array)$array;
  236. }
  237. if(is_array($array)) {
  238. foreach($array as $key=>$value) {
  239. $array[$key] = object_array($value);
  240. }
  241. }
  242. return $array;
  243. }
  244. }
  245. if (!function_exists("getDateFull"))
  246. {
  247. /**
  248. * @param string $format
  249. * @param string $time
  250. * @return string
  251. */
  252. function getDateFull(string $format = "Y-m-d H:i:s",string $time = ""): string
  253. {
  254. if (empty($time)) $time = time();
  255. return date($format??'Y-m-d H:i:s',$time);
  256. }
  257. }
  258. if (!function_exists("events"))
  259. {
  260. /**
  261. * 事件发布
  262. * @param $name
  263. * @param $data
  264. * @return array|mixed|null
  265. */
  266. function events($name,$data): mixed
  267. {
  268. return Event::dispatch($name,$data);
  269. }
  270. }
  271. if (!function_exists("success")) {
  272. /**
  273. * @param string $msg
  274. * @param array $data
  275. * @param int $code
  276. * @return Response
  277. */
  278. function success(string $msg = "", array $data = [], int $code = 1): Response
  279. {
  280. return json(compact('code', 'data', 'msg'));
  281. }
  282. }
  283. if (!function_exists("successTrans")) {
  284. /**
  285. * 消息返回
  286. * @param string $message
  287. * @param array $data
  288. * @param int $code
  289. * @return Response
  290. */
  291. function successTrans(string $message, array $data = [], int $code = 1): Response
  292. {
  293. $msg = trans($message);
  294. return json(compact("msg", "code", "data"));
  295. }
  296. }
  297. if (!function_exists("error")) {
  298. /**
  299. * @param $msg
  300. * @param array $data
  301. * @param int $code
  302. * @return Response
  303. */
  304. function error($msg, array $data = [], int $code = 0): Response
  305. {
  306. return json(compact('code', 'data', 'msg'));
  307. }
  308. }
  309. if (!function_exists("errorTrans")) {
  310. /**
  311. * 消息返回
  312. * @param string $message
  313. * @param array $data
  314. * @param int $code
  315. * @return Response
  316. */
  317. function errorTrans(string $message = "", array $data = [], int $code = 0): Response
  318. {
  319. $msg = trans($message);
  320. return json(compact("msg", "code", "data"));
  321. }
  322. }
  323. if (!function_exists("pageFormat")) {
  324. /**
  325. * @param $data
  326. * @param int $size
  327. * @return array {page:"",pageSize:"",rows:[],total:""}
  328. */
  329. function pageFormat($data, int $size = 10): array
  330. {
  331. if (empty($data)) return [];
  332. return [
  333. 'total' => $data->total(),
  334. 'page' => $data->currentPage(),
  335. 'pageSize' => $size,
  336. 'rows' => $data->items()
  337. ];
  338. }
  339. }
  340. if (!function_exists("pageFormatMsg")) {
  341. /**
  342. * @param $data
  343. * @param int $size
  344. * @return array {page:"",pageSize:"",rows:[],total:""}
  345. */
  346. function pageFormatMsg($data, int $size = 10): array
  347. {
  348. if (empty($data)) return [];
  349. $rowsData = [];
  350. if ($data->total() > 0) {
  351. foreach ($data as $key=>$row) {
  352. $rowsData[$key] = $row->toArray();
  353. }
  354. }
  355. return [
  356. 'total' => $data->total(),
  357. 'page' => $data->currentPage(),
  358. 'pageSize' => $size,
  359. 'rows' => list_sort_by($rowsData,"id","asc")
  360. ];
  361. }
  362. }
  363. if(!function_exists('format_money')){
  364. function format_money($str,$len = '2',$append = ""): string
  365. {
  366. if (empty($str)) {
  367. return "0.00";
  368. }
  369. return number_format($str, $len, ".", $append);
  370. }
  371. }
  372. if (!function_exists('sConf')) {
  373. /**
  374. * 获取或配置系统参数
  375. * @param string $name 参数名称
  376. * @param string|null $value 参数内容
  377. */
  378. function sConf(string $name = '', string $value = null)
  379. {
  380. if (is_null($value) && is_string($name)) {
  381. return SystemService::get($name);
  382. } else {
  383. return SystemService::set($name, $value);
  384. }
  385. }
  386. }
  387. if (!function_exists('sData')) {
  388. /**
  389. * JSON 数据读取与存储
  390. * @param string $name 数据名称
  391. * @param mixed $value 数据内容
  392. */
  393. function sData(string $name,mixed $value = null)
  394. {
  395. if (is_null($value)) {
  396. return SystemService::getData($name);
  397. } else {
  398. return SystemService::setData($name, $value);
  399. }
  400. }
  401. }
  402. if (!function_exists('sOplog')) {
  403. /**
  404. * 写入系统日志
  405. * @param string $action 日志行为
  406. * @param string $content 日志内容
  407. * @param string $userName
  408. * @return boolean
  409. */
  410. function sOplog(string $action, string $content, string $userName): bool
  411. {
  412. return SystemService::setOplog($action, $content,$userName);
  413. }
  414. }
  415. if(!function_exists('store_region')){
  416. /**
  417. * 返回附近司机数据
  418. * @param $latitude
  419. * @param $longitude
  420. * @param $type 1 返回查询影响记录数;2 返回数据数组
  421. */
  422. function store_region($latitude,$longitude,$type = 1,$where = 'status = 1',$field="*"){
  423. $sql = "select ".$field." from(
  424. SELECT id,poi_id,poi_name,status,poi_address,longitude,latitude,
  425. ROUND(6378.138*2*ASIN(SQRT(POW(SIN(({$latitude}*PI()/180-latitude*PI()/180)/2),2)+COS({$latitude}*PI()/180)*COS(latitude*PI()/180)*POW(SIN(({$longitude}*PI()/180-longitude*PI()/180)/2),2)))*1000) AS juli
  426. FROM saas_store_shop where ".$where.") as tmp_table_name order by juli asc";
  427. if($type == 1){
  428. return \think\facade\Db::execute($sql);
  429. }
  430. return \think\facade\Db::query($sql);
  431. }
  432. }
  433. if(!function_exists('getHourlyTimeSlots')){
  434. function getHourlyTimeSlots($startHour = 9, $endHour = 22, $format = 'H:i'): array
  435. {
  436. $slots = [];
  437. for ($hour = $startHour; $hour < $endHour; $hour++) {
  438. $startTime = sprintf('%02d:00', $hour);
  439. $endTime = sprintf('%02d:00', $hour + 1);
  440. $slots[] = [
  441. 'start' => $startTime,
  442. 'end' => $endTime,
  443. 'display' => $startTime . ' - ' . $endTime
  444. ];
  445. }
  446. return $slots;
  447. }
  448. }
  449. if (!function_exists("strToUniqueNumberV4"))
  450. {
  451. /**
  452. * 名称加密
  453. * @param string $str
  454. * @param string $salt
  455. * @return string
  456. */
  457. function strToUniqueNumberV4(string $str = "", string $salt = ''): string
  458. {
  459. // 添加盐值增加唯一性
  460. $str = $str . $salt;
  461. // 使用多种哈希组合
  462. $crc = abs(crc32($str));
  463. $md5 = hexdec(substr(md5($str), 0, 8));
  464. $sha1 = hexdec(substr(sha1($str), 0, 8));
  465. // 组合并取模
  466. $number = ($crc + $md5 + $sha1) % 1000000000000;
  467. // 使用时间戳微调确保唯一性(针对同一字符串)
  468. static $lastStr = '';
  469. static $counter = 0;
  470. if ($str === $lastStr) {
  471. $counter++;
  472. $number = ($number + $counter) % 1000000000000;
  473. } else {
  474. $lastStr = $str;
  475. $counter = 0;
  476. }
  477. return str_pad($number, 12, '0', STR_PAD_LEFT);
  478. }
  479. }
  480. if (!function_exists("extractNodesClean"))
  481. {
  482. /**
  483. * 多维数组转N个一维
  484. * @param $data
  485. * @param array $result
  486. * @return array
  487. */
  488. function extractNodesClean($data, array &$result = []): array
  489. {
  490. foreach ($data as $node) {
  491. // 复制节点并删除 sub_tree_infos
  492. $cleanNode = $node;
  493. unset($cleanNode['sub_tree_infos']);
  494. $result[] = $cleanNode;
  495. // 递归处理子节点
  496. if (isset($node['sub_tree_infos']) && is_array($node['sub_tree_infos'])) {
  497. extractNodesClean($node['sub_tree_infos'], $result);
  498. }
  499. }
  500. return $result;
  501. }
  502. }
  503. if (!function_exists("formatTime"))
  504. {
  505. /**
  506. * 格式化时间:今天、昨天、前天、正常日期
  507. * @param string $timestamp 时间戳 或 日期字符串
  508. * @return string
  509. */
  510. function formatTime(string $timestamp): string
  511. {
  512. // 先统一转成时间戳
  513. $time = is_numeric($timestamp) ? $timestamp : strtotime($timestamp);
  514. $today = strtotime('today'); // 今天 00:00
  515. $yesterday = strtotime('yesterday'); // 昨天 00:00
  516. $beforeYesterday = strtotime('-2 days'); // 前天 00:00
  517. if ($time >= $today) {
  518. return '今天';
  519. } elseif ($time >= $yesterday) {
  520. return '昨天';
  521. } elseif ($time >= $beforeYesterday) {
  522. return '前天';
  523. } else {
  524. // 其他时间返回正常格式
  525. return date('Y-m-d', $time);
  526. }
  527. }
  528. }
  529. if(! function_exists ('timeDiff') ) {
  530. function timeDiff($begin_time, $end_time): array
  531. {
  532. if ($begin_time < $end_time) {
  533. return ["day" => 0, "hour" => 0, "min" => 0, "sec" => 0];
  534. } else {
  535. $starttime = $end_time;
  536. $endtime = $begin_time;
  537. }
  538. //计算天数
  539. $timediff = $endtime - $starttime;
  540. $days = intval($timediff / 86400);
  541. //计算小时数
  542. $remain = $timediff % 86400;
  543. $hours = intval($remain / 3600);
  544. //计算分钟数
  545. $remain = $remain % 3600;
  546. $mins = intval($remain / 60);
  547. //计算秒数
  548. $secs = $remain % 60;
  549. return ["day" => $days, "hour" => $hours, "min" => $mins, "sec" => $secs];
  550. }
  551. }