common.php 51 KB

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