functions.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. use support\Response;
  3. use Webman\Event\Event;
  4. if (!function_exists("getDateFull"))
  5. {
  6. /**
  7. * @param string $format
  8. * @param string $time
  9. * @return string
  10. */
  11. function getDateFull(string $format = "Y-m-d H:i:s",string $time = ""): string
  12. {
  13. if (empty($time)) $time = time();
  14. return date($format??'Y-m-d H:i:s',$time);
  15. }
  16. }
  17. if (!function_exists("events"))
  18. {
  19. /**
  20. * 事件发布
  21. * @param $name
  22. * @param $data
  23. * @return array|mixed|null
  24. */
  25. function events($name,$data): mixed
  26. {
  27. return Event::dispatch($name,$data);
  28. }
  29. }
  30. if (!function_exists("_json")) {
  31. /**
  32. * @param string $msg
  33. * @param array $data
  34. * @param int $code
  35. * @return Response
  36. */
  37. function _json(int $code = 1, string $msg = "", array $data = []): Response
  38. {
  39. return json(compact('code', 'data', 'msg'));
  40. }
  41. }
  42. if (!function_exists("pageFormat")) {
  43. /**
  44. * @param $data
  45. * @param int $size
  46. * @return array {page:"",pageSize:"",rows:[],total:""}
  47. */
  48. function pageFormat($data, int $size = 10): array
  49. {
  50. if (empty($data)) return [];
  51. return [
  52. 'total' => $data->total(),
  53. 'page' => $data->currentPage(),
  54. 'pageSize' => $size,
  55. 'rows' => $data->items()
  56. ];
  57. }
  58. }
  59. if(!function_exists('format_money')){
  60. function format_money($str,$len = '2',$append = ""): string
  61. {
  62. if (empty($str)) {
  63. return "0.00";
  64. }
  65. return number_format($str, $len, ".", $append);
  66. }
  67. }