functions.php 14 KB

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