= 1024 && $i < 6; $i++) { $size /= 1024; } return round($size, 2) . $delimiter . $units[$i]; } } if (!function_exists('datetime')) { /** * 将时间戳转换为日期时间 * @param int $time 时间戳 * @param string $format 日期时间格式 * @return string */ function datetime($time, $format = 'Y-m-d H:i:s') { $time = is_numeric($time) ? $time : strtotime($time); return date($format, $time); } } if (!function_exists('human_date')) { /** * 获取语义化时间 * @param int $time 时间 * @param int $local 本地时间 * @return string */ function human_date($time, $local = null) { return \fast\Date::human($time, $local); } } if (!function_exists('cdnurl')) { /** * 获取上传资源的CDN的地址 * @param string $url 资源相对地址 * @param boolean $domain 是否显示域名 或者直接传入域名 * @return string */ function cdnurl($url, $domain = false) { $regex = "/^((?:[a-z]+:)?\/\/|data:image\/)(.*)/i"; $url = preg_match($regex, $url) ? $url : \think\Config::get('upload.cdnurl') . $url; if ($domain && !preg_match($regex, $url)) { $domain = is_bool($domain) ? request()->domain() : $domain; $url = $domain . $url; } return $url; } } if (!function_exists('is_really_writable')) { /** * 判断文件或文件夹是否可写 * @param string $file 文件或目录 * @return bool */ function is_really_writable($file) { if (DIRECTORY_SEPARATOR === '/') { return is_writable($file); } if (is_dir($file)) { $file = rtrim($file, '/') . '/' . md5(mt_rand()); if (($fp = @fopen($file, 'ab')) === false) { return false; } fclose($fp); @chmod($file, 0777); @unlink($file); return true; } elseif (!is_file($file) or ($fp = @fopen($file, 'ab')) === false) { return false; } fclose($fp); return true; } } if (!function_exists('rmdirs')) { /** * 删除文件夹 * @param string $dirname 目录 * @param bool $withself 是否删除自身 * @return boolean */ function rmdirs($dirname, $withself = true) { if (!is_dir($dirname)) { return false; } $files = new RecursiveIteratorIterator( new RecursiveDirectoryIterator($dirname, RecursiveDirectoryIterator::SKIP_DOTS), RecursiveIteratorIterator::CHILD_FIRST ); foreach ($files as $fileinfo) { $todo = ($fileinfo->isDir() ? 'rmdir' : 'unlink'); $todo($fileinfo->getRealPath()); } if ($withself) { @rmdir($dirname); } return true; } } if (!function_exists('copydirs')) { /** * 复制文件夹 * @param string $source 源文件夹 * @param string $dest 目标文件夹 */ function copydirs($source, $dest) { if (!is_dir($dest)) { mkdir($dest, 0755, true); } foreach ( $iterator = new RecursiveIteratorIterator( new RecursiveDirectoryIterator($source, RecursiveDirectoryIterator::SKIP_DOTS), RecursiveIteratorIterator::SELF_FIRST ) as $item ) { if ($item->isDir()) { $sontDir = $dest . DS . $iterator->getSubPathName(); if (!is_dir($sontDir)) { mkdir($sontDir, 0755, true); } } else { copy($item, $dest . DS . $iterator->getSubPathName()); } } } } if (!function_exists('mb_ucfirst')) { function mb_ucfirst($string) { return mb_strtoupper(mb_substr($string, 0, 1)) . mb_strtolower(mb_substr($string, 1)); } } if (!function_exists('addtion')) { /** * 附加关联字段数据 * @param array $items 数据列表 * @param mixed $fields 渲染的来源字段 * @return array */ function addtion($items, $fields) { if (!$items || !$fields) { return $items; } $fieldsArr = []; if (!is_array($fields)) { $arr = explode(',', $fields); foreach ($arr as $k => $v) { $fieldsArr[$v] = ['field' => $v]; } } else { foreach ($fields as $k => $v) { if (is_array($v)) { $v['field'] = isset($v['field']) ? $v['field'] : $k; } else { $v = ['field' => $v]; } $fieldsArr[$v['field']] = $v; } } foreach ($fieldsArr as $k => &$v) { $v = is_array($v) ? $v : ['field' => $v]; $v['display'] = isset($v['display']) ? $v['display'] : str_replace(['_ids', '_id'], ['_names', '_name'], $v['field']); $v['primary'] = isset($v['primary']) ? $v['primary'] : ''; $v['column'] = isset($v['column']) ? $v['column'] : 'name'; $v['model'] = isset($v['model']) ? $v['model'] : ''; $v['table'] = isset($v['table']) ? $v['table'] : ''; $v['name'] = isset($v['name']) ? $v['name'] : str_replace(['_ids', '_id'], '', $v['field']); } unset($v); $ids = []; $fields = array_keys($fieldsArr); foreach ($items as $k => $v) { foreach ($fields as $m => $n) { if (isset($v[$n])) { $ids[$n] = array_merge(isset($ids[$n]) && is_array($ids[$n]) ? $ids[$n] : [], explode(',', $v[$n])); } } } $result = []; foreach ($fieldsArr as $k => $v) { if ($v['model']) { $model = new $v['model']; } else { $model = $v['name'] ? \think\Db::name($v['name']) : \think\Db::table($v['table']); } $primary = $v['primary'] ? $v['primary'] : $model->getPk(); $result[$v['field']] = $model->where($primary, 'in', $ids[$v['field']])->column("{$primary},{$v['column']}"); } foreach ($items as $k => &$v) { foreach ($fields as $m => $n) { if (isset($v[$n])) { $curr = array_flip(explode(',', $v[$n])); $v[$fieldsArr[$n]['display']] = implode(',', array_intersect_key($result[$n], $curr)); } } } return $items; } } if (!function_exists('var_export_short')) { /** * 返回打印数组结构 * @param string $var 数组 * @return string */ function var_export_short($var) { return VarExporter::export($var); } } if (!function_exists('letter_avatar')) { /** * 首字母头像 * @param $text * @return string */ function letter_avatar($text) { $total = unpack('L', hash('adler32', $text, true))[1]; $hue = $total % 360; list($r, $g, $b) = hsv2rgb($hue / 360, 0.3, 0.9); $bg = "rgb({$r},{$g},{$b})"; $color = "#ffffff"; $first = mb_strtoupper(mb_substr($text, 0, 1)); $src = base64_encode('' . $first . ''); $value = 'data:image/svg+xml;base64,' . $src; return $value; } } if (!function_exists('hsv2rgb')) { function hsv2rgb($h, $s, $v) { $r = $g = $b = 0; $i = floor($h * 6); $f = $h * 6 - $i; $p = $v * (1 - $s); $q = $v * (1 - $f * $s); $t = $v * (1 - (1 - $f) * $s); switch ($i % 6) { case 0: $r = $v; $g = $t; $b = $p; break; case 1: $r = $q; $g = $v; $b = $p; break; case 2: $r = $p; $g = $v; $b = $t; break; case 3: $r = $p; $g = $q; $b = $v; break; case 4: $r = $t; $g = $p; $b = $v; break; case 5: $r = $v; $g = $p; $b = $q; break; } return [ floor($r * 255), floor($g * 255), floor($b * 255) ]; } } if (!function_exists('check_nav_active')) { /** * 检测会员中心导航是否高亮 */ function check_nav_active($url, $classname = 'active') { $auth = \app\common\library\Auth::instance(); $requestUrl = $auth->getRequestUri(); $url = ltrim($url, '/'); return $requestUrl === str_replace(".", "/", $url) ? $classname : ''; } } if (!function_exists('check_cors_request')) { /** * 跨域检测 */ function check_cors_request() { if (isset($_SERVER['HTTP_ORIGIN']) && $_SERVER['HTTP_ORIGIN']) { $info = parse_url($_SERVER['HTTP_ORIGIN']); $domainArr = array_filter(array_map('trim', explode(',', config('fastadmin.cors_request_domain')))); $domainArr[] = request()->host(true); if (in_array("*", $domainArr) || in_array($_SERVER['HTTP_ORIGIN'], $domainArr) || (isset($info['host']) && in_array($info['host'], $domainArr))) { header('Vary: Origin'); header("Access-Control-Allow-Origin: " . $_SERVER['HTTP_ORIGIN']); } else { header('HTTP/1.1 403 Forbidden'); exit; } header('Access-Control-Allow-Credentials: true'); header('Access-Control-Max-Age: 86400'); if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') { if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD'])) { header("Access-Control-Allow-Methods: GET, POST, PUT, PATCH, DELETE, OPTIONS"); } if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS'])) { header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}"); } exit; } } } } if (!function_exists('xss_clean')) { /** * 清理XSS */ function xss_clean($content, $is_image = false) { return \app\common\library\Security::instance()->xss_clean($content, $is_image); } } /** * * @param type $url * @param type $type * @param type $arr * @return type */ function http_curl($url, $type = 'get', $arr = '') { if($arr){ $o = ""; foreach ( $arr as $k => $v ) { $o.= "$k=" . urlencode( $v ). "&" ; } $arr = substr($o,0,-1); } $ch = curl_init(); $user_agent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.146 Safari/537.36"; curl_setopt($ch, CURLOPT_USERAGENT,$user_agent); curl_setopt($ch, CURLOPT_URL, $url); //设置访问的地址 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //获取的信息返回 // curl_setopt($ch, CURLOPT_PROXY, "hk2.cable-modem.org"); //代理服务器地址 // curl_setopt($ch, CURLOPT_PROXYPORT,"46543"); //代理服务器端口 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_TIMEOUT, 20000); if ($type == 'post') { curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $arr); } $output = curl_exec($ch); if (curl_error($ch)) { return curl_error($ch); } return $output; } if (!function_exists('getRedis')) { function getRedis($config = []) { $redis = new redis(); // $redis->connect($config['host'], $config['port']); $redis->connect("127.0.0.1", "6379"); return $redis; } } if (!function_exists('pushRedis')) { //防重复点击逻辑,开启redis数据 毫秒级防重复 function pushRedis($symbol ,$time = 3000) { $redis = getRedis(\think\Config::get("redis")); $flags = $redis->lLen($symbol); if ($flags < 1) { $redis->rpush($symbol, getMillisecond()); } $nowtime = getMillisecond(); $newflag = $flags - 1; if ($flags >= 1 && ($nowtime - $redis->lIndex($symbol, $newflag)) < $time) { $redis->rpush($symbol, getMillisecond()); return false; } else { if ($flags > 1) { for ($i = 0; $i < $flags; $i++) { //提交时间大于1s if ($nowtime - $redis->lIndex($symbol, 0) > $time) { //出栈 $redis->lpop($symbol); } } } if ($redis->lLen($symbol) <= 1) { $redis->rpush($symbol, getMillisecond()); } } return true; } } if (!function_exists('lopRedis')) { //防重复点击逻辑,开启redis数据 毫秒级防重复 出栈解除 function lopRedis($symbol ,$time = 3000) { $redis = getRedis(\think\Config::get("redis")); //出栈 $redis->lpop($symbol); return true; } } //获取毫秒级时间戳 function getMillisecond() { list($t1, $t2) = explode(' ', microtime()); return (float)sprintf('%.0f',(floatval($t1)+floatval($t2))*1000); } if (!function_exists('createApi')) { //防重复点击逻辑,开启redis数据 毫秒级防重复 出栈解除 function createApi() { $data['apikey'] = \fast\Random::alnum('28'); $data['secretkey'] = \fast\Random::alnum('40'); return $data; } } function http_post($url, $data_string) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'X-AjaxPro-Method:ShowList', 'Content-Type: application/json; charset=utf-8', 'Content-Length: ' . strlen($data_string)) ); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); $data = curl_exec($ch); curl_close($ch); //var_dump(curl_error($ch));die; return $data; } //创建宝塔任务 function create_bt_cron($strategy_id,$url = 'http://127.0.0.1:15196/api/tasklh/open_strategy?strategy_id=',$times = 20) { $taskexit = Db::name('app_ai_task') ->where('strategy_id',$strategy_id)->find(); if($taskexit){ return ['status' => true]; } $btapi = new Btapi(); $url .= $strategy_id; $step = 19; $sleep = rand(1,10); $params = [ 'name' => '策略-'.$strategy_id, 'type' => 'minute-n', 'where1' => '1', 'hour' => '', 'minute' => '', 'week' => '', 'sType' => 'toShell', 'sBody' => '#!/bin/bash PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PATH step='.$step.' for (( i = 0; i < 3; i=(i+1) )); do sleep '.$sleep.' curl -sS --connect-timeout 10 -m 60 "'.$url.'" echo "----------------------------------------------------------------------------" endDate=`date +"%Y-%m-%d %H:%M:%S"` echo "★[$endDate] Successful" echo "----------------------------------------------------------------------------" sleep 15 done exit 0', 'sName'=>'', 'backupTo' => 'localhost', 'save' => '', 'urladdress' => '', ]; $result = $btapi->addCrontab($params); if(isset($result) && $result['status'] && isset($result['id'])) { $addata = [ 'name' => 'strategy_'.$strategy_id, 'task_sn' => 'strategy_'.$strategy_id, 'status' => '1', 'strategy_id' => $strategy_id, 'btcron_id' => $result['id'], 'createtime' => time(), ]; Db::name('app_ai_task')->insert($addata); } return $result; } //删除宝塔任务 function del_bt_cron($strategy_id) { $btapi = new Btapi(); $task = Db::name('app_ai_task')->where('strategy_id',$strategy_id)->find(); if(empty($task)){ return ['status'=>false]; } $result = $btapi->delCrontab($task['btcron_id']); if(isset($result['status']) && $result['status']){ Db::name('app_ai_task')->where('id',$task['id'])->delete(); } return $result; } //获取宝塔任务 function get_bt_cron($strategy_id) { $btapi = new Btapi(); $task = Db::name('app_ai_task')->where('strategy_id',$strategy_id)->find(); if(empty($task)){ return ['status'=>false]; } $result = $btapi->getCrontabFind($task['btcron_id']); return $result; } //创建宝塔任务 function create_bt_kline($id,$url = 'http://127.0.0.1:15196/api/task_trade/get_kline?id=',$times = 20) { $taskexit = Db::name('app_curr_release')->where('id',$id)->find(); if(!$taskexit){ return ['status' => false]; } $btapi = new Btapi(); $url .= $id; $step = 1; $params = [ 'name' => '获取最新K线-'.$id, 'type' => 'minute-n', 'where1' => '1', 'hour' => '', 'minute' => '', 'week' => '', 'sType' => 'toShell', 'sBody' => '#!/bin/bash PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PATH step=20 for (( i = 0; i < 60; i=(i+step) )); do curl -sS --connect-timeout 10 -m 60 "'.$url.'" echo "----------------------------------------------------------------------------" endDate=`date +"%Y-%m-%d %H:%M:%S"` echo "★[$endDate] Successful" echo "----------------------------------------------------------------------------" sleep $step done exit 0', 'sName'=>'', 'backupTo' => 'localhost', 'save' => '', 'urladdress' => '', ]; $result = $btapi->addCrontab($params); if(isset($result) && $result['status'] && isset($result['id'])) { $update = [ 'btcron_id' => $result['id'], ]; Db::name('app_curr_release')->where('id',$id)->update($update); } return $result; } //删除宝塔任务 function del_bt_trade($id) { $btapi = new Btapi(); $task = Db::name('app_curr_release')->where('id',$id)->find(); if(empty($task) || $task['btcron_id'] == 0){ return ['status'=>false]; } $result = $btapi->delCrontab($task['btcron_id']); if(isset($result['status']) && $result['status']){ Db::name('app_curr_release')->where('id',$task['id'])->update(['btcron_id'=>0]); } return $result; } //获取宝塔任务 function get_bt_trade($id) { $btapi = new Btapi(); $task = Db::name('app_curr_release')->where('id',$id)->find(); if(empty($task) || $task['btcron_id'] == 0){ return ['status'=>false]; } $result = $btapi->getCrontabFind($task['btcron_id']); return $result; }