helpers.php 4.5 KB

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