common.php 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697
  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(100000, 999999);
  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 json_encode($result, 256);
  1087. }
  1088. }
  1089. if (!function_exists('lang')) {
  1090. /**
  1091. * 语言转换
  1092. * @param $value
  1093. * @return array|\Illuminate\Contracts\Translation\Translator|null|string
  1094. */
  1095. function lang($value, $app = 'api')
  1096. {
  1097. $value = $app ? $app . '.' . $value : $value;
  1098. return trim(trans($value), $app . '.');
  1099. }
  1100. }
  1101. if (!function_exists('num2rmb')) {
  1102. /**
  1103. * 数字金额转大写
  1104. * @param float $num 金额
  1105. * @return string 返回大写金额
  1106. * @author wesmiler
  1107. * @date 2019/6/6
  1108. */
  1109. function num2rmb($num)
  1110. {
  1111. $c1 = "零壹贰叁肆伍陆柒捌玖";
  1112. $c2 = "分角元拾佰仟万拾佰仟亿";
  1113. $num = round($num, 2);
  1114. $num = $num * 100;
  1115. if (strlen($num) > 10) {
  1116. return "oh,sorry,the number is too long!";
  1117. }
  1118. $i = 0;
  1119. $c = "";
  1120. while (1) {
  1121. if ($i == 0) {
  1122. $n = substr($num, strlen($num) - 1, 1);
  1123. } else {
  1124. $n = $num % 10;
  1125. }
  1126. $p1 = substr($c1, 3 * $n, 3);
  1127. $p2 = substr($c2, 3 * $i, 3);
  1128. if ($n != '0' || ($n == '0' && ($p2 == '亿' || $p2 == '万' || $p2 == '元'))) {
  1129. $c = $p1 . $p2 . $c;
  1130. } else {
  1131. $c = $p1 . $c;
  1132. }
  1133. $i = $i + 1;
  1134. $num = $num / 10;
  1135. $num = (int)$num;
  1136. if ($num == 0) {
  1137. break;
  1138. }
  1139. }
  1140. $j = 0;
  1141. $slen = strlen($c);
  1142. while ($j < $slen) {
  1143. $m = substr($c, $j, 6);
  1144. if ($m == '零元' || $m == '零万' || $m == '零亿' || $m == '零零') {
  1145. $left = substr($c, 0, $j);
  1146. $right = substr($c, $j + 3);
  1147. $c = $left . $right;
  1148. $j = $j - 3;
  1149. $slen = $slen - 3;
  1150. }
  1151. $j = $j + 3;
  1152. }
  1153. if (substr($c, strlen($c) - 3, 3) == '零') {
  1154. $c = substr($c, 0, strlen($c) - 3);
  1155. } // if there is a '0' on the end , chop it out
  1156. return $c . "整";
  1157. }
  1158. }
  1159. if (!function_exists('object_array')) {
  1160. /**
  1161. * 对象转数组
  1162. * @param $object 对象
  1163. * @return mixed 返回结果
  1164. * @author wesmiler
  1165. * @date 2019/5/23
  1166. */
  1167. function object_array($object)
  1168. {
  1169. //先编码成json字符串,再解码成数组
  1170. return json_decode(json_encode($object), true);
  1171. }
  1172. }
  1173. if (!function_exists('parse_attr')) {
  1174. /**
  1175. * 配置值解析成数组
  1176. * @param string $value 参数值
  1177. * @return array 返回结果
  1178. * @author wesmiler
  1179. * @date 2019/6/6
  1180. */
  1181. function parse_attr($value = '')
  1182. {
  1183. if (is_array($value)) {
  1184. return $value;
  1185. }
  1186. $array = preg_split('/[,;\r\n]+/', trim($value, ",;\r\n"));
  1187. if (strpos($value, ':')) {
  1188. $value = array();
  1189. foreach ($array as $val) {
  1190. list($k, $v) = explode(':', $val);
  1191. $value[$k] = $v;
  1192. }
  1193. } else {
  1194. $value = $array;
  1195. }
  1196. return $value;
  1197. }
  1198. }
  1199. if (!function_exists('strip_html_tags')) {
  1200. /**
  1201. * 去除HTML标签、图像等 仅保留文本
  1202. * @param string $str 字符串
  1203. * @param int $length 长度
  1204. * @return string 返回结果
  1205. * @author wesmiler
  1206. * @date 2019/5/23
  1207. */
  1208. function strip_html_tags($str, $length = 0)
  1209. {
  1210. // 把一些预定义的 HTML 实体转换为字符
  1211. $str = htmlspecialchars_decode($str);
  1212. // 将空格替换成空
  1213. $str = str_replace("&nbsp;", "", $str);
  1214. // 函数剥去字符串中的 HTML、XML 以及 PHP 的标签,获取纯文本内容
  1215. $str = strip_tags($str);
  1216. $str = str_replace(array("\n", "\r\n", "\r"), ' ', $str);
  1217. $preg = '/<script[\s\S]*?<\/script>/i';
  1218. // 剥离JS代码
  1219. $str = preg_replace($preg, "", $str, -1);
  1220. if ($length == 2) {
  1221. // 返回字符串中的前100字符串长度的字符
  1222. $str = mb_substr($str, 0, $length, "utf-8");
  1223. }
  1224. return $str;
  1225. }
  1226. }
  1227. if (!function_exists('sub_str')) {
  1228. /**
  1229. * 字符串截取
  1230. * @param string $str 需要截取的字符串
  1231. * @param int $start 开始位置
  1232. * @param int $length 截取长度
  1233. * @param bool $suffix 截断显示字符
  1234. * @param string $charset 编码格式
  1235. * @return string 返回结果
  1236. * @author wesmiler
  1237. * @date 2019/5/23
  1238. */
  1239. function sub_str($str, $start = 0, $length = 10, $suffix = true, $charset = "utf-8")
  1240. {
  1241. if (function_exists("mb_substr")) {
  1242. $slice = mb_substr($str, $start, $length, $charset);
  1243. } elseif (function_exists('iconv_substr')) {
  1244. $slice = iconv_substr($str, $start, $length, $charset);
  1245. } else {
  1246. $re['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
  1247. $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
  1248. $re['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
  1249. $re['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
  1250. preg_match_all($re[$charset], $str, $match);
  1251. $slice = join("", array_slice($match[0], $start, $length));
  1252. }
  1253. $omit = mb_strlen($str) >= $length ? '...' : '';
  1254. return $suffix ? $slice . $omit : $slice;
  1255. }
  1256. }
  1257. if (!function_exists('save_image')) {
  1258. /**
  1259. * 保存图片
  1260. * @param string $img_url 网络图片地址
  1261. * @param string $save_dir 图片保存目录
  1262. * @return string 返回路径
  1263. * @author wesmiler
  1264. * @date 2019/5/23
  1265. */
  1266. function save_image($img_url, $save_dir = '/')
  1267. {
  1268. if (!$img_url) {
  1269. return false;
  1270. }
  1271. $save_dir = trim($save_dir, "/");
  1272. $imgExt = pathinfo($img_url, PATHINFO_EXTENSION);
  1273. // 是否是本站图片
  1274. if (strpos($img_url, IMG_URL) !== false) {
  1275. // 是否是临时文件
  1276. if (strpos($img_url, 'temp') === false) {
  1277. return str_replace(IMG_URL, "", $img_url);
  1278. }
  1279. $new_path = create_image_path($save_dir, $imgExt);
  1280. $old_path = str_replace(IMG_URL, ATTACHMENT_PATH, $img_url);
  1281. if (!file_exists($old_path)) {
  1282. return false;
  1283. }
  1284. rename($old_path, ATTACHMENT_PATH . $new_path);
  1285. return $new_path;
  1286. } else {
  1287. // 保存远程图片
  1288. $new_path = save_remote_image($img_url, $save_dir);
  1289. }
  1290. return $new_path;
  1291. }
  1292. }
  1293. if (!function_exists('create_image_path')) {
  1294. /**
  1295. * 创建图片存储目录
  1296. * @param string $save_dir 存储目录
  1297. * @param string $image_ext 图片后缀
  1298. * @param string $image_root 图片存储根目录路径
  1299. * @return string 返回文件目录
  1300. * @author wesmiler
  1301. * @date 2019/5/23
  1302. */
  1303. function create_image_path($save_dir = "", $image_ext = "", $image_root = IMG_PATH)
  1304. {
  1305. $image_dir = date("/Ymd/");
  1306. if ($image_dir) {
  1307. $image_dir = ($save_dir ? "/" : '') . $save_dir . $image_dir;
  1308. }
  1309. // 未指定后缀默认使用JPG
  1310. if (!$image_ext) {
  1311. $image_ext = "jpg";
  1312. }
  1313. $image_path = $image_root . $image_dir;
  1314. if (!is_dir($image_path)) {
  1315. // 创建目录并赋予权限
  1316. mkdir($image_path, 0777, true);
  1317. }
  1318. $file_name = substr(md5(time() . rand(0, 999999)), 8, 16) . rand(100, 999) . ".{$image_ext}";
  1319. $file_path = str_replace(ATTACHMENT_PATH, "", IMG_PATH) . $image_dir . $file_name;
  1320. return $file_path;
  1321. }
  1322. }
  1323. if (!function_exists('save_remote_image')) {
  1324. /**
  1325. * 保存网络图片到本地
  1326. * @param string $img_url 网络图片地址
  1327. * @param string $save_dir 保存目录
  1328. * @return bool|string 图片路径
  1329. * @author wesmiler
  1330. * @date 2019/5/23
  1331. */
  1332. function save_remote_image($img_url, $save_dir = '/')
  1333. {
  1334. $content = file_get_contents($img_url);
  1335. if (!$content) {
  1336. return false;
  1337. }
  1338. if ($content{0} . $content{1} == "\xff\xd8") {
  1339. $image_ext = 'jpg';
  1340. } elseif ($content{0} . $content{1} . $content{2} == "\x47\x49\x46") {
  1341. $image_ext = 'gif';
  1342. } elseif ($content{0} . $content{1} . $content{2} == "\x89\x50\x4e") {
  1343. $image_ext = 'png';
  1344. } else {
  1345. // 不是有效图片
  1346. return false;
  1347. }
  1348. $save_path = create_image_path($save_dir, $image_ext);
  1349. return file_put_contents(IMG_PATH . $save_path, $content) ? $save_path : false;
  1350. }
  1351. }
  1352. if (!function_exists('save_image_content')) {
  1353. /**
  1354. * 富文本信息处理
  1355. * @param string $content 富文本内容
  1356. * @param bool $title 标题
  1357. * @param string $path 图片存储路径
  1358. * @return bool|int 返回结果
  1359. * @author wesmiler
  1360. * @date 2020-04-21
  1361. */
  1362. function save_image_content(&$content, $title = false, $path = 'article')
  1363. {
  1364. // 图片处理
  1365. preg_match_all("/<img.*?src=[\"|\']?(.*?)[\"|\']?\s.*?>/i", str_ireplace("\\", "", $content), $match);
  1366. if ($match[1]) {
  1367. foreach ($match[1] as $id => $val) {
  1368. $save_image = save_image($val, $path);
  1369. if ($save_image) {
  1370. $content = str_replace($val, "[IMG_URL]" . $save_image, $content);
  1371. }
  1372. }
  1373. }
  1374. // 视频处理
  1375. preg_match_all("/<embed .*?src=[\"|\']?(.*?)[\"|\']?\s.*?>/i", str_ireplace("\\", "", $content), $match2);
  1376. if ($match2[1]) {
  1377. foreach ($match2[1] as $vo) {
  1378. $save_video = save_image($vo, $path);
  1379. if ($save_video) {
  1380. $content = str_replace($vo, "[IMG_URL]" . str_replace(ATTACHMENT_PATH, "", IMG_PATH) . $save_video, $content);
  1381. }
  1382. }
  1383. }
  1384. // 提示标签替换
  1385. if ((strpos($content, 'alt=\"\"') !== false) && $title) {
  1386. $content = str_replace('alt=\"\"', 'alt=\"' . $title . '\"', $content);
  1387. }
  1388. return true;
  1389. }
  1390. }
  1391. if (!function_exists('upload_image')) {
  1392. /**
  1393. * 上传图片
  1394. * @param $request 网络请求
  1395. * @param string $form_name 文件表单名
  1396. * @return array 返回结果
  1397. * @author wesmiler
  1398. * @date 2019/5/23
  1399. */
  1400. function upload_image($request, $form_name = 'file')
  1401. {
  1402. // 检测请求中是否包含name=$form_name的上传文件
  1403. if (!$request->hasFile($form_name)) {
  1404. return message("请上传文件", false);
  1405. }
  1406. // 文件对象
  1407. $file = $request->file($form_name);
  1408. // 判断图片上传是否错误
  1409. if (!$file->isValid()) {
  1410. // 文件上传失败
  1411. return message("上传文件验证失败", false);
  1412. }
  1413. // 文件原名
  1414. $original_name = $file->getClientOriginalName();
  1415. // 文件扩展名(文件后缀)
  1416. $ext = $file->getClientOriginalExtension();
  1417. // 临时文件的绝对路径
  1418. $real_path = $file->getRealPath();
  1419. // 文件类型
  1420. $type = $file->getClientMimeType();
  1421. // 文件大小
  1422. $size = $file->getSize();
  1423. // 文件大小校验
  1424. if ($size > 5 * 1024 * 1024) {
  1425. return message("文件大小超过了5M", false);
  1426. }
  1427. // 文件后缀校验
  1428. $ext_arr = array('jpg', 'jpeg', 'png', 'gif');
  1429. if (!in_array($ext, $ext_arr)) {
  1430. return message("文件格式不正确", false);
  1431. }
  1432. // 文件路径
  1433. $file_dir = UPLOAD_TEMP_PATH . "/" . date('Ymd');
  1434. // 检测文件路径是否存在,不存在则创建
  1435. if (!file_exists($file_dir)) {
  1436. mkdir($file_dir, 0777, true);
  1437. }
  1438. // 文件名称
  1439. $file_name = uniqid() . '.' . $ext;
  1440. // 重命名保存
  1441. $path = $file->move($file_dir, $file_name);
  1442. // 文件临时路径
  1443. $file_path = str_replace(ATTACHMENT_PATH, '', $file_dir) . '/' . $file_name;
  1444. // 返回结果
  1445. $result = [
  1446. 'img_original_name' => $original_name,
  1447. 'img_ext' => $ext,
  1448. 'img_real_path' => $real_path,
  1449. 'img_type' => $type,
  1450. 'img_size' => $size,
  1451. 'img_name' => $file_name,
  1452. 'img_path' => $file_path,
  1453. ];
  1454. return message(MESSAGE_OK, true, $result);
  1455. }
  1456. }
  1457. if (!function_exists('upload_file')) {
  1458. /**
  1459. * 上传文件
  1460. * @param $request 网络请求
  1461. * @param string $form_name 文件表单名
  1462. * @return array 返回结果
  1463. * @author wesmiler
  1464. * @date 2019/5/23
  1465. */
  1466. function upload_file($request, $form_name = 'file')
  1467. {
  1468. // 检测请求中是否包含上传的文件
  1469. if (!$request->hasFile($form_name)) {
  1470. return message("请上传文件", false);
  1471. }
  1472. // 文件对象
  1473. $file = $request->file($form_name);
  1474. // 判断图片上传是否错误
  1475. if (!$file->isValid()) {
  1476. // 文件上传失败
  1477. return message("上传文件验证失败", false);
  1478. }
  1479. // 文件原名
  1480. $original_name = $file->getClientOriginalName();
  1481. // 文件扩展名(文件后缀)
  1482. $ext = $file->getClientOriginalExtension();
  1483. // 临时文件的绝对路径
  1484. $real_path = $file->getRealPath();
  1485. // 文件类型
  1486. $type = $file->getClientMimeType();
  1487. // 文件大小
  1488. $size = $file->getSize();
  1489. // 文件大小校验(MAX=5M)
  1490. $file_max_size = 50 * 1024 * 1024;
  1491. if ($size > $file_max_size) {
  1492. return message("您上传的文件过大,最大值为" . $file_max_size / 1024 / 1024 . "MB", false);
  1493. }
  1494. // 允许上传的文件后缀
  1495. $file_exts = array('xls', 'xlsx', 'csv','mp3','mp4','WMA','MPEG');
  1496. if (!in_array($ext, $file_exts)) {
  1497. return message("文件格式不正确", false);
  1498. }
  1499. // 文件路径
  1500. $file_dir = UPLOAD_FILE_PATH . "/" . date('Ymd');
  1501. // 检测文件路径是否存在,不存在则创建
  1502. if (!file_exists($file_dir)) {
  1503. mkdir($file_dir, 0777, true);
  1504. }
  1505. // 文件名称
  1506. $file_name = uniqid() . '.' . $ext;
  1507. // 重命名保存
  1508. $path = $file->move($file_dir, $file_name);
  1509. // 文件临时路径
  1510. $file_path = str_replace(ATTACHMENT_PATH, '', $file_dir) . '/' . $file_name;
  1511. // 返回结果
  1512. $result = [
  1513. 'file_original_name' => $original_name,
  1514. 'file_ext' => $ext,
  1515. 'file_real_path' => $real_path,
  1516. 'file_type' => $type,
  1517. 'file_size' => $size,
  1518. 'file_name' => $file_name,
  1519. 'file_path' => $file_path,
  1520. ];
  1521. return message(MESSAGE_OK, true, $result);
  1522. }
  1523. }
  1524. if (!function_exists('widget')) {
  1525. /**
  1526. * 加载系统组件,传入的名字会以目录和类名区别
  1527. * 如Home.Common就代表Widget目录下的Home/Common.php这个widget。
  1528. * @param $widgetName 组件名称
  1529. * @return bool|mixed
  1530. * @author wesmiler
  1531. * @date 2019/5/23
  1532. */
  1533. function widget($widgetName)
  1534. {
  1535. $widgetNameEx = explode('.', $widgetName);
  1536. if (!isset($widgetNameEx[1])) {
  1537. return false;
  1538. }
  1539. $widgetClass = 'App\\Widget\\' . $widgetNameEx[0] . '\\' . $widgetNameEx[1];
  1540. if (app()->bound($widgetName)) {
  1541. return app()->make($widgetName);
  1542. }
  1543. app()->singleton($widgetName, function () use ($widgetClass) {
  1544. return new $widgetClass();
  1545. });
  1546. return app()->make($widgetName);
  1547. }
  1548. }