common.php 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. // 此文件为系统框架核心公共函数文件,为了系统的稳定与安全,未经允许不得擅自改动
  12. if (!function_exists('getValidatorError')) {
  13. /**
  14. * 错误验证
  15. * @param $error
  16. * @return mixed
  17. */
  18. function getValidatorError($validator)
  19. {
  20. $errors = $validator->errors()->all();
  21. return isset($errors[0]) ? $errors[0] : 1013;
  22. }
  23. }
  24. if (!function_exists('array_sort')) {
  25. /**
  26. * 数组排序
  27. * @param array $arr 数据源
  28. * @param $keys KEY
  29. * @param bool $desc 排序方式(默认:asc)
  30. * @return array 返回结果
  31. * @author laravel开发员
  32. * @date 2019/5/23
  33. */
  34. function array_sort($arr, $keys, $desc = false)
  35. {
  36. $key_value = $new_array = array();
  37. foreach ($arr as $k => $v) {
  38. $key_value[$k] = $v[$keys];
  39. }
  40. if ($desc) {
  41. arsort($key_value);
  42. } else {
  43. asort($key_value);
  44. }
  45. reset($key_value);
  46. foreach ($key_value as $k => $v) {
  47. $new_array[$k] = $arr[$k];
  48. }
  49. return $new_array;
  50. }
  51. }
  52. if (!function_exists('xml2array')) {
  53. /**
  54. * xml转数组
  55. * @param $xml xml文本
  56. * @return string
  57. * @author laravel开发员
  58. * @date 2019/6/6
  59. */
  60. function xml2array(&$xml)
  61. {
  62. $xml = "<xml>";
  63. foreach ($xml as $key => $val) {
  64. if (is_numeric($val)) {
  65. $xml .= "<" . $key . ">" . $val . "</" . $key . ">";
  66. } else {
  67. $xml .= "<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">";
  68. }
  69. }
  70. $xml .= "</xml>";
  71. return $xml;
  72. }
  73. }
  74. if (!function_exists('array2xml')) {
  75. /**
  76. * 数组转xml
  77. * @param $arr 原始数据(数组)
  78. * @param bool $ignore 是否忽视true或fasle
  79. * @param int $level 级别(默认:1)
  80. * @return string 返回结果
  81. * @author laravel开发员
  82. * @date 2019/6/6
  83. */
  84. function array2xml($arr, $ignore = true, $level = 1)
  85. {
  86. $s = $level == 1 ? "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\r\n<root>\r\n" : '';
  87. $space = str_repeat("\t", $level);
  88. foreach ($arr as $k => $v) {
  89. if (!is_array($v)) {
  90. $s .= $space . "<item id=\"$k\">" . ($ignore ? '<![CDATA[' : '') . $v . ($ignore ? ']]>' : '')
  91. . "</item>\r\n";
  92. } else {
  93. $s .= $space . "<item id=\"$k\">\r\n" . array2xml($v, $ignore, $level + 1) . $space . "</item>\r\n";
  94. }
  95. }
  96. $s = preg_replace("/([\x01-\x08\x0b-\x0c\x0e-\x1f])+/", ' ', $s);
  97. return $level == 1 ? $s . "</root>" : $s;
  98. }
  99. }
  100. if (!function_exists('array_merge_multiple')) {
  101. /**
  102. * 多为数组合并
  103. * @param array $array1 数组1
  104. * @param array $array2 数组2
  105. * @return array 返回合并后的数组
  106. * @author laravel开发员
  107. * @date 2019/6/6
  108. */
  109. function array_merge_multiple($array1, $array2)
  110. {
  111. $merge = $array1 + $array2;
  112. $data = [];
  113. foreach ($merge as $key => $val) {
  114. if (isset($array1[$key])
  115. && is_array($array1[$key])
  116. && isset($array2[$key])
  117. && is_array($array2[$key])
  118. ) {
  119. $data[$key] = array_merge_multiple($array1[$key], $array2[$key]);
  120. } else {
  121. $data[$key] = isset($array2[$key]) ? $array2[$key] : $array1[$key];
  122. }
  123. }
  124. return $data;
  125. }
  126. }
  127. if (!function_exists('array_key_value')) {
  128. /**
  129. * 获取数组中某个字段的所有值
  130. * @param array $arr 数据源
  131. * @param string $name 字段名
  132. * @return array 返回结果
  133. * @author laravel开发员
  134. * @date 2019/6/6
  135. */
  136. function array_key_value($arr, $name = "")
  137. {
  138. $result = [];
  139. if ($arr) {
  140. foreach ($arr as $key => $val) {
  141. if ($name) {
  142. $result[] = $val[$name];
  143. } else {
  144. $result[] = $key;
  145. }
  146. }
  147. }
  148. $result = array_unique($result);
  149. return $result;
  150. }
  151. }
  152. if (!function_exists('curl_url')) {
  153. /**
  154. * 获取当前访问的完整地址
  155. * @return string 返回结果
  156. * @author laravel开发员
  157. * @date 2019/6/6
  158. */
  159. function curl_url()
  160. {
  161. $page_url = 'http';
  162. if (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] === 'on') {
  163. $page_url .= "s";
  164. }
  165. $page_url .= "://";
  166. if ($_SERVER["SERVER_PORT"] != "80") {
  167. $page_url .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"];
  168. } else {
  169. $page_url .= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
  170. }
  171. return $page_url;
  172. }
  173. }
  174. if (!function_exists('curl_get')) {
  175. /**
  176. * curl请求(GET)
  177. * @param $url 请求地址
  178. * @param array $data 请求参数
  179. * @return bool|string 返回结果
  180. * @author laravel开发员
  181. * @date 2019/6/5
  182. */
  183. function curl_get($url, $data = [])
  184. {
  185. if (!empty($data)) {
  186. $url = $url . '?' . http_build_query($data);
  187. }
  188. // 初始化
  189. $ch = curl_init();
  190. // 设置抓取的url
  191. curl_setopt($ch, CURLOPT_URL, $url);
  192. // 设置头文件的信息作为数据流输出
  193. curl_setopt($ch, CURLOPT_HEADER, false);
  194. // 是否要求返回数据
  195. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  196. // 是否检测服务器的证书是否由正规浏览器认证过的授权CA颁发的
  197. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  198. // 执行命令
  199. $result = curl_exec($ch);
  200. // 关闭URL请求(释放句柄)
  201. curl_close($ch);
  202. return $result;
  203. }
  204. }
  205. if (!function_exists('curl_post')) {
  206. /**
  207. * curl请求(POST)
  208. * @param $url 请求地址
  209. * @param array $data 请求参数
  210. * @return bool|string 返回结果
  211. * @author laravel开发员
  212. * @date 2019/6/5
  213. */
  214. function curl_post($url, $data = [])
  215. {
  216. // 初始化
  217. $ch = curl_init();
  218. // 设置post方式提交
  219. curl_setopt($ch, CURLOPT_POST, 1);
  220. // 设置头文件的信息作为数据流输出
  221. curl_setopt($ch, CURLOPT_HEADER, 0);
  222. // 是否要求返回数据
  223. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  224. // 设置抓取的url
  225. curl_setopt($ch, CURLOPT_URL, $url);
  226. // 提交的数据
  227. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  228. // 是否检测服务器的证书是否由正规浏览器认证过的授权CA颁发的
  229. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  230. // 执行命令
  231. $result = curl_exec($ch);
  232. // 关闭URL请求(释放句柄)
  233. curl_close($ch);
  234. return $result;
  235. }
  236. }
  237. if (!function_exists('curl_request')) {
  238. /**
  239. * curl请求(支持get和post)
  240. * @param $url 请求地址
  241. * @param array $data 请求参数
  242. * @param string $type 请求类型(默认:post)
  243. * @param bool $https 是否https请求true或false
  244. * @return bool|string 返回请求结果
  245. * @author laravel开发员
  246. * @date 2019/6/6
  247. */
  248. function curl_request($url, $data = [], $type = 'post', $https = false)
  249. {
  250. // 初始化
  251. $ch = curl_init();
  252. curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
  253. curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)');
  254. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
  255. // 设置超时时间
  256. curl_setopt($ch, CURLOPT_TIMEOUT, 30);
  257. // 是否要求返回数据
  258. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  259. if ($https) {
  260. // 对认证证书来源的检查
  261. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  262. // 从证书中检查SSL加密算法是否存在
  263. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  264. }
  265. if (strtolower($type) == 'post') {
  266. // 设置post方式提交
  267. curl_setopt($ch, CURLOPT_POST, true);
  268. // 提交的数据
  269. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  270. } elseif (!empty($data) && is_array($data)) {
  271. // get网络请求
  272. $url = $url . '?' . http_build_query($data);
  273. }
  274. // 设置抓取的url
  275. curl_setopt($ch, CURLOPT_URL, $url);
  276. // 执行命令
  277. $result = curl_exec($ch);
  278. if ($result === false) {
  279. return false;
  280. }
  281. // 关闭URL请求(释放句柄)
  282. curl_close($ch);
  283. return $result;
  284. }
  285. }
  286. if (!function_exists('datetime')) {
  287. /**
  288. * 格式化日期函数
  289. * @param $time 时间戳
  290. * @param string $format 输出日期格式
  291. * @return string 返回格式化的日期
  292. * @author laravel开发员
  293. * @date 2019/5/23
  294. */
  295. function datetime($time, $format = 'Y-m-d H:i:s')
  296. {
  297. $time = is_numeric($time) ? $time : strtotime($time);
  298. return date($format, $time);
  299. }
  300. }
  301. if (!function_exists('data_auth_sign')) {
  302. /**
  303. * 数据签名认证
  304. * @param $data 数据源
  305. * @return string
  306. * @author laravel开发员
  307. * @date 2019/6/6
  308. */
  309. function data_auth_sign($data)
  310. {
  311. // 数据类型检测
  312. if (!is_array($data)) {
  313. $data = (array)$data;
  314. }
  315. // 排序
  316. ksort($data);
  317. // url编码并生成query字符串
  318. $code = http_build_query($data);
  319. // 生成签名
  320. $sign = sha1($code);
  321. return $sign;
  322. }
  323. }
  324. if (!function_exists('decrypt')) {
  325. /**
  326. * DES解密
  327. * @param string $str 解密字符串
  328. * @param string $key 解密KEY
  329. * @return mixed
  330. * @author laravel开发员
  331. * @date 2019/6/6
  332. */
  333. function decrypt($str, $key = 'p@ssw0rd')
  334. {
  335. $str = base64_decode($str);
  336. $str = mcrypt_decrypt(MCRYPT_DES, $key, $str, MCRYPT_MODE_ECB);
  337. $block = mcrypt_get_block_size('des', 'ecb');
  338. $pad = ord($str[($len = strlen($str)) - 1]);
  339. if ($pad && $pad < $block && preg_match('/' . chr($pad) . '{' . $pad . '}$/', $str)) {
  340. $str = substr($str, 0, strlen($str) - $pad);
  341. }
  342. return unserialize($str);
  343. }
  344. }
  345. if (!function_exists('encrypt')) {
  346. /**
  347. *
  348. * @param string $str 加密字符串
  349. * @param string $key 加密KEY
  350. * @return string
  351. * @author laravel开发员
  352. * @date 2019/6/6
  353. */
  354. function encrypt($str, $key = 'p@ssw0rd')
  355. {
  356. $prep_code = serialize($str);
  357. $block = mcrypt_get_block_size('des', 'ecb');
  358. if (($pad = $block - (strlen($prep_code) % $block)) < $block) {
  359. $prep_code .= str_repeat(chr($pad), $pad);
  360. }
  361. $encrypt = mcrypt_encrypt(MCRYPT_DES, $key, $prep_code, MCRYPT_MODE_ECB);
  362. return base64_encode($encrypt);
  363. }
  364. }
  365. if (!function_exists('export_excel')) {
  366. /**
  367. * 导出Excel文件
  368. * @param string $file_name 文件名
  369. * @param array $title 标题
  370. * @param array $data 数据源
  371. * @author laravel开发员
  372. * @date 2019/6/6
  373. */
  374. function export_excel($file_name, $title = [], $data = [])
  375. {
  376. // 默认支持最大512M
  377. ini_set('memory_limit', '512M');
  378. ini_set('max_execution_time', 0);
  379. ob_end_clean();
  380. ob_start();
  381. header("Content-Type: text/csv");
  382. header("Content-Disposition:filename=" . $file_name);
  383. $fp = fopen('php://output', 'w');
  384. // 转码 防止乱码(比如微信昵称)
  385. fwrite($fp, chr(0xEF) . chr(0xBB) . chr(0xBF));
  386. fputcsv($fp, $title);
  387. $index = 0;
  388. foreach ($data as $item) {
  389. if ($index == 1000) {
  390. $index = 0;
  391. ob_flush();
  392. flush();
  393. }
  394. $index++;
  395. fputcsv($fp, $item);
  396. }
  397. ob_flush();
  398. flush();
  399. ob_end_clean();
  400. }
  401. }
  402. if (!function_exists('ecm_define')) {
  403. /**
  404. * 定义常量(读取数组或引用文件)
  405. * @param $value 数据源
  406. * @return bool
  407. * @author laravel开发员
  408. * @date 2019/6/6
  409. */
  410. function ecm_define($value)
  411. {
  412. if (is_string($value)) {
  413. /* 导入数组 */
  414. $value = include($value);
  415. }
  416. if (!is_array($value)) {
  417. /* 不是数组,无法定义 */
  418. return false;
  419. }
  420. foreach ($value as $key => $val) {
  421. if (is_string($val) || is_numeric($val) || is_bool($val) || is_null($val)) {
  422. // 判断是否已定义过,否则进行定义
  423. defined(strtoupper($key)) or define(strtoupper($key), $val);
  424. }
  425. }
  426. }
  427. }
  428. if (!function_exists('format_time')) {
  429. /**
  430. * 格式化时间段
  431. * @param int $time 时间戳
  432. * @return string 输出格式化时间
  433. * @author laravel开发员
  434. * @date 2019/5/23
  435. */
  436. function format_time($time)
  437. {
  438. $interval = time() - $time;
  439. $format = array(
  440. '31536000' => '年',
  441. '2592000' => '个月',
  442. '604800' => '星期',
  443. '86400' => '天',
  444. '3600' => '小时',
  445. '60' => '分钟',
  446. '1' => '秒',
  447. );
  448. foreach ($format as $key => $val) {
  449. $match = floor($interval / (int)$key);
  450. if (0 != $match) {
  451. return $match . $val . '前';
  452. }
  453. }
  454. return date('Y-m-d', $time);
  455. }
  456. }
  457. if (!function_exists('format_bytes')) {
  458. /**
  459. * 将字节转换为可读文本
  460. * @param int $size 字节大小
  461. * @param string $delimiter 分隔符
  462. * @return string 返回结果
  463. * @author laravel开发员
  464. * @date 2019/5/23
  465. */
  466. function format_bytes($size, $delimiter = '')
  467. {
  468. $units = array('B', 'KB', 'MB', 'GB', 'TB', 'PB');
  469. for ($i = 0; $size >= 1024 && $i < 6; $i++) {
  470. $size /= 1024;
  471. }
  472. return round($size, 2) . $delimiter . $units[$i];
  473. }
  474. }
  475. if (!function_exists('format_yuan')) {
  476. /**
  477. * 以分为单位的金额转换成元
  478. * @param int $money 金额
  479. * @return string 返回格式化的金额
  480. * @author laravel开发员
  481. * @date 2019/5/23
  482. */
  483. function format_yuan($money = 0)
  484. {
  485. if ($money > 0) {
  486. return number_format($money / 100, 2, ".", "");
  487. }
  488. return "0.00";
  489. }
  490. }
  491. if (!function_exists('format_cent')) {
  492. /**
  493. * 以元为单位的金额转化成分
  494. * @param $money 金额
  495. * @return string 返回格式化的金额
  496. * @author laravel开发员
  497. * @date 2019/5/23
  498. */
  499. function format_cent($money)
  500. {
  501. return (string)($money * 100);
  502. }
  503. }
  504. if (!function_exists('format_bank_card')) {
  505. /**
  506. * 银行卡格式转换
  507. * @param string $card_no 银行卡号
  508. * @param bool $is_format 是否格式化
  509. * @return string 输出结果
  510. * @author laravel开发员
  511. * @date 2019/5/23
  512. */
  513. function format_bank_card($card_no, $is_format = true)
  514. {
  515. if ($is_format) {
  516. // 截取银行卡号前4位
  517. $prefix = substr($card_no, 0, 4);
  518. // 截取银行卡号后4位
  519. $suffix = substr($card_no, -4, 4);
  520. $format_card_no = $prefix . " **** **** **** " . $suffix;
  521. } else {
  522. // 4的意思就是每4个为一组
  523. $arr = str_split($card_no, 4);
  524. $format_card_no = implode(' ', $arr);
  525. }
  526. return $format_card_no;
  527. }
  528. }
  529. if (!function_exists('format_mobile')) {
  530. /**
  531. * 格式化手机号码
  532. * @param string $mobile 手机号码
  533. * @return string 返回结果
  534. * @author laravel开发员
  535. * @date 2019/5/23
  536. */
  537. function format_mobile($mobile)
  538. {
  539. return substr($mobile, 0, 5) . "****" . substr($mobile, 9, 2);
  540. }
  541. }
  542. if (!function_exists('get_random_code')) {
  543. /**
  544. * 生成唯一字符串
  545. * @param int $length
  546. * @param string $prefix
  547. * @param string $ext
  548. * @return string
  549. */
  550. function get_random_code($length=8, $prefix='', $ext=''){
  551. $length = $length<9? 9 : $length;
  552. $code = strtoupper(md5(uniqid(mt_rand(), true).$ext.microtime()));
  553. $start = rand(1, strlen($code)-$length);
  554. return $prefix.mb_substr($code, $start, $length);
  555. }
  556. }
  557. if (!function_exists('get_password')) {
  558. /**
  559. * 获取双MD5加密密码
  560. * @param string $password 加密字符串
  561. * @return string 输出MD5加密字符串
  562. * @author laravel开发员
  563. * @date 2019/5/23
  564. */
  565. function get_password($password)
  566. {
  567. return md5(md5($password.md5($password . env('APP_NAME'))));
  568. }
  569. }
  570. if (!function_exists('get_order_num')) {
  571. /**
  572. * 生成订单号
  573. * @param string $prefix 订单前缀(如:JD-)
  574. * @return string 输出订单号字符串
  575. * @author laravel开发员
  576. * @date 2019/5/23
  577. */
  578. function get_order_num($prefix = '')
  579. {
  580. $micro = substr(microtime(), 2, 3);
  581. return $prefix . date("YmdHis") . $micro . rand(100000, 999999);
  582. }
  583. }
  584. if (!function_exists('getter')) {
  585. /**
  586. * 获取数组的下标值
  587. * @param array $data 数据源
  588. * @param string $field 字段名称
  589. * @param string $default 默认值
  590. * @return mixed|string 返回结果
  591. * @author laravel开发员
  592. * @date 2019/5/23
  593. */
  594. function getter($data, $field, $default = '')
  595. {
  596. $result = $default;
  597. if (isset($data[$field])) {
  598. $result = $data[$field];
  599. }
  600. return $result;
  601. }
  602. }
  603. if (!function_exists('get_zodiac_sign')) {
  604. /**
  605. * 根据月、日获取星座
  606. * @param $month 月份
  607. * @param $day 日期
  608. * @return string 返回结果
  609. * @author laravel开发员
  610. * @date 2019/5/23
  611. */
  612. function get_zodiac_sign($month, $day)
  613. {
  614. // 检查参数有效性
  615. if ($month < 1 || $month > 12 || $day < 1 || $day > 31) {
  616. return false;
  617. }
  618. // 星座名称以及开始日期
  619. $signs = array(
  620. array("20" => "水瓶座"),
  621. array("19" => "双鱼座"),
  622. array("21" => "白羊座"),
  623. array("20" => "金牛座"),
  624. array("21" => "双子座"),
  625. array("22" => "巨蟹座"),
  626. array("23" => "狮子座"),
  627. array("23" => "处女座"),
  628. array("23" => "天秤座"),
  629. array("24" => "天蝎座"),
  630. array("22" => "射手座"),
  631. array("22" => "摩羯座")
  632. );
  633. list($sign_start, $sign_name) = each($signs[(int)$month - 1]);
  634. if ($day < $sign_start) {
  635. list($sign_start, $sign_name) = each($signs[($month - 2 < 0) ? $month = 11 : $month -= 2]);
  636. }
  637. return $sign_name;
  638. }
  639. }
  640. if (!function_exists('get_image_url')) {
  641. /**
  642. * 获取图片地址
  643. * @param $image_url 图片地址
  644. * @return string 返回图片网络地址
  645. * @author laravel开发员
  646. * @date 2019/6/6
  647. */
  648. function get_image_url($image_url)
  649. {
  650. $host = request()->header('HOST');
  651. $host = ((strpos($host,'127') === false)&&strpos($host,'localhost') === false)? env('IMG_URL') : 'http://'.$host.'/uploads';
  652. return strpos($image_url,'http') === false? $host . '/'.ltrim($image_url,'/') : $image_url;
  653. }
  654. }
  655. if (!function_exists('get_hash')) {
  656. /**
  657. * 获取HASH值
  658. * @return string 返回hash字符串
  659. * @author laravel开发员
  660. * @date 2019/6/6
  661. */
  662. function get_hash()
  663. {
  664. $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()+-';
  665. $random = $chars[mt_rand(0, 73)] . $chars[mt_rand(0, 73)] . $chars[mt_rand(0, 73)]
  666. . $chars[mt_rand(0, 73)] . $chars[mt_rand(0, 73)];
  667. $content = uniqid() . $random;
  668. return sha1($content);
  669. }
  670. }
  671. if (!function_exists('get_server_ip')) {
  672. /**
  673. * 获取服务端IP地址
  674. * @return string 返回IP地址
  675. * @author laravel开发员
  676. * @date 2019/6/6
  677. */
  678. function get_server_ip()
  679. {
  680. if (isset($_SERVER)) {
  681. if ($_SERVER['SERVER_ADDR']) {
  682. $server_ip = $_SERVER['SERVER_ADDR'];
  683. } else {
  684. $server_ip = $_SERVER['LOCAL_ADDR'];
  685. }
  686. } else {
  687. $server_ip = getenv('SERVER_ADDR');
  688. }
  689. return $server_ip;
  690. }
  691. }
  692. if (!function_exists('get_client_ip')) {
  693. /**
  694. * 获取客户端IP地址
  695. * @param int $type 返回类型 0 返回IP地址 1 返回IPV4地址数字
  696. * @param bool $adv 否进行高级模式获取(有可能被伪装)
  697. * @return mixed 返回IP
  698. * @author laravel开发员
  699. * @date 2019/5/23
  700. */
  701. function get_client_ip($type = 0, $adv = false)
  702. {
  703. $type = $type ? 1 : 0;
  704. static $ip = null;
  705. if ($ip !== null) {
  706. return $ip[$type];
  707. }
  708. if ($adv) {
  709. if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  710. $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
  711. $pos = array_search('unknown', $arr);
  712. if (false !== $pos) {
  713. unset($arr[$pos]);
  714. }
  715. $ip = trim($arr[0]);
  716. } elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
  717. $ip = $_SERVER['HTTP_CLIENT_IP'];
  718. } elseif (isset($_SERVER['REMOTE_ADDR'])) {
  719. $ip = $_SERVER['REMOTE_ADDR'];
  720. }
  721. } elseif (isset($_SERVER['REMOTE_ADDR'])) {
  722. $ip = $_SERVER['REMOTE_ADDR'];
  723. }
  724. // IP地址合法验证
  725. $long = sprintf("%u", ip2long($ip));
  726. $ip = $long ? array($ip, $long) : array('0.0.0.0', 0);
  727. return $ip[$type];
  728. }
  729. }
  730. if (!function_exists('get_guid_v4')) {
  731. /**
  732. * 获取唯一性GUID
  733. * @param bool $trim 是否去除{}
  734. * @return string 返回GUID字符串
  735. * @author laravel开发员
  736. * @date 2019/6/6
  737. */
  738. function get_guid_v4($trim = true)
  739. {
  740. // Windows
  741. if (function_exists('com_create_guid') === true) {
  742. $charid = com_create_guid();
  743. return $trim == true ? trim($charid, '{}') : $charid;
  744. }
  745. // OSX/Linux
  746. if (function_exists('openssl_random_pseudo_bytes') === true) {
  747. $data = openssl_random_pseudo_bytes(16);
  748. $data[6] = chr(ord($data[6]) & 0x0f | 0x40); // set version to 0100
  749. $data[8] = chr(ord($data[8]) & 0x3f | 0x80); // set bits 6-7 to 10
  750. return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
  751. }
  752. // Fallback (PHP 4.2+)
  753. mt_srand((double)microtime() * 10000);
  754. $charid = strtolower(md5(uniqid(rand(), true)));
  755. $hyphen = chr(45); // "-"
  756. $lbrace = $trim ? "" : chr(123); // "{"
  757. $rbrace = $trim ? "" : chr(125); // "}"
  758. $guidv4 = $lbrace .
  759. substr($charid, 0, 8) . $hyphen .
  760. substr($charid, 8, 4) . $hyphen .
  761. substr($charid, 12, 4) . $hyphen .
  762. substr($charid, 16, 4) . $hyphen .
  763. substr($charid, 20, 12) .
  764. $rbrace;
  765. return $guidv4;
  766. }
  767. }
  768. if (!function_exists('is_email')) {
  769. /**
  770. * 判断是否为邮箱
  771. * @param string $str 邮箱
  772. * @return false 返回结果true或false
  773. * @author laravel开发员
  774. * @date 2019/5/23
  775. */
  776. function is_email($str)
  777. {
  778. return preg_match('/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/', $str);
  779. }
  780. }
  781. if (!function_exists('is_mobile')) {
  782. /**
  783. * 判断是否为手机号
  784. * @param string $num 手机号码
  785. * @return false 返回结果true或false
  786. * @author laravel开发员
  787. * @date 2019/5/23
  788. */
  789. function is_mobile($num)
  790. {
  791. return preg_match('/^1(3|4|5|7|8)\d{9}$/', $num);
  792. }
  793. }
  794. if (!function_exists('is_zipcode')) {
  795. /**
  796. * 验证邮编是否正确
  797. * @param string $code 邮编
  798. * @return false 返回结果true或false
  799. * @author laravel开发员
  800. * @date 2019/5/23
  801. */
  802. function is_zipcode($code)
  803. {
  804. return preg_match('/^[1-9][0-9]{5}$/', $code);
  805. }
  806. }
  807. if (!function_exists('is_idcard')) {
  808. /**
  809. * 验证身份证是否正确
  810. * @param string $idno 身份证号
  811. * @return bool 返回结果true或false
  812. * @author laravel开发员
  813. * @date 2019/5/23
  814. */
  815. function is_idcard($idno)
  816. {
  817. $idno = strtoupper($idno);
  818. $regx = '/(^\d{15}$)|(^\d{17}([0-9]|X)$)/';
  819. $arr_split = array();
  820. if (!preg_match($regx, $idno)) {
  821. return false;
  822. }
  823. // 检查15位
  824. if (15 == strlen($idno)) {
  825. $regx = '/^(\d{6})+(\d{2})+(\d{2})+(\d{2})+(\d{3})$/';
  826. @preg_match($regx, $idno, $arr_split);
  827. $dtm_birth = "19" . $arr_split[2] . '/' . $arr_split[3] . '/' . $arr_split[4];
  828. if (!strtotime($dtm_birth)) {
  829. return false;
  830. } else {
  831. return true;
  832. }
  833. } else {
  834. // 检查18位
  835. $regx = '/^(\d{6})+(\d{4})+(\d{2})+(\d{2})+(\d{3})([0-9]|X)$/';
  836. @preg_match($regx, $idno, $arr_split);
  837. $dtm_birth = $arr_split[2] . '/' . $arr_split[3] . '/' . $arr_split[4];
  838. // 检查生日日期是否正确
  839. if (!strtotime($dtm_birth)) {
  840. return false;
  841. } else {
  842. // 检验18位身份证的校验码是否正确。
  843. // 校验位按照ISO 7064:1983.MOD 11-2的规定生成,X可以认为是数字10。
  844. $arr_int = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
  845. $arr_ch = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
  846. $sign = 0;
  847. for ($i = 0; $i < 17; $i++) {
  848. $b = (int)$idno{$i};
  849. $w = $arr_int[$i];
  850. $sign += $b * $w;
  851. }
  852. $n = $sign % 11;
  853. $val_num = $arr_ch[$n];
  854. if ($val_num != substr($idno, 17, 1)) {
  855. return false;
  856. } else {
  857. return true;
  858. }
  859. }
  860. }
  861. }
  862. }
  863. if (!function_exists('is_empty')) {
  864. /**
  865. * 判断是否为空
  866. * @param $value 参数值
  867. * @return bool 返回结果true或false
  868. * @author laravel开发员
  869. * @date 2019/6/5
  870. */
  871. function is_empty($value)
  872. {
  873. // 判断是否存在该值
  874. if (!isset($value)) {
  875. return true;
  876. }
  877. // 判断是否为empty
  878. if (empty($value)) {
  879. return true;
  880. }
  881. // 判断是否为null
  882. if ($value === null) {
  883. return true;
  884. }
  885. // 判断是否为空字符串
  886. if (trim($value) === '') {
  887. return true;
  888. }
  889. // 默认返回false
  890. return false;
  891. }
  892. }
  893. if (!function_exists('mkdirs')) {
  894. /**
  895. * 递归创建目录
  896. * @param string $dir 需要创建的目录路径
  897. * @param int $mode 权限值
  898. * @return bool 返回结果true或false
  899. * @author laravel开发员
  900. * @date 2019/6/6
  901. */
  902. function mkdirs($dir, $mode = 0777)
  903. {
  904. if (is_dir($dir) || mkdir($dir, $mode, true)) {
  905. return true;
  906. }
  907. if (!mkdirs(dirname($dir), $mode)) {
  908. return false;
  909. }
  910. return mkdir($dir, $mode, true);
  911. }
  912. }
  913. if (!function_exists('rmdirs')) {
  914. /**
  915. * 删除文件夹
  916. * @param string $dir 文件夹路径
  917. * @param bool $rmself 是否删除本身true或false
  918. * @return bool 返回删除结果
  919. * @author laravel开发员
  920. * @date 2019/6/6
  921. */
  922. function rmdirs($dir, $rmself = true)
  923. {
  924. if (!is_dir($dir)) {
  925. return false;
  926. }
  927. $files = new RecursiveIteratorIterator(
  928. new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS),
  929. RecursiveIteratorIterator::CHILD_FIRST
  930. );
  931. foreach ($files as $file) {
  932. $todo = ($file->isDir() ? 'rmdir' : 'unlink');
  933. $todo($file->getRealPath());
  934. }
  935. if ($rmself) {
  936. @rmdir($dir);
  937. }
  938. return true;
  939. }
  940. }
  941. if (!function_exists('copydirs')) {
  942. /**
  943. * 复制文件夹
  944. * @param string $source 原文件夹路径
  945. * @param string $dest 目的文件夹路径
  946. * @author laravel开发员
  947. * @date 2019/6/6
  948. */
  949. function copydirs($source, $dest)
  950. {
  951. if (!is_dir($dest)) {
  952. mkdir($dest, 0755, true);
  953. }
  954. $iterator = new RecursiveIteratorIterator(
  955. new RecursiveDirectoryIterator($source, RecursiveDirectoryIterator::SKIP_DOTS),
  956. RecursiveIteratorIterator::SELF_FIRST
  957. );
  958. foreach ($iterator as $item) {
  959. if ($item->isDir()) {
  960. $sent_dir = $dest . "/" . $iterator->getSubPathName();
  961. if (!is_dir($sent_dir)) {
  962. mkdir($sent_dir, 0755, true);
  963. }
  964. } else {
  965. copy($item, $dest . "/" . $iterator->getSubPathName());
  966. }
  967. }
  968. }
  969. }
  970. if(!function_exists('lang')){
  971. function lang($msg, $replace=[], $local=''){
  972. $local = $local? $local : config('app.locale');
  973. $data = __('api.'.$msg, $replace, $local);
  974. if(strpos($data,'api',0) !== false){
  975. return __($msg, $replace, $local);
  976. }
  977. return __('api.'.$msg, $replace, $local);
  978. }
  979. }
  980. if (!function_exists('message')) {
  981. /**
  982. * 消息数组
  983. * @param string $msg 提示文字
  984. * @param bool $success 是否成功true或false
  985. * @param array $data 结果数据
  986. * @param int $code 编码
  987. * @return array 返回结果
  988. * @author laravel开发员
  989. * @date 2019/5/28
  990. */
  991. function message($msg = "操作成功", $success = true, $data = [], $code = 0, $type='json')
  992. {
  993. $result = ['success' => $success, 'msg' => lang($msg), 'data' => $data];
  994. if ($success) {
  995. $result['code'] = 0;
  996. } else {
  997. $result['code'] = $code ? $code : -1;
  998. }
  999. return $result;
  1000. // return $type=='json'? json_encode($result, 256): $result;
  1001. }
  1002. }
  1003. if (!function_exists('showJson')) {
  1004. /**
  1005. * 消息JSON
  1006. * @param string $msg 提示文字
  1007. * @param bool $success 是否成功true或false
  1008. * @param array $data 结果数据
  1009. * @param int $code 编码
  1010. * @return array 返回结果
  1011. * @author laravel开发员
  1012. * @date 2019/5/28
  1013. */
  1014. function showJson($msg = "1002", $success = true, $data = [], $code = 0)
  1015. {
  1016. $result = ['success' => $success, 'msg' => lang($msg), 'data' => $data];
  1017. if ($success) {
  1018. $result['code'] = 0;
  1019. } else {
  1020. $result['code'] = $code ? $code : -1;
  1021. }
  1022. return json_encode($result, 256);
  1023. }
  1024. }
  1025. if (!function_exists('num2rmb')) {
  1026. /**
  1027. * 数字金额转大写
  1028. * @param float $num 金额
  1029. * @return string 返回大写金额
  1030. * @author laravel开发员
  1031. * @date 2019/6/6
  1032. */
  1033. function num2rmb($num)
  1034. {
  1035. $c1 = "零壹贰叁肆伍陆柒捌玖";
  1036. $c2 = "分角元拾佰仟万拾佰仟亿";
  1037. $num = round($num, 2);
  1038. $num = $num * 100;
  1039. if (strlen($num) > 10) {
  1040. return "oh,sorry,the number is too long!";
  1041. }
  1042. $i = 0;
  1043. $c = "";
  1044. while (1) {
  1045. if ($i == 0) {
  1046. $n = substr($num, strlen($num) - 1, 1);
  1047. } else {
  1048. $n = $num % 10;
  1049. }
  1050. $p1 = substr($c1, 3 * $n, 3);
  1051. $p2 = substr($c2, 3 * $i, 3);
  1052. if ($n != '0' || ($n == '0' && ($p2 == '亿' || $p2 == '万' || $p2 == '元'))) {
  1053. $c = $p1 . $p2 . $c;
  1054. } else {
  1055. $c = $p1 . $c;
  1056. }
  1057. $i = $i + 1;
  1058. $num = $num / 10;
  1059. $num = (int)$num;
  1060. if ($num == 0) {
  1061. break;
  1062. }
  1063. }
  1064. $j = 0;
  1065. $slen = strlen($c);
  1066. while ($j < $slen) {
  1067. $m = substr($c, $j, 6);
  1068. if ($m == '零元' || $m == '零万' || $m == '零亿' || $m == '零零') {
  1069. $left = substr($c, 0, $j);
  1070. $right = substr($c, $j + 3);
  1071. $c = $left . $right;
  1072. $j = $j - 3;
  1073. $slen = $slen - 3;
  1074. }
  1075. $j = $j + 3;
  1076. }
  1077. if (substr($c, strlen($c) - 3, 3) == '零') {
  1078. $c = substr($c, 0, strlen($c) - 3);
  1079. } // if there is a '0' on the end , chop it out
  1080. return $c . "整";
  1081. }
  1082. }
  1083. if (!function_exists('object_array')) {
  1084. /**
  1085. * 对象转数组
  1086. * @param $object 对象
  1087. * @return mixed 返回结果
  1088. * @author laravel开发员
  1089. * @date 2019/5/23
  1090. */
  1091. function object_array($object)
  1092. {
  1093. //先编码成json字符串,再解码成数组
  1094. return json_decode(json_encode($object), true);
  1095. }
  1096. }
  1097. if (!function_exists('parse_attr')) {
  1098. /**
  1099. * 配置值解析成数组
  1100. * @param string $value 参数值
  1101. * @return array 返回结果
  1102. * @author laravel开发员
  1103. * @date 2019/6/6
  1104. */
  1105. function parse_attr($value = '')
  1106. {
  1107. if (is_array($value)) {
  1108. return $value;
  1109. }
  1110. $array = preg_split('/[,;\r\n]+/', trim($value, ",;\r\n"));
  1111. if (strpos($value, ':')) {
  1112. $value = array();
  1113. foreach ($array as $val) {
  1114. list($k, $v) = explode(':', $val);
  1115. $value[$k] = $v;
  1116. }
  1117. } else {
  1118. $value = $array;
  1119. }
  1120. return $value;
  1121. }
  1122. }
  1123. if (!function_exists('strip_html_tags')) {
  1124. /**
  1125. * 去除HTML标签、图像等 仅保留文本
  1126. * @param string $str 字符串
  1127. * @param int $length 长度
  1128. * @return string 返回结果
  1129. * @author laravel开发员
  1130. * @date 2019/5/23
  1131. */
  1132. function strip_html_tags($str, $length = 0)
  1133. {
  1134. // 把一些预定义的 HTML 实体转换为字符
  1135. $str = htmlspecialchars_decode($str);
  1136. // 将空格替换成空
  1137. $str = str_replace("&nbsp;", "", $str);
  1138. // 函数剥去字符串中的 HTML、XML 以及 PHP 的标签,获取纯文本内容
  1139. $str = strip_tags($str);
  1140. $str = str_replace(array("\n", "\r\n", "\r"), ' ', $str);
  1141. $preg = '/<script[\s\S]*?<\/script>/i';
  1142. // 剥离JS代码
  1143. $str = preg_replace($preg, "", $str, -1);
  1144. if ($length == 2) {
  1145. // 返回字符串中的前100字符串长度的字符
  1146. $str = mb_substr($str, 0, $length, "utf-8");
  1147. }
  1148. return $str;
  1149. }
  1150. }
  1151. if (!function_exists('sub_str')) {
  1152. /**
  1153. * 字符串截取
  1154. * @param string $str 需要截取的字符串
  1155. * @param int $start 开始位置
  1156. * @param int $length 截取长度
  1157. * @param bool $suffix 截断显示字符
  1158. * @param string $charset 编码格式
  1159. * @return string 返回结果
  1160. * @author laravel开发员
  1161. * @date 2019/5/23
  1162. */
  1163. function sub_str($str, $start = 0, $length = 10, $suffix = true, $charset = "utf-8")
  1164. {
  1165. if (function_exists("mb_substr")) {
  1166. $slice = mb_substr($str, $start, $length, $charset);
  1167. } elseif (function_exists('iconv_substr')) {
  1168. $slice = iconv_substr($str, $start, $length, $charset);
  1169. } else {
  1170. $re['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
  1171. $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
  1172. $re['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
  1173. $re['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
  1174. preg_match_all($re[$charset], $str, $match);
  1175. $slice = join("", array_slice($match[0], $start, $length));
  1176. }
  1177. $omit = mb_strlen($str) >= $length ? '...' : '';
  1178. return $suffix ? $slice . $omit : $slice;
  1179. }
  1180. }
  1181. if (!function_exists('save_image')) {
  1182. /**
  1183. * 保存图片
  1184. * @param string $img_url 网络图片地址
  1185. * @param string $save_dir 图片保存目录
  1186. * @return string 返回路径
  1187. * @author laravel开发员
  1188. * @date 2019/5/23
  1189. */
  1190. function save_image($img_url, $save_dir = '/')
  1191. {
  1192. if (!$img_url) {
  1193. return false;
  1194. }
  1195. $save_dir = trim($save_dir, "/");
  1196. $imgExt = pathinfo($img_url, PATHINFO_EXTENSION);
  1197. // 是否是本站图片
  1198. if (strpos($img_url, IMG_URL) !== false) {
  1199. // 是否是临时文件
  1200. if (strpos($img_url, 'temp') === false) {
  1201. return str_replace(IMG_URL, "", $img_url);
  1202. }
  1203. $new_path = create_image_path($save_dir, $imgExt);
  1204. $old_path = str_replace(IMG_URL, ATTACHMENT_PATH, $img_url);
  1205. if (!file_exists($old_path)) {
  1206. return false;
  1207. }
  1208. rename($old_path, ATTACHMENT_PATH . $new_path);
  1209. return $new_path;
  1210. } else {
  1211. // 保存远程图片
  1212. $new_path = save_remote_image($img_url, $save_dir);
  1213. }
  1214. return $new_path;
  1215. }
  1216. }
  1217. if (!function_exists('create_image_path')) {
  1218. /**
  1219. * 创建图片存储目录
  1220. * @param string $save_dir 存储目录
  1221. * @param string $image_ext 图片后缀
  1222. * @param string $image_root 图片存储根目录路径
  1223. * @return string 返回文件目录
  1224. * @author laravel开发员
  1225. * @date 2019/5/23
  1226. */
  1227. function create_image_path($save_dir = "", $image_ext = "", $image_root = IMG_PATH)
  1228. {
  1229. $image_dir = date("/Ymd/");
  1230. if ($image_dir) {
  1231. $image_dir = ($save_dir ? "/" : '') . $save_dir . $image_dir;
  1232. }
  1233. // 未指定后缀默认使用JPG
  1234. if (!$image_ext) {
  1235. $image_ext = "jpg";
  1236. }
  1237. $image_path = $image_root . $image_dir;
  1238. if (!is_dir($image_path)) {
  1239. // 创建目录并赋予权限
  1240. mkdir($image_path, 0777, true);
  1241. }
  1242. $file_name = substr(md5(time() . rand(0, 999999)), 8, 16) . rand(100, 999) . ".{$image_ext}";
  1243. $file_path = str_replace(ATTACHMENT_PATH, "", IMG_PATH) . $image_dir . $file_name;
  1244. return $file_path;
  1245. }
  1246. }
  1247. if (!function_exists('save_remote_image')) {
  1248. /**
  1249. * 保存网络图片到本地
  1250. * @param string $img_url 网络图片地址
  1251. * @param string $save_dir 保存目录
  1252. * @return bool|string 图片路径
  1253. * @author laravel开发员
  1254. * @date 2019/5/23
  1255. */
  1256. function save_remote_image($img_url, $save_dir = '/')
  1257. {
  1258. $content = file_get_contents($img_url);
  1259. if (!$content) {
  1260. return false;
  1261. }
  1262. if ($content{0} . $content{1} == "\xff\xd8") {
  1263. $image_ext = 'jpg';
  1264. } elseif ($content{0} . $content{1} . $content{2} == "\x47\x49\x46") {
  1265. $image_ext = 'gif';
  1266. } elseif ($content{0} . $content{1} . $content{2} == "\x89\x50\x4e") {
  1267. $image_ext = 'png';
  1268. } else {
  1269. // 不是有效图片
  1270. return false;
  1271. }
  1272. $save_path = create_image_path($save_dir, $image_ext);
  1273. return file_put_contents(ATTACHMENT_PATH . $save_path, $content) ? $save_path : false;
  1274. }
  1275. }
  1276. if (!function_exists('save_image_content')) {
  1277. /**
  1278. * 富文本信息处理
  1279. * @param string $content 富文本内容
  1280. * @param bool $title 标题
  1281. * @param string $path 图片存储路径
  1282. * @return bool|int 返回结果
  1283. * @author laravel开发员
  1284. * @date 2020-04-21
  1285. */
  1286. function save_image_content(&$content, $title = false, $path = 'article')
  1287. {
  1288. // 图片处理
  1289. preg_match_all("/<img.*?src=[\"|\']?(.*?)[\"|\']?\s.*?>/i", str_ireplace("\\", "", $content), $match);
  1290. if ($match[1]) {
  1291. foreach ($match[1] as $id => $val) {
  1292. $save_image = save_image($val, $path);
  1293. if ($save_image) {
  1294. $content = str_replace($val, "[IMG_URL]" . $save_image, $content);
  1295. }
  1296. }
  1297. }
  1298. // 视频处理
  1299. preg_match_all("/<embed .*?src=[\"|\']?(.*?)[\"|\']?\s.*?>/i", str_ireplace("\\", "", $content), $match2);
  1300. if ($match2[1]) {
  1301. foreach ($match2[1] as $vo) {
  1302. $save_video = save_image($vo, $path);
  1303. if ($save_video) {
  1304. $content = str_replace($vo, "[IMG_URL]" . str_replace(ATTACHMENT_PATH, "", IMG_PATH) . $save_video, $content);
  1305. }
  1306. }
  1307. }
  1308. // 提示标签替换
  1309. if ((strpos($content, 'alt=\"\"') !== false) && $title) {
  1310. $content = str_replace('alt=\"\"', 'alt=\"' . $title . '\"', $content);
  1311. }
  1312. return true;
  1313. }
  1314. }
  1315. if (!function_exists('upload_image')) {
  1316. /**
  1317. * 上传图片
  1318. * @param $request 网络请求
  1319. * @param string $form_name 文件表单名
  1320. * @return array 返回结果
  1321. * @author laravel开发员
  1322. * @date 2019/5/23
  1323. */
  1324. function upload_image($request, $form_name = 'file')
  1325. {
  1326. // 检测请求中是否包含name=$form_name的上传文件
  1327. if (!$request->hasFile($form_name)) {
  1328. return message("请上传文件", false);
  1329. }
  1330. // 文件对象
  1331. $file = $request->file($form_name);
  1332. // 判断图片上传是否错误
  1333. if (!$file->isValid()) {
  1334. // 文件上传失败
  1335. return message("上传文件验证失败", false);
  1336. }
  1337. // 文件原名
  1338. $original_name = $file->getClientOriginalName();
  1339. // 文件扩展名(文件后缀)
  1340. $ext = $file->getClientOriginalExtension();
  1341. // 临时文件的绝对路径
  1342. $real_path = $file->getRealPath();
  1343. // 文件类型
  1344. $type = $file->getClientMimeType();
  1345. // 文件大小
  1346. $size = $file->getSize();
  1347. // 文件大小校验
  1348. if ($size > 5 * 1024 * 1024) {
  1349. return message("文件大小超过了5M", false);
  1350. }
  1351. // 文件后缀校验
  1352. $ext_arr = array('jpg', 'jpeg', 'png', 'gif');
  1353. if (!in_array($ext, $ext_arr)) {
  1354. return message("文件格式不正确", false);
  1355. }
  1356. // 文件路径
  1357. $file_dir = UPLOAD_TEMP_PATH . "/" . date('Ymd');
  1358. // 检测文件路径是否存在,不存在则创建
  1359. if (!file_exists($file_dir)) {
  1360. mkdir($file_dir, 0777, true);
  1361. }
  1362. // 文件名称
  1363. $file_name = uniqid() . '.' . $ext;
  1364. // 重命名保存
  1365. $path = $file->move($file_dir, $file_name);
  1366. // 文件临时路径
  1367. $file_path = str_replace(ATTACHMENT_PATH, '', $file_dir) . '/' . $file_name;
  1368. // 返回结果
  1369. $result = [
  1370. 'img_original_name' => $original_name,
  1371. 'img_ext' => $ext,
  1372. 'img_real_path' => $real_path,
  1373. 'img_type' => $type,
  1374. 'img_size' => $size,
  1375. 'img_name' => $file_name,
  1376. 'img_path' => $file_path,
  1377. ];
  1378. return message(MESSAGE_OK, true, $result);
  1379. }
  1380. }
  1381. if (!function_exists('upload_file')) {
  1382. /**
  1383. * 上传文件
  1384. * @param $request 网络请求
  1385. * @param string $form_name 文件表单名
  1386. * @return array 返回结果
  1387. * @author laravel开发员
  1388. * @date 2019/5/23
  1389. */
  1390. function upload_file($request, $form_name = 'file')
  1391. {
  1392. // 检测请求中是否包含上传的文件
  1393. if (!$request->hasFile($form_name)) {
  1394. return message("请上传文件");
  1395. }
  1396. // 文件对象
  1397. $file = $request->file($form_name);
  1398. // 判断图片上传是否错误
  1399. if (!$file->isValid()) {
  1400. // 文件上传失败
  1401. return message("上传文件验证失败");
  1402. }
  1403. // 文件原名
  1404. $original_name = $file->getClientOriginalName();
  1405. // 文件扩展名(文件后缀)
  1406. $ext = $file->getClientOriginalExtension();
  1407. // 临时文件的绝对路径
  1408. $real_path = $file->getRealPath();
  1409. // 文件类型
  1410. $type = $file->getClientMimeType();
  1411. // 文件大小
  1412. $size = $file->getSize();
  1413. // 文件大小校验(MAX=5M)
  1414. $file_max_size = 5 * 1024 * 1024;
  1415. if ($size > $file_max_size) {
  1416. return message("您上传的文件过大,最大值为" . $file_max_size / 1024 / 1024 . "MB");
  1417. }
  1418. // 允许上传的文件后缀
  1419. $file_exts = array('xls', 'xlsx', 'csv');
  1420. if (!in_array($ext, $file_exts)) {
  1421. return message("文件格式不正确");
  1422. }
  1423. // 文件路径
  1424. $file_dir = UPLOAD_TEMP_PATH . "/" . date('Ymd');
  1425. // 检测文件路径是否存在,不存在则创建
  1426. if (!file_exists($file_dir)) {
  1427. mkdir($file_dir, 0777, true);
  1428. }
  1429. // 文件名称
  1430. $file_name = uniqid() . '.' . $ext;
  1431. // 重命名保存
  1432. $path = $file->move($file_dir, $file_name);
  1433. // 文件临时路径
  1434. $file_path = str_replace(IMG_PATH, '', $file_dir) . '/' . $file_name;
  1435. // 返回结果
  1436. $result = [
  1437. 'file_original_name' => $original_name,
  1438. 'file_ext' => $ext,
  1439. 'file_real_path' => $real_path,
  1440. 'file_type' => $type,
  1441. 'file_size' => $size,
  1442. 'file_name' => $file_name,
  1443. 'file_path' => $file_path,
  1444. ];
  1445. return message(MESSAGE_OK, true, $result);
  1446. }
  1447. }
  1448. if (!function_exists('widget')) {
  1449. /**
  1450. * 加载系统组件,传入的名字会以目录和类名区别
  1451. * 如Home.Common就代表Widget目录下的Home/Common.php这个widget。
  1452. * @param $widgetName 组件名称
  1453. * @return bool|mixed
  1454. * @author laravel开发员
  1455. * @date 2019/5/23
  1456. */
  1457. function widget($widgetName)
  1458. {
  1459. $widgetNameEx = explode('.', $widgetName);
  1460. if (!isset($widgetNameEx[1])) {
  1461. return false;
  1462. }
  1463. $widgetClass = 'App\\Widget\\' . $widgetNameEx[0] . '\\' . $widgetNameEx[1];
  1464. if (app()->bound($widgetName)) {
  1465. return app()->make($widgetName);
  1466. }
  1467. app()->singleton($widgetName, function () use ($widgetClass) {
  1468. return new $widgetClass();
  1469. });
  1470. return app()->make($widgetName);
  1471. }
  1472. }
  1473. // 树结构数据
  1474. if (!function_exists('get_tree')){
  1475. function get_tree($datas, $pid=0, $tempArr=[]){
  1476. if(empty($datas)){
  1477. return false;
  1478. }
  1479. foreach ($datas as $key=> $v){
  1480. $data = [];
  1481. $id = isset($v['id'])? $v['id'] : 0;
  1482. $currentPid = isset($v['parent_id'])? $v['parent_id'] : 0;
  1483. $nickname = isset($v['nickname'])? $v['nickname'] : '';
  1484. $username = isset($v['username'])? $v['username'] : '';
  1485. $mobile = isset($v['mobile'])? $v['mobile'] : '';
  1486. $data['id'] = $v['id'];
  1487. $data['parent_id'] = $v['parent_id'];
  1488. $data['mobile'] = $mobile;
  1489. $data['label'] = $nickname.($mobile?"({$mobile})":"");
  1490. if($currentPid && $currentPid == $pid){
  1491. $data['children'] = get_tree($datas, $v['id'], []);
  1492. $tempArr[] = $data;
  1493. }else if ($currentPid == 0 && $pid==0){
  1494. $data['children'] = get_tree($datas, $v['id'], []);
  1495. $tempArr[$id] = $data;
  1496. }
  1497. }
  1498. return array_values($tempArr);
  1499. }
  1500. }
  1501. // 获取上传图片相对路径
  1502. if (!function_exists('get_image_path')){
  1503. function get_image_path($url){
  1504. $url = preg_replace("/(.*uploads)\//",'/', $url);
  1505. return str_replace(IMG_URL, "", $url);
  1506. }
  1507. }
  1508. // 获取图片数组预览
  1509. if (!function_exists('get_images_preview')){
  1510. /**
  1511. * @param $urls 图片数组
  1512. * @param string $keyName 图片地址键名,空则无键名
  1513. * @return false
  1514. */
  1515. function get_images_preview($urls, $keyName='url'){
  1516. if(empty($urls)){
  1517. return [];
  1518. }
  1519. foreach($urls as &$item){
  1520. if($keyName){
  1521. $item[$keyName] = get_image_url($item[$keyName]);
  1522. }else{
  1523. $item = get_image_url($item);
  1524. }
  1525. }
  1526. unset($item);
  1527. return $urls;
  1528. }
  1529. }
  1530. if (!function_exists('httpRequest')) {
  1531. /**
  1532. *
  1533. * 接口请求
  1534. * @author wesmiler
  1535. * @param $url 接口地址
  1536. * @param $data
  1537. * @param $type
  1538. * @param int $timeout
  1539. * @return mixed
  1540. */
  1541. function httpRequest($url, $data = '', $type = 'post', $cookie = '', $timeout = 60)
  1542. {
  1543. try {
  1544. set_time_limit($timeout);
  1545. $data = $data && is_array($data) ? http_build_query($data) : $data;
  1546. $url = strtolower($type) == 'get' ? $url . (strpos($url, '?') === false ? '?' : '&') . $data : $url;
  1547. $ch = curl_init($url);
  1548. if (!empty($cookie)) {
  1549. curl_setopt($ch, CURLOPT_COOKIE, $cookie);
  1550. }
  1551. if ($type == 'post') {
  1552. curl_setopt($ch, CURLOPT_POST, 1);
  1553. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  1554. }
  1555. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //禁止 cURL 验证对等证书
  1556. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); //是否检测服务器的域名与证书上的是否一致
  1557. curl_setopt($ch, CURLOPT_HEADER, 0);
  1558. curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
  1559. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  1560. curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
  1561. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  1562. $ret = curl_exec($ch);
  1563. $ret = trim($ret);
  1564. curl_close($ch);
  1565. if (!preg_match("/^{/", $ret)) {
  1566. return ['code' => 'err', 'msg' => $ret];
  1567. }
  1568. $retArr = $ret ? json_decode($ret, true) : ['code' => 'err', 'msg' => 'unknow error'];
  1569. if(empty($retArr)){
  1570. $ret = iconv('gb2312','utf-8', $ret);
  1571. $retArr = json_decode($ret, true);
  1572. }
  1573. } catch (\Exception $exception) {
  1574. $retArr = ['code' => 'error', 'msg' => 'request error'];
  1575. }
  1576. return $retArr;
  1577. }
  1578. }
  1579. /**
  1580. *
  1581. * 接口请求
  1582. * @author wesmiler
  1583. * @param $url 接口地址
  1584. * @param $data
  1585. * @param $type
  1586. * @param int $timeout
  1587. * @return mixed
  1588. */
  1589. function requestGetCookies($url, $data = '', $header = '', $timeout = 60)
  1590. {
  1591. try {
  1592. set_time_limit($timeout);
  1593. $ch = curl_init($url);
  1594. if ($header) {
  1595. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  1596. }
  1597. if ($data) {
  1598. curl_setopt($ch, CURLOPT_POST, 1);
  1599. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  1600. }
  1601. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //禁止 cURL 验证对等证书
  1602. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); //是否检测服务器的域名与证书上的是否一致
  1603. curl_setopt($ch, CURLOPT_HEADER, 1);
  1604. curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
  1605. curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
  1606. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //do not output directly, use variable
  1607. curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1); //do a binary transfer
  1608. curl_setopt($ch, CURLOPT_FAILONERROR, 1);
  1609. curl_setopt($ch, CURLINFO_HEADER_OUT, 1);
  1610. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  1611. $ret = curl_exec($ch);
  1612. curl_close($ch);
  1613. $cookie = '';
  1614. //file_put_contents("./logs/page_".date('YmdHis').".html", $url."\n".$ret);
  1615. $preg_cookie = '/(Set-Cookie|set-cookie): (.*?);/m';
  1616. if ($ret && preg_match_all($preg_cookie, $ret, $cookies)) {
  1617. if (isset($cookies[2])) {
  1618. array_filter($cookies[2]);
  1619. $cookie = implode(';', $cookies[2]);
  1620. }
  1621. }
  1622. //file_put_contents("./logs/cookies_".date('YmdHis').".txt", $cookie);
  1623. } catch (\Exception $exception) {
  1624. $cookie = '';
  1625. }
  1626. return $cookie;
  1627. }
  1628. /**
  1629. * HTTP请求
  1630. * @param $url 链接
  1631. * @param $data 提交数据
  1632. * @param string $type 请求类型:get post
  1633. * @param string $dataType 返回数据类型 array json
  1634. * @param int $timeout
  1635. * @return mixed
  1636. */
  1637. function grabRequest($url, $header = [], $data = [], $type = 'get', $dataType = 'array', $cookie = '', $timeout = 60)
  1638. {
  1639. $getData = $data && is_array($data) ? http_build_query($data) : '';
  1640. $url = strtolower($type) == 'get' ? $url . (strpos($url, '?') === false && $getData ? '?' : '') . $getData : $url;
  1641. $ch = curl_init($url);
  1642. if ($header) {
  1643. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  1644. } else {
  1645. curl_setopt($ch, CURLOPT_HEADER, 0);
  1646. }
  1647. if (!empty($cookie)) {
  1648. curl_setopt($ch, CURLOPT_COOKIE, $cookie);
  1649. }
  1650. curl_setopt($ch, CURLOPT_POST, 1);
  1651. curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
  1652. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  1653. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  1654. curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
  1655. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查,0-规避ssl的证书检查
  1656. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  1657. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  1658. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  1659. $ret = curl_exec($ch);
  1660. curl_close($ch);
  1661. if (strtolower($dataType) == 'array' && !is_array($ret)) {
  1662. $ret = json_decode($ret, true);
  1663. }
  1664. return $ret;
  1665. }