diff($birthDate); return $age->y; } } if (!function_exists('get_string_translate')) { function get_string_translate(string $key): string { $value = __("messages.translate.{$key}"); if ($value == "messages.translate.{$key}") return $key; return $value; } } if (!function_exists('generate_random_string')) { function generate_random_string(int $length = 6): string { $characters = '0123456789abcdefghijklmnopqrstuvwxyz'; $randomString = ''; for ($i = 0; $i < $length; $i++) { $randomString .= $characters[rand(0, strlen($characters) - 1)]; } return $randomString; } } if (!function_exists('get_tags')) { function get_tags(int $length = 3): array { $array = ['Reading', 'Traveling', 'Photography', 'Music', 'Painting', 'Yoga', 'Cooking', 'Calligraphy', 'Crafts', 'History', 'Drinking', 'Coding', 'Valuing family', 'Skydiving', 'Diving']; $length = $length < 1 ? 1 : ($length > count($array) ? count($array) : $length); $random_keys = array_rand($array, $length); $random_elements = []; foreach ($random_keys as $key) { $random_elements[] = $array[$key]; } return $random_elements; } } /** * 辅助函数:十进制转十六进制(支持大数) */ function bcdechex($dec) { $hex = ''; while (bccomp($dec, 0) > 0) { $last = bcmod($dec, 16); $hex = dechex($last) . $hex; $dec = bcdiv($dec, 16, 0); } return $hex ?: '0'; } if (!function_exists('is_multiple_of_eight')) { function is_multiple_of_eight(int $number): bool { if ($number % 8 == 0) { return true; // 是 8 的倍数 } else { return false; // 不是 8 的倍数 } } } if (!function_exists('get_invitation_code')) { function get_invitation_code(): string { do { $invitation_code = generate_random_string(); } while (\App\Models\User::where('invitation_code', $invitation_code)->first() != null); return $invitation_code; } } if (!function_exists('generate_order_number')) { function generate_order_number() { $dateTime = date('YmdHis'); $randomNumber = mt_rand(1000, 9999); $orderNumber = $dateTime . $randomNumber; return $orderNumber; } } function removeZero($str) { if (empty($str)) { return 0; } if (!is_numeric($str)) { return $str; } $number = number_format($str, 10, '.', ''); // 使用 rtrim 移除末尾的零和小数点 return rtrim(rtrim($number, '0'), '.'); } /** * 唯一订单号 * @return string */ function createOrderNo(): string { $str = microtime(true); $arr = explode('.', $str); $decimal = $arr[1]; return date('YmdHis') . $decimal; } /** * 获取精度 * @param $number * @return int */ function getScale($number): int { if (!is_numeric($number)) { return 1; } $sub = strrchr($number, "."); if (empty($sub)) { return 1; } $scale = strlen(substr($sub, 1)); if ($scale == 0) { return 1; } return $scale; }