ufutx.dma/app/Helper/functions.php
2026-03-04 14:42:40 +08:00

451 lines
12 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
use App\Facades\UploadService;
use App\Models\NewFatLog;
use App\Models\User;
use Illuminate\Support\Facades\Redis;
use SimpleSoftwareIO\QrCode\Facades\QrCode;
/**
* 获取ip
*/
if(!function_exists('get_real_ip')){
function get_real_ip()
{
$ip=false;
if(!empty($_SERVER["HTTP_CLIENT_IP"])){
$ip = $_SERVER["HTTP_CLIENT_IP"];
}
if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
}
return($ip ? $ip : $_SERVER['REMOTE_ADDR']);
}
}
/**
* http get请求
*/
if(!function_exists('http_get_request')){
//HTTP请求支持HTTP/HTTPS支持GET/POST
function http_get_request($url, $data = null,$headers=[])
{
try{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
if (!empty($data)) {
curl_setopt($curl, CURLOPT_POST, TRUE);
curl_setopt($curl, CURLOPT_POSTFIELDS,$data);
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
// 设置自定义Header参数
if (!empty($headers)) {
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
}
$output = curl_exec($curl);
curl_close($curl);
return json_decode($output,true);
}catch(\Exception $e){
// HandleLogs::error('HTTP请求 异常','微信内容安全',$e);
return '';
}
}
}
/**
* 获取日期和第二天时间间隔
*/
if(!function_exists('get_day_time')){
function get_day_time($date){
if(empty($date)){
$date = date('Y-m-d');
}
// 获取今天零点的时间戳
$today_start = strtotime($date);
// 获取明天零点的时间戳
$tomorrow_start = $today_start + 86400;
// 格式化时间戳为日期时间字符串
$start_time = date('Y-m-d H:i:s', $today_start);
$end_time = date('Y-m-d H:i:s', $tomorrow_start);
return [$start_time,$end_time];
}
}
/**
* 获取用户单位
*/
if(!function_exists('get_user_unit')){
function get_user_unit($language,$unit){
if(in_array($language,[User::LANGUAGE_CH,User::LANGUAGE_TW]) && $unit == 'lb'){
$unit = '斤';
}
if($language == User::LANGUAGE_EN && $unit == '斤'){
$unit = 'lb';
}
return $unit;
}
}
if(!function_exists('change_unit')){
function change_unit($language,$user_unit){
if(in_array($language,[User::LANGUAGE_CH,User::LANGUAGE_TW]) && $user_unit == 'lb'){
$user_unit = '斤';
}
if($language == User::LANGUAGE_EN){
$user_unit = 'lb';
}
return $user_unit;
}
}
/**
* 随机生成16位数退款订单号
* @return string 退款订单号
*/
if(!function_exists('get_refund_trade_no')) {
function get_refund_trade_no()
{
$dateline = time();
$mix_1 = rand(100, 999);
$mix_2 = rand(10, 99);
return 'r' . $dateline . $mix_1 . $mix_2;
}
}
/**
* 直接获取小数位
*/
if(!function_exists('get_two_float')) {
function get_two_float($number,$precision)
{
$number = (float)$number;
return (round($number * pow(10, $precision))) / pow(10, $precision);
}
}
/**
* 拼接get请求参数
*/
if(!function_exists('get_request_url_param')) {
function get_request_url_param($url, $params)
{
// 拼接参数到 URL
$query = http_build_query($params);
$url = $url . '?' . $query;
return $url;
}
}
/**
* 处理电话号码中间四位
*/
if(!function_exists('masked_phone')) {
function masked_phone($phone)
{
return substr($phone, 0, 3) . str_repeat("*", 4) . substr($phone, 7);
}
}
/**
* 生成网易云信ac cid
*/
if(!function_exists('make_wangyiyun_accid')) {
function make_wangyiyun_accid($id)
{
return config('app.env')."_".$id;
}
}
/**
* 网易云信accid 返回用户id
*/
if (!function_exists('get_userid_by_accid')){
function get_userid_by_accid($accid)
{
return str_replace(config('app.env')."_","",$accid);
}
}
/**
* 手机号码验证
*/
if (!function_exists('match_mobile')){
function match_phone($mobile)
{
return preg_match("/^(\+?0?86-?)?[1-9]\d{10}$/", $mobile);
}
}
/**
* 获取阿里云配置
*/
if (!function_exists('gmt_iso8601')){
function gmt_iso8601($time)
{
$dt_str = date("c", $time);
$date_time = new \DateTime($dt_str);
$expiration = $date_time->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;
}
}