Helpers.php 56 KB

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