helpers.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. if (!function_exists('get_cache_key')) {
  3. function get_cache_key(string $id): string
  4. {
  5. return 'user_' . $id . '_captcha_code';
  6. }
  7. }
  8. if (!function_exists('get_step_key')) {
  9. function get_step_key($chatId)
  10. {
  11. return "{$chatId}_status";
  12. }
  13. }
  14. if (!function_exists('is_valid_date')) {
  15. function is_valid_date($date)
  16. {
  17. // 使用正则表达式匹配 yyyy-mm-dd 格式的日期
  18. return preg_match('/^\d{4}-\d{2}-\d{2}$/', $date) === 1;
  19. }
  20. }
  21. if (!function_exists('getUuid')) {
  22. function getUuid()
  23. {
  24. // Generate 16 bytes (128 bits) of random data or use the data passed into the function.
  25. $data = random_bytes(16);
  26. // Set version to 0100
  27. $data[6] = chr(ord($data[6]) & 0x0f | 0x40);
  28. // Set bits 6-7 to 10
  29. $data[8] = chr(ord($data[8]) & 0x3f | 0x80);
  30. // Output the 36 character UUID.
  31. return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
  32. }
  33. }
  34. if (!function_exists('calculate_age')) {
  35. function calculate_age($birthDate)
  36. {
  37. $currentDate = new DateTime();
  38. $birthDate = new DateTime($birthDate);
  39. $age = $currentDate->diff($birthDate);
  40. return $age->y;
  41. }
  42. }
  43. if (!function_exists('get_string_translate')) {
  44. function get_string_translate(string $key): string
  45. {
  46. $value = __("messages.translate.{$key}");
  47. if ($value == "messages.translate.{$key}") return $key;
  48. return $value;
  49. }
  50. }
  51. if (!function_exists('generate_random_string')) {
  52. function generate_random_string(int $length = 6): string
  53. {
  54. $characters = '0123456789abcdefghijklmnopqrstuvwxyz';
  55. $randomString = '';
  56. for ($i = 0; $i < $length; $i++) {
  57. $randomString .= $characters[rand(0, strlen($characters) - 1)];
  58. }
  59. return $randomString;
  60. }
  61. }
  62. if (!function_exists('get_tags')) {
  63. function get_tags(int $length = 3): array
  64. {
  65. $array = ['Reading', 'Traveling', 'Photography', 'Music', 'Painting', 'Yoga', 'Cooking', 'Calligraphy', 'Crafts',
  66. 'History', 'Drinking', 'Coding', 'Valuing family', 'Skydiving', 'Diving'];
  67. $length = $length < 1 ? 1 : ($length > count($array) ? count($array) : $length);
  68. $random_keys = array_rand($array, $length);
  69. $random_elements = [];
  70. foreach ($random_keys as $key) {
  71. $random_elements[] = $array[$key];
  72. }
  73. return $random_elements;
  74. }
  75. }
  76. /**
  77. * 辅助函数:十进制转十六进制(支持大数)
  78. */
  79. function bcdechex($dec)
  80. {
  81. $hex = '';
  82. while (bccomp($dec, 0) > 0) {
  83. $last = bcmod($dec, 16);
  84. $hex = dechex($last) . $hex;
  85. $dec = bcdiv($dec, 16, 0);
  86. }
  87. return $hex ?: '0';
  88. }
  89. if (!function_exists('is_multiple_of_eight')) {
  90. function is_multiple_of_eight(int $number): bool
  91. {
  92. if ($number % 8 == 0) {
  93. return true; // 是 8 的倍数
  94. } else {
  95. return false; // 不是 8 的倍数
  96. }
  97. }
  98. }
  99. if (!function_exists('get_invitation_code')) {
  100. function get_invitation_code(): string
  101. {
  102. do {
  103. $invitation_code = generate_random_string();
  104. } while (\App\Models\User::where('invitation_code', $invitation_code)->first() != null);
  105. return $invitation_code;
  106. }
  107. }
  108. if (!function_exists('generate_order_number')) {
  109. function generate_order_number()
  110. {
  111. $dateTime = date('YmdHis');
  112. $randomNumber = mt_rand(1000, 9999);
  113. $orderNumber = $dateTime . $randomNumber;
  114. return $orderNumber;
  115. }
  116. }
  117. function removeZero($str)
  118. {
  119. if (empty($str)) {
  120. return 0;
  121. }
  122. if (!is_numeric($str)) {
  123. return $str;
  124. }
  125. $number = number_format($str, 10, '.', '');
  126. // 使用 rtrim 移除末尾的零和小数点
  127. return rtrim(rtrim($number, '0'), '.');
  128. }
  129. /**
  130. * 唯一订单号
  131. * @return string
  132. */
  133. function createOrderNo(): string
  134. {
  135. $str = microtime(true);
  136. $arr = explode('.', $str);
  137. $decimal = $arr[1];
  138. return date('YmdHis') . $decimal;
  139. }
  140. /**
  141. * 获取精度
  142. * @param $number
  143. * @return int
  144. */
  145. function getScale($number): int
  146. {
  147. if (!is_numeric($number)) {
  148. return 1;
  149. }
  150. $sub = strrchr($number, ".");
  151. if (empty($sub)) {
  152. return 1;
  153. }
  154. $scale = strlen(substr($sub, 1));
  155. if ($scale == 0) {
  156. return 1;
  157. }
  158. return $scale;
  159. }