helpers.php 6.1 KB

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