= 3) { $c = explode('-', $con[0]); //库编号 0开始 1结束 if (count($c) >= 2) { if ($c[0] <= $mod && $mod <= $c[1]) { $database = Config::get('database'); if ($database['deploy'] == 1 && count($con) >= 5) { //开启主从 & 带主从配置 $database['hostname'] = $con[1] . ',' . $con[3]; //192.168.1.149,192.168.1.150 $database['hostport'] = $con[2] . ',' . $con[4]; //3306,3306 } else { //只有主库 $database['hostname'] = $con[1]; $database['hostport'] = $con[2]; $database['deploy'] = 0; $database['rw_separate'] = false; } Log::info("分库获取成功 IP:{$database['hostname']} port: {$database['hostport']}"); $database['database'] = 'mysql'; $database['table'] = str_replace('$mod', $mod, $db[$deploy . '_database']); return $database; } } } } Log::error("分库获取失败!"); return []; } } if (!function_exists('get_db_collect')) { /** * 获取dbcollect的配置参数 */ function get_db_collect($table) { try { $dbcollect = Config::get('dbcollect'); $hostNames = explode(',', $dbcollect['hostname']); $hostPorts = explode(',', $dbcollect['hostport']); $dbName = $dbcollect['database']; $aTables = explode(',', $dbcollect['tables']); $database = Config::get('database'); if (in_array($table, $aTables)) { if (isset($dbcollect['deploy']) && $dbcollect['deploy'] == 1 && count($hostNames) == 2) { //开启主从 & 带主从配置 $database['deploy'] = 1; $database['hostname'] = $hostNames[0] . ',' . $hostNames[1]; //192.168.1.149,192.168.1.150 $database['hostport'] = $hostPorts[0] . ',' . $hostPorts[1]; //3306,3306 } else { //只有主库 $database['deploy'] = 0; $database['hostname'] = $hostNames[0]; $database['hostport'] = $hostPorts[0]; } if (array_key_exists('username', $dbcollect)) { $database['username'] = $dbcollect['username']; } if (array_key_exists('password', $dbcollect)) { $database['password'] = $dbcollect['password']; } $database['database'] = $dbName; } return $database; }catch (\Exception $exception) { throw new Exception('get_db_collect error! message: ' . $exception->getMessage()); } return []; } } if (!function_exists('curl_dot')) { /** * 打点 */ function curl_dot($query = []) { try { $url = Config::get('site.dothost'); $client = new Http([ 'connect_timeout' => 3, 'timeout' => 3, 'http_errors' => true, //抛出异常 true是 false否 'verify' => false, //不验证ssl证书 ]); $queryParam = json_encode($query); $url = $url.'?json='.$queryParam; Log::write($queryParam,'打点信息'); $res = $client->request('GET', $url); if ($res->getStatusCode() != '200') { Log::error('API返回状态错误!Url:' . $url . ' Params:' . json_encode($query) . ' StatusCode:' . $res->getStatusCode()); return ['code' => 1, 'msg' => 'API返回状态错误!']; } } catch (\Exception $exception) { Log::error('API请求异常!Url:' . $url . ' Params:' . json_encode($query) . ' ErrorMessage:' . $exception->getMessage()); } } } if(!function_exists('curl_user_dot')){ // 用户画像打点 function curl_user_dot($query = []) { if (Config::get('site.user_log_switch')) { try { $url = Config::get('site.user_loghost').'/h5user.php'; //$url = 'http://log.dev.kpread.com/h6.php'; $client = new Http([ 'connect_timeout' => 3, 'timeout' => 3, 'http_errors' => true, //抛出异常 true是 false否 'verify' => false, //不验证ssl证书 ]); $queryParam = json_encode($query); $url = $url . '?json=' . $queryParam; $res = $client->request('GET', $url); /*$res = $client->request('POST', $url, [ 'headers' => [ 'Content-Type' => 'application/json', 'charset' => 'utf-8' ], 'body' => $queryParam ] );*/ if ($res->getStatusCode() != '200') { Log::error('用户画像打点异常:返回状态错误!Url:' . $url . ' Params:' . json_encode($query) . ' StatusCode:' . $res->getStatusCode()); return ['code' => 1, 'msg' => 'API返回状态错误!']; } } catch (\Exception $exception) { Log::error('用户画像打点异常:URL:' . $url . ' Params:' . json_encode($query) . ' ErrorMessage:' . $exception->getMessage()); } } } } if (!function_exists('get_db_connect')) { /** * 获取db分库connect参数 * @param int $param 被取模数(用户ID | hash code) * @return array|mixed * @throws array|mixed */ function get_db_connect($table, $param) { Log::info("get_db_connect table:{$table} params:{$param}"); if (!$table || !$param) { Log::error('get_db_connect 数据库分库链接表名或参数错误'); throw new Exception('数据库分库链接表名或参数错误'); } try { $pos = strrpos($table, '.'); if ($pos !== false) { $table = substr($table, ++$pos); } $deploy = false; foreach (['user', 'shard', 'shelf'] as $value) { $tables = Config::get('db.' . $value . '_tables'); $tablesArr = explode(',', strtolower($tables)); if (in_array(strtolower($table), $tablesArr)) { $deploy = $value; break; } } if (!$deploy) { Log::error('get_db_connect table表名匹配失败'); } else { $database = get_db_deploy($param, $deploy); if (!empty($database)) { $database['table'] = $database['table'] . "." . $table; return $database; } } } catch (\Exception $exception) { throw new Exception('get_db_connect error! message: ' . $exception->getMessage()); } return []; } } if (!function_exists('get_polardb_connect')) { function get_polardb_connect() { $database = Config::get('polardb'); if ($database) { return $database; }else{ return []; } } } if (!function_exists('hash_code')) { /** * 将str hash 之后取code */ function hash_code($str) { $h = 0; for ($i = 0; $i < strlen($str); $i++) { $h = 31 * $h + ord($str[$i]); $h = $h & 0x00000000FFFFFFFF; } $h = ((~$h) & 0x00000000FFFFFFFF) + (($h << 21) & 0x00000000FFFFFFFF); // $h = ($h << 21) - $h - 1; $h = $h & 0x00000000FFFFFFFF; if ((($h & 0x0000000080000000) >> 31) == 1) { $a = $h >> 24 | 0xFFFFFF00; } else { $a = $h >> 24; } $a = $a & 0x00000000FFFFFFFF; $h = $h ^ $a; $h = $h & 0x00000000FFFFFFFF; $h = (($h + ($h << 3) & 0x00000000FFFFFFFF) & 0x00000000FFFFFFFF) + (($h << 8) & 0x00000000FFFFFFFF); // $h * 265 $h = $h & 0x00000000FFFFFFFF; if ((($h & 0x0000000080000000) >> 31) == 1) { $a = ($h >> 14) | 0x00000000FFFC0000; } else { $a = $h >> 14; } $a = $a & 0x00000000FFFFFFFF; $h = $h ^ $a; $h = $h & 0x00000000FFFFFFFF; $h = (($h + (($h << 2) & 0x00000000FFFFFFFF)) & 0x00000000FFFFFFFF) + (($h << 4) & 0x00000000FFFFFFFF); // $h * 21 $h = $h & 0x00000000FFFFFFFF; if ((($h & 0x0000000080000000) >> 31) == 1) { $a = ($h >> 28) | 0x00000000FFFFFFF0; } else { $a = $h >> 28; } $a = $a & 0x00000000FFFFFFFF; $h = $h ^ $a; $h = $h & 0x00000000FFFFFFFF; $h = $h + (($h << 31) & 0x00000000FFFFFFFF); $h = $h & 0x00000000FFFFFFFF; if (($h & 0x00000000FFFFFFFF) >> 31 == 1) { return $h | 0xFFFFFFFF80000000; } else { return $h; } } } if (!function_exists('del_url_params')) { /** * 删除url中的多余参数 * * @param string $url 要处理的url * @param string|array $params 要删除的参数 支持字符串删除一个,或者数组删除多个 */ function del_url_params($url, $params) { $arr = parse_url($url); $old_params = []; if (isset($arr['query']) && !empty($arr['query'])) { $queryParts = explode('&', $arr['query']); if ($queryParts) { foreach ($queryParts as $param) { $item = explode('=', $param); if (count($item) == 2) { $old_params[$item[0]] = $item[1]; } } } } if (is_string($params)) { if (isset($old_params[$params])) { unset($old_params[$params]); } } elseif (is_array($params)) { foreach ($params as $item) { if (isset($old_params[$item])) { unset($old_params[$item]); } } } $tmp_url = empty($arr['scheme']) ? 'http' : $arr['scheme'] . '://' . $arr['host'] . $arr['path']; if (!empty($old_params)) { $i = 0; foreach ($old_params as $k => $v) { $tmp_url = $tmp_url . ($i == 0 ? '?' : '&') . $k . '=' . $v; $i++; } } return $tmp_url; } } if (!function_exists('get_host_no_port')) { /** * 获取host域名,且不带端口 */ function get_host_no_port() { $host = Request::instance()->host(); return strtolower(preg_replace('/:\d+$/', '', trim($host))); } } if (!function_exists('get_url_no_port')) { /** * 获取url全连接,且不带端口号 * * @param null|bool $domain 是否带domain 【默认null不带,true带】 * @return string */ function get_url_no_port($domain = null) { $url = Request::instance()->url($domain); $url = strtolower(preg_replace('/:\d+$/', '', trim($url))); //去除 :端口 结束的 return preg_replace('/:\d+\//', '/', trim($url)); //去除 :端口/ } } if(!function_exists('friend_date')){ /* * 友好时间展示 * @param $time */ function friend_date($time){ if(!$time){ return 0; } if($time >= 100000000){ return round($time/100000000,2).'亿'; } if($time >= 10000){ return round($time/10000,2).'万'; } if($time < 10000){ return $time; } } } if (!function_exists('asset')) { /** * 获取静态资源全连接 (自动补全cdn、版本) * * @param string $file 要获取url路径的静态资源名,如 /css/frontend/chapter.css * @return string */ function asset($file) { if (substr($file, 0, 1) != '/') { //必须以 / 开头 $file = '/' . $file; } $extension = pathinfo($file,PATHINFO_EXTENSION); $assetsPath = ROOT_PATH . 'public' . DS. 'assets' . DS; $theme = Config::get('template.view_theme'); if($theme && is_file($assetsPath.$theme.DS.$file)){ $file = "/assets/{$theme}{$file}"; }else{ $file = "/assets{$file}"; } if (Config::get('site.nocdn') == 1) { //关闭cdn if (strtolower($extension) != 'css' && strtolower($extension) != 'js') { //表示只关闭css 其他类型还走cdn $file = Config::get('upload.cdnurl') . $file; } } else { //开启cdn $file = Config::get('upload.cdnurl') . $file; } return $file . '?v='. Config::get('site.version'); } } if (!function_exists('client_asset')) { /** * 获取静态资源全连接 (自动补全cdn、版本) * * @param string $file 要获取url路径的静态资源名,如 /css/frontend/chapter.css * @return string */ function client_asset($file, $theme = 'clientweb') { if (substr($file, 0, 1) != '/') { //必须以 / 开头 $file = '/' . $file; } $extension = pathinfo($file,PATHINFO_EXTENSION); $assetsPath = ROOT_PATH . 'public' . DS. 'assets' . DS; if($theme && is_file($assetsPath.$theme.DS.$file)){ $file = "/assets/{$theme}{$file}"; }else{ $file = "/assets{$file}"; } if (strtolower($extension) != 'js') { if (Config::get('site.nocdn') == 1) { //关闭cdn if (strtolower($extension) != 'css') { //表示只关闭css 其他类型还走cdn $file = Config::get('upload.cdnurl') . $file; } } else { //开启cdn $file = Config::get('upload.cdnurl') . $file; } } return $file . '?v='. Config::get('site.version'); } } if (!function_exists('rm_space')) { /** * 过滤字符串空格 */ function rm_space($str) { $str = str_replace(" ", "", $str); return preg_replace('/[\s]/','', $str); } } if (!function_exists('get_number')) { /** * 取出数字 * @$isIdNumber 是否为身份证号 */ function get_number($str, $isIdNumber = false) { $res = ''; if ($isIdNumber) { $p = '/[\dxX]/i'; } else { $p = '/[\d]/'; } if (preg_match_all($p, $str, $m)) { $res = implode('', $m[0]); } return $res; } } if (!function_exists('isMobile')) { function isMobile($value) { $rule = '^1(3|4|5|7|8|9)[0-9]\d{8}$^'; $result = preg_match($rule, $value); if ($result) { return true; } else { return false; } } } if (!function_exists("buildAppUrl")) { function buildAppUrl($path, $params = []) { $url = \app\main\service\ClientAppService::instance()->parseAppUrl($path, $params); return urlencode($url); } } if (!function_exists('cpslog')) { function cpslog() { \app\main\service\PvuvCollectService::instance()->indexDot(); } }