common.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2024 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. use app\store\service\Auth;
  13. /**
  14. * 应用公共函数库文件
  15. */
  16. /**
  17. * 验证指定url是否有访问权限
  18. * @param string|array $url
  19. * @param bool $strict 严格模式
  20. * @return bool
  21. */
  22. function checkPrivilege($url, bool $strict = true): bool
  23. {
  24. try {
  25. return Auth::getInstance()->checkPrivilege($url, $strict);
  26. } catch (\Exception $e) {
  27. return false;
  28. }
  29. }
  30. /**
  31. * 日期转换时间戳
  32. * 例如: 2020-04-01 08:15:08 => 1585670400
  33. * @param string $date
  34. * @param bool $isWithTime 是否包含时间
  35. * @return false|int
  36. */
  37. function str2date(string $date, bool $isWithTime = false)
  38. {
  39. if (!$isWithTime) {
  40. $date = date('Y-m-d', strtotime($date));
  41. }
  42. return strtotime($date);
  43. }
  44. /**
  45. * 格式化起止时间(为了兼容前端RangePicker组件)
  46. * 2020-04-01T08:15:08.891Z => 1585670400
  47. * @param array $times
  48. * @param bool $isWithTime 是否包含时间
  49. * @return array
  50. */
  51. function between_time(array $times, bool $isWithTime = false): array
  52. {
  53. foreach ($times as &$time) {
  54. $time = trim($time, '&quot;');
  55. $time = str2date($time, $isWithTime);
  56. }
  57. return ['start_time' => current($times), 'end_time' => next($times)];
  58. }