functions.php 17 KB

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