123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- <?php
- if (!function_exists('get_cache_key')) {
- function get_cache_key(string $id): string
- {
- return 'user_' . $id . '_captcha_code';
- }
- }
- if (!function_exists('get_step_key')) {
- function get_step_key($chatId)
- {
- return "{$chatId}_status";
- }
- }
- if (!function_exists('is_valid_date')) {
- function is_valid_date($date)
- {
- // 使用正则表达式匹配 yyyy-mm-dd 格式的日期
- return preg_match('/^\d{4}-\d{2}-\d{2}$/', $date) === 1;
- }
- }
- if (!function_exists('getUuid')) {
- function getUuid()
- {
- // Generate 16 bytes (128 bits) of random data or use the data passed into the function.
- $data = random_bytes(16);
- // Set version to 0100
- $data[6] = chr(ord($data[6]) & 0x0f | 0x40);
- // Set bits 6-7 to 10
- $data[8] = chr(ord($data[8]) & 0x3f | 0x80);
- // Output the 36 character UUID.
- return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
- }
- }
- if (!function_exists('calculate_age')) {
- function calculate_age($birthDate)
- {
- $currentDate = new DateTime();
- $birthDate = new DateTime($birthDate);
- $age = $currentDate->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;
- }
|