common.php 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764
  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('getter')) {
  666. /**
  667. * 获取数组的下标值
  668. * @param array $data 数据源
  669. * @param string $field 字段名称
  670. * @param string $default 默认值
  671. * @return mixed|string 返回结果
  672. * @author wesmiler
  673. * @date 2019/5/23
  674. */
  675. function getter($data, $field, $default = '')
  676. {
  677. $result = $default;
  678. if (isset($data[$field])) {
  679. $result = $data[$field];
  680. }
  681. return $result;
  682. }
  683. }
  684. if (!function_exists('get_zodiac_sign')) {
  685. /**
  686. * 根据月、日获取星座
  687. * @param $month 月份
  688. * @param $day 日期
  689. * @return string 返回结果
  690. * @author wesmiler
  691. * @date 2019/5/23
  692. */
  693. function get_zodiac_sign($month, $day)
  694. {
  695. // 检查参数有效性
  696. if ($month < 1 || $month > 12 || $day < 1 || $day > 31) {
  697. return false;
  698. }
  699. // 星座名称以及开始日期
  700. $signs = array(
  701. array("20" => "水瓶座"),
  702. array("19" => "双鱼座"),
  703. array("21" => "白羊座"),
  704. array("20" => "金牛座"),
  705. array("21" => "双子座"),
  706. array("22" => "巨蟹座"),
  707. array("23" => "狮子座"),
  708. array("23" => "处女座"),
  709. array("23" => "天秤座"),
  710. array("24" => "天蝎座"),
  711. array("22" => "射手座"),
  712. array("22" => "摩羯座")
  713. );
  714. list($sign_start, $sign_name) = each($signs[(int)$month - 1]);
  715. if ($day < $sign_start) {
  716. list($sign_start, $sign_name) = each($signs[($month - 2 < 0) ? $month = 11 : $month -= 2]);
  717. }
  718. return $sign_name;
  719. }
  720. }
  721. if (!function_exists('get_image_url')) {
  722. /**
  723. * 获取图片地址
  724. * @param $image_url 图片地址
  725. * @return string 返回图片网络地址
  726. * @author wesmiler
  727. * @date 2019/6/6
  728. */
  729. function get_image_url($image_url)
  730. {
  731. return strpos($image_url,'http') === false? IMG_URL . $image_url : $image_url;
  732. }
  733. }
  734. if (!function_exists('get_file_url')) {
  735. /**
  736. * 获取文件地址
  737. * @param $file_url 文件地址
  738. * @return string 返回图片网络地址
  739. * @author wesmiler
  740. * @date 2019/6/6
  741. */
  742. function get_file_url($file_url)
  743. {
  744. return strpos($file_url,'http') === false? IMG_URL . $file_url : $file_url;
  745. }
  746. }
  747. if (!function_exists('get_hash')) {
  748. /**
  749. * 获取HASH值
  750. * @return string 返回hash字符串
  751. * @author wesmiler
  752. * @date 2019/6/6
  753. */
  754. function get_hash()
  755. {
  756. $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()+-';
  757. $random = $chars[mt_rand(0, 73)] . $chars[mt_rand(0, 73)] . $chars[mt_rand(0, 73)]
  758. . $chars[mt_rand(0, 73)] . $chars[mt_rand(0, 73)];
  759. $content = uniqid() . $random;
  760. return sha1($content);
  761. }
  762. }
  763. if (!function_exists('get_server_ip')) {
  764. /**
  765. * 获取服务端IP地址
  766. * @return string 返回IP地址
  767. * @author wesmiler
  768. * @date 2019/6/6
  769. */
  770. function get_server_ip()
  771. {
  772. if (isset($_SERVER)) {
  773. if ($_SERVER['SERVER_ADDR']) {
  774. $server_ip = $_SERVER['SERVER_ADDR'];
  775. } else {
  776. $server_ip = $_SERVER['LOCAL_ADDR'];
  777. }
  778. } else {
  779. $server_ip = getenv('SERVER_ADDR');
  780. }
  781. return $server_ip;
  782. }
  783. }
  784. if (!function_exists('get_client_ip')) {
  785. /**
  786. * 获取客户端IP地址
  787. * @param int $type 返回类型 0 返回IP地址 1 返回IPV4地址数字
  788. * @param bool $adv 否进行高级模式获取(有可能被伪装)
  789. * @return mixed 返回IP
  790. * @author wesmiler
  791. * @date 2019/5/23
  792. */
  793. function get_client_ip($type = 0, $adv = false)
  794. {
  795. $type = $type ? 1 : 0;
  796. static $ip = null;
  797. if ($ip !== null) {
  798. return $ip[$type];
  799. }
  800. if ($adv) {
  801. if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  802. $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
  803. $pos = array_search('unknown', $arr);
  804. if (false !== $pos) {
  805. unset($arr[$pos]);
  806. }
  807. $ip = trim($arr[0]);
  808. } elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
  809. $ip = $_SERVER['HTTP_CLIENT_IP'];
  810. } elseif (isset($_SERVER['REMOTE_ADDR'])) {
  811. $ip = $_SERVER['REMOTE_ADDR'];
  812. }
  813. } elseif (isset($_SERVER['REMOTE_ADDR'])) {
  814. $ip = $_SERVER['REMOTE_ADDR'];
  815. }
  816. // IP地址合法验证
  817. $long = sprintf("%u", ip2long($ip));
  818. $ip = $long ? array($ip, $long) : array('0.0.0.0', 0);
  819. return $ip[$type];
  820. }
  821. }
  822. if (!function_exists('formatName')) {
  823. /**
  824. * 格式化账号
  825. * @param $name
  826. */
  827. function formatName($name)
  828. {
  829. if(mb_strlen($name,'utf-8')<=6){
  830. return mb_substr($name, 0, 1,'utf-8') . '****' . mb_substr($name, -1, 1,'utf-8');
  831. }else{
  832. return mb_substr($name, 0, 2,'utf-8') . '****' . mb_substr($name, -2, 2,'utf-8');
  833. }
  834. }
  835. }
  836. if (!function_exists('moneyFormat')) {
  837. /**
  838. * 金钱格式化小数点
  839. * @param $money
  840. * @param int $decimal 小数点位数,默认系统配置2位
  841. * @return float
  842. */
  843. function moneyFormat($money, $decimal = null)
  844. {
  845. $formatConfig = [];
  846. $charset = !empty($formatConfig['charset']) ? trim($formatConfig['charset']) : 'utf-8';
  847. if ($decimal === null) {
  848. $decimal = isset($formatConfig['moneyDecimal']) ? intval($formatConfig['moneyDecimal']) : 2;
  849. }
  850. $money = round($money, $decimal + 1);
  851. $data = explode('.', $money);
  852. $money = isset($data[0]) ? $data[0] : 0;
  853. $float = isset($data[1]) ? $data[1] : '';
  854. $len = $float ? mb_strlen($float, $charset) : 0;
  855. $decimal = $decimal >= 0 ? intval($decimal) : 2;
  856. $num1 = isset($data[1]) ? $data[1] : '';
  857. $float = $len >= $decimal ? mb_substr($num1, 0, $decimal, $charset) : $float . str_repeat('0', $decimal - $len);
  858. $money = $money . '.' . $float;
  859. return number_format($money, $decimal, '.', '');
  860. }
  861. }
  862. if (!function_exists('get_guid_v4')) {
  863. /**
  864. * 获取唯一性GUID
  865. * @param bool $trim 是否去除{}
  866. * @return string 返回GUID字符串
  867. * @author wesmiler
  868. * @date 2019/6/6
  869. */
  870. function get_guid_v4($trim = true)
  871. {
  872. // Windows
  873. if (function_exists('com_create_guid') === true) {
  874. $charid = com_create_guid();
  875. return $trim == true ? trim($charid, '{}') : $charid;
  876. }
  877. // OSX/Linux
  878. if (function_exists('openssl_random_pseudo_bytes') === true) {
  879. $data = openssl_random_pseudo_bytes(16);
  880. $data[6] = chr(ord($data[6]) & 0x0f | 0x40); // set version to 0100
  881. $data[8] = chr(ord($data[8]) & 0x3f | 0x80); // set bits 6-7 to 10
  882. return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
  883. }
  884. // Fallback (PHP 4.2+)
  885. mt_srand((double)microtime() * 10000);
  886. $charid = strtolower(md5(uniqid(rand(), true)));
  887. $hyphen = chr(45); // "-"
  888. $lbrace = $trim ? "" : chr(123); // "{"
  889. $rbrace = $trim ? "" : chr(125); // "}"
  890. $guidv4 = $lbrace .
  891. substr($charid, 0, 8) . $hyphen .
  892. substr($charid, 8, 4) . $hyphen .
  893. substr($charid, 12, 4) . $hyphen .
  894. substr($charid, 16, 4) . $hyphen .
  895. substr($charid, 20, 12) .
  896. $rbrace;
  897. return $guidv4;
  898. }
  899. }
  900. if (!function_exists('is_email')) {
  901. /**
  902. * 判断是否为邮箱
  903. * @param string $str 邮箱
  904. * @return false 返回结果true或false
  905. * @author wesmiler
  906. * @date 2019/5/23
  907. */
  908. function is_email($str)
  909. {
  910. return preg_match('/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/', $str);
  911. }
  912. }
  913. if (!function_exists('is_mobile')) {
  914. /**
  915. * 判断是否为手机号
  916. * @param string $num 手机号码
  917. * @return false 返回结果true或false
  918. * @author wesmiler
  919. * @date 2019/5/23
  920. */
  921. function is_mobile($num)
  922. {
  923. return preg_match('/^1(3|4|5|7|8)\d{9}$/', $num);
  924. }
  925. }
  926. if (!function_exists('is_zipcode')) {
  927. /**
  928. * 验证邮编是否正确
  929. * @param string $code 邮编
  930. * @return false 返回结果true或false
  931. * @author wesmiler
  932. * @date 2019/5/23
  933. */
  934. function is_zipcode($code)
  935. {
  936. return preg_match('/^[1-9][0-9]{5}$/', $code);
  937. }
  938. }
  939. if (!function_exists('is_idcard')) {
  940. /**
  941. * 验证身份证是否正确
  942. * @param string $idno 身份证号
  943. * @return bool 返回结果true或false
  944. * @author wesmiler
  945. * @date 2019/5/23
  946. */
  947. function is_idcard($idno)
  948. {
  949. $idno = strtoupper($idno);
  950. $regx = '/(^\d{15}$)|(^\d{17}([0-9]|X)$)/';
  951. $arr_split = array();
  952. if (!preg_match($regx, $idno)) {
  953. return false;
  954. }
  955. // 检查15位
  956. if (15 == strlen($idno)) {
  957. $regx = '/^(\d{6})+(\d{2})+(\d{2})+(\d{2})+(\d{3})$/';
  958. @preg_match($regx, $idno, $arr_split);
  959. $dtm_birth = "19" . $arr_split[2] . '/' . $arr_split[3] . '/' . $arr_split[4];
  960. if (!strtotime($dtm_birth)) {
  961. return false;
  962. } else {
  963. return true;
  964. }
  965. } else {
  966. // 检查18位
  967. $regx = '/^(\d{6})+(\d{4})+(\d{2})+(\d{2})+(\d{3})([0-9]|X)$/';
  968. @preg_match($regx, $idno, $arr_split);
  969. $dtm_birth = $arr_split[2] . '/' . $arr_split[3] . '/' . $arr_split[4];
  970. // 检查生日日期是否正确
  971. if (!strtotime($dtm_birth)) {
  972. return false;
  973. } else {
  974. // 检验18位身份证的校验码是否正确。
  975. // 校验位按照ISO 7064:1983.MOD 11-2的规定生成,X可以认为是数字10。
  976. $arr_int = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
  977. $arr_ch = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
  978. $sign = 0;
  979. for ($i = 0; $i < 17; $i++) {
  980. $b = (int)$idno{$i};
  981. $w = $arr_int[$i];
  982. $sign += $b * $w;
  983. }
  984. $n = $sign % 11;
  985. $val_num = $arr_ch[$n];
  986. if ($val_num != substr($idno, 17, 1)) {
  987. return false;
  988. } else {
  989. return true;
  990. }
  991. }
  992. }
  993. }
  994. }
  995. if (!function_exists('is_empty')) {
  996. /**
  997. * 判断是否为空
  998. * @param $value 参数值
  999. * @return bool 返回结果true或false
  1000. * @author wesmiler
  1001. * @date 2019/6/5
  1002. */
  1003. function is_empty($value)
  1004. {
  1005. // 判断是否存在该值
  1006. if (!isset($value)) {
  1007. return true;
  1008. }
  1009. // 判断是否为empty
  1010. if (empty($value)) {
  1011. return true;
  1012. }
  1013. // 判断是否为null
  1014. if ($value === null) {
  1015. return true;
  1016. }
  1017. // 判断是否为空字符串
  1018. if (trim($value) === '') {
  1019. return true;
  1020. }
  1021. // 默认返回false
  1022. return false;
  1023. }
  1024. }
  1025. if (!function_exists('mkdirs')) {
  1026. /**
  1027. * 递归创建目录
  1028. * @param string $dir 需要创建的目录路径
  1029. * @param int $mode 权限值
  1030. * @return bool 返回结果true或false
  1031. * @author wesmiler
  1032. * @date 2019/6/6
  1033. */
  1034. function mkdirs($dir, $mode = 0777)
  1035. {
  1036. if (is_dir($dir) || mkdir($dir, $mode, true)) {
  1037. return true;
  1038. }
  1039. if (!mkdirs(dirname($dir), $mode)) {
  1040. return false;
  1041. }
  1042. return mkdir($dir, $mode, true);
  1043. }
  1044. }
  1045. if (!function_exists('rmdirs')) {
  1046. /**
  1047. * 删除文件夹
  1048. * @param string $dir 文件夹路径
  1049. * @param bool $rmself 是否删除本身true或false
  1050. * @return bool 返回删除结果
  1051. * @author wesmiler
  1052. * @date 2019/6/6
  1053. */
  1054. function rmdirs($dir, $rmself = true)
  1055. {
  1056. if (!is_dir($dir)) {
  1057. return false;
  1058. }
  1059. $files = new RecursiveIteratorIterator(
  1060. new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS),
  1061. RecursiveIteratorIterator::CHILD_FIRST
  1062. );
  1063. foreach ($files as $file) {
  1064. $todo = ($file->isDir() ? 'rmdir' : 'unlink');
  1065. $todo($file->getRealPath());
  1066. }
  1067. if ($rmself) {
  1068. @rmdir($dir);
  1069. }
  1070. return true;
  1071. }
  1072. }
  1073. if (!function_exists('copydirs')) {
  1074. /**
  1075. * 复制文件夹
  1076. * @param string $source 原文件夹路径
  1077. * @param string $dest 目的文件夹路径
  1078. * @author wesmiler
  1079. * @date 2019/6/6
  1080. */
  1081. function copydirs($source, $dest)
  1082. {
  1083. if (!is_dir($dest)) {
  1084. mkdir($dest, 0755, true);
  1085. }
  1086. $iterator = new RecursiveIteratorIterator(
  1087. new RecursiveDirectoryIterator($source, RecursiveDirectoryIterator::SKIP_DOTS),
  1088. RecursiveIteratorIterator::SELF_FIRST
  1089. );
  1090. foreach ($iterator as $item) {
  1091. if ($item->isDir()) {
  1092. $sent_dir = $dest . "/" . $iterator->getSubPathName();
  1093. if (!is_dir($sent_dir)) {
  1094. mkdir($sent_dir, 0755, true);
  1095. }
  1096. } else {
  1097. copy($item, $dest . "/" . $iterator->getSubPathName());
  1098. }
  1099. }
  1100. }
  1101. }
  1102. if (!function_exists('message')) {
  1103. /**
  1104. * 消息数组
  1105. * @param string $msg 提示文字
  1106. * @param bool $success 是否成功true或false
  1107. * @param array $data 结果数据
  1108. * @param int $code 编码
  1109. * @return array 返回结果
  1110. * @author wesmiler
  1111. * @date 2019/5/28
  1112. */
  1113. function message($msg = "操作成功", $success = true, $data = [], $code = 0)
  1114. {
  1115. $msg = is_int($msg)? lang($msg, 'api') : $msg;
  1116. $result = ['success' => $success, 'msg' => $msg, 'data' => $data];
  1117. if ($success) {
  1118. $result['code'] = 0;
  1119. } else {
  1120. $result['code'] = $code ? $code : -1;
  1121. }
  1122. return $result;
  1123. }
  1124. }
  1125. if (!function_exists('getValidatorError')) {
  1126. /**
  1127. * 错误验证
  1128. * @param $error
  1129. * @return mixed
  1130. */
  1131. function getValidatorError($validator)
  1132. {
  1133. $errors = $validator->errors()->all();
  1134. return isset($errors[0]) ? $errors[0] : 'validator error';
  1135. }
  1136. }
  1137. if (!function_exists('getDateText')) {
  1138. /**
  1139. * 获取实际日期格式
  1140. * @param $error
  1141. * @return mixed
  1142. */
  1143. function getDateText($date, $format='Y-m-d')
  1144. {
  1145. $dateText = str_replace(['年','月','日'],['-','-',''], $date);
  1146. if(!preg_match("/年/", $date)){
  1147. $dateText = date('Y-').$dateText;
  1148. }
  1149. return date($format, strtotime($dateText));
  1150. }
  1151. }
  1152. if (!function_exists('lang')) {
  1153. /**
  1154. * 语言转换
  1155. * @param $value
  1156. * @return array|\Illuminate\Contracts\Translation\Translator|null|string
  1157. */
  1158. function lang($value, $app = 'api')
  1159. {
  1160. $value = $app ? $app . '.' . $value : $value;
  1161. return trim(trans($value), $app . '.');
  1162. }
  1163. }
  1164. if (!function_exists('num2rmb')) {
  1165. /**
  1166. * 数字金额转大写
  1167. * @param float $num 金额
  1168. * @return string 返回大写金额
  1169. * @author wesmiler
  1170. * @date 2019/6/6
  1171. */
  1172. function num2rmb($num)
  1173. {
  1174. $c1 = "零壹贰叁肆伍陆柒捌玖";
  1175. $c2 = "分角元拾佰仟万拾佰仟亿";
  1176. $num = round($num, 2);
  1177. $num = $num * 100;
  1178. if (strlen($num) > 10) {
  1179. return "oh,sorry,the number is too long!";
  1180. }
  1181. $i = 0;
  1182. $c = "";
  1183. while (1) {
  1184. if ($i == 0) {
  1185. $n = substr($num, strlen($num) - 1, 1);
  1186. } else {
  1187. $n = $num % 10;
  1188. }
  1189. $p1 = substr($c1, 3 * $n, 3);
  1190. $p2 = substr($c2, 3 * $i, 3);
  1191. if ($n != '0' || ($n == '0' && ($p2 == '亿' || $p2 == '万' || $p2 == '元'))) {
  1192. $c = $p1 . $p2 . $c;
  1193. } else {
  1194. $c = $p1 . $c;
  1195. }
  1196. $i = $i + 1;
  1197. $num = $num / 10;
  1198. $num = (int)$num;
  1199. if ($num == 0) {
  1200. break;
  1201. }
  1202. }
  1203. $j = 0;
  1204. $slen = strlen($c);
  1205. while ($j < $slen) {
  1206. $m = substr($c, $j, 6);
  1207. if ($m == '零元' || $m == '零万' || $m == '零亿' || $m == '零零') {
  1208. $left = substr($c, 0, $j);
  1209. $right = substr($c, $j + 3);
  1210. $c = $left . $right;
  1211. $j = $j - 3;
  1212. $slen = $slen - 3;
  1213. }
  1214. $j = $j + 3;
  1215. }
  1216. if (substr($c, strlen($c) - 3, 3) == '零') {
  1217. $c = substr($c, 0, strlen($c) - 3);
  1218. } // if there is a '0' on the end , chop it out
  1219. return $c . "整";
  1220. }
  1221. }
  1222. if (!function_exists('object_array')) {
  1223. /**
  1224. * 对象转数组
  1225. * @param $object 对象
  1226. * @return mixed 返回结果
  1227. * @author wesmiler
  1228. * @date 2019/5/23
  1229. */
  1230. function object_array($object)
  1231. {
  1232. //先编码成json字符串,再解码成数组
  1233. return json_decode(json_encode($object), true);
  1234. }
  1235. }
  1236. if (!function_exists('parse_attr')) {
  1237. /**
  1238. * 配置值解析成数组
  1239. * @param string $value 参数值
  1240. * @return array 返回结果
  1241. * @author wesmiler
  1242. * @date 2019/6/6
  1243. */
  1244. function parse_attr($value = '')
  1245. {
  1246. if (is_array($value)) {
  1247. return $value;
  1248. }
  1249. $array = preg_split('/[,;\r\n]+/', trim($value, ",;\r\n"));
  1250. if (strpos($value, ':')) {
  1251. $value = array();
  1252. foreach ($array as $val) {
  1253. list($k, $v) = explode(':', $val);
  1254. $value[$k] = $v;
  1255. }
  1256. } else {
  1257. $value = $array;
  1258. }
  1259. return $value;
  1260. }
  1261. }
  1262. if (!function_exists('strip_html_tags')) {
  1263. /**
  1264. * 去除HTML标签、图像等 仅保留文本
  1265. * @param string $str 字符串
  1266. * @param int $length 长度
  1267. * @return string 返回结果
  1268. * @author wesmiler
  1269. * @date 2019/5/23
  1270. */
  1271. function strip_html_tags($str, $length = 0)
  1272. {
  1273. // 把一些预定义的 HTML 实体转换为字符
  1274. $str = htmlspecialchars_decode($str);
  1275. // 将空格替换成空
  1276. $str = str_replace("&nbsp;", "", $str);
  1277. // 函数剥去字符串中的 HTML、XML 以及 PHP 的标签,获取纯文本内容
  1278. $str = strip_tags($str);
  1279. $str = str_replace(array("\n", "\r\n", "\r"), ' ', $str);
  1280. $preg = '/<script[\s\S]*?<\/script>/i';
  1281. // 剥离JS代码
  1282. $str = preg_replace($preg, "", $str, -1);
  1283. if ($length == 2) {
  1284. // 返回字符串中的前100字符串长度的字符
  1285. $str = mb_substr($str, 0, $length, "utf-8");
  1286. }
  1287. return $str;
  1288. }
  1289. }
  1290. if (!function_exists('sub_str')) {
  1291. /**
  1292. * 字符串截取
  1293. * @param string $str 需要截取的字符串
  1294. * @param int $start 开始位置
  1295. * @param int $length 截取长度
  1296. * @param bool $suffix 截断显示字符
  1297. * @param string $charset 编码格式
  1298. * @return string 返回结果
  1299. * @author wesmiler
  1300. * @date 2019/5/23
  1301. */
  1302. function sub_str($str, $start = 0, $length = 10, $suffix = true, $charset = "utf-8")
  1303. {
  1304. if (function_exists("mb_substr")) {
  1305. $slice = mb_substr($str, $start, $length, $charset);
  1306. } elseif (function_exists('iconv_substr')) {
  1307. $slice = iconv_substr($str, $start, $length, $charset);
  1308. } else {
  1309. $re['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
  1310. $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
  1311. $re['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
  1312. $re['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
  1313. preg_match_all($re[$charset], $str, $match);
  1314. $slice = join("", array_slice($match[0], $start, $length));
  1315. }
  1316. $omit = mb_strlen($str) >= $length ? '...' : '';
  1317. return $suffix ? $slice . $omit : $slice;
  1318. }
  1319. }
  1320. if (!function_exists('save_image')) {
  1321. /**
  1322. * 保存图片
  1323. * @param string $img_url 网络图片地址
  1324. * @param string $save_dir 图片保存目录
  1325. * @return string 返回路径
  1326. * @author wesmiler
  1327. * @date 2019/5/23
  1328. */
  1329. function save_image($img_url, $save_dir = '/')
  1330. {
  1331. if (!$img_url) {
  1332. return false;
  1333. }
  1334. $save_dir = trim($save_dir, "/");
  1335. $imgExt = pathinfo($img_url, PATHINFO_EXTENSION);
  1336. // 是否是本站图片
  1337. if (strpos($img_url, IMG_URL) !== false) {
  1338. // 是否是临时文件
  1339. if (strpos($img_url, 'temp') === false) {
  1340. return str_replace(IMG_URL, "", $img_url);
  1341. }
  1342. $new_path = create_image_path($save_dir, $imgExt);
  1343. $old_path = str_replace(IMG_URL, ATTACHMENT_PATH, $img_url);
  1344. if (!file_exists($old_path)) {
  1345. return false;
  1346. }
  1347. rename($old_path, ATTACHMENT_PATH . $new_path);
  1348. return $new_path;
  1349. } else {
  1350. // 保存远程图片
  1351. $new_path = save_remote_image($img_url, $save_dir);
  1352. }
  1353. return $new_path;
  1354. }
  1355. }
  1356. if (!function_exists('create_image_path')) {
  1357. /**
  1358. * 创建图片存储目录
  1359. * @param string $save_dir 存储目录
  1360. * @param string $image_ext 图片后缀
  1361. * @param string $image_root 图片存储根目录路径
  1362. * @return string 返回文件目录
  1363. * @author wesmiler
  1364. * @date 2019/5/23
  1365. */
  1366. function create_image_path($save_dir = "", $image_ext = "", $image_root = IMG_PATH)
  1367. {
  1368. $image_dir = date("/Ymd/");
  1369. if ($image_dir) {
  1370. $image_dir = ($save_dir ? "/" : '') . $save_dir . $image_dir;
  1371. }
  1372. // 未指定后缀默认使用JPG
  1373. if (!$image_ext) {
  1374. $image_ext = "jpg";
  1375. }
  1376. $image_path = $image_root . $image_dir;
  1377. if (!is_dir($image_path)) {
  1378. // 创建目录并赋予权限
  1379. mkdir($image_path, 0777, true);
  1380. }
  1381. $file_name = substr(md5(time() . rand(0, 999999)), 8, 16) . rand(100, 999) . ".{$image_ext}";
  1382. $file_path = str_replace(ATTACHMENT_PATH, "", IMG_PATH) . $image_dir . $file_name;
  1383. return $file_path;
  1384. }
  1385. }
  1386. if (!function_exists('save_remote_image')) {
  1387. /**
  1388. * 保存网络图片到本地
  1389. * @param string $img_url 网络图片地址
  1390. * @param string $save_dir 保存目录
  1391. * @return bool|string 图片路径
  1392. * @author wesmiler
  1393. * @date 2019/5/23
  1394. */
  1395. function save_remote_image($img_url, $save_dir = '/')
  1396. {
  1397. $content = file_get_contents($img_url);
  1398. if (!$content) {
  1399. return false;
  1400. }
  1401. if ($content{0} . $content{1} == "\xff\xd8") {
  1402. $image_ext = 'jpg';
  1403. } elseif ($content{0} . $content{1} . $content{2} == "\x47\x49\x46") {
  1404. $image_ext = 'gif';
  1405. } elseif ($content{0} . $content{1} . $content{2} == "\x89\x50\x4e") {
  1406. $image_ext = 'png';
  1407. } else {
  1408. // 不是有效图片
  1409. return false;
  1410. }
  1411. $save_path = create_image_path($save_dir, $image_ext);
  1412. return file_put_contents(IMG_PATH . $save_path, $content) ? $save_path : false;
  1413. }
  1414. }
  1415. if (!function_exists('save_image_content')) {
  1416. /**
  1417. * 富文本信息处理
  1418. * @param string $content 富文本内容
  1419. * @param bool $title 标题
  1420. * @param string $path 图片存储路径
  1421. * @return bool|int 返回结果
  1422. * @author wesmiler
  1423. * @date 2020-04-21
  1424. */
  1425. function save_image_content(&$content, $title = false, $path = 'article')
  1426. {
  1427. // 图片处理
  1428. preg_match_all("/<img.*?src=[\"|\']?(.*?)[\"|\']?\s.*?>/i", str_ireplace("\\", "", $content), $match);
  1429. if ($match[1]) {
  1430. foreach ($match[1] as $id => $val) {
  1431. $save_image = save_image($val, $path);
  1432. if ($save_image) {
  1433. $content = str_replace($val, "[IMG_URL]" . $save_image, $content);
  1434. }
  1435. }
  1436. }
  1437. // 视频处理
  1438. preg_match_all("/<embed .*?src=[\"|\']?(.*?)[\"|\']?\s.*?>/i", str_ireplace("\\", "", $content), $match2);
  1439. if ($match2[1]) {
  1440. foreach ($match2[1] as $vo) {
  1441. $save_video = save_image($vo, $path);
  1442. if ($save_video) {
  1443. $content = str_replace($vo, "[IMG_URL]" . str_replace(ATTACHMENT_PATH, "", IMG_PATH) . $save_video, $content);
  1444. }
  1445. }
  1446. }
  1447. // 提示标签替换
  1448. if ((strpos($content, 'alt=\"\"') !== false) && $title) {
  1449. $content = str_replace('alt=\"\"', 'alt=\"' . $title . '\"', $content);
  1450. }
  1451. return true;
  1452. }
  1453. }
  1454. if (!function_exists('upload_image')) {
  1455. /**
  1456. * 上传图片
  1457. * @param $request 网络请求
  1458. * @param string $form_name 文件表单名
  1459. * @return array 返回结果
  1460. * @author wesmiler
  1461. * @date 2019/5/23
  1462. */
  1463. function upload_image($request, $form_name = 'file')
  1464. {
  1465. // 检测请求中是否包含name=$form_name的上传文件
  1466. if (!$request->hasFile($form_name)) {
  1467. return message("请上传文件", false);
  1468. }
  1469. // 文件对象
  1470. $file = $request->file($form_name);
  1471. // 判断图片上传是否错误
  1472. if (!$file->isValid()) {
  1473. // 文件上传失败
  1474. return message("上传文件验证失败", false);
  1475. }
  1476. // 文件原名
  1477. $original_name = $file->getClientOriginalName();
  1478. // 文件扩展名(文件后缀)
  1479. $ext = $file->getClientOriginalExtension();
  1480. // 临时文件的绝对路径
  1481. $real_path = $file->getRealPath();
  1482. // 文件类型
  1483. $type = $file->getClientMimeType();
  1484. // 文件大小
  1485. $size = $file->getSize();
  1486. // 文件大小校验
  1487. if ($size > 5 * 1024 * 1024) {
  1488. return message("文件大小超过了5M", false);
  1489. }
  1490. // 文件后缀校验
  1491. $ext_arr = array('jpg', 'jpeg', 'png', 'gif');
  1492. if (!in_array($ext, $ext_arr)) {
  1493. return message("文件格式不正确", false);
  1494. }
  1495. // 文件路径
  1496. $file_dir = UPLOAD_TEMP_PATH . "/" . date('Ymd');
  1497. // 检测文件路径是否存在,不存在则创建
  1498. if (!file_exists($file_dir)) {
  1499. mkdir($file_dir, 0777, true);
  1500. }
  1501. // 文件名称
  1502. $file_name = uniqid() . '.' . $ext;
  1503. // 重命名保存
  1504. $path = $file->move($file_dir, $file_name);
  1505. // 文件临时路径
  1506. $file_path = str_replace(ATTACHMENT_PATH, '', $file_dir) . '/' . $file_name;
  1507. // 返回结果
  1508. $result = [
  1509. 'img_original_name' => $original_name,
  1510. 'img_ext' => $ext,
  1511. 'img_real_path' => $real_path,
  1512. 'img_type' => $type,
  1513. 'img_size' => $size,
  1514. 'img_name' => $file_name,
  1515. 'img_path' => $file_path,
  1516. ];
  1517. return message(MESSAGE_OK, true, $result);
  1518. }
  1519. }
  1520. if (!function_exists('upload_file')) {
  1521. /**
  1522. * 上传文件
  1523. * @param $request 网络请求
  1524. * @param string $form_name 文件表单名
  1525. * @return array 返回结果
  1526. * @author wesmiler
  1527. * @date 2019/5/23
  1528. */
  1529. function upload_file($request, $form_name = 'file')
  1530. {
  1531. // 检测请求中是否包含上传的文件
  1532. if (!$request->hasFile($form_name)) {
  1533. return message("请上传文件", false);
  1534. }
  1535. // 文件对象
  1536. $file = $request->file($form_name);
  1537. // 判断图片上传是否错误
  1538. if (!$file->isValid()) {
  1539. // 文件上传失败
  1540. return message("上传文件验证失败", false);
  1541. }
  1542. // 文件原名
  1543. $original_name = $file->getClientOriginalName();
  1544. // 文件扩展名(文件后缀)
  1545. $ext = $file->getClientOriginalExtension();
  1546. // 临时文件的绝对路径
  1547. $real_path = $file->getRealPath();
  1548. // 文件类型
  1549. $type = $file->getClientMimeType();
  1550. // 文件大小
  1551. $size = $file->getSize();
  1552. // 文件大小校验(MAX=5M)
  1553. $file_max_size = 50 * 1024 * 1024;
  1554. if ($size > $file_max_size) {
  1555. return message("您上传的文件过大,最大值为" . $file_max_size / 1024 / 1024 . "MB", false);
  1556. }
  1557. // 允许上传的文件后缀
  1558. $file_exts = array('xls', 'xlsx', 'csv','mp3','mp4','WMA','MPEG');
  1559. if (!in_array($ext, $file_exts)) {
  1560. return message("文件格式不正确", false);
  1561. }
  1562. // 文件路径
  1563. $file_dir = UPLOAD_FILE_PATH . "/" . date('Ymd');
  1564. // 检测文件路径是否存在,不存在则创建
  1565. if (!file_exists($file_dir)) {
  1566. mkdir($file_dir, 0777, true);
  1567. }
  1568. // 文件名称
  1569. $file_name = uniqid() . '.' . $ext;
  1570. // 重命名保存
  1571. $path = $file->move($file_dir, $file_name);
  1572. // 文件临时路径
  1573. $file_path = str_replace(ATTACHMENT_PATH, '', $file_dir) . '/' . $file_name;
  1574. // 返回结果
  1575. $result = [
  1576. 'file_original_name' => $original_name,
  1577. 'file_ext' => $ext,
  1578. 'file_real_path' => $real_path,
  1579. 'file_type' => $type,
  1580. 'file_size' => $size,
  1581. 'file_name' => $file_name,
  1582. 'file_path' => $file_path,
  1583. ];
  1584. return message(MESSAGE_OK, true, $result);
  1585. }
  1586. }
  1587. if (!function_exists('widget')) {
  1588. /**
  1589. * 加载系统组件,传入的名字会以目录和类名区别
  1590. * 如Home.Common就代表Widget目录下的Home/Common.php这个widget。
  1591. * @param $widgetName 组件名称
  1592. * @return bool|mixed
  1593. * @author wesmiler
  1594. * @date 2019/5/23
  1595. */
  1596. function widget($widgetName)
  1597. {
  1598. $widgetNameEx = explode('.', $widgetName);
  1599. if (!isset($widgetNameEx[1])) {
  1600. return false;
  1601. }
  1602. $widgetClass = 'App\\Widget\\' . $widgetNameEx[0] . '\\' . $widgetNameEx[1];
  1603. if (app()->bound($widgetName)) {
  1604. return app()->make($widgetName);
  1605. }
  1606. app()->singleton($widgetName, function () use ($widgetClass) {
  1607. return new $widgetClass();
  1608. });
  1609. return app()->make($widgetName);
  1610. }
  1611. }