helper.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: wangfanchang
  5. * Date: 18/1/4
  6. * Time: 下午8:58
  7. * 自定义助手函数文件
  8. */
  9. use app\main\service\LogService;
  10. use GuzzleHttp\Client as Http;
  11. use think\Config;
  12. use think\Log;
  13. use think\Request;
  14. if (!function_exists('get_db_deploy')) {
  15. /**
  16. * 获取db分库的配置参数
  17. *
  18. * @param string|int $param 取模值
  19. * @param string $deploy 分库前缀
  20. * @return array
  21. */
  22. function get_db_deploy($param, $deploy = 'shard')
  23. {
  24. $db = Config::get('db');
  25. $mod = $param % $db[$deploy . '_num'];
  26. $mod = abs($mod);
  27. $list = explode(';', $db[$deploy . '_list']);
  28. Log::info("分库配置参数:{$param} 标志:{$deploy} 库数量:" . $db[$deploy . '_num'] . " 模:{$mod}");
  29. foreach ($list as $item) {
  30. $con = explode(':', $item); // 0=0-191库编号 1=192.168.1.149主IP 2=3306主端口 3=192.168.1.150从IP 4=3306从端口
  31. if (count($con) >= 3) {
  32. $c = explode('-', $con[0]); //库编号 0开始 1结束
  33. if (count($c) >= 2) {
  34. if ($c[0] <= $mod && $mod <= $c[1]) {
  35. $database = Config::get('database');
  36. if ($database['deploy'] == 1 && count($con) >= 5) { //开启主从 & 带主从配置
  37. $database['hostname'] = $con[1] . ',' . $con[3]; //192.168.1.149,192.168.1.150
  38. $database['hostport'] = $con[2] . ',' . $con[4]; //3306,3306
  39. } else { //只有主库
  40. $database['hostname'] = $con[1];
  41. $database['hostport'] = $con[2];
  42. $database['deploy'] = 0;
  43. $database['rw_separate'] = false;
  44. }
  45. Log::info("分库获取成功 IP:{$database['hostname']} port: {$database['hostport']}");
  46. $database['database'] = 'mysql';
  47. $database['table'] = str_replace('$mod', $mod, $db[$deploy . '_database']);
  48. return $database;
  49. }
  50. }
  51. }
  52. }
  53. Log::error("分库获取失败!");
  54. return [];
  55. }
  56. }
  57. if (!function_exists('get_db_collect')) {
  58. /**
  59. * 获取dbcollect的配置参数
  60. */
  61. function get_db_collect($table)
  62. {
  63. try {
  64. $dbcollect = Config::get('dbcollect');
  65. $hostNames = explode(',', $dbcollect['hostname']);
  66. $hostPorts = explode(',', $dbcollect['hostport']);
  67. $dbName = $dbcollect['database'];
  68. $aTables = explode(',', $dbcollect['tables']);
  69. $database = Config::get('database');
  70. if (in_array($table, $aTables)) {
  71. if (isset($dbcollect['deploy']) && $dbcollect['deploy'] == 1 && count($hostNames) == 2) { //开启主从 & 带主从配置
  72. $database['deploy'] = 1;
  73. $database['hostname'] = $hostNames[0] . ',' . $hostNames[1]; //192.168.1.149,192.168.1.150
  74. $database['hostport'] = $hostPorts[0] . ',' . $hostPorts[1]; //3306,3306
  75. } else { //只有主库
  76. $database['deploy'] = 0;
  77. $database['hostname'] = $hostNames[0];
  78. $database['hostport'] = $hostPorts[0];
  79. }
  80. if (array_key_exists('username', $dbcollect)) {
  81. $database['username'] = $dbcollect['username'];
  82. }
  83. if (array_key_exists('password', $dbcollect)) {
  84. $database['password'] = $dbcollect['password'];
  85. }
  86. $database['database'] = $dbName;
  87. }
  88. return $database;
  89. }catch (\Exception $exception) {
  90. throw new Exception('get_db_collect error! message: ' . $exception->getMessage());
  91. }
  92. return [];
  93. }
  94. }
  95. if (!function_exists('curl_dot')) {
  96. /**
  97. * 打点
  98. */
  99. function curl_dot($query = [])
  100. {
  101. try {
  102. $url = Config::get('site.dothost');
  103. $client = new Http([
  104. 'connect_timeout' => 3,
  105. 'timeout' => 3,
  106. 'http_errors' => true, //抛出异常 true是 false否
  107. 'verify' => false, //不验证ssl证书
  108. ]);
  109. $queryParam = json_encode($query);
  110. $url = $url.'?json='.$queryParam;
  111. Log::write($queryParam,'打点信息');
  112. $res = $client->request('GET', $url);
  113. if ($res->getStatusCode() != '200') {
  114. Log::error('API返回状态错误!Url:' . $url . ' Params:' . json_encode($query) . ' StatusCode:' . $res->getStatusCode());
  115. return ['code' => 1, 'msg' => 'API返回状态错误!'];
  116. }
  117. } catch (\Exception $exception) {
  118. Log::error('API请求异常!Url:' . $url . ' Params:' . json_encode($query) . ' ErrorMessage:' . $exception->getMessage());
  119. }
  120. }
  121. }
  122. if(!function_exists('curl_user_dot')){
  123. // 用户画像打点
  124. function curl_user_dot($query = [])
  125. {
  126. if (Config::get('site.user_log_switch')) {
  127. try {
  128. $url = Config::get('site.user_loghost').'/h5user.php';
  129. //$url = 'http://log.dev.kpread.com/h6.php';
  130. $client = new Http([
  131. 'connect_timeout' => 3,
  132. 'timeout' => 3,
  133. 'http_errors' => true, //抛出异常 true是 false否
  134. 'verify' => false, //不验证ssl证书
  135. ]);
  136. $queryParam = json_encode($query);
  137. $url = $url . '?json=' . $queryParam;
  138. $res = $client->request('GET', $url);
  139. /*$res = $client->request('POST',
  140. $url,
  141. [
  142. 'headers' => [
  143. 'Content-Type' => 'application/json',
  144. 'charset' => 'utf-8'
  145. ],
  146. 'body' => $queryParam
  147. ]
  148. );*/
  149. if ($res->getStatusCode() != '200') {
  150. Log::error('用户画像打点异常:返回状态错误!Url:' . $url . ' Params:' . json_encode($query) . ' StatusCode:' . $res->getStatusCode());
  151. return ['code' => 1, 'msg' => 'API返回状态错误!'];
  152. }
  153. } catch (\Exception $exception) {
  154. Log::error('用户画像打点异常:URL:' . $url . ' Params:' . json_encode($query) . ' ErrorMessage:' . $exception->getMessage());
  155. }
  156. }
  157. }
  158. }
  159. if (!function_exists('get_db_connect')) {
  160. /**
  161. * 获取db分库connect参数
  162. * @param int $param 被取模数(用户ID | hash code)
  163. * @return array|mixed
  164. * @throws array|mixed
  165. */
  166. function get_db_connect($table, $param)
  167. {
  168. Log::info("get_db_connect table:{$table} params:{$param}");
  169. if (!$table || !$param) {
  170. Log::error('get_db_connect 数据库分库链接表名或参数错误');
  171. throw new Exception('数据库分库链接表名或参数错误');
  172. }
  173. try {
  174. $pos = strrpos($table, '.');
  175. if ($pos !== false) {
  176. $table = substr($table, ++$pos);
  177. }
  178. $deploy = false;
  179. foreach (['user', 'shard', 'shelf'] as $value) {
  180. $tables = Config::get('db.' . $value . '_tables');
  181. $tablesArr = explode(',', strtolower($tables));
  182. if (in_array(strtolower($table), $tablesArr)) {
  183. $deploy = $value;
  184. break;
  185. }
  186. }
  187. if (!$deploy) {
  188. Log::error('get_db_connect table表名匹配失败');
  189. } else {
  190. $database = get_db_deploy($param, $deploy);
  191. if (!empty($database)) {
  192. $database['table'] = $database['table'] . "." . $table;
  193. return $database;
  194. }
  195. }
  196. } catch (\Exception $exception) {
  197. throw new Exception('get_db_connect error! message: ' . $exception->getMessage());
  198. }
  199. return [];
  200. }
  201. }
  202. if (!function_exists('get_polardb_connect')) {
  203. function get_polardb_connect()
  204. {
  205. $database = Config::get('polardb');
  206. if ($database) {
  207. return $database;
  208. }else{
  209. return [];
  210. }
  211. }
  212. }
  213. if (!function_exists('hash_code')) {
  214. /**
  215. * 将str hash 之后取code
  216. */
  217. function hash_code($str)
  218. {
  219. $h = 0;
  220. for ($i = 0; $i < strlen($str); $i++) {
  221. $h = 31 * $h + ord($str[$i]);
  222. $h = $h & 0x00000000FFFFFFFF;
  223. }
  224. $h = ((~$h) & 0x00000000FFFFFFFF) + (($h << 21) & 0x00000000FFFFFFFF); // $h = ($h << 21) - $h - 1;
  225. $h = $h & 0x00000000FFFFFFFF;
  226. if ((($h & 0x0000000080000000) >> 31) == 1) {
  227. $a = $h >> 24 | 0xFFFFFF00;
  228. } else {
  229. $a = $h >> 24;
  230. }
  231. $a = $a & 0x00000000FFFFFFFF;
  232. $h = $h ^ $a;
  233. $h = $h & 0x00000000FFFFFFFF;
  234. $h = (($h + ($h << 3) & 0x00000000FFFFFFFF) & 0x00000000FFFFFFFF) + (($h << 8) & 0x00000000FFFFFFFF); // $h * 265
  235. $h = $h & 0x00000000FFFFFFFF;
  236. if ((($h & 0x0000000080000000) >> 31) == 1) {
  237. $a = ($h >> 14) | 0x00000000FFFC0000;
  238. } else {
  239. $a = $h >> 14;
  240. }
  241. $a = $a & 0x00000000FFFFFFFF;
  242. $h = $h ^ $a;
  243. $h = $h & 0x00000000FFFFFFFF;
  244. $h = (($h + (($h << 2) & 0x00000000FFFFFFFF)) & 0x00000000FFFFFFFF) + (($h << 4) & 0x00000000FFFFFFFF); // $h * 21
  245. $h = $h & 0x00000000FFFFFFFF;
  246. if ((($h & 0x0000000080000000) >> 31) == 1) {
  247. $a = ($h >> 28) | 0x00000000FFFFFFF0;
  248. } else {
  249. $a = $h >> 28;
  250. }
  251. $a = $a & 0x00000000FFFFFFFF;
  252. $h = $h ^ $a;
  253. $h = $h & 0x00000000FFFFFFFF;
  254. $h = $h + (($h << 31) & 0x00000000FFFFFFFF);
  255. $h = $h & 0x00000000FFFFFFFF;
  256. if (($h & 0x00000000FFFFFFFF) >> 31 == 1) {
  257. return $h | 0xFFFFFFFF80000000;
  258. } else {
  259. return $h;
  260. }
  261. }
  262. }
  263. if (!function_exists('del_url_params')) {
  264. /**
  265. * 删除url中的多余参数
  266. *
  267. * @param string $url 要处理的url
  268. * @param string|array $params 要删除的参数 支持字符串删除一个,或者数组删除多个
  269. */
  270. function del_url_params($url, $params)
  271. {
  272. $arr = parse_url($url);
  273. $old_params = [];
  274. if (isset($arr['query']) && !empty($arr['query'])) {
  275. $queryParts = explode('&', $arr['query']);
  276. if ($queryParts) {
  277. foreach ($queryParts as $param) {
  278. $item = explode('=', $param);
  279. if (count($item) == 2) {
  280. $old_params[$item[0]] = $item[1];
  281. }
  282. }
  283. }
  284. }
  285. if (is_string($params)) {
  286. if (isset($old_params[$params])) {
  287. unset($old_params[$params]);
  288. }
  289. } elseif (is_array($params)) {
  290. foreach ($params as $item) {
  291. if (isset($old_params[$item])) {
  292. unset($old_params[$item]);
  293. }
  294. }
  295. }
  296. $tmp_url = empty($arr['scheme']) ? 'http' : $arr['scheme'] . '://' . $arr['host'] . $arr['path'];
  297. if (!empty($old_params)) {
  298. $i = 0;
  299. foreach ($old_params as $k => $v) {
  300. $tmp_url = $tmp_url . ($i == 0 ? '?' : '&') . $k . '=' . $v;
  301. $i++;
  302. }
  303. }
  304. return $tmp_url;
  305. }
  306. }
  307. if (!function_exists('get_host_no_port')) {
  308. /**
  309. * 获取host域名,且不带端口
  310. */
  311. function get_host_no_port()
  312. {
  313. $host = Request::instance()->host();
  314. return strtolower(preg_replace('/:\d+$/', '', trim($host)));
  315. }
  316. }
  317. if (!function_exists('get_url_no_port')) {
  318. /**
  319. * 获取url全连接,且不带端口号
  320. *
  321. * @param null|bool $domain 是否带domain 【默认null不带,true带】
  322. * @return string
  323. */
  324. function get_url_no_port($domain = null)
  325. {
  326. $url = Request::instance()->url($domain);
  327. $url = strtolower(preg_replace('/:\d+$/', '', trim($url))); //去除 :端口 结束的
  328. return preg_replace('/:\d+\//', '/', trim($url)); //去除 :端口/
  329. }
  330. }
  331. if(!function_exists('friend_date')){
  332. /*
  333. * 友好时间展示
  334. * @param $time
  335. */
  336. function friend_date($time){
  337. if(!$time){
  338. return 0;
  339. }
  340. if($time >= 100000000){
  341. return round($time/100000000,2).'亿';
  342. }
  343. if($time >= 10000){
  344. return round($time/10000,2).'万';
  345. }
  346. if($time < 10000){
  347. return $time;
  348. }
  349. }
  350. }
  351. if (!function_exists('asset')) {
  352. /**
  353. * 获取静态资源全连接 (自动补全cdn、版本)
  354. *
  355. * @param string $file 要获取url路径的静态资源名,如 /css/frontend/chapter.css
  356. * @return string
  357. */
  358. function asset($file)
  359. {
  360. if (substr($file, 0, 1) != '/') { //必须以 / 开头
  361. $file = '/' . $file;
  362. }
  363. $extension = pathinfo($file,PATHINFO_EXTENSION);
  364. $assetsPath = ROOT_PATH . 'public' . DS. 'assets' . DS;
  365. $theme = Config::get('template.view_theme');
  366. if($theme && is_file($assetsPath.$theme.DS.$file)){
  367. $file = "/assets/{$theme}{$file}";
  368. }else{
  369. $file = "/assets{$file}";
  370. }
  371. if (Config::get('site.nocdn') == 1) { //关闭cdn
  372. if (strtolower($extension) != 'css' && strtolower($extension) != 'js') { //表示只关闭css 其他类型还走cdn
  373. $file = Config::get('upload.cdnurl') . $file;
  374. }
  375. } else { //开启cdn
  376. $file = Config::get('upload.cdnurl') . $file;
  377. }
  378. return $file . '?v='. Config::get('site.version');
  379. }
  380. }
  381. if (!function_exists('client_asset')) {
  382. /**
  383. * 获取静态资源全连接 (自动补全cdn、版本)
  384. *
  385. * @param string $file 要获取url路径的静态资源名,如 /css/frontend/chapter.css
  386. * @return string
  387. */
  388. function client_asset($file, $theme = 'clientweb')
  389. {
  390. if (substr($file, 0, 1) != '/') { //必须以 / 开头
  391. $file = '/' . $file;
  392. }
  393. $extension = pathinfo($file,PATHINFO_EXTENSION);
  394. $assetsPath = ROOT_PATH . 'public' . DS. 'assets' . DS;
  395. if($theme && is_file($assetsPath.$theme.DS.$file)){
  396. $file = "/assets/{$theme}{$file}";
  397. }else{
  398. $file = "/assets{$file}";
  399. }
  400. if (strtolower($extension) != 'js') {
  401. if (Config::get('site.nocdn') == 1) { //关闭cdn
  402. if (strtolower($extension) != 'css') { //表示只关闭css 其他类型还走cdn
  403. $file = Config::get('upload.cdnurl') . $file;
  404. }
  405. } else { //开启cdn
  406. $file = Config::get('upload.cdnurl') . $file;
  407. }
  408. }
  409. return $file . '?v='. Config::get('site.version');
  410. }
  411. }
  412. if (!function_exists('rm_space')) {
  413. /**
  414. * 过滤字符串空格
  415. */
  416. function rm_space($str)
  417. {
  418. $str = str_replace(" ", "", $str);
  419. return preg_replace('/[\s]/','', $str);
  420. }
  421. }
  422. if (!function_exists('get_number')) {
  423. /**
  424. * 取出数字
  425. * @$isIdNumber 是否为身份证号
  426. */
  427. function get_number($str, $isIdNumber = false)
  428. {
  429. $res = '';
  430. if ($isIdNumber) {
  431. $p = '/[\dxX]/i';
  432. } else {
  433. $p = '/[\d]/';
  434. }
  435. if (preg_match_all($p, $str, $m)) {
  436. $res = implode('', $m[0]);
  437. }
  438. return $res;
  439. }
  440. }
  441. if (!function_exists('isMobile')) {
  442. function isMobile($value)
  443. {
  444. $rule = '^1(3|4|5|7|8|9)[0-9]\d{8}$^';
  445. $result = preg_match($rule, $value);
  446. if ($result) {
  447. return true;
  448. } else {
  449. return false;
  450. }
  451. }
  452. }
  453. if (!function_exists("buildAppUrl")) {
  454. function buildAppUrl($path, $params = []) {
  455. $url = \app\main\service\ClientAppService::instance()->parseAppUrl($path, $params);
  456. return urlencode($url);
  457. }
  458. }
  459. if (!function_exists('cpslog')) {
  460. function cpslog()
  461. {
  462. \app\main\service\PvuvCollectService::instance()->indexDot();
  463. }
  464. }