helpers.php 4.6 KB

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