| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- <?php
- use support\Response;
- use Webman\Event\Event;
- 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("events"))
- {
- /**
- * 事件发布
- * @param $name
- * @param $data
- * @return array|mixed|null
- */
- function events($name,$data): mixed
- {
- return Event::dispatch($name,$data);
- }
- }
- 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("_json")) {
- /**
- * @param string $msg
- * @param array $data
- * @param int $code
- * @return Response
- */
- function _json(int $code = 1, string $msg = "", array $data = []): Response
- {
- return json(compact('code', 'data', 'msg'));
- }
- }
- 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()
- ];
- }
- }
- if(!function_exists('format_money')){
- function format_money($str,$len = '2',$append = ""): string
- {
- if (empty($str)) {
- return "0.00";
- }
- return number_format($str, $len, ".", $append);
- }
- }
- if (!function_exists("clearAllBlank"))
- {
- /**
- * 全局替换所有空白为空
- * @param string $str
- * @return string
- */
- function clearAllBlank(string $str): string
- {
- return preg_replace('/[\s\x{3000}]+/u', '', $str);
- }
- }
- if (!function_exists("splitTransAndNote")) {
- /**
- * 拆分译文和注释
- * @param string $text 原始混合文本
- * @return array ['trans' => 译文内容, 'note' => 注释内容]
- */
- function splitTransAndNote(string $text): array
- {
- // 正则分割:匹配【译文】【注释】边界(兼容有无中括号两种写法)
- $pattern = '/(译文|注释)/';
- $parts = preg_split($pattern, $text, -1, PREG_SPLIT_DELIM_CAPTURE);
- $trans = '';
- $note = '';
- // 遍历分割结果组装内容
- $flag = '';
- foreach ($parts as $item) {
- if ($item === '译文') {
- $flag = 'trans';
- } elseif ($item === '注释') {
- $flag = 'note';
- } else {
- $item = trim($item);
- if ($flag === 'trans' && $item !== '') {
- $trans = $item;
- } elseif ($flag === 'note' && $item !== '') {
- $note = $item;
- }
- }
- }
- // 清除多余空白、换行
- $trans = preg_replace('/\s+/', '', $trans);
- $note = preg_replace('/\s+/', PHP_EOL, $note); // 注释每条换行展示,可读性更好
- return [
- 'trans' => $trans,
- 'note' => $note
- ];
- }
- }
|