helpers.php 6.0 KB

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