123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505 |
- <?php
- /**
- * Created by PhpStorm.
- * User: wangfanchang
- * Date: 18/1/4
- * Time: 下午8:58
- * 自定义助手函数文件
- */
- use app\main\service\LogService;
- use GuzzleHttp\Client as Http;
- use think\Config;
- use think\Log;
- use think\Request;
- if (!function_exists('get_db_deploy')) {
- /**
- * 获取db分库的配置参数
- *
- * @param string|int $param 取模值
- * @param string $deploy 分库前缀
- * @return array
- */
- function get_db_deploy($param, $deploy = 'shard')
- {
- $db = Config::get('db');
- $mod = $param % $db[$deploy . '_num'];
- $mod = abs($mod);
- $list = explode(';', $db[$deploy . '_list']);
- Log::info("分库配置参数:{$param} 标志:{$deploy} 库数量:" . $db[$deploy . '_num'] . " 模:{$mod}");
- foreach ($list as $item) {
- $con = explode(':', $item); // 0=0-191库编号 1=192.168.1.149主IP 2=3306主端口 3=192.168.1.150从IP 4=3306从端口
- if (count($con) >= 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();
- }
- }
|