format(\DateTime::ISO8601); $pos = strpos($expiration, '+'); $expiration = substr($expiration, 0, $pos); return $expiration . "Z"; } } /** * 处理单位 */ if (!function_exists('change_user_unit')){ function change_user_unit($log,$unit,$language,$fat_device=User::FAT_DEVICE_YUNKANGBAO) { $res_fat_data = $log['fat_data']; $res_unit = $log['unit']; if ($log['unit'] == 'kg' && $unit == '斤' && in_array($language,[User::LANGUAGE_CH,User::LANGUAGE_TW])) { $res_fat_data = get_two_float($log['fat_data'] * NewFatLog::WEIGHT_JIN_KG_RATE, 2); $res_unit = '斤'; } if ($log['unit'] == 'kg' && $unit == '斤' && $language == User::LANGUAGE_EN) { // $res_fat_data = get_two_float($log['fat_data'] * NewFatLog::WEIGHT_RATE, 2); $res_fat_data = get_two_float(get_unit_rate($log['fat_data'],$fat_device), 1); $res_unit = 'lb'; } if($log['unit'] == 'kg' && $unit == 'lb' && $language == User::LANGUAGE_EN){ // $res_fat_data = get_two_float($log['fat_data'] * NewFatLog::WEIGHT_RATE, 2); $res_fat_data = get_two_float(get_unit_rate($log['fat_data'],$fat_device), 1); $res_unit = 'lb'; } if($log['unit'] == 'kg' && $unit == 'lb' && in_array($language,[User::LANGUAGE_CH,User::LANGUAGE_TW])){ $res_fat_data = get_two_float($log['fat_data'] * NewFatLog::WEIGHT_JIN_KG_RATE, 2); $res_unit = '斤'; } if($log['unit'] == 'lb' && $unit == 'kg' ){ // $res_fat_data = get_two_float($log['fat_data'] / NewFatLog::WEIGHT_RATE, 2); $res_fat_data = get_two_float(($log['fat_data'] / get_unit_rate($log['fat_data'],$fat_device)), 2); $res_unit = 'kg'; } if($log['unit'] == 'lb' && $unit == '斤' ){ // $res_fat_data = get_two_float($log['fat_data'] / NewFatLog::WEIGHT_JIN_LB_RATE, 2); $res_fat_data = get_two_float(($log['fat_data'] / (get_unit_rate($log['fat_data'],$fat_device)))/2, 2); $res_unit = '斤'; } $log['fat_data'] = $res_fat_data; $log['unit'] = $res_unit; return $log; } } if (!function_exists('get_unit_rate')){ function get_unit_rate($weight,$fat_device=User::FAT_DEVICE_YUNKANGBAO) { $rate = 1; if(empty($weight)){ return $rate; } $rate = (((($weight * 100 * 11023 + 50000) / 100000) << 1) / 10); if($fat_device == 0){ $rate = $weight*NewFatLog::WEIGHT_RATE; } return $rate; } } /** * 处理boundaries */ if (!function_exists('change_boundaries')){ function change_boundaries($boundaries,$unit,$language) { $rate = 1; if($unit == 'kg'){ return $boundaries; } if ($unit == '斤' && $language == 0) { $rate = NewFatLog::WEIGHT_JIN_KG_RATE; } if ($unit == '斤' && $language == 1) { $rate = NewFatLog::WEIGHT_RATE; } if($unit == 'lb' && $language == 1){ $rate = NewFatLog::WEIGHT_RATE; } if($unit == 'lb' && $language == 0){ $rate = NewFatLog::WEIGHT_JIN_KG_RATE; } foreach ($boundaries as $key =>$value){ $boundaries[$key] = get_two_float($value * $rate, 2); } return $boundaries; } } /** * 服务层返回错误 */ if (!function_exists('service_fail')){ function service_fail($msg) { return ['status'=>false,'msg'=>$msg]; } } /** * 服务层返回数据 */ if (!function_exists('service_success')){ function service_success($data=[]) { return ['status'=>true,'data'=>$data]; } } /** * 获取异常信息数据统一处理 */ if (!function_exists('get_error_msg')){ function get_error_msg($e) { $file = "file:".$e->getFile()."\r\n"; $line = "line:".$e->getLine()."\r\n"; $message = "message:".$e->getMessage()."\r\n"; $msg = $file.$line.$message; return $msg; } } //随机返回数组里面的n条数据 if (!function_exists('get_random_elements')) { function get_random_elements(array $array, int $count): array { $keys = array_rand($array, min($count, count($array))); if (!is_array($keys)) { $keys = [$keys]; } return array_intersect_key($array, array_flip($keys)); } } /** * 获取步幅 * 步幅(m)=(身高(cm)*0.413)/100 */ if (!function_exists('get_stride_by_height')) { function get_stride_by_height($height) { if (!$height) { return 0; } return number_format($height * 0.413 / 100, 2); } } /** * 获取距离 * 距离(km)=步数*步幅(meters)/1000 */ if (!function_exists('get_km_by_step')) { function get_km_by_step($step, $height) { $stride = get_stride_by_height($height); return number_format($step * $stride / 1000, 2); } } /** * 获取卡路里 * 卡路里=步数*步幅(米)*体重(千克)/步数每千米(1240) */ if (!function_exists('get_cal_by_step')) { function get_cal_by_step($step, $height, $weight) { $stride = get_stride_by_height($height); return number_format($step * $stride * $weight / 1240, 2); } } /** * 获取二维码 */ if (!function_exists('get_qrcode')) { function get_qrcode($key_name,$url,$id) { $redis = Redis::client(); $qrcode = $redis->get($key_name.'_'.$id); $pic = ''; if($qrcode){ $pic = $qrcode; }else{ $path = storage_path('qrcode/' . $id . '_activity.svg'); QrCode::generate($url, $path); if (file_exists($path)) { $pic = UploadService::uploadFile($path); $redis->set($key_name.'_'.$id,$pic); @unlink($path); } } return $pic; } } /** * 生成友福id */ if (!function_exists('make_user_id')) { function make_user_id($uid){ return 5233000000 + $uid; } } if (!function_exists('downloadVideoToLocal')) { function downloadVideoToLocal($videoUrl, $localFilePath) { $videoContent = file_get_contents($videoUrl); file_put_contents($localFilePath, $videoContent); } } if (!function_exists('getVideoDuration')) { function getVideoDuration($filePath) { $command = "ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 $filePath"; $duration = shell_exec($command); return intval(round(floatval($duration))); } } /** * 获取当天还剩多少s */ if (!function_exists('getDayExpire')) { function getDayExpire() { // 获取当前时间戳 $currentTimestamp = time(); // 获取当天结束的时间戳 $endOfDayTimestamp = strtotime('tomorrow') - 1; // 计算距离当天结束还有多少秒过去 $secondsUntilEndOfDay = $endOfDayTimestamp - $currentTimestamp; return $secondsUntilEndOfDay; } } /** * 加密 */ if (!function_exists("aesEncryptCBC")) { function aesEncryptCBC($data){ $key = env("ENCRYPT_KEY"); $iv = env("ENCRYPT_IV"); $encrypted = openssl_encrypt($data, "AES-128-CBC", $key, OPENSSL_PKCS1_PADDING, $iv); //增加前缀 ufutx_ $prefix = "Ec!AEC$:"; return $prefix . base64_encode($encrypted); } } /** * 解密 */ if (!function_exists("aesDecryptCBC")) { function aesDecryptCBC($data){ $prefix = "Ec!AEC$:"; $substring = substr($data, 0, 8); if ($substring == $prefix) { $data = substr($data, 8, -1); } $key = env("ENCRYPT_KEY"); $iv = env("ENCRYPT_IV"); $decrypted = openssl_decrypt(base64_decode($data), "AES-128-CBC", $key, OPENSSL_PKCS1_PADDING, $iv); return $decrypted; } }