functions.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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("success")) {
  31. /**
  32. * @param string $msg
  33. * @param array $data
  34. * @param int $code
  35. * @return Response
  36. */
  37. function success(string $msg = "", array $data = [], int $code = 1): Response
  38. {
  39. return json(compact('code', 'data', 'msg'));
  40. }
  41. }
  42. if (!function_exists("successTrans")) {
  43. /**
  44. * 消息返回
  45. * @param string $message
  46. * @param array $data
  47. * @param int $code
  48. * @return Response
  49. */
  50. function successTrans(string $message, array $data = [], int $code = 1): Response
  51. {
  52. $msg = trans($message);
  53. return json(compact("msg", "code", "data"));
  54. }
  55. }
  56. if (!function_exists("error")) {
  57. /**
  58. * @param $msg
  59. * @param array $data
  60. * @param int $code
  61. * @return Response
  62. */
  63. function error($msg, array $data = [], int $code = 0): Response
  64. {
  65. return json(compact('code', 'data', 'msg'));
  66. }
  67. }
  68. if (!function_exists("errorTrans")) {
  69. /**
  70. * 消息返回
  71. * @param string $message
  72. * @param array $data
  73. * @param int $code
  74. * @return Response
  75. */
  76. function errorTrans(string $message = "", array $data = [], int $code = 0): Response
  77. {
  78. $msg = trans($message);
  79. return json(compact("msg", "code", "data"));
  80. }
  81. }
  82. if (!function_exists("_json")) {
  83. /**
  84. * @param string $msg
  85. * @param array $data
  86. * @param int $code
  87. * @return Response
  88. */
  89. function _json(int $code = 1, string $msg = "", array $data = []): Response
  90. {
  91. return json(compact('code', 'data', 'msg'));
  92. }
  93. }
  94. if (!function_exists("pageFormat")) {
  95. /**
  96. * @param $data
  97. * @param int $size
  98. * @return array {page:"",pageSize:"",rows:[],total:""}
  99. */
  100. function pageFormat($data, int $size = 10): array
  101. {
  102. if (empty($data)) return [];
  103. return [
  104. 'total' => $data->total(),
  105. 'page' => $data->currentPage(),
  106. 'pageSize' => $size,
  107. 'rows' => $data->items()
  108. ];
  109. }
  110. }
  111. if(!function_exists('format_money')){
  112. function format_money($str,$len = '2',$append = ""): string
  113. {
  114. if (empty($str)) {
  115. return "0.00";
  116. }
  117. return number_format($str, $len, ".", $append);
  118. }
  119. }
  120. if (!function_exists("clearAllBlank"))
  121. {
  122. /**
  123. * 全局替换所有空白为空
  124. * @param string $str
  125. * @return string
  126. */
  127. function clearAllBlank(string $str): string
  128. {
  129. return preg_replace('/[\s\x{3000}]+/u', '', $str);
  130. }
  131. }
  132. if (!function_exists("splitTransAndNote")) {
  133. /**
  134. * 拆分译文和注释
  135. * @param string $text 原始混合文本
  136. * @return array ['trans' => 译文内容, 'note' => 注释内容]
  137. */
  138. function splitTransAndNote(string $text): array
  139. {
  140. // 正则分割:匹配【译文】【注释】边界(兼容有无中括号两种写法)
  141. $pattern = '/(译文|注释)/';
  142. $parts = preg_split($pattern, $text, -1, PREG_SPLIT_DELIM_CAPTURE);
  143. $trans = '';
  144. $note = '';
  145. // 遍历分割结果组装内容
  146. $flag = '';
  147. foreach ($parts as $item) {
  148. if ($item === '译文') {
  149. $flag = 'trans';
  150. } elseif ($item === '注释') {
  151. $flag = 'note';
  152. } else {
  153. $item = trim($item);
  154. if ($flag === 'trans' && $item !== '') {
  155. $trans = $item;
  156. } elseif ($flag === 'note' && $item !== '') {
  157. $note = $item;
  158. }
  159. }
  160. }
  161. // 清除多余空白、换行
  162. $trans = preg_replace('/\s+/', '', $trans);
  163. $note = preg_replace('/\s+/', PHP_EOL, $note); // 注释每条换行展示,可读性更好
  164. return [
  165. 'trans' => $trans,
  166. 'note' => $note
  167. ];
  168. }
  169. }