helpers.php 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: ywl
  5. * Date: 2020/2/26
  6. * Time: 下午4:48
  7. */
  8. /**
  9. * 强转UTF-8
  10. * @param $str
  11. * @return mixed|string
  12. */
  13. function mbEncodingUtf8($str)
  14. {
  15. if (isUTF8($str))
  16. return $str;
  17. else {
  18. return mb_convert_encoding($str, 'UTF-8', 'ascii,GB2312,UTF-8,gbk');
  19. }
  20. }
  21. /**
  22. * http请求
  23. * @param $method
  24. * @param $url
  25. * @param array $params
  26. * @param array $headers
  27. * @param int $timeout
  28. * @return string
  29. * @throws \App\Exceptions\ApiException
  30. */
  31. function httpClient($method, $url, $params = [], $headers = [], $timeout = 0)
  32. {
  33. $curl = new \Curl\Curl();
  34. $curl->setOpt(CURLOPT_FOLLOWLOCATION, 5);
  35. $curl->setUserAgent('Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36');
  36. //https 不验证ssl证书
  37. $curl->setOpt(CURLOPT_SSL_VERIFYPEER, false);
  38. if ($timeout > 0) {
  39. $curl->setOpt(CURLOPT_TIMEOUT, $timeout);
  40. }
  41. if (is_array($headers) && !empty($headers)) {
  42. foreach ($headers as $key => $value) {
  43. $curl->setHeader($key, $value);
  44. }
  45. }
  46. $method = strtolower($method);
  47. $curl->$method($url, $params);
  48. $code = $curl->http_status_code;
  49. $response = $curl->response;
  50. $curl->close();
  51. if ($code != 200) {
  52. throw new \App\Exceptions\ApiException($curl->error_code);
  53. }
  54. return $response;
  55. }
  56. /**
  57. * 对象重组
  58. */
  59. function mapByKey(array $map, $key)
  60. {
  61. $data = [];
  62. foreach ($map as $item) {
  63. if (is_array($item)) {
  64. $data[$item[$key]] = $item;
  65. } else if (is_object($item)) {
  66. $data[$item->$key] = (array)$item;
  67. }
  68. }
  69. return $data;
  70. }
  71. /**
  72. * 当前时间
  73. * @param bool|int $ts
  74. * @return string
  75. */
  76. function nowTime($ts = false)
  77. {
  78. return $ts ? date('Y-m-d H:i:s', $ts) : date('Y-m-d H:i:s');
  79. }
  80. /**
  81. * 验证email格式
  82. * @param $email
  83. * @return bool
  84. */
  85. function validEmail($email)
  86. {
  87. $email = trim($email);
  88. $pattern = "/^[\w\-\.]+@[\w\-\.]+(\.\w+)+$/";
  89. if (preg_match($pattern, $email)) {
  90. return true;
  91. } else {
  92. return false;
  93. }
  94. }
  95. /**
  96. * 加密解密函数
  97. * @param $string
  98. * @param string $operation
  99. * @param string $key
  100. * @param int $expiry
  101. * @return bool|string
  102. */
  103. function authCode($string, $operation = 'DECODE', $key = '', $expiry = 0)
  104. {
  105. $ckey_length = 4;
  106. $key = md5($key ? $key : 'share.matchpages.com');
  107. $keya = md5(substr($key, 0, 16));
  108. $keyb = md5(substr($key, 16, 16));
  109. $keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length) : substr(md5(microtime()), -$ckey_length)) : '';
  110. $cryptkey = $keya . md5($keya . $keyc);
  111. $key_length = strlen($cryptkey);
  112. $string = $operation == 'DECODE' ? base64_decode(substr($string, $ckey_length)) : sprintf('%010d', $expiry ? $expiry + time() : 0) . substr(md5($string . $keyb), 0, 16) . $string;
  113. $string_length = strlen($string);
  114. $result = '';
  115. $box = range(0, 255);
  116. $rndkey = array();
  117. for ($i = 0; $i <= 255; $i++) {
  118. $rndkey[$i] = ord($cryptkey[$i % $key_length]);
  119. }
  120. for ($j = $i = 0; $i < 256; $i++) {
  121. $j = ($j + $box[$i] + $rndkey[$i]) % 256;
  122. $tmp = $box[$i];
  123. $box[$i] = $box[$j];
  124. $box[$j] = $tmp;
  125. }
  126. for ($a = $j = $i = 0; $i < $string_length; $i++) {
  127. $a = ($a + 1) % 256;
  128. $j = ($j + $box[$a]) % 256;
  129. $tmp = $box[$a];
  130. $box[$a] = $box[$j];
  131. $box[$j] = $tmp;
  132. $result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
  133. }
  134. if ($operation == 'DECODE') {
  135. if ((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26) . $keyb), 0, 16)) {
  136. return substr($result, 26);
  137. } else {
  138. return '';
  139. }
  140. } else {
  141. return $keyc . str_replace('=', '', base64_encode($result));
  142. }
  143. }
  144. /**
  145. * html转为text
  146. * @param $str
  147. * @param int $formatText
  148. * @return mixed
  149. */
  150. function html2text($str, $formatText = 1)
  151. {
  152. $str = preg_replace("/<style .*?<\\/style>/is", "", $str);
  153. $str = preg_replace("/<script .*?<\\/script>/is", "", $str);
  154. $str = preg_replace("/<br \\s*\\/>/i", $formatText ? ">>>>" : "", $str);
  155. $str = preg_replace("/<\\/?p>/i", $formatText ? ">>>>" : "", $str);
  156. $str = preg_replace("/<\\/?td>/i", "", $str);
  157. $str = preg_replace("/<\\/?div>/i", $formatText ? ">>>>" : "", $str);
  158. $str = preg_replace("/<\\/?blockquote>/i", "", $str);
  159. $str = preg_replace("/<\\/?li>/i", $formatText ? ">>>>" : "", $str);
  160. $str = preg_replace("/ /i", " ", $str);
  161. $str = preg_replace("/ /i", " ", $str);
  162. $str = preg_replace("/&/i", "&", $str);
  163. $str = preg_replace("/&/i", "&", $str);
  164. $str = preg_replace("/</i", "<", $str);
  165. $str = preg_replace("/</i", "<", $str);
  166. $str = preg_replace("/“/i", '"', $str);
  167. $str = preg_replace("/&ldquo/i", '"', $str);
  168. $str = preg_replace("/‘/i", "'", $str);
  169. $str = preg_replace("/&lsquo/i", "'", $str);
  170. $str = preg_replace("/’/i", "'", $str);
  171. $str = preg_replace("/&rsquo/i", "'", $str);
  172. $str = preg_replace("/>/i", ">", $str);
  173. $str = preg_replace("/>/i", ">", $str);
  174. $str = preg_replace("/”/i", '"', $str);
  175. $str = preg_replace("/&rdquo/i", '"', $str);
  176. $str = strip_tags($str);
  177. $str = html_entity_decode($str, ENT_QUOTES, "utf-8");
  178. $str = preg_replace("/&#.*?;/i", "", $str);
  179. return $str;
  180. }
  181. /**
  182. * 去掉内容中的html代码
  183. */
  184. function strip($str)
  185. {
  186. $str = str_replace("<br>", "", $str);
  187. return strip_tags($str);
  188. }
  189. /**
  190. * 创建多级文件目录
  191. * @param $dir
  192. * @return bool
  193. */
  194. function mkDirs($dir)
  195. {
  196. if (!is_dir($dir)) {
  197. if (!mkdirs(dirname($dir))) {
  198. return false;
  199. }
  200. if (!mkdir($dir, 0777, true)) {
  201. return false;
  202. }
  203. }
  204. return true;
  205. }
  206. /**
  207. * 下载远程文件
  208. * @param $fileUrl
  209. * @param $saveTo
  210. * @param $fileContent
  211. */
  212. function downloadRemoteFile($fileUrl, $saveTo)
  213. {
  214. $ch = curl_init();
  215. curl_setopt($ch, CURLOPT_POST, 0);
  216. curl_setopt($ch, CURLOPT_URL, $fileUrl);
  217. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 信任任何证书
  218. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  219. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  220. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 2);
  221. curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11");
  222. $fileContent = curl_exec($ch);
  223. curl_close($ch);
  224. $downloaded_file = fopen($saveTo, 'w');
  225. fwrite($downloaded_file, $fileContent);
  226. fclose($downloaded_file);
  227. }
  228. /**
  229. * 获取html的meta标签
  230. * @param $link
  231. * @return mixed
  232. */
  233. function getMetaOfLink($link)
  234. {
  235. if (!stristr($link, 'http')) {
  236. $link = 'http://' . $link;
  237. }
  238. $content = httpClient('get', $link);
  239. if (!$content) {
  240. $content = file_get_contents($link);
  241. }
  242. $encode = mb_detect_encoding($content, ['ASCII', 'UTF-8', 'GB2312', 'GBK', 'BIG5']);
  243. $content = iconv($encode, 'utf-8', $content);
  244. $reg = "/<title>(.*?)<\/title>/s";
  245. preg_match($reg, $content, $arr);
  246. if ($arr[1]) $meta['title'] = $arr[1];
  247. preg_match_all("/<meta[^>]+name=\"([^\"]*)\"[^>]" . "+content=\"([^\"]*)\"[^>]*>/i", $content, $r, PREG_PATTERN_ORDER);
  248. for ($i = 0; $i < count($r[1]); $i++) {
  249. if (strtolower($r[1][$i]) == "keywords") $meta['keywords'] = $r[2][$i];
  250. if (strtolower($r[1][$i]) == "description") $meta['description'] = $r[2][$i];
  251. }
  252. return $meta;
  253. }
  254. /**
  255. * 是否为utf8编码
  256. * @param $str
  257. * @return bool
  258. */
  259. function isUTF8($str)
  260. {
  261. $encoding = mb_detect_encoding($str, 'GB2312,UTF-8');
  262. if ($encoding == 'UTF-8') {
  263. return true;
  264. } else {
  265. return false;
  266. }
  267. }
  268. //格式化url
  269. function completeUrl($url)
  270. {
  271. if ($url) {
  272. $url = trim($url);
  273. if ($url[strlen($url) - 1] == '/') {
  274. $url = substr($url, 0, strlen($url) - 1);
  275. }
  276. }
  277. return $url;
  278. }
  279. //去掉http、https以及www
  280. function parseUrl($url)
  281. {
  282. if ($url) {
  283. $url = trim($url);
  284. $reg = '/^((https|http)?:\/\/)/';
  285. $strs = preg_split($reg, $url);
  286. $url = $strs[count($strs) - 1];
  287. if (strpos($url, 'www.') === 0)
  288. $url = substr($url, 4);
  289. }
  290. return $url;
  291. }
  292. /**
  293. * 获取客户端IP地址
  294. * @param integer $type 返回类型 0 返回IP地址 1 返回IPV4地址数字
  295. * @param boolean $adv 是否进行高级模式获取(有可能被伪装)
  296. * @return mixed
  297. */
  298. function getClientIp($type = 0, $adv = false)
  299. {
  300. $type = $type ? 1 : 0;
  301. static $ip = NULL;
  302. if ($ip !== NULL) return $ip[$type];
  303. if ($adv) {
  304. if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  305. $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
  306. $pos = array_search('unknown', $arr);
  307. if (false !== $pos) unset($arr[$pos]);
  308. $ip = trim($arr[0]);
  309. } elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
  310. $ip = $_SERVER['HTTP_CLIENT_IP'];
  311. } elseif (isset($_SERVER['REMOTE_ADDR'])) {
  312. $ip = $_SERVER['REMOTE_ADDR'];
  313. }
  314. } elseif (isset($_SERVER['REMOTE_ADDR'])) {
  315. $ip = $_SERVER['REMOTE_ADDR'];
  316. }
  317. // IP地址合法验证
  318. $long = sprintf("%u", ip2long($ip));
  319. $ip = $long ? array($ip, $long) : array('0.0.0.0', 0);
  320. return $ip[$type];
  321. }
  322. /**
  323. * 其他时区时间转为北京时间 $timezone_time 时区时间,$timezone时区
  324. * @param $timezoneTime
  325. * @param $timezone
  326. * @param int $isDefault
  327. * @return false|string
  328. */
  329. function toTimezone($timezoneTime, $timezone, $isDefault = 0)
  330. {
  331. $beiJin = 8;
  332. if (!$isDefault) {
  333. $diffS = ($beiJin - $timezone) * 3600;//相差秒数
  334. } else {
  335. $diffS = ($timezone - $beiJin) * 3600;//相差秒数
  336. }
  337. $zoneTime = strtotime($timezoneTime);
  338. $time = date('Y-m-d H:i', $zoneTime + $diffS);
  339. return $time;
  340. }
  341. function toTimeAgo($time = '')
  342. {
  343. $currentDate = new DateTime();
  344. // 给定日期
  345. $givenDate = new DateTime($time);
  346. // 计算差值
  347. $interval = $currentDate->diff($givenDate);
  348. // 输出年、月、天
  349. $yearsAgo = $interval->y;
  350. $monthsAgo = $interval->m;
  351. $daysAgo = $interval->d;
  352. // 判断并返回信息
  353. if ($yearsAgo > 0) {
  354. return "Years ago {$yearsAgo}";
  355. } elseif ($monthsAgo > 0) {
  356. return "Months ago {$monthsAgo}";
  357. } elseif ($daysAgo > 0) {
  358. return "Days ago {$daysAgo}";
  359. } else {
  360. return "";
  361. }
  362. }
  363. /**
  364. * 时间显示格式
  365. * @param $theTime
  366. * @param int $isDay
  367. * @return false|string
  368. */
  369. function timeTran($theTime, $isDay = 0)
  370. {
  371. $nowTime = time();
  372. $showTime = $theTime;
  373. $dur = $nowTime - $showTime;
  374. if ($dur < 0) {
  375. return date('Y-m-d H:i:s', $theTime);
  376. } else {
  377. if ($dur < 60) {
  378. return $dur . '秒前';
  379. } else {
  380. if ($dur < 3600) {
  381. return floor($dur / 60) . '分钟前';
  382. } else {
  383. if ($dur < 86400) {
  384. return floor($dur / 3600) . '小时前';
  385. } else {
  386. if ($isDay) {
  387. return floor($dur / 86400) . '天前';
  388. } else {
  389. if ($dur < 86400 * 3) {//3天内
  390. return floor($dur / 86400) . '天前';
  391. } else {
  392. return date('Y-m-d H:i:s', $theTime);
  393. }
  394. }
  395. }
  396. }
  397. }
  398. }
  399. }
  400. /**
  401. * 根据数组某个字段排序
  402. * @param $array 待排序数组
  403. * @param $field 排序字段
  404. * @param bool $desc 是否降序
  405. */
  406. function sortArrByField(&$array, $field, $desc = false)
  407. {
  408. $fieldArr = array();
  409. foreach ($array as $k => $v) {
  410. $fieldArr[$k] = $v[$field];
  411. }
  412. $sort = $desc == false ? SORT_ASC : SORT_DESC;
  413. array_multisort($fieldArr, $sort, $array);
  414. }
  415. function MouthDataTimeArray($start_time, $end_time)
  416. {
  417. $start_date = new DateTime($start_time);
  418. $end_date = new DateTime($end_time);
  419. $start_date_year = $start_date->format('Y');
  420. $end_date_year = $end_date->format('Y');
  421. $start_date_m = $start_date->format('m');
  422. $end_date_m = $end_date->format('m');
  423. if ($start_date_year === $end_date_year && $start_date_m === $end_date_m) {
  424. return ($start_date->format('Y-m-d'));
  425. }
  426. if ($start_date_year === $end_date_year && $start_date_m !== $end_date_m) {
  427. return ($start_date->format('Y.m')) . '~' . ($end_date->format('m'));
  428. }
  429. if ($start_date_year !== $end_date_year) {
  430. return ($start_date->format('Y-m-d')) . '~' . ($end_date->format('Y-m-d'));
  431. }
  432. }
  433. //将查询字符中的特殊查询字符处理掉
  434. function formatQueryKey($key)
  435. {
  436. $key = str_replace('%', '\\%', $key);
  437. $key = str_replace('_', '\\_', $key);
  438. $key = str_replace("'", "\\'", $key);
  439. $key = str_replace('"', '\\"', $key);
  440. if (strstr($key, '[!')) {
  441. $key = str_replace('[!', '\\[\\!', $key);
  442. } elseif (strstr($key, '[^')) {
  443. $key = str_replace('[!', '\\[\\^', $key);
  444. } elseif (strstr($key, '[')) {
  445. $key = str_replace('[!', '\\[', $key);
  446. }
  447. return $key;
  448. }
  449. //图片转base64
  450. function base64EncodeImage($image_file)
  451. {
  452. $base64_image = '';
  453. $image_data = fread(fopen($image_file, 'r'), filesize($image_file));
  454. $base64_image = chunk_split(base64_encode($image_data));
  455. return $base64_image;
  456. }
  457. /*
  458. * 把数组转换成树
  459. */
  460. function listToTree($list, $root = 0, $pk = 'id', $pid = 'pid', $child = '_child')
  461. {
  462. // 创建Tree
  463. $tree = array();
  464. if (is_array($list)) {
  465. // 创建基于主键的数组引用
  466. $refer = array();
  467. foreach ($list as $key => $data) {
  468. $refer[$data[$pk]] = &$list[$key];
  469. }
  470. foreach ($list as $key => $data) {
  471. // 判断是否存在parent
  472. $parentId = 0;
  473. if (isset($data[$pid])) {
  474. $parentId = $data[$pid];
  475. }
  476. if ((string)$root == $parentId) {
  477. $tree[] = &$list[$key];
  478. } else {
  479. if (isset($refer[$parentId])) {
  480. $parent = &$refer[$parentId];
  481. $parent[$child][] = &$list[$key];
  482. } else {
  483. //如果找不到父项那么自己就是父项
  484. $tree[] =& $list[$key];
  485. }
  486. }
  487. }
  488. }
  489. return $tree;
  490. }
  491. /**
  492. * 过滤并返回整形数组
  493. * @param array $arr
  494. */
  495. function filterIntArray($arr)
  496. {
  497. if ($arr) {
  498. foreach ($arr as $k => $v) {
  499. $arr[$k] = (int)$v;
  500. if (!$arr[$k]) {
  501. unset($arr[$k]);
  502. }
  503. }
  504. }
  505. return $arr;
  506. }
  507. //合并查询结果数组(报表用)
  508. /**
  509. * @param $arr_master 主数组
  510. * @param $arr_attendants 要合并的从数组
  511. * @param multilist 是多表合并 默认fase
  512. * @param $key_field 作为判断合并的关键字段(必填)
  513. * @param $merge_fields 不要合并的字段(非必填)
  514. */
  515. function combineArray($arr_master, $arr_attendants = array(), $key_field, $merge_fields = array(), $multilist = false)
  516. {
  517. if (!empty($arr_attendants) && !empty($arr_master) && !empty($key_field)) {
  518. if ($multilist) {
  519. foreach ($arr_attendants as $value) {
  520. $arr_master = combineArray($arr_master, $value, $key_field, $merge_fields);
  521. }
  522. } else {
  523. if (is_array($arr_master)) {
  524. $arr_master = mapByKey($arr_master, $key_field);
  525. $arr_attendants = mapByKey($arr_attendants, $key_field);
  526. foreach ($arr_master as $key => $value) {
  527. if (isset($arr_attendants[$key])) {
  528. if ($merge_fields) {
  529. foreach ($merge_fields as $value) {
  530. unset($arr_attendants[$key][$value]);
  531. }
  532. }
  533. $arr_master[$key] = array_merge($arr_master[$key], $arr_attendants[$key]);
  534. }
  535. }
  536. }
  537. if (is_object($arr_master)) {
  538. $arr_attendants = mapByKey($arr_attendants, $key_field);
  539. foreach ($arr_master as &$value) {
  540. if (isset($arr_attendants[$value[$key_field]])) {
  541. if ($merge_fields) {
  542. foreach ($merge_fields as $value) {
  543. unset($arr_attendants[$value[$key_field]][$value]);
  544. }
  545. }
  546. foreach ($arr_attendants[$value[$key_field]] as $sub_key => $sub_val) {
  547. $value[$sub_key] = $sub_val;
  548. }
  549. }
  550. }
  551. }
  552. }
  553. }
  554. return $arr_master;
  555. }
  556. /**
  557. * 两个标量数组间的array_diff,数组值需为合法的键名
  558. * @param $b
  559. * @param $a
  560. * @return array
  561. */
  562. function flipArrayDiffKey($b, $a)
  563. {
  564. $at = array_flip($a);
  565. $bt = array_flip($b);
  566. $d = array_diff_key($bt, $at);
  567. return array_keys($d);
  568. }
  569. if (!function_exists('ftok')) {
  570. function ftok($filePath, $projectId)
  571. {
  572. $fileStats = stat($filePath);
  573. if (!$fileStats) {
  574. return -1;
  575. }
  576. return sprintf('%u',
  577. ($fileStats['ino'] & 0xffff) | (($fileStats['dev'] & 0xff) << 16) | ((ord($projectId) & 0xff) << 24)
  578. );
  579. }
  580. }
  581. /**
  582. * @param $request_uri
  583. * @return string
  584. */
  585. function currentUrl($request_uri)
  586. {
  587. if ($request_uri && strpos($request_uri, 'http') !== FALSE) {
  588. return $request_uri;
  589. }
  590. $protocol = 'http://';
  591. if (!empty($_SERVER['HTTPS']) || ($_SERVER['HTTP_X_FORWARDED_PROTO'] ?? 'http') === 'https') {
  592. $protocol = 'https://';
  593. }
  594. if (isset($_SERVER['HTTP_HOST'])) {
  595. $ret_url = $protocol . $_SERVER['HTTP_HOST'] . ($request_uri ?: $_SERVER['REQUEST_URI']);
  596. } else {
  597. $ret_url = 'https://api.kaihedh.com' . ($request_uri ?: $_SERVER['REQUEST_URI']);
  598. }
  599. return $ret_url;
  600. }
  601. /**
  602. * 使用方法赋值传递数组[key=>value] ,取值传key值即可
  603. * session
  604. * @param null $option
  605. * @param null $default
  606. * @return mixed
  607. */
  608. function session($option = null, $default = null)
  609. {
  610. $session = app('session');
  611. if (is_null($option)) {
  612. return $session;
  613. }
  614. if (is_array($option)) {
  615. return $session->put($option);
  616. }
  617. return $session->get($option, $default);
  618. }
  619. /**
  620. * 获取随机字符串
  621. * @param $prefix
  622. * @return string
  623. */
  624. function getRandomStr($prefix = 'WX')
  625. {
  626. $addHead = '';
  627. $addTail = '';
  628. $i = 0;
  629. while ($i < 3) {
  630. $addHead .= mt_rand(0, 9);
  631. $addTail .= mt_rand(0, 9);
  632. $i = $i + 1;
  633. }
  634. return $prefix . $addHead . time() . $addTail;
  635. }
  636. /**
  637. * 构造返回页面数据
  638. * @param $data //数据
  639. * @param $formNb //开始数量
  640. * @param $page //页数
  641. * @param $perPageSize //每页数量
  642. * @param $total //总数量
  643. * @return mixed
  644. */
  645. function buildPage($data = [], $formNb = 0, $page = 1, $perPageSize = 10, $total = 0)
  646. {
  647. $lastPage = ceil($total / $perPageSize);
  648. $toNb = $page * $perPageSize;
  649. if (empty($formNb)) {
  650. $formNb = $toNb - $perPageSize;
  651. }
  652. if ($toNb > $total) {
  653. $toNb = $total;
  654. }
  655. $resultDdata = array(
  656. 'current_page' => $page,
  657. 'data' => $data,
  658. 'from' => $formNb,
  659. 'last_page' => $lastPage,
  660. 'per_page' => $perPageSize,
  661. 'to' => $toNb,
  662. 'total' => $total,
  663. );
  664. return $resultDdata;
  665. }
  666. /**
  667. * php实现下载远程图片保存到本地
  668. **
  669. * $url 图片所在地址
  670. * $path 保存图片的路径
  671. * $filename 图片自定义命名
  672. * $type 使用什么方式下载
  673. * 0:curl方式,1:readfile方式,2file_get_contents方式
  674. *
  675. * return 文件名
  676. */
  677. function getFile($url, $path = '', $filename = '', $type = 0)
  678. {
  679. if ($url == '') {
  680. return false;
  681. }
  682. //获取远程文件数据
  683. if ($type === 0) {
  684. $ch = curl_init();
  685. $timeout = 5;
  686. curl_setopt($ch, CURLOPT_URL, $url);
  687. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  688. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);//最长执行时间
  689. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);//最长等待时间
  690. $img = curl_exec($ch);
  691. curl_close($ch);
  692. }
  693. if ($type === 1) {
  694. ob_start();
  695. readfile($url);
  696. $img = ob_get_contents();
  697. ob_end_clean();
  698. }
  699. if ($type === 2) {
  700. $img = file_get_contents($url);
  701. }
  702. //判断下载的数据 是否为空 下载超时问题
  703. if (empty($img)) {
  704. throw new \Exception("下载错误,无法获取下载文件!");
  705. }
  706. //没有指定路径则默认当前路径
  707. if ($path === '') {
  708. $path = "./";
  709. }
  710. //如果命名为空
  711. if ($filename === "") {
  712. $filename = md5($img);
  713. }
  714. //获取后缀名
  715. $ext = substr($url, strrpos($url, '.'));
  716. if ($ext && strlen($ext) < 5) {
  717. $filename .= $ext;
  718. }
  719. //防止"/"没有添加
  720. $path = rtrim($path, "/") . "/";
  721. //var_dump($path.$filename);die();
  722. $fp2 = @fopen($path . $filename, 'a');
  723. fwrite($fp2, $img);
  724. fclose($fp2);
  725. //echo "finish";
  726. return $filename;
  727. }
  728. /**
  729. * 下载文件
  730. * @param $fileUrl 文件远程地址
  731. * @param $saveTo 保存地址
  732. */
  733. function downloadFile($fileUrl, $saveTo)
  734. {
  735. $ch = curl_init();
  736. curl_setopt($ch, CURLOPT_POST, 0);
  737. curl_setopt($ch, CURLOPT_URL, $fileUrl);
  738. // curl_setopt($ch, CURLOPT_PROXY, '127.0.0.1:19180');
  739. // curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
  740. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 信任任何证书
  741. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  742. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  743. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 2);
  744. curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11");
  745. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  746. 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
  747. 'Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
  748. 'Connection: keep-alive'
  749. ));
  750. $fileContent = curl_exec($ch);
  751. curl_close($ch);
  752. $downloaded_file = fopen($saveTo, 'w');
  753. fwrite($downloaded_file, $fileContent);
  754. fclose($downloaded_file);
  755. }
  756. /**
  757. * 居中裁剪图片
  758. * @param string $source [原图路径]
  759. * @param int $width [设置宽度]
  760. * @param int $height [设置高度]
  761. * @param string $target [目标路径]
  762. * @return bool [裁剪结果]
  763. */
  764. function imageCenterCrop($source, $width, $height, $target)
  765. {
  766. //if (!file_exists($source)) return false;
  767. /* 根据类型载入图像 */
  768. switch (exif_imagetype($source)) {
  769. case IMAGETYPE_JPEG:
  770. $image = imagecreatefromjpeg($source);
  771. break;
  772. case IMAGETYPE_PNG:
  773. $image = imagecreatefrompng($source);
  774. break;
  775. case IMAGETYPE_GIF:
  776. $image = imagecreatefromgif($source);
  777. break;
  778. }
  779. if (!isset($image)) return false;
  780. /* 获取图像尺寸信息 */
  781. $target_w = $width;
  782. $target_h = $height;
  783. $source_w = imagesx($image);
  784. $source_h = imagesy($image);
  785. /* 计算裁剪宽度和高度 */
  786. $judge = (($source_w / $source_h) > ($target_w / $target_h));
  787. $resize_w = $judge ? ($source_w * $target_h) / $source_h : $target_w;
  788. $resize_h = !$judge ? ($source_h * $target_w) / $source_w : $target_h;
  789. $start_x = $judge ? ($resize_w - $target_w) / 2 : 0;
  790. $start_y = !$judge ? ($resize_h - $target_h) / 2 : 0;
  791. /* 绘制居中缩放图像 */
  792. $resize_img = imagecreatetruecolor($resize_w, $resize_h);
  793. imagecopyresampled($resize_img, $image, 0, 0, 0, 0, $resize_w, $resize_h, $source_w, $source_h);
  794. $target_img = imagecreatetruecolor($target_w, $target_h);
  795. imagecopy($target_img, $resize_img, 0, 0, $start_x, $start_y, $resize_w, $resize_h);
  796. /* 将图片保存至文件 */
  797. // if (!file_exists(dirname($target))) mkdir(dirname($target), 0777, true);
  798. switch (exif_imagetype($source)) {
  799. case IMAGETYPE_JPEG:
  800. imagejpeg($target_img, $target);
  801. break;
  802. case IMAGETYPE_PNG:
  803. imagepng($target_img, $target);
  804. break;
  805. case IMAGETYPE_GIF:
  806. imagegif($target_img, $target);
  807. break;
  808. }
  809. return boolval(file_exists($target));
  810. }
  811. //增加redis的连接判断,默认如果连接错误会抛出异常,这边捕获异常,下次会重新连接
  812. function checkRedisPing($config = null)
  813. {
  814. if ($config) {
  815. $redis = app('redis')->connection($config);
  816. } else {
  817. $redis = app('redis.connection');
  818. }
  819. try {
  820. $redis->ping();
  821. } catch (\Exception $e) {
  822. }
  823. }
  824. function returnAssetsPath()
  825. {
  826. return config('app.assets_path');
  827. }
  828. function getLanguagePath() {
  829. if (!empty(config('app.language_path')) && config('app.language_path')) {
  830. $language_path = config('app.language_path');
  831. return '/'.$language_path;
  832. }
  833. return '';
  834. }
  835. function getRouterPath($menu_data = [], $router = null): string
  836. {
  837. $language_path = getLanguagePath();
  838. if ($router) {
  839. if ($router === 'home') {
  840. return $language_path.'/';
  841. }
  842. if (empty($menu_data[$router . '.index'])) {
  843. return $language_path.'/' . $router;
  844. } else {
  845. return $language_path.'/' . $menu_data[$router . '.index'];
  846. }
  847. }
  848. return '/';
  849. }
  850. function getMenuRouterActive($list = null)
  851. {
  852. if ($list) {
  853. if (strpos($list, $_SERVER['REQUEST_URI']) && $_SERVER['REQUEST_URI'] !== '/') {
  854. return 'menu_active';
  855. }
  856. }
  857. }
  858. function getRouterActive($menu_data = [], $router = null)
  859. {
  860. $language_path = getLanguagePath();
  861. if ($router) {
  862. if (!empty($menu_data[$router . '.index']) && (urldecode($_SERVER['REQUEST_URI']) === $language_path.'/' . $menu_data[$router . '.index'])) {
  863. return true;
  864. } else {
  865. return false;
  866. }
  867. } else {
  868. return false;
  869. }
  870. }
  871. function getPageValue($page_content, $module_key, $page_key)
  872. {
  873. if (isset($page_content) && isset($page_content[$module_key]['module']) && isset($page_content[$module_key]['module'][$page_key])) {
  874. return $page_content[$module_key]['module'][$page_key]['value'];
  875. }
  876. return '';
  877. }
  878. function getPageItemHref($menu_data, $element_item, $tag_key)
  879. {
  880. $element = $element_item[$tag_key];
  881. $language_path = getLanguagePath();
  882. if (!empty($element['link_type']) && $element['link_type'] === '1') {
  883. if (!empty($element['href'])) {
  884. return $language_path.$element['href'];
  885. }
  886. } elseif (!empty($element['link_type']) && $element['link_type'] === '2') {
  887. return getRouterPath($menu_data, !empty($element['route']) ? $element['route'] : '');
  888. }
  889. return '';
  890. }
  891. function getElementClass($page_content, $module_key)
  892. {
  893. $result = '';
  894. if (isset($page_content) && isset($page_content[$module_key]) && !empty($page_content[$module_key]['class']) && count($page_content[$module_key]['class']) > 0) {
  895. foreach ($page_content[$module_key]['class'] as $item) {
  896. $result .= ' ' . $item;
  897. }
  898. }
  899. return $result;
  900. }
  901. function getElementData($page_content, $module_key, $page_key)
  902. {
  903. if (isset($page_content) && isset($page_content[$module_key]['module']) && isset($page_content[$module_key]['module'][$page_key])) {
  904. return $page_content[$module_key]['module'][$page_key];
  905. }
  906. return '';
  907. }
  908. function getPageContent($page_content, $module_key, $page_key, $type = '')
  909. {
  910. if (isset($page_content) && isset($page_content[$module_key]) && isset($page_content[$module_key]['module'][$page_key])) {
  911. if ($type === 'obj') {
  912. return $page_content[$module_key]['module'][$page_key];
  913. } else {
  914. return $page_content[$module_key]['module'][$page_key]['value'];
  915. }
  916. } else {
  917. return '';
  918. }
  919. }
  920. //适配旧项目方法
  921. function getPageData($page_content,$page_key) {
  922. if (!empty($page_content[$page_key])) {
  923. return $page_content[$page_key];
  924. } else {
  925. return '';
  926. }
  927. }
  928. //适配旧项目方法
  929. function getPageDataValue($page_content,$page_key) {
  930. if (!empty($page_content[$page_key]['value'])) {
  931. return $page_content[$page_key]['value'];
  932. } else {
  933. return '';
  934. }
  935. }
  936. function isShowElement($page_content, $module_key = '', $page_key = ''): bool
  937. {
  938. if (!empty($page_key)) {
  939. $data = getElementData($page_content, $module_key, $page_key);
  940. } else {
  941. $data = $page_content[$module_key] ?? [];
  942. $is_hide = false;
  943. }
  944. if (empty($data)) {
  945. return false;
  946. }
  947. if ((isset($data['is_hide']) && $data['is_hide'] === true) || (!isset($data['module']) && isset($data['value']) && empty($data['value']))) {
  948. $is_hide = true;
  949. } else {
  950. $is_hide = false;
  951. }
  952. return !$is_hide;
  953. }
  954. function isShowListItem($element_content, $page_key = ''): bool
  955. {
  956. $data = $element_content[$page_key];
  957. if (empty($data)) {
  958. return false;
  959. }
  960. if (isset($data['is_hide']) && $data['is_hide'] === true) {
  961. $is_hide = true;
  962. } else {
  963. $is_hide = false;
  964. }
  965. return !$is_hide;
  966. }
  967. function returnElementValue($menu_data, $page_content, $element_key,$tag_key): string {
  968. if (!empty($page_content[$element_key]['module'][$tag_key])) {
  969. $element = $page_content[$element_key]['module'][$tag_key];
  970. } else {
  971. return '';
  972. }
  973. if (!empty($element['value'])) {
  974. return $element['value'];
  975. } else {
  976. return '';
  977. }
  978. }
  979. function returnElementHref($menu_data, $page_content, $element_key,$tag_key): string {
  980. if (!empty($page_content[$element_key]['module'][$tag_key])) {
  981. $element = $page_content[$element_key]['module'][$tag_key];
  982. } else {
  983. return '';
  984. }
  985. if (!empty($element['href'])) {
  986. $href = $element['href'];
  987. } else {
  988. $href = '';
  989. }
  990. if (!empty($element['link_type']) && $element['link_type'] === '2') {
  991. return getRouterPath($menu_data, !empty($element['route']) ? $element['route'] : '');
  992. } else {
  993. return $href;
  994. }
  995. }
  996. function returnElement($menu_data, $content, $element_key, $tag, $class = '',$lazyType = true): string
  997. {
  998. if (!empty($content[$element_key])) {
  999. $element = $content[$element_key];
  1000. } else {
  1001. return '';
  1002. }
  1003. if (!empty($element['is_hide']) && $element['is_hide'] === true) {
  1004. return '';
  1005. }
  1006. if (!empty($element['value'])) {
  1007. $value = $element['value'];
  1008. } else {
  1009. $value = '';
  1010. }
  1011. if (isMediaUrl($value)) {
  1012. $value = extractResourcePath($value);
  1013. }
  1014. if (!empty($element['href'])) {
  1015. $href = $element['href'];
  1016. } else {
  1017. $href = '';
  1018. }
  1019. if (!empty($element['alt'])) {
  1020. $alt = $element['alt'];
  1021. } else {
  1022. $alt = $element_key;
  1023. }
  1024. if (!empty($element['type'])) {
  1025. $type = $element['type'];
  1026. } else {
  1027. $type = '0';
  1028. }
  1029. if ($type === '1') {
  1030. if ($lazyType) {
  1031. $html = '<img class="lazy-load" data-src="' . $value . '" alt="' . $alt . '" />';
  1032. } else {
  1033. $html = '<img src="' . $value . '" alt="' . $alt . '" />';
  1034. }
  1035. } elseif ($type === '4') {
  1036. $html = '<video x5-playsinline="" playsinline="" webkit-playsinline="" src="' . $value . '" />';
  1037. } else {
  1038. $html = $value;
  1039. }
  1040. $element_key_str = '';
  1041. if (!empty($element_key) && isEditWebsite()) {
  1042. $element_key_str = ' data-key=' . $element_key . ' ';
  1043. }
  1044. $element_class_str = '';
  1045. if (!empty($class)) {
  1046. if (!empty($tag) && $tag === 'p') {
  1047. $class .= ' module_desc';
  1048. }
  1049. $element_class_str = ' class="' . $class . '" ';
  1050. } else {
  1051. if (!empty($tag) && $tag === 'p') {
  1052. $element_class_str = ' class="module_desc" ';
  1053. }
  1054. }
  1055. if (!empty($tag) && $tag === 'p') {
  1056. $tag = 'div';
  1057. }
  1058. $element_href = '';
  1059. $link_type_str = '';
  1060. if (!empty($element['link_type']) && $element['link_type'] === '1') {
  1061. $element_href = ' href="' . $href . '" ';
  1062. $link_type_str = 'target="_blank"';
  1063. } elseif (!empty($element['link_type']) && $element['link_type'] === '2') {
  1064. $element_href = ' href="' . getRouterPath($menu_data, !empty($element['route']) ? $element['route'] : '') . '" ';
  1065. }
  1066. if (!empty($element['link_type']) && ($element['link_type'] === '1' || $element['link_type'] === '2')) {
  1067. $html = '<a ' . $element_class_str . $element_href . $link_type_str . $element_key_str . '>' . $html . '</a>';
  1068. } else {
  1069. if (!empty($tag)) {
  1070. $html = '<' . $tag . $element_class_str . $element_href . $element_key_str . ' >' . $html . '</' . $tag . '>';
  1071. }
  1072. }
  1073. return htmlspecialchars_decode($html);
  1074. }
  1075. function isMediaUrl($url)
  1076. {
  1077. $imageExtensions = ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'webp','mp4','avi'];
  1078. // 获取文件扩展名
  1079. $extension = pathinfo(parse_url($url, PHP_URL_PATH), PATHINFO_EXTENSION);
  1080. //不是本地 并且 检查扩展名是否在图片扩展名列表中
  1081. return env('APP_NAME') !== 'website-dev' && in_array(strtolower($extension), $imageExtensions);
  1082. }
  1083. function extractResourcePath($url)
  1084. {
  1085. // 解析 URL
  1086. /* $parsedUrl = parse_url($url);
  1087. // 获取路径部分
  1088. $path = $parsedUrl['path'];
  1089. // 查找 "resources" 的位置
  1090. $resourcesPos = strpos($path, '/resources/');
  1091. // 如果找到了 "resources",提取其后面的部分
  1092. if ($resourcesPos !== false) {
  1093. if (env('WEB_URL')) {
  1094. return env('WEB_URL') . '/resources/'.substr($path, $resourcesPos + strlen('/resources/'));
  1095. } else {
  1096. return '/resources/'.substr($path, $resourcesPos + strlen('/resources/'));
  1097. }
  1098. }*/
  1099. return $url;
  1100. }
  1101. function createElement($menu_data, $data, $module_key, $key, $class, $type, $default_val, $tag_name = '',$lazyType=true)
  1102. {
  1103. if (empty($module_key)) {
  1104. //适配旧项目方法
  1105. $page_content = getPageData($data, $key);
  1106. } else {
  1107. $page_content = getPageContent($data, $module_key, $key, 'obj');
  1108. if (!isShowElement($data, $module_key, $key)) {
  1109. return '';
  1110. }
  1111. }
  1112. if (!empty($page_content['value'])) {
  1113. $value = $page_content['value'];
  1114. } else {
  1115. $value = '';
  1116. }
  1117. if (!empty($page_content['href'])) {
  1118. $href = $page_content['href'];
  1119. } else {
  1120. $href = '';
  1121. }
  1122. if (!empty($page_content['alt'])) {
  1123. $alt = $page_content['alt'];
  1124. } else {
  1125. $alt = $key;
  1126. }
  1127. $element_key_str = '';
  1128. if (!empty($key) && isEditWebsite()) {
  1129. $element_key_str = ' data-key=' . $key . ' ';
  1130. }
  1131. $element_class_str = '';
  1132. if (!empty($class)) {
  1133. if (!empty($tag_name) && $tag_name === 'p') {
  1134. $class .= ' module_desc';
  1135. }
  1136. $element_class_str = ' class="' . $class . '" ';
  1137. } else {
  1138. if (!empty($tag_name) && $tag_name === 'p') {
  1139. $element_class_str = ' class="module_desc" ';
  1140. }
  1141. }
  1142. if (!empty($tag_name) && $tag_name === 'p') {
  1143. $tag_name = 'div';
  1144. }
  1145. $element_href = '';
  1146. $link_type_str = '';
  1147. if (!empty($page_content['link_type']) && $page_content['link_type'] === '1') {
  1148. $element_href = ' href="' . $href . '" ';
  1149. $link_type_str = ' target="_blank"';
  1150. } elseif (!empty($page_content['link_type']) && $page_content['link_type'] === '2') {
  1151. $element_href = ' href="' . getRouterPath($menu_data, !empty($page_content['route']) ? $page_content['route'] : '') . '" ';
  1152. }
  1153. if (isMediaUrl($value)) {
  1154. $value = extractResourcePath($value);
  1155. }
  1156. if ($type === 'image') {
  1157. if ($lazyType) {
  1158. $image_str = '<img class="lazy-load" data-src="' . $value . '" alt="' . $alt . '">';
  1159. } else {
  1160. $image_str = '<img src="' . $value . '" alt="' . $alt . '">';
  1161. }
  1162. if (!empty($element_href)) {
  1163. return '<a ' . $element_class_str . $element_key_str . $element_href . $link_type_str.'><img src="' . $value . '" alt="' . $alt . '">' . $image_str . '</a>';
  1164. } else {
  1165. return '<'.$tag_name . $element_class_str . $element_key_str . '>' . $image_str . '</'.$tag_name.'>';
  1166. }
  1167. } elseif ($type === 'video') {
  1168. if (!empty($element_href)) {
  1169. return '<a ' . $element_class_str . $element_key_str . $element_href .$link_type_str. '><video x5-playsinline="" playsinline="" webkit-playsinline="" src="' . $value . '" /></a>';
  1170. } else {
  1171. return '<' . $tag_name . $element_class_str . $element_key_str . '><video x5-playsinline="" playsinline="" webkit-playsinline="" src="' . $value . '" /></' . $tag_name . '>';
  1172. }
  1173. } elseif ($type === 'text') {
  1174. if (!empty($element_href)) {
  1175. if ($tag_name === 'h2') {
  1176. return '<h2><a ' . $element_class_str . $element_key_str . $element_href .$link_type_str. '>'. $value . '</a></h2>';
  1177. } else {
  1178. return '<a ' . $element_class_str . $element_key_str . $element_href .$link_type_str. '>'. $value . '</a>';
  1179. }
  1180. } else {
  1181. return htmlspecialchars_decode('<' . $tag_name . $element_class_str . $element_key_str . ' >' . $value . '</' . $tag_name . '>');
  1182. }
  1183. } else {
  1184. return $value;
  1185. }
  1186. }
  1187. /**
  1188. * 汉语 - 给用户自动生成昵称
  1189. * @param int $type 1生成昵称,2生成姓名
  1190. * @return mixed|string
  1191. */
  1192. if (!function_exists('randNickName')) {
  1193. function randNickName($type = 1)
  1194. {
  1195. /**
  1196. * 随机昵称 形容词
  1197. */
  1198. $nick_name_tou = ['迷你的', '鲜艳的', '飞快的', '真实的', '清新的', '幸福的', '可耐的', '快乐的', '冷静的', '醉熏的', '潇洒的', '糊涂的', '积极的', '冷酷的', '深情的', '粗暴的',
  1199. '温柔的', '可爱的', '愉快的', '义气的', '认真的', '威武的', '帅气的', '传统的', '潇洒的', '漂亮的', '自然的', '专一的', '听话的', '昏睡的', '狂野的', '等待的', '搞怪的',
  1200. '幽默的', '魁梧的', '活泼的', '开心的', '高兴的', '超帅的', '留胡子的', '坦率的', '直率的', '轻松的', '痴情的', '完美的', '精明的', '无聊的', '有魅力的', '丰富的', '繁荣的',
  1201. '饱满的', '炙热的', '暴躁的', '碧蓝的', '俊逸的', '英勇的', '健忘的', '故意的', '无心的', '土豪的', '朴实的', '兴奋的', '幸福的', '淡定的', '不安的', '阔达的', '孤独的',
  1202. '独特的', '疯狂的', '时尚的', '落后的', '风趣的', '忧伤的', '大胆的', '爱笑的', '矮小的', '健康的', '合适的', '玩命的', '沉默的', '斯文的', '香蕉', '苹果', '鲤鱼', '鳗鱼',
  1203. '任性的', '细心的', '粗心的', '大意的', '甜甜的', '酷酷的', '健壮的', '英俊的', '霸气的', '阳光的', '默默的', '大力的', '孝顺的', '忧虑的', '着急的', '紧张的', '善良的',
  1204. '凶狠的', '害怕的', '重要的', '危机的', '欢喜的', '欣慰的', '满意的', '跳跃的', '诚心的', '称心的', '如意的', '怡然的', '娇气的', '无奈的', '无语的', '激动的', '愤怒的',
  1205. '美好的', '感动的', '激情的', '激昂的', '震动的', '虚拟的', '超级的', '寒冷的', '精明的', '明理的', '犹豫的', '忧郁的', '寂寞的', '奋斗的', '勤奋的', '现代的', '过时的',
  1206. '稳重的', '热情的', '含蓄的', '开放的', '无辜的', '多情的', '纯真的', '拉长的', '热心的', '从容的', '体贴的', '风中的', '曾经的', '追寻的', '儒雅的', '优雅的', '开朗的',
  1207. '外向的', '内向的', '清爽的', '文艺的', '长情的', '平常的', '单身的', '伶俐的', '高大的', '懦弱的', '柔弱的', '爱笑的', '乐观的', '耍酷的', '酷炫的', '神勇的', '年轻的',
  1208. '唠叨的', '瘦瘦的', '无情的', '包容的', '顺心的', '畅快的', '舒适的', '靓丽的', '负责的', '背后的', '简单的', '谦让的', '彩色的', '缥缈的', '欢呼的', '生动的', '复杂的',
  1209. '慈祥的', '仁爱的', '魔幻的', '虚幻的', '淡然的', '受伤的', '雪白的', '高高的', '糟糕的', '顺利的', '闪闪的', '羞涩的', '缓慢的', '迅速的', '优秀的', '聪明的', '含糊的',
  1210. '俏皮的', '淡淡的', '坚强的', '平淡的', '欣喜的', '能干的', '灵巧的', '友好的', '机智的', '机灵的', '正直的', '谨慎的', '俭朴的', '殷勤的', '虚心的', '辛勤的', '自觉的',
  1211. '无私的', '无限的', '踏实的', '老实的', '现实的', '可靠的', '务实的', '拼搏的', '个性的', '粗犷的', '活力的', '成就的', '勤劳的', '单纯的', '落寞的', '朴素的', '悲凉的',
  1212. '忧心的', '洁净的', '清秀的', '自由的', '小巧的', '单薄的', '贪玩的', '刻苦的', '干净的', '壮观的', '和谐的', '文静的', '调皮的', '害羞的', '安详的', '自信的', '端庄的',
  1213. '坚定的', '美满的', '舒心的', '温暖的', '专注的', '勤恳的', '美丽的', '腼腆的', '优美的', '甜美的', '甜蜜的', '整齐的', '动人的', '典雅的', '尊敬的', '舒服的', '妩媚的',
  1214. '秀丽的', '喜悦的', '甜美的', '彪壮的', '强健的', '大方的', '俊秀的', '聪慧的', '迷人的', '陶醉的', '悦耳的', '动听的', '明亮的', '结实的', '魁梧的', '标致的', '清脆的',
  1215. '敏感的', '光亮的', '大气的', '老迟到的', '知性的', '冷傲的', '呆萌的', '野性的', '隐形的', '笑点低的', '微笑的', '笨笨的', '难过的', '沉静的', '火星上的', '失眠的',
  1216. '安静的', '纯情的', '要减肥的', '迷路的', '烂漫的', '哭泣的', '贤惠的', '苗条的', '温婉的', '发嗲的', '会撒娇的', '贪玩的', '执着的', '眯眯眼的', '花痴的', '想人陪的',
  1217. '眼睛大的', '高贵的', '傲娇的', '心灵美的', '爱撒娇的', '细腻的', '天真的', '怕黑的', '感性的', '飘逸的', '怕孤独的', '忐忑的', '高挑的', '傻傻的', '冷艳的', '爱听歌的',
  1218. '还单身的', '怕孤单的', '懵懂的'];
  1219. $nick_name_wei = ['嚓茶', '皮皮虾', '皮卡丘', '马里奥', '小霸王', '凉面', '便当', '毛豆', '花生', '可乐', '灯泡', '哈密瓜', '野狼', '背包', '眼神', '缘分', '雪碧', '人生', '牛排',
  1220. '蚂蚁', '飞鸟', '灰狼', '斑马', '汉堡', '悟空', '巨人', '绿茶', '自行车', '保温杯', '大碗', '墨镜', '魔镜', '煎饼', '月饼', '月亮', '星星', '芝麻', '啤酒', '玫瑰',
  1221. '大叔', '小伙', '哈密瓜,数据线', '太阳', '树叶', '芹菜', '黄蜂', '蜜粉', '蜜蜂', '信封', '西装', '外套', '裙子', '大象', '猫咪', '母鸡', '路灯', '蓝天', '白云',
  1222. '星月', '彩虹', '微笑', '摩托', '板栗', '高山', '大地', '大树', '电灯胆', '砖头', '楼房', '水池', '鸡翅', '蜻蜓', '红牛', '咖啡', '机器猫', '枕头', '大船', '诺言',
  1223. '钢笔', '刺猬', '天空', '飞机', '大炮', '冬天', '洋葱', '春天', '夏天', '秋天', '冬日', '航空', '毛衣', '豌豆', '黑米', '玉米', '眼睛', '老鼠', '白羊', '帅哥', '美女',
  1224. '季节', '鲜花', '服饰', '裙子', '白开水', '秀发', '大山', '火车', '汽车', '歌曲', '舞蹈', '老师', '导师', '方盒', '大米', '麦片', '水杯', '水壶', '手套', '鞋子', '自行车',
  1225. '鼠标', '手机', '电脑', '书本', '奇迹', '身影', '香烟', '夕阳', '台灯', '宝贝', '未来', '皮带', '钥匙', '心锁', '故事', '花瓣', '滑板', '画笔', '画板', '学姐', '店员',
  1226. '电源', '饼干', '宝马', '过客', '大白', '时光', '石头', '钻石', '河马', '犀牛', '西牛', '绿草', '抽屉', '柜子', '往事', '寒风', '路人', '橘子', '耳机', '鸵鸟', '朋友',
  1227. '苗条', '铅笔', '钢笔', '硬币', '热狗', '大侠', '御姐', '萝莉', '毛巾', '期待', '盼望', '白昼', '黑夜', '大门', '黑裤', '钢铁侠', '哑铃', '板凳', '枫叶', '荷花', '乌龟',
  1228. '仙人掌', '衬衫', '大神', '草丛', '早晨', '心情', '茉莉', '流沙', '蜗牛', '战斗机', '冥王星', '猎豹', '棒球', '篮球', '乐曲', '电话', '网络', '世界', '中心', '鱼', '鸡', '狗',
  1229. '老虎', '鸭子', '雨', '羽毛', '翅膀', '外套', '火', '丝袜', '书包', '钢笔', '冷风', '八宝粥', '烤鸡', '大雁', '音响', '招牌', '胡萝卜', '冰棍', '帽子', '菠萝', '蛋挞', '香水',
  1230. '泥猴桃', '吐司', '溪流', '黄豆', '樱桃', '小鸽子', '小蝴蝶', '爆米花', '花卷', '小鸭子', '小海豚', '日记本', '小熊猫', '小懒猪', '小懒虫', '荔枝', '镜子', '曲奇', '金针菇',
  1231. '小松鼠', '小虾米', '酒窝', '紫菜', '金鱼', '柚子', '果汁', '百褶裙', '项链', '帆布鞋', '火龙果', '奇异果', '煎蛋', '唇彩', '小土豆', '高跟鞋', '戒指', '雪糕', '睫毛', '铃铛',
  1232. '手链', '香氛', '红酒', '月光', '酸奶', '银耳汤', '咖啡豆', '小蜜蜂', '小蚂蚁', '蜡烛', '棉花糖', '向日葵', '水蜜桃', '小蝴蝶', '小刺猬', '小丸子', '指甲油', '康乃馨', '糖豆',
  1233. '薯片', '口红', '超短裙', '乌冬面', '冰淇淋', '棒棒糖', '长颈鹿', '豆芽', '发箍', '发卡', '发夹', '发带', '铃铛', '小馒头', '小笼包', '小甜瓜', '冬瓜', '香菇', '小兔子',
  1234. '含羞草', '短靴', '睫毛膏', '小蘑菇', '跳跳糖', '小白菜', '草莓', '柠檬', '月饼', '百合', '纸鹤', '小天鹅', '云朵', '芒果', '面包', '海燕', '小猫咪', '龙猫', '唇膏', '鞋垫',
  1235. '羊', '黑猫', '白猫', '万宝路', '金毛', '山水', '音响', '纸飞机', '烧鹅'];
  1236. /**
  1237. * 百家姓
  1238. */
  1239. $arrXing = ['赵', '钱', '孙', '李', '周', '吴', '郑', '王', '冯', '陈', '褚', '卫', '蒋', '沈', '韩', '杨', '朱', '秦', '尤', '许', '何', '吕', '施', '张', '孔', '曹', '严', '华', '金', '魏', '陶', '姜', '戚', '谢', '邹',
  1240. '喻', '柏', '水', '窦', '章', '云', '苏', '潘', '葛', '奚', '范', '彭', '郎', '鲁', '韦', '昌', '马', '苗', '凤', '花', '方', '任', '袁', '柳', '鲍', '史', '唐', '费', '薛', '雷', '贺', '倪', '汤', '滕', '殷', '罗',
  1241. '毕', '郝', '安', '常', '傅', '卞', '齐', '元', '顾', '孟', '平', '黄', '穆', '萧', '尹', '姚', '邵', '湛', '汪', '祁', '毛', '狄', '米', '伏', '成', '戴', '谈', '宋', '茅', '庞', '熊', '纪', '舒', '屈', '项', '祝',
  1242. '董', '梁', '杜', '阮', '蓝', '闵', '季', '贾', '路', '娄', '江', '童', '颜', '郭', '梅', '盛', '林', '钟', '徐', '邱', '骆', '高', '夏', '蔡', '田', '樊', '胡', '凌', '霍', '虞', '万', '支', '柯', '管', '卢', '莫',
  1243. '柯', '房', '裘', '缪', '解', '应', '宗', '丁', '宣', '邓', '单', '杭', '洪', '包', '诸', '左', '石', '崔', '吉', '龚', '程', '嵇', '邢', '裴', '陆', '荣', '翁', '荀', '于', '惠', '甄', '曲', '封', '储', '仲', '伊',
  1244. '宁', '仇', '甘', '武', '符', '刘', '景', '詹', '龙', '叶', '幸', '司', '黎', '溥', '印', '怀', '蒲', '邰', '从', '索', '赖', '卓', '屠', '池', '乔', '胥', '闻', '莘', '党', '翟', '谭', '贡', '劳', '逄', '姬', '申',
  1245. '扶', '堵', '冉', '宰', '雍', '桑', '寿', '通', '燕', '浦', '尚', '农', '温', '别', '庄', '晏', '柴', '瞿', '阎', '连', '习', '容', '向', '古', '易', '廖', '庾', '终', '步', '都', '耿', '满', '弘', '匡', '国', '文',
  1246. '寇', '广', '禄', '阙', '东', '欧', '利', '师', '巩', '聂', '关', '荆', '司马', '上官', '欧阳', '夏侯', '诸葛', '闻人', '东方', '赫连', '皇甫', '尉迟', '公羊', '澹台', '公冶', '宗政', '濮阳', '淳于', '单于', '太叔',
  1247. '申屠', '公孙', '仲孙', '轩辕', '令狐', '徐离', '宇文', '长孙', '慕容', '司徒', '司空', '皮'];
  1248. /**
  1249. * 名
  1250. */
  1251. $arrMing = ['伟', '刚', '勇', '毅', '俊', '峰', '强', '军', '平', '保', '东', '文', '辉', '力', '明', '永', '健', '世', '广', '志', '义', '兴', '良', '海', '山', '仁', '波', '宁', '贵', '福', '生', '龙', '元', '全'
  1252. , '国', '胜', '学', '祥', '才', '发', '武', '新', '利', '清', '飞', '彬', '富', '顺', '信', '子', '杰', '涛', '昌', '成', '康', '星', '光', '天', '达', '安', '岩', '中', '茂', '进', '林', '有', '坚', '和', '彪', '博', '诚'
  1253. , '先', '敬', '震', '振', '壮', '会', '思', '群', '豪', '心', '邦', '承', '乐', '绍', '功', '松', '善', '厚', '庆', '磊', '民', '友', '裕', '河', '哲', '江', '超', '浩', '亮', '政', '谦', '亨', '奇', '固', '之', '轮', '翰'
  1254. , '朗', '伯', '宏', '言', '若', '鸣', '朋', '斌', '梁', '栋', '维', '启', '克', '伦', '翔', '旭', '鹏', '泽', '晨', '辰', '士', '以', '建', '家', '致', '树', '炎', '德', '行', '时', '泰', '盛', '雄', '琛', '钧', '冠', '策'
  1255. , '腾', '楠', '榕', '风', '航', '弘', '秀', '娟', '英', '华', '慧', '巧', '美', '娜', '静', '淑', '惠', '珠', '翠', '雅', '芝', '玉', '萍', '红', '娥', '玲', '芬', '芳', '燕', '彩', '春', '菊', '兰', '凤', '洁', '梅', '琳'
  1256. , '素', '云', '莲', '真', '环', '雪', '荣', '爱', '妹', '霞', '香', '月', '莺', '媛', '艳', '瑞', '凡', '佳', '嘉', '琼', '勤', '珍', '贞', '莉', '桂', '娣', '叶', '璧', '璐', '娅', '琦', '晶', '妍', '茜', '秋', '珊', '莎'
  1257. , '锦', '黛', '青', '倩', '婷', '姣', '婉', '娴', '瑾', '颖', '露', '瑶', '怡', '婵', '雁', '蓓', '纨', '仪', '荷', '丹', '蓉', '眉', '君', '琴', '蕊', '薇', '菁', '梦', '岚', '苑', '婕', '馨', '瑗', '琰', '韵', '融', '园'
  1258. , '艺', '咏', '卿', '聪', '澜', '纯', '毓', '悦', '昭', '冰', '爽', '琬', '茗', '羽', '希', '欣', '飘', '育', '滢', '馥', '筠', '柔', '竹', '霭', '凝', '晓', '欢', '霄', '枫', '芸', '菲', '寒', '伊', '亚', '宜', '可', '姬'
  1259. , '舒', '影', '荔', '枝', '丽', '阳', '妮', '宝', '贝', '初', '程', '梵', '罡', '恒', '鸿', '桦', '骅', '剑', '娇', '纪', '宽', '苛', '灵', '玛', '媚', '琪', '晴', '容', '睿', '烁', '堂', '唯', '威', '韦', '雯', '苇', '萱'
  1260. , '阅', '彦', '宇', '雨', '洋', '忠', '宗', '曼', '紫', '逸', '贤', '蝶', '菡', '绿', '蓝', '儿', '翠', '烟'];
  1261. $nick_name = '';
  1262. switch ($type) {
  1263. case 1:
  1264. $tou_num = rand(0, count($nick_name_tou) - 1);
  1265. $wei_num = rand(0, count($nick_name_wei) - 1);
  1266. $nick_name = $nick_name_tou[$tou_num] . $nick_name_wei[$wei_num];
  1267. break;
  1268. case 2:
  1269. $nick_name = $arrXing[mt_rand(0, count($arrXing) - 1)];
  1270. for ($i = 1; $i <= 3; $i++) {
  1271. $nick_name .= (mt_rand(0, 1) ? $arrMing[mt_rand(0, count($arrMing) - 1)] : $arrMing[mt_rand(0, count($arrMing) - 1)]);
  1272. }
  1273. break;
  1274. case 3:
  1275. $nick_name = rand(0, count($nick_name_tou) - 1);
  1276. for ($i = 1; $i <= 3; $i++) {
  1277. $nick_name .= (mt_rand(0, 1) ? $arrMing[mt_rand(0, count($arrMing) - 1)] : $arrMing[mt_rand(0, count($arrMing) - 1)]);
  1278. }
  1279. break;
  1280. }
  1281. return $nick_name;
  1282. }
  1283. }
  1284. if (!function_exists('getExtFromUrl')) {
  1285. function getExtFromUrl($url)
  1286. {
  1287. $ext = null;
  1288. if (filter_var($url, FILTER_VALIDATE_URL) !== false) {
  1289. $headers = get_headers($url);
  1290. foreach ($headers as $header) {
  1291. if (preg_match('/Content-Type: (.*)/', $header, $match) && !empty($match[1])) {
  1292. $matchArr = explode('/', $match[1]);
  1293. if (!empty($matchArr[1])) {
  1294. $ext = $matchArr[1];
  1295. }
  1296. break;
  1297. }
  1298. }
  1299. }
  1300. if (empty($ext)) {
  1301. $urlArr = parse_url($url);
  1302. if (!empty($urlArr['scheme']) && !empty($urlArr['host'])) {
  1303. $pureUrl = $urlArr['scheme'] . '://' . $urlArr['host'] . $urlArr['path'];
  1304. $ext = pathinfo($pureUrl, PATHINFO_EXTENSION);
  1305. }
  1306. }
  1307. return $ext;
  1308. }
  1309. }
  1310. function isEditWebsite()
  1311. {
  1312. // 获取当前页面的URL
  1313. $current_url = "$_SERVER[REQUEST_URI]";
  1314. $needle = "website_type=edit";
  1315. if (strpos($current_url, $needle) !== false) {
  1316. return true;
  1317. } else {
  1318. return false;
  1319. }
  1320. }
  1321. function returnDataModule($code,$dataType='data-module'): string
  1322. {
  1323. if (isEditWebsite()) {
  1324. return $dataType.'=' . $code;
  1325. } else {
  1326. return '';
  1327. }
  1328. }
  1329. //$format : Y=年 m=月 d=日
  1330. function returnDateFormat($lang, $date, $format = 'Y-m-d') {
  1331. // 验证输入日期是否有效
  1332. if (!strtotime($date)) {
  1333. return '';
  1334. // throw new InvalidArgumentException("Invalid date format: $date");
  1335. }
  1336. // 处理中文和日文语言
  1337. if ($lang === 'zh-cn' || $lang === 'ja-jp' || $lang === 'ja') {
  1338. switch ($format) {
  1339. case 'Y':
  1340. return date("Y", strtotime($date));
  1341. case 'm':
  1342. return date("m", strtotime($date));
  1343. case 'd':
  1344. return date("d", strtotime($date));
  1345. case 'Y-m':
  1346. return date("Y年m月", strtotime($date));
  1347. case 'm-d':
  1348. return date("m月d日", strtotime($date));
  1349. case 'Y-m-d':
  1350. default:
  1351. return date("Y年m月d日", strtotime($date));
  1352. }
  1353. }
  1354. // 创建 DateTime 对象
  1355. $date_obj = new DateTime($date);
  1356. // 处理其他语言
  1357. switch ($format) {
  1358. case 'Y':
  1359. return $date_obj->format('Y');
  1360. case 'm':
  1361. return $date_obj->format('F');
  1362. case 'Y-m':
  1363. return $date_obj->format('F, Y');
  1364. case 'm-d':
  1365. return $date_obj->format('F j');
  1366. case 'd':
  1367. return $date_obj->format('j');
  1368. case 'Y-m-d':
  1369. default:
  1370. return $date_obj->format('F j, Y');
  1371. }
  1372. }
  1373. function returnTimeFormat($start_time, $end_time, $lang = 'zh-cn'): string
  1374. {
  1375. // 创建 DateTime 对象
  1376. $start_date = new DateTime($start_time);
  1377. $end_date = new DateTime($end_time);
  1378. // 判断日期关系
  1379. $is_same_day = $start_date->format('Y-m-d') === $end_date->format('Y-m-d');
  1380. $is_same_month = $start_date->format('Y-m') === $end_date->format('Y-m');
  1381. $is_same_year = $start_date->format('Y') === $end_date->format('Y');
  1382. // 根据语言选择格式化模板
  1383. $templates = [
  1384. 'zh-cn' => [
  1385. 'same_day' => 'Y年m月d日 H:i - H:i',
  1386. 'same_month' => 'Y年m月d日 H:i - m月d日 H:i',
  1387. 'same_year' => 'Y年m月d日 H:i - m月d日 H:i',
  1388. 'different' => 'Y年m月d日 H:i - Y年m月d日 H:i'
  1389. ],
  1390. 'ja-jp' => [
  1391. 'same_day' => 'Y年m月d日 H:i - H:i',
  1392. 'same_month' => 'Y年m月d日 H:i - m月d日 H:i',
  1393. 'same_year' => 'Y年m月d日 H:i - m月d日 H:i',
  1394. 'different' => 'Y年m月d日 H:i - Y年m月d日 H:i'
  1395. ],
  1396. 'ja' => [
  1397. 'same_day' => 'Y年m月d日 H:i - H:i',
  1398. 'same_month' => 'Y年m月d日 H:i - m月d日 H:i',
  1399. 'same_year' => 'Y年m月d日 H:i - m月d日 H:i',
  1400. 'different' => 'Y年m月d日 H:i - Y年m月d日 H:i'
  1401. ],
  1402. 'default' => [
  1403. 'same_day' => 'l, F j, Y g:i A - g:i A',
  1404. 'same_month' => 'l, F j, Y g:i A - l, j g:i A',
  1405. 'same_year' => 'l, F j, Y g:i A - l, F j g:i A',
  1406. 'different' => 'l, F j, Y g:i A - l, F j, Y g:i A'
  1407. ]
  1408. ];
  1409. // 获取当前语言的模板
  1410. $current_templates = $templates[$lang] ?? $templates['default'];
  1411. // 根据日期关系选择模板
  1412. if ($is_same_day) {
  1413. $format = $current_templates['same_day'];
  1414. } elseif ($is_same_month) {
  1415. $format = $current_templates['same_month'];
  1416. } elseif ($is_same_year) {
  1417. $format = $current_templates['same_year'];
  1418. } else {
  1419. $format = $current_templates['different'];
  1420. }
  1421. // 格式化开始日期
  1422. $start_formatted = $start_date->format(substr($format, 0, strpos($format, ' - ')));
  1423. // 格式化结束日期
  1424. $end_formatted = $end_date->format(substr($format, strpos($format, ' - ') + 3));
  1425. // 返回最终结果
  1426. return $start_formatted . ' - ' . $end_formatted;
  1427. }
  1428. function returnConciseFormat($start_time, $end_time): string
  1429. {
  1430. // 创建DateTime对象
  1431. $start_date = new DateTime($start_time);
  1432. $end_date = new DateTime($end_time);
  1433. // 格式化输出
  1434. return $start_date->format('F j') . '-' . $end_date->format('j, Y');
  1435. }
  1436. function returnUrlParams($name) {
  1437. return isset($_GET[$name]) ? $_GET[$name] : null;
  1438. }
  1439. function initPageParams($page=1,$page_size=10,$keyword='')
  1440. {
  1441. if (!empty($_GET['page'])) {
  1442. $page = $_GET['page'];
  1443. }
  1444. if (!empty($_GET['page_size'])) {
  1445. $page_size = $_GET['page_size'];
  1446. }
  1447. if (!empty($_GET['keyword'])) {
  1448. $keyword = $_GET['keyword'];
  1449. }
  1450. return [
  1451. 'page' => $page,
  1452. 'page_size' => $page_size,
  1453. 'keyword' => $keyword,
  1454. ];
  1455. }
  1456. function isMobile() {
  1457. // 获取用户代理信息
  1458. $userAgent = $_SERVER['HTTP_USER_AGENT'];
  1459. // 定义常见的移动设备关键词
  1460. $mobileKeywords = [
  1461. 'Android', 'iPhone', 'iPod', 'iPad', 'Windows Phone',
  1462. 'BlackBerry', 'Opera Mini', 'IEMobile', 'Mobile', 'Touch'
  1463. ];
  1464. // 检查用户代理中是否包含任何移动关键词
  1465. foreach ($mobileKeywords as $keyword) {
  1466. if (stripos($userAgent, $keyword) !== false) {
  1467. return true;
  1468. }
  1469. }
  1470. // 如果没有找到任何移动关键词,则认为是桌面端
  1471. return false;
  1472. }
  1473. function isIframeUrl($string) {
  1474. // 正则表达式匹配iframe标签及其中的src属性
  1475. $pattern = '/<iframe\s+src=["\']([^"\']+)["\']/i';
  1476. preg_match($pattern, $string, $matches);
  1477. // 如果匹配成功,说明字符串中包含iframe地址
  1478. if (!empty($matches)) {
  1479. // $matches[1]将包含src属性中的地址
  1480. return true;
  1481. }
  1482. return false;
  1483. }
  1484. function createGuid()
  1485. {
  1486. $charid = strtoupper(md5(uniqid(mt_rand(), true)));
  1487. $hyphen = chr(45);// "-"
  1488. $uuid = chr(123)// "{"
  1489. . substr($charid, 0, 8) . $hyphen
  1490. . substr($charid, 8, 4) . $hyphen
  1491. . substr($charid, 12, 4) . $hyphen
  1492. . substr($charid, 16, 4) . $hyphen
  1493. . substr($charid, 20, 12)
  1494. . chr(125);// "}"
  1495. return $uuid;
  1496. }
  1497. function getFileName($url) {
  1498. // 使用 parse_url 解析 URL
  1499. $parsedUrl = parse_url($url);
  1500. // 获取路径部分
  1501. $path = $parsedUrl['path'];
  1502. // 使用 basename 提取文件名
  1503. $filename = basename($path);
  1504. return $filename;
  1505. }
  1506. function commonWords($text,$lang='zh-cn')
  1507. {
  1508. if ($lang === 'zh-cn') {
  1509. return $text;
  1510. }
  1511. if ($text === '了解更多') {
  1512. switch ($lang) {
  1513. case 'en-us':
  1514. return 'Learn More';
  1515. case 'ja':
  1516. return 'もっと詳しく';
  1517. case 'es':
  1518. return 'Más información';
  1519. case 'fr':
  1520. return 'En savoir plus';
  1521. case 'de':
  1522. return 'Mehr erfahren';
  1523. case 'it':
  1524. return 'Scopri di più';
  1525. default:
  1526. return $text; // 如果没有匹配的语言,返回原始文本
  1527. }
  1528. }
  1529. if ($text === '了解详情') {
  1530. switch ($lang) {
  1531. case 'en-us':
  1532. return 'Learn Details';
  1533. case 'ja':
  1534. return '詳細を知る';
  1535. case 'es':
  1536. return 'Más detalles';
  1537. case 'fr':
  1538. return 'En savoir les détails';
  1539. case 'de':
  1540. return 'Details erfahren';
  1541. case 'it':
  1542. return 'Scopri i dettagli';
  1543. default:
  1544. return $text; // 如果没有匹配的语言,返回原始文本
  1545. }
  1546. }
  1547. return $text; // 如果没有匹配的文本,返回原始文本
  1548. }
  1549. function quarterDate($text, $lang)
  1550. {
  1551. if (strpos($text, '-') !== false) {
  1552. $mapZh = array(
  1553. '1-3' => '1-3月',
  1554. '4-6' => '4-6月',
  1555. '7-9' => '7-9月',
  1556. '10-12' => '10-12月'
  1557. );
  1558. $monthMap = array(
  1559. '1-3' => 'from January to March',
  1560. '4-6' => 'from April to June',
  1561. '7-9' => 'from July to September',
  1562. '10-12' => 'from October to December'
  1563. );
  1564. if ($lang === 'zh-cn') {
  1565. return $mapZh[$text] ?? $text;
  1566. } else {
  1567. return $monthMap[$text] ?? $text;
  1568. }
  1569. } else {
  1570. if ($lang === 'zh-cn') {
  1571. return $text . '年';
  1572. } else {
  1573. return $text;
  1574. }
  1575. }
  1576. }
  1577. //旧项目
  1578. function getPageDataHref($page_content,$page_key) {
  1579. if(isset($page_content) && isset($page_content[$page_key]['href']) && !empty($page_content[$page_key]['href'])) {
  1580. return $page_content[$page_key]['href'];
  1581. } else {
  1582. return 'javascript:void(0)';
  1583. }
  1584. }
  1585. function determinePageData($page_content,$page_key) {
  1586. if(isset($page_content) && isset($page_content[$page_key]) && !empty($page_content[$page_key])) {
  1587. return true;
  1588. } else {
  1589. return false;
  1590. }
  1591. }
  1592. function getPageDataAlt($page_content,$page_key) {
  1593. if(isset($page_content) && isset($page_content[$page_key]['alt']) && !empty($page_content[$page_key]['alt'])) {
  1594. return $page_content[$page_key]['alt'];
  1595. } else {
  1596. return '';
  1597. }
  1598. }
  1599. function createElementOld($menu_data, $data, $module_key, $class, $type, $default_val, $tag_name = '') {
  1600. return createElement($menu_data, $data, '', $module_key, $class, $type, $default_val, $tag_name);
  1601. }