common.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. <?php
  2. use think\facade\Request;
  3. use think\facade\Log;
  4. // 应用公共文件
  5. /**
  6. * 打印调试函数
  7. * @param $content
  8. * @param $is_die
  9. */
  10. function p($content, $is_die = true)
  11. {
  12. header('Content-type: text/html; charset=utf-8');
  13. echo '<pre>' . print_r($content, true);
  14. $is_die && die();
  15. }
  16. /**
  17. * 隐藏敏感字符
  18. * @param $value
  19. * @return string
  20. */
  21. function substr_cut($value)
  22. {
  23. $strlen = mb_strlen($value, 'utf-8');
  24. if ($strlen <= 1) return $value;
  25. $firstStr = mb_substr($value, 0, 1, 'utf-8');
  26. $lastStr = mb_substr($value, -1, 1, 'utf-8');
  27. return $strlen == 2 ? $firstStr . str_repeat('*', $strlen - 1) : $firstStr . str_repeat("*", $strlen - 2) . $lastStr;
  28. }
  29. /**
  30. * 字符串进行加密。
  31. */
  32. function authcode($string,$operation)
  33. {
  34. $key ='887wesfds5fg56r';
  35. $key = md5($key ? $key : $GLOBALS['auth_key']);
  36. $key_length = strlen($key);
  37. $string = $operation == 'DECODE' ? base64_decode($string) : substr(md5($string . $key), 0, 8) . $string;
  38. $string_length = strlen($string);
  39. $rndkey = $box = array();
  40. $result = '';
  41. for ($i = 0; $i < 256; $i++) {
  42. $rndkey[$i] = ord($key[$i % $key_length]);
  43. $box[$i] = $i;
  44. }
  45. for ($j = $i = 0; $i < 256; $i++) {
  46. $j = ($j + $box[$i] + $rndkey[$i]) % 256;
  47. $tmp = $box[$i];
  48. $box[$i] = $box[$j];
  49. $box[$j] = $tmp;
  50. }
  51. for ($a = $j = $i = 0; $i < $string_length; $i++) {
  52. $a = ($a + 1) % 256;
  53. $j = ($j + $box[$a]) % 256;
  54. $tmp = $box[$a];
  55. $box[$a] = $box[$j];
  56. $box[$j] = $tmp;
  57. $result .= chr(ord($string[$i]) ^ $box[($box[$a] + $box[$j]) % 256]);
  58. }
  59. if ($operation == 'DECODE') {
  60. if (substr($result, 0, 8) == substr(md5(substr($result, 8) . $key), 0, 8)) {
  61. return substr($result, 8);
  62. } else {
  63. return '';
  64. }
  65. } else {
  66. return str_replace('=', '', base64_encode($result));
  67. }
  68. }
  69. /**
  70. * 获取当前系统版本号
  71. * @return mixed|null
  72. * @throws Exception
  73. */
  74. function get_version()
  75. {
  76. try {
  77. $file = root_path() . '/version.json';
  78. $version = json_decode(file_get_contents($file), true);
  79. return $version['version'];
  80. } catch (\Exception $e) {
  81. return '';
  82. }
  83. }
  84. /**
  85. * 驼峰命名转下划线命名
  86. * @param $str
  87. * @return string
  88. */
  89. function toUnderScore($str)
  90. {
  91. $dstr = preg_replace_callback('/([A-Z]+)/', function ($matchs) {
  92. return '_' . strtolower($matchs[0]);
  93. }, $str);
  94. return trim(preg_replace('/_{2,}/', '_', $dstr), '_');
  95. }
  96. /**
  97. * 生成密码hash值
  98. * @param $password
  99. * @return string
  100. */
  101. function salt_hash($password)
  102. {
  103. return md5(md5($password) . 'jjjshop_salt_2020');
  104. }
  105. /**
  106. * curl请求指定url (post)
  107. * @param $url
  108. * @param array $data
  109. * @return mixed
  110. */
  111. function curlPost($url, $data = [])
  112. {
  113. $ch = curl_init();
  114. curl_setopt($ch, CURLOPT_POST, 1);
  115. curl_setopt($ch, CURLOPT_HEADER, 0);
  116. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  117. curl_setopt($ch, CURLOPT_URL, $url);
  118. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  119. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  120. $result = curl_exec($ch);
  121. curl_close($ch);
  122. return $result;
  123. }
  124. /**
  125. * 多维数组合并
  126. * @param $array1
  127. * @param $array2
  128. * @return array
  129. */
  130. function array_merge_multiple($array1, $array2)
  131. {
  132. $merge = $array1 + $array2;
  133. $data = [];
  134. foreach ($merge as $key => $val) {
  135. if (
  136. isset($array1[$key])
  137. && is_array($array1[$key])
  138. && isset($array2[$key])
  139. && is_array($array2[$key])
  140. ) {
  141. $data[$key] = array_merge_multiple($array1[$key], $array2[$key]);
  142. } else {
  143. $data[$key] = isset($array2[$key]) ? $array2[$key] : $array1[$key];
  144. }
  145. }
  146. return $data;
  147. }
  148. /**
  149. * 二维数组排序
  150. * @param $arr
  151. * @param $keys
  152. * @param bool $desc
  153. * @return mixed
  154. */
  155. function array_sort($arr, $keys, $desc = false)
  156. {
  157. $key_value = $new_array = array();
  158. foreach ($arr as $k => $v) {
  159. $key_value[$k] = $v[$keys];
  160. }
  161. if ($desc) {
  162. arsort($key_value);
  163. } else {
  164. asort($key_value);
  165. }
  166. reset($key_value);
  167. foreach ($key_value as $k => $v) {
  168. $new_array[$k] = $arr[$k];
  169. }
  170. return $new_array;
  171. }
  172. /**
  173. * 数据导出到excel(csv文件)
  174. * @param $fileName
  175. * @param array $tileArray
  176. * @param array $dataArray
  177. */
  178. function export_excel($fileName, $tileArray = [], $dataArray = [])
  179. {
  180. ini_set('memory_limit', '512M');
  181. ini_set('max_execution_time', 0);
  182. ob_end_clean();
  183. ob_start();
  184. header("Content-Type: text/csv");
  185. header("Content-Disposition:filename=" . $fileName);
  186. $fp = fopen('php://output', 'w');
  187. fwrite($fp, chr(0xEF) . chr(0xBB) . chr(0xBF));// 转码 防止乱码(比如微信昵称)
  188. fputcsv($fp, $tileArray);
  189. $index = 0;
  190. foreach ($dataArray as $item) {
  191. if ($index == 1000) {
  192. $index = 0;
  193. ob_flush();
  194. flush();
  195. }
  196. $index++;
  197. fputcsv($fp, $item);
  198. }
  199. ob_flush();
  200. flush();
  201. ob_end_clean();
  202. }
  203. /**
  204. * 写入日志
  205. * @param $value
  206. * @param string $type
  207. */
  208. function log_write($value, $channel = '')
  209. {
  210. $msg = is_string($value) ? $value : var_export($value, true);
  211. if($channel != ''){
  212. Log::channel('task')->write($msg);
  213. }else{
  214. Log::channel($channel)->write($msg);
  215. }
  216. }
  217. /**
  218. * 获取当前域名及根路径
  219. * @return string
  220. */
  221. function base_url()
  222. {
  223. static $baseUrl = '';
  224. if (empty($baseUrl)) {
  225. $request = Request::instance();
  226. //$subDir = str_replace('\\', '/', dirname($request->server('PHP_SELF')));
  227. $baseUrl = $request->scheme() . '://' . $request->host() . '/';
  228. }
  229. return $baseUrl;
  230. // return replaceBaseUrl($baseUrl);
  231. }
  232. /**
  233. * 替换就域名
  234. * @param $baseUrl
  235. * @return mixed|string|string[]
  236. */
  237. function replaceBaseUrl($baseUrl)
  238. {
  239. $origin = 'cni-ekshop.cni.com.cn'; // 旧域名
  240. $modern = 'bs.cni-ekshop.com'; // 新域名
  241. $index = strpos($baseUrl, $origin);
  242. if ($index > 0) {
  243. $baseUrl = str_replace($origin, $modern, $baseUrl);
  244. }
  245. return $baseUrl;
  246. }
  247. /**
  248. * 左侧填充0
  249. * @param $value
  250. * @param int $padLength
  251. * @return string
  252. */
  253. function pad_left($value, $padLength = 2)
  254. {
  255. return \str_pad($value, $padLength, "0", STR_PAD_LEFT);
  256. }
  257. /**
  258. * 过滤emoji表情
  259. * @param $text
  260. * @return null|string|string[]
  261. */
  262. function filter_emoji($text)
  263. {
  264. // 此处的preg_replace用于过滤emoji表情
  265. // 如需支持emoji表情, 需将mysql的编码改为utf8mb4
  266. return preg_replace('/[\xf0-\xf7].{3}/', '', $text);
  267. }
  268. /**
  269. * 获取全局唯一标识符
  270. * @param bool $trim
  271. * @return string
  272. */
  273. function getGuidV4($trim = true)
  274. {
  275. // Windows
  276. if (function_exists('com_create_guid') === true) {
  277. $charid = com_create_guid();
  278. return $trim == true ? trim($charid, '{}') : $charid;
  279. }
  280. // OSX/Linux
  281. if (function_exists('openssl_random_pseudo_bytes') === true) {
  282. $data = openssl_random_pseudo_bytes(16);
  283. $data[6] = chr(ord($data[6]) & 0x0f | 0x40); // set version to 0100
  284. $data[8] = chr(ord($data[8]) & 0x3f | 0x80); // set bits 6-7 to 10
  285. return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
  286. }
  287. // Fallback (PHP 4.2+)
  288. mt_srand((double)microtime() * 10000);
  289. $charid = strtolower(md5(uniqid(rand(), true)));
  290. $hyphen = chr(45); // "-"
  291. $lbrace = $trim ? "" : chr(123); // "{"
  292. $rbrace = $trim ? "" : chr(125); // "}"
  293. return $lbrace .
  294. substr($charid, 0, 8) . $hyphen .
  295. substr($charid, 8, 4) . $hyphen .
  296. substr($charid, 12, 4) . $hyphen .
  297. substr($charid, 16, 4) . $hyphen .
  298. substr($charid, 20, 12) .
  299. $rbrace;
  300. }
  301. function format_time($value)
  302. {
  303. return date('Y-m-d', $value);
  304. }
  305. /**
  306. * curl请求指定url (get)
  307. * @param $url
  308. * @param array $data
  309. * @return mixed
  310. */
  311. function curl($url, $data = [])
  312. {
  313. // 处理get数据
  314. if (!empty($data)) {
  315. $url = $url . '?' . http_build_query($data);
  316. }
  317. $curl = curl_init();
  318. curl_setopt($curl, CURLOPT_URL, $url);
  319. curl_setopt($curl, CURLOPT_HEADER, false);
  320. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  321. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);//这个是重点。
  322. $result = curl_exec($curl);
  323. curl_close($curl);
  324. return $result;
  325. }
  326. /**
  327. * json 转换true,false,数字转成vue可直接用的
  328. */
  329. function jsonRecursive(&$array)
  330. {
  331. foreach ($array as $key => $value) {
  332. if (is_array($value)) {
  333. jsonRecursive($array[$key]);
  334. } else {
  335. if($value === 'true'){
  336. $array[$key] = true;
  337. } else if($value === 'false'){
  338. $array[$key] = false;
  339. }
  340. }
  341. }
  342. }
  343. /**
  344. * 判断浏览器名称和版本
  345. */
  346. function get_client_browser()
  347. {
  348. if(empty($_SERVER['HTTP_USER_AGENT'])){
  349. return 'robot!';
  350. }
  351. if( (false == strpos($_SERVER['HTTP_USER_AGENT'],'MSIE')) && (strpos($_SERVER['HTTP_USER_AGENT'], 'Trident')!==FALSE) ){
  352. return 'Internet Explorer 11.0';
  353. }
  354. if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'MSIE 10.0')){
  355. return 'Internet Explorer 10.0';
  356. }
  357. if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'MSIE 9.0')){
  358. return 'Internet Explorer 9.0';
  359. }
  360. if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'MSIE 8.0')){
  361. return 'Internet Explorer 8.0';
  362. }
  363. if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'MSIE 7.0')){
  364. return 'Internet Explorer 7.0';
  365. }
  366. if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'MSIE 6.0')){
  367. return 'Internet Explorer 6.0';
  368. }
  369. if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'Edge')){
  370. return 'Edge';
  371. }
  372. if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'Firefox')){
  373. return 'Firefox';
  374. }
  375. if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'Chrome')){
  376. return 'Chrome';
  377. }
  378. if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'Safari')){
  379. return 'Safari';
  380. }
  381. if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'Opera')){
  382. return 'Opera';
  383. }
  384. if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'360SE')){
  385. return '360SE';
  386. }
  387. //微信浏览器
  388. if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'MicroMessage')){
  389. return 'MicroMessage';
  390. }
  391. return '';
  392. }
  393. /**
  394. * 数组转义为json
  395. */
  396. function jsonEncode($data)
  397. {
  398. return json_encode($data, JSON_UNESCAPED_UNICODE);
  399. }
  400. /**
  401. * json转义为数组
  402. */
  403. function jsonDecode($json)
  404. {
  405. return json_decode($json, true);
  406. }
  407. /**
  408. * 检查日期是否合法
  409. * @param string $date
  410. * @param array $formats
  411. * @return bool
  412. */
  413. function checkDateIsValid(string $date, array $formats = ["Y-m-d", "Y/m/d"]): bool
  414. {
  415. $unixTime = strtotime($date);
  416. if (!$unixTime) {
  417. return false;
  418. }
  419. foreach ($formats as $format) {
  420. if (date($format, $unixTime) == $date) {
  421. return true;
  422. }
  423. }
  424. return false;
  425. }