Helpers.php 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592
  1. <?php
  2. use GuzzleHttp\Client;
  3. use Illuminate\Support\Facades\DB;
  4. use Illuminate\Support\Facades\Log;
  5. use Monolog\Handler\StreamHandler;
  6. use Monolog\Logger;
  7. use OSS\OssClient;
  8. use OSS\Core\OssException;
  9. /**
  10. * 截取字符串
  11. *
  12. * @param $string
  13. * @param $length
  14. * @param bool $append
  15. * @return array|string
  16. */
  17. function sysSubStr($string, $length, $append = false)
  18. {
  19. if (strlen($string) <= $length) {
  20. return $string;
  21. }
  22. $i = 0;
  23. $stringLast = [];
  24. while ($i < $length) {
  25. $stringTMP = mb_substr($string, $i, 1);
  26. if (ord($stringTMP) >= 224) {
  27. $stringTMP = mb_substr($string, $i, 3);
  28. $i += 3;
  29. } elseif (ord($stringTMP) >= 192) {
  30. $stringTMP = mb_substr($string, $i, 2);
  31. $i += 2;
  32. } else {
  33. ++$i;
  34. }
  35. $stringLast[] = $stringTMP;
  36. }
  37. $stringLast = implode('', $stringLast);
  38. if ($append) {
  39. $stringLast .= '...';
  40. }
  41. return $stringLast;
  42. }
  43. /**
  44. * 自定义日志
  45. *
  46. * @param $name
  47. * @param string $filename
  48. * @return \Illuminate\Log\Writer
  49. */
  50. function myLog($name, $filename = '')
  51. {
  52. if (!$filename) {
  53. $filename = $name;
  54. }
  55. $filename = $filename . '.log';
  56. $logger = new Logger($name);
  57. $writer = new \Illuminate\Log\Writer($logger);
  58. $writer->useDailyFiles(storage_path('logs/' . $filename));
  59. return $writer;
  60. }
  61. /**
  62. * 业务调试日志
  63. *
  64. * @return Logger
  65. */
  66. function commonLog()
  67. {
  68. $name = 'common';
  69. $filename = $name . '.log';
  70. $logger = new Logger($name);
  71. $logger->pushHandler(new StreamHandler(storage_path('logs/' . $filename), 'debug'));
  72. return $logger;
  73. }
  74. /**
  75. * @param $name
  76. * @return Logger
  77. */
  78. function dLog($name)
  79. {
  80. $filename = $name . '-' . date('Y-m-d') . '.log';
  81. $logger = new Logger($name);
  82. $logger->pushHandler(new StreamHandler(storage_path('logs/' . $filename), 'debug'));
  83. return $logger;
  84. }
  85. /**
  86. * 获取对象或数组的属性值
  87. *
  88. * @param $param
  89. * @param $key
  90. * @param string $default
  91. * @return mixed|string
  92. */
  93. function getProp($param, $key, $default = '')
  94. {
  95. $result = $default;
  96. if (is_object($param) && isset($param->$key)) {
  97. $result = $param->$key;
  98. }
  99. if (is_array($param) && isset($param[$key])) {
  100. $result = $param[$key];
  101. }
  102. return $result;
  103. }
  104. /**
  105. * @param $data
  106. * @param array $trans
  107. * @return array
  108. */
  109. function assetData($data, $trans = []): array
  110. {
  111. $result = [];
  112. if (empty($data)) {
  113. return $result;
  114. }
  115. if (empty($trans)) {
  116. return $data;
  117. }
  118. foreach ($trans as $tran) {
  119. [$originKey, $newKey, $conv, $default] = [$tran['o'], $tran['n'], $tran['conv'], $tran['default']];
  120. if (isset($data[$originKey])) {
  121. $result[$newKey] = $conv(getProp($data, $originKey, $default));
  122. }
  123. }
  124. return $result;
  125. }
  126. /**
  127. * 随机数
  128. *
  129. * @param int $num
  130. * @return string
  131. * @throws Exception
  132. */
  133. function random($num = 16)
  134. {
  135. $bytes = random_bytes($num);
  136. return bin2hex($bytes);
  137. }
  138. /**
  139. * 生成订单
  140. *
  141. * @param string $prefix 订单前缀
  142. * @return string
  143. */
  144. function generateOrderSn($prefix = '')
  145. {
  146. return $prefix . date('YmdHis') . getMillisecond() . rand(1000, 9999);
  147. }
  148. /**
  149. * 分页数据
  150. *
  151. * @param $data
  152. * @return array
  153. */
  154. function getMeta($data)
  155. {
  156. $currentPage = (int)$data->currentPage();
  157. $lastPage = (int)$data->lastPage();
  158. return [
  159. 'current_page' => $currentPage,
  160. 'next_page' => $currentPage >= $lastPage ? $lastPage : ++$currentPage,
  161. 'last_page' => $lastPage,
  162. 'per_page' => (int)$data->perPage(),
  163. 'total' => (int)$data->total(),
  164. 'is_end' => !$data->hasMorePages(),
  165. 'next_page_url' => (string)$data->nextPageUrl(),
  166. 'prev_page_url' => (string)$data->previousPageUrl()
  167. ];
  168. }
  169. /**
  170. * 钉钉通知异常
  171. *
  172. * @param $message
  173. */
  174. function sendNotice($message)
  175. {
  176. $webHook = env('DD_WEB_HOOK');
  177. $data = [
  178. 'msgtype' => 'text',
  179. 'text' => [
  180. 'content' => $message
  181. ],
  182. 'at' => [
  183. 'isAll' => true
  184. ]
  185. ];
  186. // 异步发送
  187. $client = new GuzzleHttp\Client();
  188. $client->post($webHook, ['json' => $data]);
  189. }
  190. /**
  191. * 判断数据是合法的json数据
  192. *
  193. * @param $string
  194. * @return bool
  195. */
  196. function is_json($string)
  197. {
  198. json_decode($string);
  199. return (json_last_error() == JSON_ERROR_NONE);
  200. }
  201. /**
  202. * 获取ip地址
  203. *
  204. * @return mixed|string
  205. */
  206. function getIpAddr()
  207. {
  208. $ipaddress = '';
  209. if (isset($_SERVER['HTTP_CLIENT_IP']))
  210. $ipaddress = $_SERVER['HTTP_CLIENT_IP'];
  211. else if (isset($_SERVER['HTTP_X_FORWARDED_FOR']))
  212. $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
  213. else if (isset($_SERVER['HTTP_X_FORWARDED']))
  214. $ipaddress = $_SERVER['HTTP_X_FORWARDED'];
  215. else if (isset($_SERVER['HTTP_FORWARDED_FOR']))
  216. $ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
  217. else if (isset($_SERVER['HTTP_FORWARDED']))
  218. $ipaddress = $_SERVER['HTTP_FORWARDED'];
  219. else if (isset($_SERVER['REMOTE_ADDR']))
  220. $ipaddress = $_SERVER['REMOTE_ADDR'];
  221. else
  222. $ipaddress = 'UNKNOWN';
  223. return $ipaddress;
  224. }
  225. /**
  226. * @param int $num
  227. * @return bool|string
  228. * @throws Exception
  229. */
  230. function randStr($num = 5)
  231. {
  232. $str = 'QWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnm';
  233. return substr(str_shuffle($str), random_int(0, strlen($str) - 11), $num);
  234. }
  235. /**
  236. * 上传文件
  237. *
  238. * @param $file
  239. * @return string
  240. * @throws Exception
  241. */
  242. function uploadFile($file)
  243. {
  244. // 阿里云主账号
  245. $accessKeyId = env('OSS_ACCESS_ID');
  246. $accessKeySecret = env('OSS_ACCESS_KEY');
  247. $endpoint = env('OSS_END_POINT');
  248. $bucket = env('OSS_BUCKET');
  249. // 设置文件名称。
  250. $object = 'channel/file/contract/' . randStr(10) . '.pdf';
  251. try {
  252. $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
  253. $ossImgBackData = $ossClient->uploadFile($bucket, $object, $file);
  254. } catch (OssException $e) {
  255. printf($e->getMessage());
  256. return '';
  257. }
  258. $urlArr = parse_url($ossImgBackData['oss-request-url']);
  259. return getProp($urlArr, 'path');
  260. }
  261. /**
  262. * 下载文件到本地
  263. *
  264. * @param $object
  265. * @param $localFile
  266. * @return string
  267. */
  268. function downloadFile($object, $localFile)
  269. {
  270. // 阿里云主账号
  271. $accessKeyId = env('OSS_ACCESS_ID');
  272. $accessKeySecret = env('OSS_ACCESS_KEY');
  273. $endpoint = env('OSS_END_POINT');
  274. $bucket = env('OSS_BUCKET');
  275. try {
  276. $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
  277. $ossClient->getObject($bucket, $object, [
  278. OssClient::OSS_FILE_DOWNLOAD => $localFile
  279. ]);
  280. } catch (OssException $e) {
  281. printf($e->getMessage());
  282. return '';
  283. }
  284. }
  285. /**
  286. * 上传封面
  287. *
  288. * @param $file
  289. * @return string
  290. * @throws Exception
  291. */
  292. function uploadCoverFile($file)
  293. {
  294. // 阿里云主账号
  295. $accessKeyId = env('OSS_ACCESS_ID');
  296. $accessKeySecret = env('OSS_ACCESS_KEY');
  297. $endpoint = env('OSS_END_POINT');
  298. $bucket = env('OSS_BUCKET');
  299. // 设置文件名称。
  300. $object = 'books/cover/' . randStr(10) . '.' . $file->getClientOriginalExtension();
  301. try {
  302. $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
  303. $ossImgBackData = $ossClient->uploadFile($bucket, $object, $file->path());
  304. } catch (OssException $e) {
  305. printf($e->getMessage());
  306. return '';
  307. }
  308. $urlArr = parse_url($ossImgBackData['oss-request-url']);
  309. return getProp($urlArr, 'path') ? 'http://' . $bucket . '.' . $endpoint . getProp($urlArr, 'path') : '';
  310. }
  311. function uploadAgreementFile($file)
  312. {
  313. // 阿里云主账号
  314. $accessKeyId = env('OSS_ACCESS_ID');
  315. $accessKeySecret = env('OSS_ACCESS_KEY');
  316. $endpoint = env('OSS_END_POINT');
  317. $bucket = env('OSS_BUCKET');
  318. $file_name = $file->getClientOriginalName();
  319. $file_name = str_replace('.' . $file->getClientOriginalExtension(), '', $file_name);
  320. // 设置文件名称。
  321. $object = 'books/contract/' . $file_name . '-' . date('YmdHi') . '.' . $file->getClientOriginalExtension();
  322. try {
  323. $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
  324. $ossImgBackData = $ossClient->uploadFile($bucket, $object, $file);
  325. } catch (OssException $e) {
  326. printf($e->getMessage());
  327. return '';
  328. }
  329. $urlArr = parse_url($ossImgBackData['oss-request-url']);
  330. return getProp($urlArr, 'path') ? 'http://' . $bucket . '.' . $endpoint . urldecode(getProp($urlArr, 'path')) : '';
  331. }
  332. /**
  333. * 上传wap推荐图片
  334. *
  335. * @param $file
  336. * @param $filename
  337. * @return string
  338. * @throws Exception
  339. */
  340. function uploadWapRecommendPic($file, $filename)
  341. {
  342. // 阿里云主账号
  343. $accessKeyId = env('OSS_ACCESS_ID');
  344. $accessKeySecret = env('OSS_ACCESS_KEY');
  345. $endpoint = env('OSS_END_POINT');
  346. $bucket = env('OSS_BUCKET_YCSD');
  347. // 设置文件名称。
  348. $object = 'ycsd_web_3nd/images/homebanners/' . $filename . '.' . $file->getClientOriginalExtension();
  349. try {
  350. $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
  351. $options = array(
  352. OssClient::OSS_HEADERS => array(
  353. 'Content-Type' => 'image/jpeg',
  354. 'Content-Disposition' => 'inline'
  355. ),
  356. );
  357. $ossImgBackData = $ossClient->uploadFile($bucket, $object, $file->path(), $options);
  358. } catch (OssException $e) {
  359. printf($e->getMessage());
  360. return '';
  361. }
  362. $urlArr = parse_url($ossImgBackData['oss-request-url']);
  363. return getProp($urlArr, 'path') ? 'http://' . $bucket . '.' . $endpoint . getProp($urlArr, 'path') : '';
  364. }
  365. /**
  366. * @param $path
  367. * @return Generator
  368. */
  369. function readFileContent($path)
  370. {
  371. if ($handle = fopen($path, 'r')) {
  372. while (!feof($handle)) {
  373. yield trim(fgets($handle));
  374. }
  375. fclose($handle);
  376. }
  377. }
  378. function exportFileCsv(array $headers, array $data, string $filename)
  379. {
  380. header("Content-type:application/vnd.ms-excel");
  381. header("Content-Disposition:attachment;filename=" . $filename . ".csv");
  382. $headers = collect($headers)->map(function ($item) {
  383. return "\"" . mb_convert_encoding($item, "GBK", "UTF-8") . "\"";
  384. })->all();
  385. echo implode(",", $headers);
  386. echo "\r\n";
  387. foreach ($data as $item) {
  388. $rows = collect($item)->map(function ($row) {
  389. return "\"" . mb_convert_encoding(is_numeric($row) && strlen($row) > 12 ? "'" . $row : $row, "GBK", "UTF-8") . "\"";
  390. })->all();
  391. echo implode(",", $rows);
  392. echo "\r\n";
  393. }
  394. exit();
  395. }
  396. //function exportFileCsv(array $header, array $data, string $filename) {
  397. // header('Content-Encoding: UTF-8');
  398. // header("Content-type:application/vnd.ms-excel;charset=UTF-8");
  399. // header('Content-Disposition: attachment;filename="' . $filename . '.csv"');
  400. //
  401. // //打开php标准输出流
  402. // $fp = fopen('php://output', 'a');
  403. //
  404. // //添加BOM头,以UTF8编码导出CSV文件,如果文件头未添加BOM头,打开会出现乱码。
  405. // fwrite($fp, chr(0xEF).chr(0xBB).chr(0xBF));
  406. // //添加导出标题
  407. // fputcsv($fp, $header);
  408. //
  409. // foreach ($data as $k => $item) {
  410. // fputcsv($fp, $item);
  411. // if ($k % 5000 == 0) {
  412. // //每1万条数据就刷新缓冲区
  413. // ob_flush();
  414. // flush();
  415. // }
  416. // }
  417. // exit();
  418. //}
  419. //function exportFileCsv(array $header, array $data, string $filename) {
  420. //
  421. // header('Content-Type: application/vnd.ms-excel');
  422. // header('Content-Disposition: attachment;filename="'.$filename.'.csv"');
  423. // header('Cache-Control: max-age=0');
  424. //
  425. // //打开PHP文件句柄,php://output 表示直接输出到浏览器
  426. // $fp = fopen('php://output', 'a');
  427. //
  428. // //输出Excel列名信息
  429. // foreach ($header as $key => $value) {
  430. // //CSV的Excel支持GBK编码,一定要转换,否则乱码
  431. // $header[$key] = iconv('utf-8', 'gbk', $value);
  432. // }
  433. //
  434. // //将数据通过fputcsv写到文件句柄
  435. // fputcsv($fp, $header);
  436. //
  437. // //计数器
  438. // $num = 0;
  439. //
  440. // //每隔$limit行,刷新一下输出buffer,不要太大,也不要太小
  441. // $limit = 10000;
  442. //
  443. // //逐行取出数据,不浪费内存
  444. // $count = count($data);
  445. // for ($i = 0; $i < $count; $i++) {
  446. //
  447. // $num++;
  448. //
  449. // //刷新一下输出buffer,防止由于数据过多造成问题
  450. // if ($limit == $num) {
  451. // ob_flush();
  452. // flush();
  453. // $num = 0;
  454. // }
  455. //
  456. // $row = $data[$i];
  457. // foreach ($row as $key => $value) {
  458. // $row[$key] = iconv('utf-8', 'gbk', $value);
  459. // }
  460. //
  461. // fputcsv($fp, $row);
  462. // }
  463. // exit();
  464. //}
  465. // 获取除空格外的字数
  466. function getSize(string $content)
  467. {
  468. $content = preg_replace('/\s+/', '', $content);
  469. return mb_strlen($content, 'utf-8');
  470. }
  471. // 获取word字符数(不计空格)
  472. function getChargeSize(string $content)
  473. {
  474. //判断是否存在替换字符
  475. $is_replace_count = substr_count($content, "龘");
  476. try {
  477. //先将回车换行符做特殊处理
  478. $str = preg_replace('/(\r\n+|\s+| +)/', "龘", $content);
  479. //处理英文字符数字,连续字母、数字、英文符号视为一个单词
  480. $str = preg_replace('/[a-z_A-Z0-9-\.!@#\$%\\\^&\*\)\(\+=\{\}\[\]\/",\'<>~`\?:;|]/', "m", $str);
  481. //合并字符m,连续字母、数字、英文符号视为一个单词
  482. $str = preg_replace('/m+/', "*", $str);
  483. //去掉回车换行符
  484. $str = preg_replace('/龘+/', "", $str);
  485. //返回字数
  486. return mb_strlen($str) + $is_replace_count;
  487. } catch (\Exception $e) {
  488. return 0;
  489. }
  490. }
  491. // 将阿拉伯数字转换成中文
  492. function chineseNum($figure, $capital = false, $mode = true)
  493. {
  494. if ($figure == '0') return '零';
  495. $numberChar = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九'];
  496. $unitChar = ['', '十', '百', '千', '', '万', '亿', '兆', '京', '垓', '秭', '穣', '沟', '涧', '正', '载', '极', '恒河沙', '阿僧祇', '那由他', '不可思议', '无量大数'];
  497. if ($capital !== false) {
  498. $numberChar = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'];
  499. $unitChar = ['', '拾', '佰', '仟', '', '万', '亿', '兆', '京', '垓', '秭', '穣', '沟', '涧', '正', '载', '极', '恒河沙', '阿僧祇', '那由他', '不可思议', '无量大数'];
  500. }
  501. $dec = "点";
  502. $target = '';
  503. $matches = [];
  504. if ($mode) {
  505. preg_match("/^0*(\d*)\.?(\d*)/", $figure, $matches);
  506. } else {
  507. preg_match("/(\d*)\.?(\d*)/", $figure, $matches);
  508. }
  509. list(, $number, $point) = $matches;
  510. if ($point) {
  511. $target = $dec . chineseNum($point, $capital, false);
  512. }
  513. if (!$number) {
  514. return $target;
  515. }
  516. $str = strrev($number);
  517. for ($i = 0; $i < strlen($str); $i++) {
  518. $out[$i] = $numberChar[$str[$i]];
  519. if ($mode === false) {
  520. continue;
  521. }
  522. $out[$i] .= $str[$i] != '0' ? $unitChar[$i % 4] : '';
  523. if ($i > 0 && $str[$i] + $str[$i - 1] == 0) {
  524. $out[$i] = '';
  525. }
  526. if ($i % 4 == 0) {
  527. $temp = substr($str, $i, 4);
  528. $out[$i] = str_replace($numberChar[0], '', $out[$i]);
  529. if (strrev($temp) > 0) {
  530. $out[$i] .= $unitChar[4 + floor($i / 4)];
  531. } else {
  532. $out[$i] .= $numberChar[0];
  533. }
  534. }
  535. }
  536. $result = join('', array_reverse($out)) . $target;
  537. return mb_substr($result, 0, 2) == '一十' ? mb_substr($result, 1) : $result;
  538. }
  539. function addPrefix($str)
  540. {
  541. if (!$str) return '';
  542. if (mb_substr($str, 0, 4) == 'http') return $str;
  543. if (mb_substr($str, 0, 7) == '/books/') return 'http://zwcontent.oss-cn-hangzhou.aliyuncs.com' . $str;
  544. if (mb_substr($str, 0, 6) == '/card/') return 'http://zwcontent.oss-cn-hangzhou.aliyuncs.com' . $str;
  545. if (mb_substr($str, 0, 15) == 'uploader/idcard') return 'http://zwcontent.oss-cn-hangzhou.aliyuncs.com/' . $str;
  546. if (mb_substr($str, 0, 6) == '/cover') return 'https://cdn-newyc.ycsd.cn/ycsd_cover/covermiddle' . mb_substr($str, 6);
  547. if (mb_substr($str, 0, 8) == '/images/') return 'https://cdn-newyc.ycsd.cn/ycsd_cover' . $str;
  548. }
  549. /**
  550. * 章节内容排版
  551. *
  552. * @param $content
  553. * @return string
  554. */
  555. function filterContent($content)
  556. {
  557. if (!$content) return '';
  558. $content = str_replace(
  559. ['&nbsp;&nbsp;', '<br /><br />', '<br>', '<br />', '&nbsp;', '<p>', '</p>', '&ldquo;', '&rdquo;', '&hellip;', '&lsquo;', '&rsquo;', '&mdash;'],
  560. [' ', PHP_EOL, PHP_EOL, PHP_EOL, ' ', '', PHP_EOL, '“', '”', '...', '‘', '’', '-'],
  561. $content);
  562. $content = preg_replace('/(\r\n)+/', PHP_EOL, $content);
  563. // 段落首字母前加两个中文空格
  564. $string = explode(PHP_EOL, $content);
  565. foreach ($string as $line => $text) {
  566. $string[$line] = str_replace([' ', "\r\n", "\r", "\n", ' '], '', $string[$line]);
  567. if (!$string[$line]) {
  568. unset($string[$line]);
  569. } else {
  570. $string[$line] = $string[$line] . PHP_EOL;
  571. // if (mb_substr($string[$line], 0, 1) == ' ') {
  572. // $string[$line] = str_replace(' ', '', $string[$line]); // 去除多个空格
  573. // $string[$line] = '  ' . $string[$line].PHP_EOL;
  574. // }
  575. // if (mb_substr($string[$line], 0, 2) != '  ') {
  576. // $string[$line] = '  ' . $string[$line].PHP_EOL;
  577. // }
  578. // if (mb_substr($string[$line], 0, 2) == '  ' && str_replace(' ', '', $string[$line])) {
  579. // $string[$line] .= PHP_EOL;
  580. // }
  581. }
  582. }
  583. $content = implode(PHP_EOL, $string);
  584. return $content;
  585. }
  586. /**
  587. * 书籍简介排版
  588. *
  589. * @param $content
  590. * @return string
  591. */
  592. function filterContent2($content)
  593. {
  594. if (!$content) return '';
  595. $content = str_replace(
  596. ['&nbsp;&nbsp;', '<br /><br />', '<br>', '<br />', '&nbsp;', '<p>', '</p>', '&ldquo;', '&rdquo;', '&hellip;', '&lsquo;', '&rsquo;', '&mdash;'],
  597. [' ', PHP_EOL, PHP_EOL, PHP_EOL, ' ', '', PHP_EOL, '“', '”', '...', '‘', '’', '-'],
  598. $content);
  599. $content = preg_replace('/(\r\n)+/', PHP_EOL, $content);
  600. // 段落首字母前加两个中文空格
  601. $string = explode(PHP_EOL, $content);
  602. $content = '';
  603. foreach ($string as $line => $text) {
  604. $string[$line] = str_replace([' ', "\r\n", "\r", "\n", ' ', '<br />'], '', $string[$line]);
  605. if (!$string[$line]) {
  606. unset($string[$line]);
  607. } else {
  608. $string[$line] = '  ' . $string[$line] . '<br />';
  609. $content .= $string[$line];
  610. }
  611. }
  612. $content = trim($content, '<br />');
  613. return $content;
  614. }
  615. /**
  616. * 书籍简介排版-抖音版
  617. *
  618. * @param $content
  619. * @return string
  620. */
  621. function filterIntro($content)
  622. {
  623. if (!$content) return '';
  624. $content = str_replace(
  625. ['&nbsp;&nbsp;', '<br /><br />', '<br>', '<br />', '&nbsp;', '<p>', '</p>', '&ldquo;', '&rdquo;', '&hellip;', '&lsquo;', '&rsquo;', '&mdash;'],
  626. [' ', ' ', ' ', ' ', ' ', '', ' ', '“', '”', '...', '‘', '’', '-'],
  627. $content);
  628. $content = preg_replace('/(\r\n)+/', PHP_EOL, $content);
  629. // 段落首字母前加两个中文空格
  630. $string = explode(PHP_EOL, $content);
  631. $content = '';
  632. foreach ($string as $line => $text) {
  633. $string[$line] = str_replace([' ', "\r\n", "\r", "\n", ' ', '<br />'], '', $string[$line]);
  634. if (!$string[$line]) {
  635. unset($string[$line]);
  636. } else {
  637. $content .= $string[$line];
  638. }
  639. }
  640. $content = trim($content, '<br />');
  641. return $content;
  642. }
  643. function sensitiveStr($list, $string)
  644. {
  645. $count = 0; //违规词的个数
  646. $sensitiveWord = ''; //违规词
  647. $stringAfter = $string; //替换后的内容
  648. $total = count($list);
  649. $size = 500;
  650. $last = ceil($total / $size);
  651. $patternList = [];
  652. for ($page = 1; $page <= $last; $page++) {
  653. $arr = array_slice($list, $size * ($page - 1), $size);
  654. $filter = [];
  655. foreach ($arr as $v) {
  656. if (preg_match('/[^a-zA-Z0-9\|\p{Han}\·]/u', $v)) continue;
  657. $filter[] = $v;
  658. // $a = preg_replace('/[^a-zA-Z0-9\|\p{Han}]/u', '', $v);
  659. // if ($a) $filter[] = $a;
  660. }
  661. $pattern = "/" . implode("|", $filter) . "/i"; //定义正则表达式
  662. if (preg_match_all($pattern, $string, $matches)) { //匹配到了结果
  663. $patternList = array_merge($patternList, $matches[0]); //匹配到的数组
  664. }
  665. }
  666. $sensitiveWord = '';
  667. if ($patternList) {
  668. $count = count($patternList);
  669. // $sensitiveWord = implode(',', $patternList); //敏感词数组转字符串
  670. $replaceArray = array_combine($patternList, array_fill(0, count($patternList), '*')); //把匹配到的数组进行合并,替换使用
  671. $stringAfter = strtr($string, $replaceArray); //结果替换
  672. // 将敏感词合并
  673. $return_pattern = [];
  674. foreach ($patternList as $v) {
  675. if (!isset($return_pattern[$v])) {
  676. $return_pattern[$v] = [
  677. 'word' => $v,
  678. 'count' => 1,
  679. ];
  680. } else {
  681. $return_pattern[$v]['count'] += 1;
  682. }
  683. }
  684. foreach ($return_pattern as $v) {
  685. $sensitiveWord .= $v['word'] . ',';
  686. }
  687. }
  688. return [
  689. 'count' => $count,
  690. 'sensitive_words' => trim($sensitiveWord, ','),
  691. 'content' => $stringAfter
  692. ];
  693. }
  694. /**
  695. * 转换时间格式
  696. *
  697. * @param $date
  698. * @param string $format
  699. * @return false|string
  700. */
  701. function transDate($date, $format = 'Y-m-d H:i:s')
  702. {
  703. return strtotime($date) > 0 ? date($format, strtotime($date)) : '';
  704. }
  705. /**
  706. * 根据网段获取计算所有IP
  707. *
  708. * @param string $segment 网段 '139.217.0.1/24'
  709. * @return array [网络地址:139.217.0.1 广播地址:139.217.0.255 IP列表: ['139.217.0.2','139.217.0.3'……'139.217.0.254']]
  710. */
  711. function getIpBySegment($segment)
  712. {
  713. $segmentInfo = explode("/", $segment);
  714. $beginIpArray = explode(".", $segmentInfo[0]);
  715. $mask = intval($segmentInfo['1']);
  716. $endIp = array();
  717. foreach ($beginIpArray as $ipKey => $item) {
  718. $beginFlag = 8 * ($ipKey); //0 8 16 24
  719. $endFlag = 8 * ($ipKey + 1);//8 16 24 32
  720. $decbinItem = str_pad(decbin($item), 8, "0", STR_PAD_LEFT);
  721. $endIp[] = $mask >= $endFlag ? $item : ($mask > $beginFlag ? bindec(str_pad(substr($decbinItem, 0, $mask - $beginFlag), 8, "1", STR_PAD_RIGHT)) : ($ipKey <= 2 ? pow(2, 8) - 1 : pow(2, 8) - 1));
  722. }
  723. $ipArray = array();
  724. for ($beginIp[0] = $beginIpArray[0]; $beginIp[0] <= $endIp[0]; $beginIp[0]++) {
  725. for ($beginIp[1] = $beginIpArray[1]; $beginIp[1] <= $endIp[1]; $beginIp[1]++) {
  726. for ($beginIp[2] = $beginIpArray[2]; $beginIp[2] <= $endIp[2]; $beginIp[2]++) {
  727. for ($beginIp[3] = $beginIpArray[3]; $beginIp[3] <= $endIp[3]; $beginIp[3]++) {
  728. $ipArray[] = implode(".", $beginIp);
  729. }
  730. }
  731. }
  732. }
  733. $network_ip_addr = $beginIpArray[0] . '.' . $beginIpArray[1] . '.' . $beginIpArray[2] . '.' . '0'; // 网络地址
  734. $broadcast_ip_addr = end($ipArray); // 广播地址
  735. if ($ipArray[0] == $network_ip_addr) { // 如果是网络地址则删掉
  736. unset($ipArray[0]);
  737. }
  738. $last = count($ipArray);
  739. unset($ipArray[$last]);
  740. return [$network_ip_addr, $broadcast_ip_addr, $ipArray];
  741. }
  742. /**
  743. * 在指定网段中分配子网段
  744. *
  745. * @param string $segment 指定网段
  746. * @param int $ipNum 需要的IP数
  747. * @param array $usedIpArray 不可用(已经使用)的IP,默认为空数组
  748. * @return bool|string 成功则返回分配的网段
  749. */
  750. function allocateSegment($segment, $ipNum, $usedIpArray = [])
  751. {
  752. $usedIpArray = empty($usedIpArray) ? [] : array_flip($usedIpArray);
  753. //计算需要多少个IP
  754. $i = 0;
  755. $ipCount = pow(2, $i);
  756. while ($ipCount < $ipNum) {
  757. $i++;
  758. $ipCount = pow(2, $i);
  759. }
  760. $newMask = 32 - $i;
  761. //大网段的开始和结束IP
  762. $segmentInfo = explode("/", $segment); //['139.217.0.1',24]
  763. $beginIpArray = explode(".", $segmentInfo[0]);//[139,217,0,1]
  764. $mask = intval($segmentInfo['1']); //24
  765. if ($newMask < $mask) {
  766. return false;
  767. }
  768. $endIp = array();
  769. $step = [];
  770. foreach ($beginIpArray as $ipKey => $item) {
  771. $beginFlag = 8 * ($ipKey); //0 8 16 24
  772. $endFlag = 8 * ($ipKey + 1);//8 16 24 32
  773. $step[$ipKey] = $newMask > $endFlag ? 1 : ($endFlag - $newMask < 8 ? pow(2, $endFlag - $newMask) : pow(2, 8));
  774. $decbinItem = str_pad(decbin($item), 8, "0", STR_PAD_LEFT);
  775. $endIp[] = $mask >= $endFlag ? $item : ($mask > $beginFlag ? bindec(str_pad(substr($decbinItem, 0, $mask - $beginFlag), 8, "1", STR_PAD_RIGHT)) : ($ipKey <= 2 ? pow(2, 8) - 1 : pow(2, 8) - 1));
  776. }
  777. //遍历生成网段
  778. for ($beginIp[0] = $beginIpArray[0]; $beginIp[0] <= $endIp[0]; $beginIp[0] += $step[0]) {
  779. for ($beginIp[1] = $beginIpArray[1]; $beginIp[1] <= $endIp[1]; $beginIp[1] += $step[1]) {
  780. for ($beginIp[2] = $beginIpArray[2]; $beginIp[2] <= $endIp[2]; $beginIp[2] += $step[2]) {
  781. for ($beginIp[3] = $beginIpArray[3]; $beginIp[3] <= $endIp[3]; $beginIp[3] += $step[3]) {
  782. $newSegment = implode('.', $beginIp) . '/' . $newMask;
  783. //获取该网段所有的IP
  784. $ipArray = getIpBySegment($newSegment);
  785. $canUse = true;
  786. //判断该网段是否可用
  787. if (!empty($usedIpArray)) {
  788. foreach ($ipArray as $ip) {
  789. if (isset($usedIpArray[$ip])) {
  790. $canUse = false;
  791. break;
  792. }
  793. }
  794. }
  795. if ($canUse) {
  796. return $newSegment;
  797. }
  798. }
  799. }
  800. }
  801. }
  802. return false;
  803. }
  804. function remove_xss($val)
  805. {
  806. // remove all non-printable characters. CR(0a) and LF(0b) and TAB(9) are allowed
  807. // this prevents some character re-spacing such as <java\0script>
  808. // note that you have to handle splits with \n, \r, and \t later since they *are* allowed in some inputs
  809. $val = preg_replace('/([\x00-\x08,\x0b-\x0c,\x0e-\x19])/', '', $val);
  810. // straight replacements, the user should never need these since they're normal characters
  811. // this prevents like <IMG SRC=@avascript:alert('XSS')>
  812. $search = 'abcdefghijklmnopqrstuvwxyz';
  813. $search .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  814. $search .= '1234567890!@#$%^&*()';
  815. $search .= '~`";:?+/={}[]-_|\'\\';
  816. for ($i = 0; $i < strlen($search); $i++) {
  817. // ;? matches the ;, which is optional
  818. // 0{0,7} matches any padded zeros, which are optional and go up to 8 chars
  819. // @ @ search for the hex values
  820. $val = preg_replace('/(&#[xX]0{0,8}' . dechex(ord($search[$i])) . ';?)/i', $search[$i], $val); // with a ;
  821. // @ @ 0{0,7} matches '0' zero to seven times
  822. $val = preg_replace('/(�{0,8}' . ord($search[$i]) . ';?)/', $search[$i], $val); // with a ;
  823. }
  824. // now the only remaining whitespace attacks are \t, \n, and \r
  825. $ra1 = array('javascript', 'vbscript', 'expression', 'applet', 'meta', 'xml', 'blink', 'link', 'style', 'script', 'embed', 'object', 'iframe', 'frame', 'frameset', 'ilayer', 'layer', 'bgsound', 'title', 'base');
  826. $ra2 = array(
  827. 'onabort', 'onactivate', 'onafterprint', 'onafterupdate', 'onbeforeactivate', 'onbeforecopy', 'onbeforecut', 'onbeforedeactivate', 'onbeforeeditfocus', 'onbeforepaste', 'onbeforeprint', 'onbeforeunload', 'onbeforeupdate', 'onblur', 'onbounce', 'oncellchange', 'onchange', 'onclick', 'oncontextmenu', 'oncontrolselect', 'oncopy', 'oncut', 'ondataavailable', 'ondatasetchanged', 'ondatasetcomplete', 'ondblclick', 'ondeactivate', 'ondrag', 'ondragend', 'ondragenter', 'ondragleave', 'ondragover', 'ondragstart', 'ondrop', 'onerror', 'onerrorupdate', 'onfilterchange', 'onfinish', 'onfocus', 'onfocusin', 'onfocusout', 'onhelp', 'onkeydown', 'onkeypress', 'onkeyup', 'onlayoutcomplete', 'onload', 'onlosecapture', 'onmousedown', 'onmouseenter', 'onmouseleave', 'onmousemove', 'onmouseout', 'onmouseover', 'onmouseup', 'onmousewheel', 'onmove', 'onmoveend', 'onmovestart', 'onpaste', 'onpropertychange', 'onreadystatechange', 'onreset', 'onresize', 'onresizeend', 'onresizestart', 'onrowenter', 'onrowexit', 'onrowsdelete', 'onrowsinserted', 'onscroll', 'onselect', 'onselectionchange', 'onselectstart', 'onstart', 'onstop', 'onsubmit', 'onunload'
  828. );
  829. $ra = array_merge($ra1, $ra2);
  830. $found = true; // keep replacing as long as the previous round replaced something
  831. while ($found == true) {
  832. $val_before = $val;
  833. for ($i = 0; $i < sizeof($ra); $i++) {
  834. $pattern = '/';
  835. for ($j = 0; $j < strlen($ra[$i]); $j++) {
  836. if ($j > 0) {
  837. $pattern .= '(';
  838. $pattern .= '(&#[xX]0{0,8}([9ab]);)';
  839. $pattern .= '|';
  840. $pattern .= '|(�{0,8}([9|10|13]);)';
  841. $pattern .= ')*';
  842. }
  843. $pattern .= $ra[$i][$j];
  844. }
  845. $pattern .= '/i';
  846. $replacement = substr($ra[$i], 0, 2) . '<x>' . substr($ra[$i], 2); // add in <> to nerf the tag
  847. $val = preg_replace($pattern, $replacement, $val); // filter out the hex tags
  848. if ($val_before == $val) {
  849. // no replacements were made, so exit the loop
  850. $found = false;
  851. }
  852. }
  853. }
  854. return $val;
  855. }
  856. /**
  857. * 计算作者积分等级
  858. *
  859. * @param $score
  860. */
  861. function calcAuthorLevel($score): int
  862. {
  863. switch (true) {
  864. case $score <= 0:
  865. $level = 0;
  866. break;
  867. case $score <= 5000:
  868. $level = 1;
  869. break;
  870. case $score <= 50000:
  871. $level = 2;
  872. break;
  873. case $score <= 100000:
  874. $level = 3;
  875. break;
  876. case $score <= 300000:
  877. $level = 4;
  878. break;
  879. case $score <= 800000:
  880. $level = 5;
  881. break;
  882. case $score <= 1500000:
  883. $level = 6;
  884. break;
  885. case $score <= 2500000:
  886. $level = 7;
  887. break;
  888. case $score <= 5000000:
  889. $level = 8;
  890. break;
  891. case $score <= 10000000:
  892. $level = 9;
  893. break;
  894. default:
  895. $level = 10;
  896. break;
  897. }
  898. return $level;
  899. }
  900. /**
  901. * 运营数据(上传附件)
  902. *
  903. * @param $file
  904. * @return string
  905. * @throws Exception
  906. */
  907. function uploadEnclosureFile($file)
  908. {
  909. // 阿里云主账号
  910. $accessKeyId = env('OSS_ACCESS_ID');
  911. $accessKeySecret = env('OSS_ACCESS_KEY');
  912. $endpoint = env('OSS_END_POINT');
  913. $bucket = env('OSS_BUCKET');
  914. // 设置文件名称。
  915. $object = 'books/enclosure/' . randStr(10) . '--' . $file->getClientOriginalName();
  916. try {
  917. $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
  918. $ossImgBackData = $ossClient->uploadFile($bucket, $object, $file->path());
  919. } catch (OssException $e) {
  920. printf($e->getMessage());
  921. return '';
  922. }
  923. $urlArr = parse_url($ossImgBackData['oss-request-url']);
  924. return getProp($urlArr, 'path') ? 'http://' . $bucket . '.' . $endpoint . getProp($urlArr, 'path') : '';
  925. }
  926. // 获取当前域名是http还是https
  927. function getHttpType()
  928. {
  929. return ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://';
  930. }
  931. // 根据id生成唯一邀请码
  932. function enCodeId($user_id)
  933. {
  934. $key = 'XzeTdSPQc1uYHRBVWmUE6x94q25g3krfCGhb8FjtDZvMNKJpnayw7s';
  935. $num = strlen($key);
  936. $code = ''; // 邀请码
  937. while ($user_id > 0) { // 转进制
  938. $mod = $user_id % $num; // 求模
  939. $user_id = ($user_id - $mod) / $num;
  940. $code = $key[$mod] . $code;
  941. }
  942. $code = str_pad($code, 6, 'A', STR_PAD_LEFT); // 不足用0补充
  943. return $code;
  944. }
  945. // 根据邀请码解密为id
  946. function deCodeId($code)
  947. {
  948. $key = 'XzeTdSPQc1uYHRBVWmUE6x94q25g3krfCGhb8FjtDZvMNKJpnayw7s';
  949. $num = strlen($key);
  950. if (strrpos($code, '0') !== false) $code = substr($code, strrpos($code, '0') + 1);
  951. $len = strlen($code);
  952. $code = strrev($code);
  953. $user_id = 0;
  954. for ($i = 0; $i < $len; $i++) {
  955. $user_id += strpos($key, $code[$i]) * pow($num, $i);
  956. }
  957. return $user_id;
  958. }
  959. /**
  960. * 将二维数组按其中的某个数组排序(此方法适用于将数据库数据按数组取出后自动按ID排序的情况 ps:即未按该数组排序)
  961. *
  962. * @param array $array 二维数组
  963. * @param array $sort 排序数组
  964. * @param string $field 排序字段(二维数组和排序数组相同的字段)
  965. * @return array
  966. */
  967. function sortByArray(array $array, array $sort, string $field): array
  968. {
  969. $data = [];
  970. if (is_array($array) && is_array($sort)) {
  971. foreach ($sort as $v) {
  972. foreach ($array as $key => $val) {
  973. if ($v == $val[$field]) {
  974. array_push($data, $array[$key]);
  975. }
  976. }
  977. }
  978. }
  979. return $data;
  980. }
  981. /**
  982. * 将二维数组按其中的字段排序(正序或倒序)
  983. *
  984. * @param array $array 二维数组
  985. * @param string $field 排序字段
  986. * @param mixed $type 排序方式(3倒序,4正序)
  987. * @return array|mixed
  988. */
  989. function sortByField(array $array, string $field, $type): array
  990. {
  991. if (is_array($array)) {
  992. array_multisort(array_column($array, $field), $type, $array);
  993. }
  994. return $array;
  995. }
  996. // 生成用户邀请码
  997. function setUserInviteCode($id)
  998. {
  999. return \Vinkla\Hashids\Facades\Hashids::connection('invite')->encode($id);
  1000. }
  1001. // 解密用户邀请码
  1002. function decodeUserInviteCode($code)
  1003. {
  1004. return \Vinkla\Hashids\Facades\Hashids::connection('invite')->decode($code);
  1005. }
  1006. function getMillisecond()
  1007. {
  1008. list($microsecond, $time) = explode(' ', microtime());
  1009. return (float)sprintf('%.0f', (floatval($microsecond) + floatval($time)) * 1000);
  1010. }
  1011. function get_client_ip($type = 0, $adv = false)
  1012. {
  1013. $type = $type ? 1 : 0;
  1014. static $ip = null;
  1015. if (null !== $ip) {
  1016. return $ip[$type];
  1017. }
  1018. if ($adv) {
  1019. if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  1020. $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
  1021. $pos = array_search('unknown', $arr);
  1022. if (false !== $pos) {
  1023. unset($arr[$pos]);
  1024. }
  1025. $ip = trim($arr[0]);
  1026. } elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
  1027. $ip = $_SERVER['HTTP_CLIENT_IP'];
  1028. } elseif (isset($_SERVER['REMOTE_ADDR'])) {
  1029. $ip = $_SERVER['REMOTE_ADDR'];
  1030. }
  1031. } elseif (isset($_SERVER['REMOTE_ADDR'])) {
  1032. $ip = $_SERVER['REMOTE_ADDR'];
  1033. }
  1034. // IP地址合法验证
  1035. $long = sprintf("%u", ip2long($ip));
  1036. $ip = $long ? array($ip, $long) : array('0.0.0.0', 0);
  1037. return $ip[$type];
  1038. }
  1039. /**
  1040. * 获取真实IP
  1041. */
  1042. function _getIp()
  1043. {
  1044. if (getenv('HTTP_X_FORWARDED_FOR')) {
  1045. $ip = getenv('HTTP_X_FORWARDED_FOR');
  1046. } else if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown"))
  1047. $ip = getenv("HTTP_CLIENT_IP");
  1048. else if (getenv("HTTP_X_FORWARD_FOR") && strcasecmp(getenv("HTTP_X_FORWARD_FOR"), "unknown"))
  1049. $ip = getenv("HTTP_X_FORWARD_FOR");
  1050. else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown"))
  1051. $ip = getenv("REMOTE_ADDR");
  1052. else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown"))
  1053. $ip = $_SERVER['REMOTE_ADDR'];
  1054. else
  1055. $ip = "unknown";
  1056. return ($ip);
  1057. }
  1058. /**
  1059. * 数组 转 对象
  1060. *
  1061. * @param array $arr 数组
  1062. * @return object
  1063. */
  1064. function array_to_object($arr)
  1065. {
  1066. if (gettype($arr) != 'array') {
  1067. return;
  1068. }
  1069. foreach ($arr as $k => $v) {
  1070. if (gettype($v) == 'array' || getType($v) == 'object') {
  1071. $arr[$k] = (object)array_to_object($v);
  1072. }
  1073. }
  1074. return (object)$arr;
  1075. }
  1076. /**
  1077. * 对象 转 数组
  1078. *
  1079. * @param object $obj 对象
  1080. * @return array
  1081. */
  1082. function object_to_array($obj)
  1083. {
  1084. $obj = (array)$obj;
  1085. foreach ($obj as $k => $v) {
  1086. if (gettype($v) == 'resource') {
  1087. return;
  1088. }
  1089. if (gettype($v) == 'object' || gettype($v) == 'array') {
  1090. $obj[$k] = (array)object_to_array($v);
  1091. }
  1092. }
  1093. return $obj;
  1094. }
  1095. /**
  1096. * 检查是否为手机号码
  1097. */
  1098. function _isPhone($number)
  1099. {
  1100. return preg_match("/^1[34578][0-9]{9}$/", $number);
  1101. }
  1102. /**
  1103. * 判断所传的参数是否缺少,如果缺少返回渠道的字段,正确返回0
  1104. *
  1105. * @param array $param
  1106. * @param array $must
  1107. * @return int|mixed
  1108. */
  1109. function checkParam(array $param, array $must)
  1110. {
  1111. foreach ($must as $item) {
  1112. if (array_key_exists($item, $param) && $param[$item] != '') {
  1113. } else {
  1114. return $item;
  1115. }
  1116. }
  1117. return 0;
  1118. }
  1119. /**
  1120. * 对象 转 数组
  1121. *
  1122. * @param object $obj 对象
  1123. * @return array
  1124. */
  1125. function ignoreKeyInArray($targetArray, $delete_keys = [], $changes = [])
  1126. {
  1127. $change_keys = array_keys($changes);
  1128. foreach ($targetArray as $key => $value) {
  1129. if (in_array($key, $delete_keys) && isset($targetArray[$key])) unset($targetArray[$key]);
  1130. if (in_array($key, $change_keys) && isset($targetArray[$key])) $targetArray[$key] = $changes[$key];
  1131. if (is_array($value)) ignoreKeyInArray($value, $delete_keys, $change_keys);
  1132. }
  1133. return $targetArray;
  1134. }
  1135. function itemTransform($trans, $data)
  1136. {
  1137. if ($data) {
  1138. return $trans->transform($data);
  1139. } else {
  1140. return [];
  1141. }
  1142. }
  1143. function collectionTransform($trans, $data)
  1144. {
  1145. $ret_data = [];
  1146. if ($data) {
  1147. foreach ($data as $item) {
  1148. $ret_data[] = $trans->transform($item);
  1149. }
  1150. }
  1151. return $ret_data;
  1152. }
  1153. function paginationTransform($trans, $paginator)
  1154. {
  1155. $ret = [];
  1156. $ret['list'] = [];
  1157. if ($paginator) {
  1158. foreach ($paginator as $item) {
  1159. $ret['list'][] = $trans->transform($item);
  1160. }
  1161. $ret['meta'] = [
  1162. 'total' => (int)$paginator->total(),
  1163. 'per_page' => (int)$paginator->perPage(),
  1164. 'current_page' => (int)$paginator->currentPage(),
  1165. 'last_page' => (int)$paginator->lastPage(),
  1166. 'next_page_url' => (string)$paginator->nextPageUrl(),
  1167. 'prev_page_url' => (string)$paginator->previousPageUrl()
  1168. ];
  1169. }
  1170. return $ret;
  1171. }
  1172. /**
  1173. * 加密site id
  1174. */
  1175. function encodeDistributionChannelId($id)
  1176. {
  1177. $encrypt_pool = [
  1178. ];
  1179. if (isset($encrypt_pool[$id])) {
  1180. return $encrypt_pool[$id];
  1181. }
  1182. $hashids = new \Hashids\Hashids('', 16, 'abcdefghjklmnopqrstuvwxyz1234567890');
  1183. return $hashids->encode($id);
  1184. }
  1185. /**
  1186. * 解密密site id
  1187. */
  1188. function decodeDistributionChannelId($code)
  1189. {
  1190. $encrypt_pool = [
  1191. ];
  1192. if (isset($encrypt_pool[$code])) {
  1193. return $encrypt_pool[$code];
  1194. }
  1195. $hashids = new \Hashids\Hashids('', 16, 'abcdefghjklmnopqrstuvwxyz1234567890');
  1196. $res = $hashids->decode($code);
  1197. if ($res && isset($res[0])) {
  1198. return $res[0];
  1199. }
  1200. return null;
  1201. }
  1202. //bid加密
  1203. function book_hash_encode($bid)
  1204. {
  1205. return Vinkla\Hashids\Facades\Hashids::encode($bid);
  1206. }
  1207. function decodeBid($encode_bid)
  1208. {
  1209. $bid = 0;
  1210. try {
  1211. $bid_arr = \Hashids::decode($encode_bid);
  1212. if (isset($bid_arr[0])) {
  1213. $bid = $bid_arr[0];
  1214. }
  1215. } catch (\Exception $e) {
  1216. return null;
  1217. }
  1218. return $bid;
  1219. }
  1220. /**
  1221. * 获取当前域名
  1222. */
  1223. function _domain()
  1224. {
  1225. return str_replace('https://', '', str_replace('http://', '', url('/')));
  1226. }
  1227. /**
  1228. * 字符串转*
  1229. *
  1230. * @param $str // 待转的字符串
  1231. * @param $start // 转*起始位置
  1232. * @param int $end // 转*结束位置
  1233. * @param string $dot // 转换的字符(必须是单字符,默认是*)
  1234. * @param string $charset // 编码方式
  1235. * @param string $end_char // 特殊字符(碰到此字符则确定end位置)
  1236. * @return string
  1237. */
  1238. function trans_pass($str, $start, $end = 0, $dot = "*", $charset = "UTF-8", $end_char = '@'): string
  1239. {
  1240. $len = mb_strlen($str, $charset);
  1241. if ($start == 0 || $start > $len) {
  1242. $start = 1;
  1243. }
  1244. if ($end != 0 && $end > $len) {
  1245. $end = $len - 2;
  1246. }
  1247. if (strstr($str, $end_char)) {
  1248. $end = $len - strrpos($str, $end_char);
  1249. }
  1250. $endStart = $len - $end;
  1251. $top = mb_substr($str, 0, $start, $charset);
  1252. $bottom = "";
  1253. if ($endStart > 0) {
  1254. $bottom = mb_substr($str, $endStart, $end, $charset);
  1255. }
  1256. $len -= mb_strlen($top, $charset);
  1257. $len -= mb_strlen($bottom, $charset);
  1258. $newStr = $top;
  1259. for ($i = 0; $i < $len; $i++) {
  1260. $newStr .= $dot;
  1261. }
  1262. $newStr .= $bottom;
  1263. return $newStr;
  1264. }
  1265. /**
  1266. * 格式化章节内容
  1267. *
  1268. * @param $content
  1269. * @return false|string
  1270. */
  1271. function formatContent($content)
  1272. {
  1273. if (!$content) return '';
  1274. $content = str_replace(
  1275. ['&nbsp;&nbsp;', '<br /><br />', '<br>', '<br />', '&nbsp;', '<p>', '</p >', '&ldquo;', '&rdquo;', '&hellip;'],
  1276. [' ', PHP_EOL, PHP_EOL, PHP_EOL, ' ', '', PHP_EOL, '“', '”', '...'],
  1277. $content);
  1278. $content = str_replace(["&nbsp;", '&ldquo;', '&hellip;', '&rdquo;', '<p>'], '', $content);
  1279. // 段落首字母前加两个中文空格
  1280. $string = explode(PHP_EOL, $content);
  1281. foreach ($string as $line => $text) {
  1282. if (mb_substr($text, 0, 2) != '  ') $string[$line] = '  ' . $text;
  1283. }
  1284. $content = implode(PHP_EOL, $string);
  1285. $content = mb_convert_encoding($content, 'UTF-8', 'UTF-8,GBK,GB2312');
  1286. $content = iconv('UTF-8', 'UTF-8//IGNORE', $content);
  1287. return $content;
  1288. }
  1289. /**
  1290. * 筛选出有效的id集合
  1291. *
  1292. * @param array $ids
  1293. * @return array
  1294. */
  1295. function filterValidIds(array $ids): array
  1296. {
  1297. // 传参
  1298. if (empty($ids)) {
  1299. return [];
  1300. }
  1301. $result = [];
  1302. foreach ($ids as $id) {
  1303. if (in_array($id, $result) || !is_numeric($id) || (int)$id < 1) {
  1304. continue;
  1305. }
  1306. $result[] = (int)$id;
  1307. }
  1308. return $result;
  1309. }
  1310. function arrayToStr($map)
  1311. {
  1312. $isMap = isArrMap($map);
  1313. $result = "";
  1314. if ($isMap) {
  1315. $result = "map[";
  1316. }
  1317. $keyArr = array_keys($map);
  1318. if ($isMap) {
  1319. sort($keyArr);
  1320. }
  1321. $paramsArr = array();
  1322. foreach ($keyArr as $k) {
  1323. $v = $map[$k];
  1324. if ($isMap) {
  1325. if (is_array($v)) {
  1326. $paramsArr[] = sprintf("%s:%s", $k, arrayToStr($v));
  1327. } else {
  1328. $paramsArr[] = sprintf("%s:%s", $k, trim(strval($v)));
  1329. }
  1330. } else {
  1331. if (is_array($v)) {
  1332. $paramsArr[] = arrayToStr($v);
  1333. } else {
  1334. $paramsArr[] = trim(strval($v));
  1335. }
  1336. }
  1337. }
  1338. $result = sprintf("%s%s", $result, join(" ", $paramsArr));
  1339. if (!$isMap) {
  1340. $result = sprintf("[%s]", $result);
  1341. } else {
  1342. $result = sprintf("%s]", $result);
  1343. }
  1344. return $result;
  1345. }
  1346. function isArrMap($map)
  1347. {
  1348. foreach ($map as $k => $v) {
  1349. if (is_string($k)) {
  1350. return true;
  1351. }
  1352. }
  1353. return false;
  1354. }
  1355. /**
  1356. * 随机字符串
  1357. *
  1358. * @param $length
  1359. * @return string
  1360. */
  1361. function makeRandStr($length): string
  1362. {
  1363. // 密码字符集,可任意添加你需要的字符
  1364. $str = [
  1365. 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
  1366. 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
  1367. 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D',
  1368. 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
  1369. 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
  1370. '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
  1371. ];
  1372. // 在 $str 中随机取 $length 个数组元素键名
  1373. $keys = array_rand($str, $length);
  1374. $password = '';
  1375. for ($i = 0; $i < $length; $i++) {
  1376. // 将 $length 个数组元素连接成字符串
  1377. $password .= $str[$keys[$i]];
  1378. }
  1379. return $password;
  1380. }
  1381. /**
  1382. * 导出数据为excel表格
  1383. *
  1384. * @param $data 一个二维数组,结构如同从数据库查出来的数组
  1385. * @param $title excel的第一行标题,一个数组,如果为空则没有标题
  1386. * @param $filename 下载的文件名
  1387. * @examlpe10
  1388. */
  1389. function exportExcel($data = [], $title = [], $filename = 'report')
  1390. {
  1391. ob_end_clean();
  1392. ob_start();
  1393. header("Content-type:application/octet-stream");
  1394. header("Accept-Ranges:bytes");
  1395. header("Content-type:application/vnd.ms-excel");
  1396. header("Content-Disposition:attachment;filename=" . $filename . ".xls");
  1397. header("Pragma: no-cache");
  1398. header("Expires: 0");
  1399. //导出xls 开始
  1400. if (!empty($title)) {
  1401. foreach ($title as $k => $v) {
  1402. $title[$k] = iconv("UTF-8", "GB2312", $v);
  1403. }
  1404. $title = implode("\t", $title);
  1405. echo "$title\n";
  1406. }
  1407. if (!empty($data)) {
  1408. foreach ($data as $key => $val) {
  1409. foreach ($val as $ck => $cv) {
  1410. $data[$key][$ck] = iconv("UTF-8", "GB2312", $cv);
  1411. }
  1412. $data[$key] = implode("\t", $data[$key]);
  1413. }
  1414. echo implode("\n", $data);
  1415. }
  1416. }
  1417. /**
  1418. * 导出csv文件
  1419. * @param string $name
  1420. * @param array $headers
  1421. * @param array $data
  1422. * @return void
  1423. */
  1424. function exportCsv(string $name, array $headers, array $data = [])
  1425. {
  1426. header('Content-Description: File Transfer');
  1427. header('Content-Type: application/csv');
  1428. header("Content-Disposition: attachment; filename=".$name.".csv");
  1429. header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  1430. $handle = fopen('php://output', 'w');
  1431. ob_clean();
  1432. fputcsv($handle, $headers);
  1433. if ($data) {
  1434. foreach ($data as $row) {
  1435. fputcsv($handle, $row);
  1436. }
  1437. }
  1438. ob_flush();
  1439. fclose($handle);
  1440. die();
  1441. }