common.php 49 KB

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