helpers.php 5.6 KB

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