common.php 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. // 此文件为系统框架核心公共函数文件,为了系统的稳定与安全,未经允许不得擅自改动
  12. if (!function_exists('getValidatorError')) {
  13. /**
  14. * 错误验证
  15. * @param $error
  16. * @return mixed
  17. */
  18. function getValidatorError($validator)
  19. {
  20. $errors = $validator->errors()->all();
  21. return isset($errors[0]) ? $errors[0] : 1013;
  22. }
  23. }
  24. if (!function_exists('array_sort')) {
  25. /**
  26. * 数组排序
  27. * @param array $arr 数据源
  28. * @param $keys KEY
  29. * @param bool $desc 排序方式(默认:asc)
  30. * @return array 返回结果
  31. * @author laravel开发员
  32. * @date 2019/5/23
  33. */
  34. function array_sort($arr, $keys, $desc = false)
  35. {
  36. $key_value = $new_array = array();
  37. foreach ($arr as $k => $v) {
  38. $key_value[$k] = $v[$keys];
  39. }
  40. if ($desc) {
  41. arsort($key_value);
  42. } else {
  43. asort($key_value);
  44. }
  45. reset($key_value);
  46. foreach ($key_value as $k => $v) {
  47. $new_array[$k] = $arr[$k];
  48. }
  49. return $new_array;
  50. }
  51. }
  52. if (!function_exists('xml2array')) {
  53. /**
  54. * xml转数组
  55. * @param $xml xml文本
  56. * @return string
  57. * @author laravel开发员
  58. * @date 2019/6/6
  59. */
  60. function xml2array(&$xml)
  61. {
  62. $xml = "<xml>";
  63. foreach ($xml as $key => $val) {
  64. if (is_numeric($val)) {
  65. $xml .= "<" . $key . ">" . $val . "</" . $key . ">";
  66. } else {
  67. $xml .= "<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">";
  68. }
  69. }
  70. $xml .= "</xml>";
  71. return $xml;
  72. }
  73. }
  74. if (!function_exists('array2xml')) {
  75. /**
  76. * 数组转xml
  77. * @param $arr 原始数据(数组)
  78. * @param bool $ignore 是否忽视true或fasle
  79. * @param int $level 级别(默认:1)
  80. * @return string 返回结果
  81. * @author laravel开发员
  82. * @date 2019/6/6
  83. */
  84. function array2xml($arr, $ignore = true, $level = 1)
  85. {
  86. $s = $level == 1 ? "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\r\n<root>\r\n" : '';
  87. $space = str_repeat("\t", $level);
  88. foreach ($arr as $k => $v) {
  89. if (!is_array($v)) {
  90. $s .= $space . "<item id=\"$k\">" . ($ignore ? '<![CDATA[' : '') . $v . ($ignore ? ']]>' : '')
  91. . "</item>\r\n";
  92. } else {
  93. $s .= $space . "<item id=\"$k\">\r\n" . array2xml($v, $ignore, $level + 1) . $space . "</item>\r\n";
  94. }
  95. }
  96. $s = preg_replace("/([\x01-\x08\x0b-\x0c\x0e-\x1f])+/", ' ', $s);
  97. return $level == 1 ? $s . "</root>" : $s;
  98. }
  99. }
  100. if (!function_exists('array_merge_multiple')) {
  101. /**
  102. * 多为数组合并
  103. * @param array $array1 数组1
  104. * @param array $array2 数组2
  105. * @return array 返回合并后的数组
  106. * @author laravel开发员
  107. * @date 2019/6/6
  108. */
  109. function array_merge_multiple($array1, $array2)
  110. {
  111. $merge = $array1 + $array2;
  112. $data = [];
  113. foreach ($merge as $key => $val) {
  114. if (isset($array1[$key])
  115. && is_array($array1[$key])
  116. && isset($array2[$key])
  117. && is_array($array2[$key])
  118. ) {
  119. $data[$key] = array_merge_multiple($array1[$key], $array2[$key]);
  120. } else {
  121. $data[$key] = isset($array2[$key]) ? $array2[$key] : $array1[$key];
  122. }
  123. }
  124. return $data;
  125. }
  126. }
  127. if (!function_exists('array_key_value')) {
  128. /**
  129. * 获取数组中某个字段的所有值
  130. * @param array $arr 数据源
  131. * @param string $name 字段名
  132. * @return array 返回结果
  133. * @author laravel开发员
  134. * @date 2019/6/6
  135. */
  136. function array_key_value($arr, $name = "")
  137. {
  138. $result = [];
  139. if ($arr) {
  140. foreach ($arr as $key => $val) {
  141. if ($name) {
  142. $result[] = $val[$name];
  143. } else {
  144. $result[] = $key;
  145. }
  146. }
  147. }
  148. $result = array_unique($result);
  149. return $result;
  150. }
  151. }
  152. if (!function_exists('curl_url')) {
  153. /**
  154. * 获取当前访问的完整地址
  155. * @return string 返回结果
  156. * @author laravel开发员
  157. * @date 2019/6/6
  158. */
  159. function curl_url()
  160. {
  161. $page_url = 'http';
  162. if (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] === 'on') {
  163. $page_url .= "s";
  164. }
  165. $page_url .= "://";
  166. if ($_SERVER["SERVER_PORT"] != "80") {
  167. $page_url .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"];
  168. } else {
  169. $page_url .= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
  170. }
  171. return $page_url;
  172. }
  173. }
  174. if (!function_exists('curl_get')) {
  175. /**
  176. * curl请求(GET)
  177. * @param $url 请求地址
  178. * @param array $data 请求参数
  179. * @return bool|string 返回结果
  180. * @author laravel开发员
  181. * @date 2019/6/5
  182. */
  183. function curl_get($url, $data = [])
  184. {
  185. if (!empty($data)) {
  186. $url = $url . '?' . http_build_query($data);
  187. }
  188. // 初始化
  189. $ch = curl_init();
  190. // 设置抓取的url
  191. curl_setopt($ch, CURLOPT_URL, $url);
  192. // 设置头文件的信息作为数据流输出
  193. curl_setopt($ch, CURLOPT_HEADER, false);
  194. // 是否要求返回数据
  195. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  196. // 是否检测服务器的证书是否由正规浏览器认证过的授权CA颁发的
  197. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  198. // 执行命令
  199. $result = curl_exec($ch);
  200. // 关闭URL请求(释放句柄)
  201. curl_close($ch);
  202. return $result;
  203. }
  204. }
  205. if (!function_exists('curl_post')) {
  206. /**
  207. * curl请求(POST)
  208. * @param $url 请求地址
  209. * @param array $data 请求参数
  210. * @return bool|string 返回结果
  211. * @author laravel开发员
  212. * @date 2019/6/5
  213. */
  214. function curl_post($url, $data = [])
  215. {
  216. // 初始化
  217. $ch = curl_init();
  218. // 设置post方式提交
  219. curl_setopt($ch, CURLOPT_POST, 1);
  220. // 设置头文件的信息作为数据流输出
  221. curl_setopt($ch, CURLOPT_HEADER, 0);
  222. // 是否要求返回数据
  223. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  224. // 设置抓取的url
  225. curl_setopt($ch, CURLOPT_URL, $url);
  226. // 提交的数据
  227. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  228. // 是否检测服务器的证书是否由正规浏览器认证过的授权CA颁发的
  229. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  230. // 执行命令
  231. $result = curl_exec($ch);
  232. // 关闭URL请求(释放句柄)
  233. curl_close($ch);
  234. return $result;
  235. }
  236. }
  237. if (!function_exists('curl_request')) {
  238. /**
  239. * curl请求(支持get和post)
  240. * @param $url 请求地址
  241. * @param array $data 请求参数
  242. * @param string $type 请求类型(默认:post)
  243. * @param bool $https 是否https请求true或false
  244. * @return bool|string 返回请求结果
  245. * @author laravel开发员
  246. * @date 2019/6/6
  247. */
  248. function curl_request($url, $data = [], $type = 'post', $https = false)
  249. {
  250. // 初始化
  251. $ch = curl_init();
  252. curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
  253. curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)');
  254. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
  255. // 设置超时时间
  256. curl_setopt($ch, CURLOPT_TIMEOUT, 30);
  257. // 是否要求返回数据
  258. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  259. if ($https) {
  260. // 对认证证书来源的检查
  261. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  262. // 从证书中检查SSL加密算法是否存在
  263. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  264. }
  265. if (strtolower($type) == 'post') {
  266. // 设置post方式提交
  267. curl_setopt($ch, CURLOPT_POST, true);
  268. // 提交的数据
  269. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  270. } elseif (!empty($data) && is_array($data)) {
  271. // get网络请求
  272. $url = $url . '?' . http_build_query($data);
  273. }
  274. // 设置抓取的url
  275. curl_setopt($ch, CURLOPT_URL, $url);
  276. // 执行命令
  277. $result = curl_exec($ch);
  278. if ($result === false) {
  279. return false;
  280. }
  281. // 关闭URL请求(释放句柄)
  282. curl_close($ch);
  283. return $result;
  284. }
  285. }
  286. if (!function_exists('datetime')) {
  287. /**
  288. * 格式化日期函数
  289. * @param $time 时间戳
  290. * @param string $format 输出日期格式
  291. * @return string 返回格式化的日期
  292. * @author laravel开发员
  293. * @date 2019/5/23
  294. */
  295. function datetime($time, $format = 'Y-m-d H:i:s')
  296. {
  297. if($time == '0000-00-00 00:00:00'){
  298. return '';
  299. }
  300. $time = is_numeric($time) ? $time : strtotime($time);
  301. return date($format, $time);
  302. }
  303. }
  304. if (!function_exists('dateForWeek')) {
  305. /**
  306. * 返回星期格式时间
  307. * @param $time 时间戳
  308. * @return false|string
  309. */
  310. function dateForWeek($time)
  311. {
  312. if(empty($time)){
  313. return false;
  314. }
  315. $time = !is_int($time)? strtotime($time) : $time;
  316. $weeks = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];
  317. $week = date("w", $time);
  318. $cweek = date("w");
  319. if ($cweek - $week >= 0 && (time() - $time) <= 7 * 86400) {
  320. return isset($weeks[$week]) ? $weeks[$week] . ' ' . date('H:i', $time) : date('m月d日 H:i', $time);
  321. } else {
  322. return date('m月d日 H:i', $time);
  323. }
  324. }
  325. }
  326. if (!function_exists('dateFormat')) {
  327. /**
  328. * 格式化时间日期
  329. * @param $time 时间戳
  330. * @return false|string
  331. */
  332. function dateFormat($time, $format='m月d日')
  333. {
  334. if(empty($time)){
  335. return false;
  336. }
  337. $time = !is_int($time)? strtotime($time) : $time;
  338. $weeks = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];
  339. $week = date("w", $time);
  340. $cweek = date("w");
  341. $dateTime = strtotime(date('Y-m-d'));
  342. // 今日
  343. if ($time >= $dateTime) {
  344. return date('H:i', $time);
  345. } // 昨日
  346. else if ($time >= $dateTime - 86400) {
  347. return date('昨天 H:i', $time);
  348. } else if ($cweek - $week > 0 && (time() - $time) < 7 * 86400) {
  349. return isset($weeks[$week]) ? $weeks[$week] . ' ' . date('H:i', $time) : date('m月d日 H:i', $time);
  350. } else {
  351. return date($format, $time);
  352. }
  353. }
  354. }
  355. if (!function_exists('formatScientific')){
  356. /**
  357. * 格式化科学计数
  358. * @param $num 数字
  359. * @return false|string
  360. */
  361. function formatScientific($num, $dec=6)
  362. {
  363. if(empty($num)){
  364. return false;
  365. }
  366. $datas = explode('E', $num);
  367. $num = floatval($datas[0]);
  368. $decimal = intval($datas[1]);
  369. if($decimal>0){
  370. return number_format($num * pow(10, $decimal),$dec);
  371. }else{
  372. return number_format($num * pow(10, $decimal),$dec);
  373. }
  374. }
  375. }
  376. if (!function_exists('data_auth_sign')) {
  377. /**
  378. * 数据签名认证
  379. * @param $data 数据源
  380. * @return string
  381. * @author laravel开发员
  382. * @date 2019/6/6
  383. */
  384. function data_auth_sign($data)
  385. {
  386. // 数据类型检测
  387. if (!is_array($data)) {
  388. $data = (array)$data;
  389. }
  390. // 排序
  391. ksort($data);
  392. // url编码并生成query字符串
  393. $code = http_build_query($data);
  394. // 生成签名
  395. $sign = sha1($code);
  396. return $sign;
  397. }
  398. }
  399. if (!function_exists('decrypt')) {
  400. /**
  401. * DES解密
  402. * @param string $str 解密字符串
  403. * @param string $key 解密KEY
  404. * @return mixed
  405. * @author laravel开发员
  406. * @date 2019/6/6
  407. */
  408. function decrypt($str, $key = 'p@ssw0rd')
  409. {
  410. $str = base64_decode($str);
  411. $str = mcrypt_decrypt(MCRYPT_DES, $key, $str, MCRYPT_MODE_ECB);
  412. $block = mcrypt_get_block_size('des', 'ecb');
  413. $pad = ord($str[($len = strlen($str)) - 1]);
  414. if ($pad && $pad < $block && preg_match('/' . chr($pad) . '{' . $pad . '}$/', $str)) {
  415. $str = substr($str, 0, strlen($str) - $pad);
  416. }
  417. return unserialize($str);
  418. }
  419. }
  420. if (!function_exists('encrypt')) {
  421. /**
  422. *
  423. * @param string $str 加密字符串
  424. * @param string $key 加密KEY
  425. * @return string
  426. * @author laravel开发员
  427. * @date 2019/6/6
  428. */
  429. function encrypt($str, $key = 'p@ssw0rd')
  430. {
  431. $prep_code = serialize($str);
  432. $block = mcrypt_get_block_size('des', 'ecb');
  433. if (($pad = $block - (strlen($prep_code) % $block)) < $block) {
  434. $prep_code .= str_repeat(chr($pad), $pad);
  435. }
  436. $encrypt = mcrypt_encrypt(MCRYPT_DES, $key, $prep_code, MCRYPT_MODE_ECB);
  437. return base64_encode($encrypt);
  438. }
  439. }
  440. if (!function_exists('export_excel')) {
  441. /**
  442. * 导出Excel文件
  443. * @param string $file_name 文件名
  444. * @param array $title 标题
  445. * @param array $data 数据源
  446. * @author laravel开发员
  447. * @date 2019/6/6
  448. */
  449. function export_excel($file_name, $title = [], $data = [])
  450. {
  451. // 默认支持最大512M
  452. ini_set('memory_limit', '512M');
  453. ini_set('max_execution_time', 0);
  454. ob_end_clean();
  455. ob_start();
  456. header("Content-Type: text/csv");
  457. header("Content-Disposition:filename=" . $file_name);
  458. $fp = fopen('php://output', 'w');
  459. // 转码 防止乱码(比如微信昵称)
  460. fwrite($fp, chr(0xEF) . chr(0xBB) . chr(0xBF));
  461. fputcsv($fp, $title);
  462. $index = 0;
  463. foreach ($data as $item) {
  464. if ($index == 1000) {
  465. $index = 0;
  466. ob_flush();
  467. flush();
  468. }
  469. $index++;
  470. fputcsv($fp, $item);
  471. }
  472. ob_flush();
  473. flush();
  474. ob_end_clean();
  475. }
  476. }
  477. if (!function_exists('ecm_define')) {
  478. /**
  479. * 定义常量(读取数组或引用文件)
  480. * @param $value 数据源
  481. * @return bool
  482. * @author laravel开发员
  483. * @date 2019/6/6
  484. */
  485. function ecm_define($value)
  486. {
  487. if (is_string($value)) {
  488. /* 导入数组 */
  489. $value = include($value);
  490. }
  491. if (!is_array($value)) {
  492. /* 不是数组,无法定义 */
  493. return false;
  494. }
  495. foreach ($value as $key => $val) {
  496. if (is_string($val) || is_numeric($val) || is_bool($val) || is_null($val)) {
  497. // 判断是否已定义过,否则进行定义
  498. defined(strtoupper($key)) or define(strtoupper($key), $val);
  499. }
  500. }
  501. }
  502. }
  503. if (!function_exists('format_time')) {
  504. /**
  505. * 格式化时间段
  506. * @param int $time 时间戳
  507. * @return string 输出格式化时间
  508. * @author laravel开发员
  509. * @date 2019/5/23
  510. */
  511. function format_time($time)
  512. {
  513. $interval = time() - $time;
  514. $format = array(
  515. '31536000' => '年',
  516. '2592000' => '个月',
  517. '604800' => '星期',
  518. '86400' => '天',
  519. '3600' => '小时',
  520. '60' => '分钟',
  521. '1' => '秒',
  522. );
  523. foreach ($format as $key => $val) {
  524. $match = floor($interval / (int)$key);
  525. if (0 != $match) {
  526. return $match . $val . '前';
  527. }
  528. }
  529. return date('Y-m-d', $time);
  530. }
  531. }
  532. if (!function_exists('format_num')){
  533. /**
  534. * @param $num
  535. */
  536. function format_num($num, $max=10000, $text='w')
  537. {
  538. return $num > 10000? moneyFormat($num/$max,1).$text : $num;
  539. }
  540. }
  541. if (!function_exists('format_bytes')) {
  542. /**
  543. * 将字节转换为可读文本
  544. * @param int $size 字节大小
  545. * @param string $delimiter 分隔符
  546. * @return string 返回结果
  547. * @author laravel开发员
  548. * @date 2019/5/23
  549. */
  550. function format_bytes($size, $delimiter = '')
  551. {
  552. $units = array('B', 'KB', 'MB', 'GB', 'TB', 'PB');
  553. for ($i = 0; $size >= 1024 && $i < 6; $i++) {
  554. $size /= 1024;
  555. }
  556. return round($size, 2) . $delimiter . $units[$i];
  557. }
  558. }
  559. if (!function_exists('format_yuan')) {
  560. /**
  561. * 以分为单位的金额转换成元
  562. * @param int $money 金额
  563. * @return string 返回格式化的金额
  564. * @author laravel开发员
  565. * @date 2019/5/23
  566. */
  567. function format_yuan($money = 0)
  568. {
  569. if ($money > 0) {
  570. return number_format($money / 100, 2, ".", "");
  571. }
  572. return "0.00";
  573. }
  574. }
  575. if (!function_exists('format_cent')) {
  576. /**
  577. * 以元为单位的金额转化成分
  578. * @param $money 金额
  579. * @return string 返回格式化的金额
  580. * @author laravel开发员
  581. * @date 2019/5/23
  582. */
  583. function format_cent($money)
  584. {
  585. return (string)($money * 100);
  586. }
  587. }
  588. if (!function_exists('format_bank_card')) {
  589. /**
  590. * 银行卡格式转换
  591. * @param string $card_no 银行卡号
  592. * @param bool $is_format 是否格式化
  593. * @return string 输出结果
  594. * @author laravel开发员
  595. * @date 2019/5/23
  596. */
  597. function format_bank_card($card_no, $is_format = true, $hidden=true)
  598. {
  599. if($hidden){
  600. $format_card_no = '****'.substr($card_no, -4, 4);
  601. }else if ($is_format) {
  602. // 截取银行卡号前4位
  603. $prefix = substr($card_no, 0, 4);
  604. // 截取银行卡号后4位
  605. $suffix = substr($card_no, -4, 4);
  606. $format_card_no = $prefix . " **** **** **** " . $suffix;
  607. } else {
  608. // 4的意思就是每4个为一组
  609. $arr = str_split($card_no, 4);
  610. $format_card_no = implode(' ', $arr);
  611. }
  612. return $format_card_no;
  613. }
  614. }
  615. if (!function_exists('format_mobile')) {
  616. /**
  617. * 格式化手机号码
  618. * @param string $mobile 手机号码
  619. * @return string 返回结果
  620. * @author laravel开发员
  621. * @date 2019/5/23
  622. */
  623. function format_mobile($mobile)
  624. {
  625. return substr($mobile, 0, 3) . "****" . substr($mobile, -4, 4);
  626. }
  627. }
  628. if (!function_exists('get_random_code')) {
  629. /**
  630. * 生成唯一字符串
  631. * @param int $length
  632. * @param string $prefix
  633. * @param string $ext
  634. * @return string
  635. */
  636. function get_random_code($length = 8, $prefix = '', $ext = '')
  637. {
  638. $length = $length < 8 ? 8 : $length;
  639. $code = strtoupper(md5(uniqid(mt_rand(), true) . $ext . microtime()));
  640. $start = rand(1, strlen($code) - $length);
  641. return $prefix . mb_substr($code, $start, $length);
  642. }
  643. }
  644. if (!function_exists('get_username')) {
  645. /**
  646. * 生成唯一字符串
  647. * @param int $length
  648. * @param string $prefix
  649. * @param string $ext
  650. * @return string
  651. */
  652. function get_username($id, $prefix = 'XL')
  653. {
  654. $length = strlen($id);
  655. if ($length >= 8) {
  656. return $prefix . $id;
  657. }
  658. return $prefix . str_repeat('0', 2) . $id;
  659. }
  660. }
  661. if (!function_exists('get_password')) {
  662. /**
  663. * 获取双MD5加密密码
  664. * @param string $password 加密字符串
  665. * @return string 输出MD5加密字符串
  666. * @author laravel开发员
  667. * @date 2019/5/23
  668. */
  669. function get_password($password)
  670. {
  671. return md5(md5($password));
  672. }
  673. }
  674. if (!function_exists('get_order_num')) {
  675. /**
  676. * 生成订单号
  677. * @param string $prefix 订单前缀(如:JD-)
  678. * @return string 输出订单号字符串
  679. * @author laravel开发员
  680. * @date 2019/5/23
  681. */
  682. function get_order_num($prefix = '')
  683. {
  684. $micro = substr(microtime(), 2, 2);
  685. return $prefix . date("YmdHis") . $micro . rand(1000, 9999);
  686. }
  687. }
  688. if (!function_exists('getter')) {
  689. /**
  690. * 获取数组的下标值
  691. * @param array $data 数据源
  692. * @param string $field 字段名称
  693. * @param string $default 默认值
  694. * @return mixed|string 返回结果
  695. * @author laravel开发员
  696. * @date 2019/5/23
  697. */
  698. function getter($data, $field, $default = '')
  699. {
  700. $result = $default;
  701. if (isset($data[$field])) {
  702. $result = $data[$field];
  703. }
  704. return $result;
  705. }
  706. }
  707. if (!function_exists('get_zodiac_sign')) {
  708. /**
  709. * 根据月、日获取星座
  710. * @param $month 月份
  711. * @param $day 日期
  712. * @return string 返回结果
  713. * @author laravel开发员
  714. * @date 2019/5/23
  715. */
  716. function get_zodiac_sign($month, $day)
  717. {
  718. // 检查参数有效性
  719. if ($month < 1 || $month > 12 || $day < 1 || $day > 31) {
  720. return false;
  721. }
  722. // 星座名称以及开始日期
  723. $signs = array(
  724. array("20" => "水瓶座"),
  725. array("19" => "双鱼座"),
  726. array("21" => "白羊座"),
  727. array("20" => "金牛座"),
  728. array("21" => "双子座"),
  729. array("22" => "巨蟹座"),
  730. array("23" => "狮子座"),
  731. array("23" => "处女座"),
  732. array("23" => "天秤座"),
  733. array("24" => "天蝎座"),
  734. array("22" => "射手座"),
  735. array("22" => "摩羯座")
  736. );
  737. list($sign_start, $sign_name) = each($signs[(int)$month - 1]);
  738. if ($day < $sign_start) {
  739. list($sign_start, $sign_name) = each($signs[($month - 2 < 0) ? $month = 11 : $month -= 2]);
  740. }
  741. return $sign_name;
  742. }
  743. }
  744. if (!function_exists('get_image_url')) {
  745. /**
  746. * 获取图片地址
  747. * @param $image_url 图片地址
  748. * @return string $domain 域名
  749. * @author laravel开发员
  750. * @date 2019/6/6
  751. */
  752. function get_image_url($image_url, $domain = '')
  753. {
  754. if (empty($image_url)) {
  755. return '';
  756. }
  757. if($domain){
  758. $host = $domain.'/uploads';
  759. }else{
  760. $host = request()->header('HOST');
  761. $https = request()->secure();
  762. $host = ($https ? 'https://' : 'http://') . $host . '/uploads';
  763. }
  764. return strpos($image_url, 'http') === false && strpos($image_url, 'file://') === false ? $host . '/' . ltrim($image_url, '/') : $image_url;
  765. }
  766. }
  767. if (!function_exists('remove_image_url')) {
  768. /**
  769. * 获取图片地址
  770. * @param $image_url 图片地址
  771. * @return string $domain 域名
  772. * @author laravel开发员
  773. * @date 2019/6/6
  774. */
  775. function remove_image_url($image_url, $domain = '')
  776. {
  777. if (empty($image_url)) {
  778. return '';
  779. }
  780. if($domain){
  781. $host = $domain.'/uploads';
  782. }else{
  783. $host = request()->header('HOST');
  784. $https = request()->secure();
  785. $host = ($https ? 'https://' : 'http://') . $host . '/uploads';
  786. }
  787. if(strpos($image_url, $host) === true){
  788. return str_replace($host, "", $image_url);
  789. }
  790. return $image_url;
  791. }
  792. }
  793. if (!function_exists('get_image_path')) {
  794. /**
  795. * 获取图片相对路径
  796. * @param $image_url 图片地址
  797. * @param $https 是否有证书
  798. * @return string 返回图片网络地址
  799. * @author laravel开发员
  800. * @date 2019/6/6
  801. */
  802. function get_image_path($image_url, $https = true)
  803. {
  804. if (empty($image_url)) {
  805. return false;
  806. }
  807. $data = explode('/uploads', $image_url);
  808. return $data ? end($data) : $image_url;
  809. }
  810. }
  811. if (!function_exists('get_format_images')) {
  812. /**
  813. * 获取多图片相对路径数组或json
  814. * @param $images 图片地址
  815. * @return string 返回图片网络地址
  816. * @author laravel开发员
  817. * @date 2019/6/6
  818. */
  819. function get_format_images($images, $key = 'url', $dataType = 'json')
  820. {
  821. if (empty($images) || !is_array($images)) {
  822. return '';
  823. }
  824. $datas = [];
  825. foreach ($images as $v) {
  826. $url = isset($v[$key]) ? $v[$key] : $v;
  827. if ($url) {
  828. $datas[] = $key? [
  829. $key=> get_image_path($url)
  830. ] : get_image_path($url);
  831. }
  832. }
  833. if ($datas) {
  834. return $dataType == 'json' ? json_encode($datas, 256) : '';
  835. } else {
  836. return $dataType == 'json' ? '' : [];
  837. }
  838. }
  839. }
  840. if(!function_exists('getImageContent')){
  841. /**
  842. * 获取图片内容
  843. * @param $url
  844. * @return false|string
  845. */
  846. function getImageContent($url){
  847. $imgUrl = $url;
  848. if(!preg_match("/^http/", $url)){
  849. $imgUrl = base_path().'/public/uploads/'.$url;
  850. }
  851. if(is_file($imgUrl) && file_exists($imgUrl)){
  852. $data =file_get_contents($imgUrl);
  853. return "data:image/jpeg;base64," . base64_encode($data);
  854. }else{
  855. return get_image_url($url);
  856. }
  857. }
  858. }
  859. if (!function_exists('get_web_url')) {
  860. /**
  861. * 获取预览地址
  862. * @param $url 地址
  863. * @return string 返回网络地址
  864. * @author laravel开发员
  865. * @date 2019/6/6
  866. */
  867. function get_web_url($url)
  868. {
  869. $host = request()->header('HOST');
  870. $host = ((strpos($host, '127') === false) && strpos($host, 'localhost') === false) ? env('WEB_URL') : 'http://' . $host;
  871. return strpos($url, 'http') === false ? $host . '/' . ltrim($url, '/') : $url;
  872. }
  873. }
  874. if (!function_exists('set_format_content')) {
  875. /**
  876. * 获取文章图片格式化后内容
  877. * @param $content
  878. * @return false|mixed|string
  879. */
  880. function set_format_content($content)
  881. {
  882. if (empty($content)) {
  883. return false;
  884. }
  885. $domain = request()->header('HOST');
  886. if(preg_match("/127/", $domain)){
  887. $host = env('IMG_URL','');
  888. if(empty($host)){
  889. $https = request()->secure();
  890. $host = ($https ? 'https://' : 'http://') . $host . '/uploads';
  891. }
  892. }else{
  893. $https = request()->secure();
  894. $host = ($https ? 'https://' : 'http://') . $domain . '/uploads';
  895. }
  896. $content = str_replace("{$host}",'/uploads', htmlspecialchars_decode($content));
  897. return $content;
  898. }
  899. }
  900. if (!function_exists('get_format_content')) {
  901. /**
  902. * 获取文章图片格式化后内容
  903. * @param $content
  904. * @return false|mixed|string
  905. */
  906. function get_format_content($content)
  907. {
  908. if (empty($content)) {
  909. return false;
  910. }
  911. $domain = request()->header('HOST');
  912. if(preg_match("/127/", $domain)){
  913. $host = env('IMG_URL','');
  914. if(empty($host)){
  915. $https = request()->secure();
  916. $host = ($https ? 'https://' : 'http://') . $host . '/uploads';
  917. }
  918. }else{
  919. $https = request()->secure();
  920. $host = ($https ? 'https://' : 'http://') . $domain . '/uploads';
  921. }
  922. $content = str_replace(["\"/uploads","'/uploads"],["\"{$host}","'{$host}"], htmlspecialchars_decode($content));
  923. return $content;
  924. }
  925. }
  926. if (!function_exists('get_hash')) {
  927. /**
  928. * 获取HASH值
  929. * @return string 返回hash字符串
  930. * @author laravel开发员
  931. * @date 2019/6/6
  932. */
  933. function get_hash()
  934. {
  935. $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()+-';
  936. $random = $chars[mt_rand(0, 73)] . $chars[mt_rand(0, 73)] . $chars[mt_rand(0, 73)]
  937. . $chars[mt_rand(0, 73)] . $chars[mt_rand(0, 73)];
  938. $content = uniqid() . $random;
  939. return sha1($content);
  940. }
  941. }
  942. if (!function_exists('get_server_ip')) {
  943. /**
  944. * 获取服务端IP地址
  945. * @return string 返回IP地址
  946. * @author laravel开发员
  947. * @date 2019/6/6
  948. */
  949. function get_server_ip()
  950. {
  951. if (isset($_SERVER)) {
  952. if ($_SERVER['SERVER_ADDR']) {
  953. $server_ip = $_SERVER['SERVER_ADDR'];
  954. } else {
  955. $server_ip = $_SERVER['LOCAL_ADDR'];
  956. }
  957. } else {
  958. $server_ip = getenv('SERVER_ADDR');
  959. }
  960. return $server_ip;
  961. }
  962. }
  963. if (!function_exists('get_client_ip')) {
  964. /**
  965. * 获取客户端IP地址
  966. * @param int $type 返回类型 0 返回IP地址 1 返回IPV4地址数字
  967. * @param bool $adv 否进行高级模式获取(有可能被伪装)
  968. * @return mixed 返回IP
  969. * @author laravel开发员
  970. * @date 2019/5/23
  971. */
  972. function get_client_ip($type = 0, $adv = false)
  973. {
  974. $type = $type ? 1 : 0;
  975. static $ip = null;
  976. if ($ip !== null) {
  977. return $ip[$type];
  978. }
  979. if ($adv) {
  980. if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  981. $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
  982. $pos = array_search('unknown', $arr);
  983. if (false !== $pos) {
  984. unset($arr[$pos]);
  985. }
  986. $ip = trim($arr[0]);
  987. } elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
  988. $ip = $_SERVER['HTTP_CLIENT_IP'];
  989. } elseif (isset($_SERVER['REMOTE_ADDR'])) {
  990. $ip = $_SERVER['REMOTE_ADDR'];
  991. }
  992. } elseif (isset($_SERVER['REMOTE_ADDR'])) {
  993. $ip = $_SERVER['REMOTE_ADDR'];
  994. }
  995. // IP地址合法验证
  996. $long = sprintf("%u", ip2long($ip));
  997. $ip = $long ? array($ip, $long) : array('0.0.0.0', 0);
  998. return $ip[$type];
  999. }
  1000. }
  1001. if (!function_exists('get_guid_v4')) {
  1002. /**
  1003. * 获取唯一性GUID
  1004. * @param bool $trim 是否去除{}
  1005. * @return string 返回GUID字符串
  1006. * @author laravel开发员
  1007. * @date 2019/6/6
  1008. */
  1009. function get_guid_v4($trim = true)
  1010. {
  1011. // Windows
  1012. if (function_exists('com_create_guid') === true) {
  1013. $charid = com_create_guid();
  1014. return $trim == true ? trim($charid, '{}') : $charid;
  1015. }
  1016. // OSX/Linux
  1017. if (function_exists('openssl_random_pseudo_bytes') === true) {
  1018. $data = openssl_random_pseudo_bytes(16);
  1019. $data[6] = chr(ord($data[6]) & 0x0f | 0x40); // set version to 0100
  1020. $data[8] = chr(ord($data[8]) & 0x3f | 0x80); // set bits 6-7 to 10
  1021. return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
  1022. }
  1023. // Fallback (PHP 4.2+)
  1024. mt_srand((double)microtime() * 10000);
  1025. $charid = strtolower(md5(uniqid(rand(), true)));
  1026. $hyphen = chr(45); // "-"
  1027. $lbrace = $trim ? "" : chr(123); // "{"
  1028. $rbrace = $trim ? "" : chr(125); // "}"
  1029. $guidv4 = $lbrace .
  1030. substr($charid, 0, 8) . $hyphen .
  1031. substr($charid, 8, 4) . $hyphen .
  1032. substr($charid, 12, 4) . $hyphen .
  1033. substr($charid, 16, 4) . $hyphen .
  1034. substr($charid, 20, 12) .
  1035. $rbrace;
  1036. return $guidv4;
  1037. }
  1038. }
  1039. if (!function_exists('is_email')) {
  1040. /**
  1041. * 判断是否为邮箱
  1042. * @param string $str 邮箱
  1043. * @return false 返回结果true或false
  1044. * @author laravel开发员
  1045. * @date 2019/5/23
  1046. */
  1047. function is_email($str)
  1048. {
  1049. return preg_match('/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/', $str);
  1050. }
  1051. }
  1052. if (!function_exists('is_mobile')) {
  1053. /**
  1054. * 判断是否为手机号
  1055. * @param string $num 手机号码
  1056. * @return false 返回结果true或false
  1057. * @author laravel开发员
  1058. * @date 2019/5/23
  1059. */
  1060. function is_mobile($num)
  1061. {
  1062. return preg_match('/^1(3|4|5|7|8)\d{9}$/', $num);
  1063. }
  1064. }
  1065. if (!function_exists('is_zipcode')) {
  1066. /**
  1067. * 验证邮编是否正确
  1068. * @param string $code 邮编
  1069. * @return false 返回结果true或false
  1070. * @author laravel开发员
  1071. * @date 2019/5/23
  1072. */
  1073. function is_zipcode($code)
  1074. {
  1075. return preg_match('/^[1-9][0-9]{5}$/', $code);
  1076. }
  1077. }
  1078. if (!function_exists('is_idcard')) {
  1079. /**
  1080. * 验证身份证是否正确
  1081. * @param string $idno 身份证号
  1082. * @return bool 返回结果true或false
  1083. * @author laravel开发员
  1084. * @date 2019/5/23
  1085. */
  1086. function is_idcard($idno)
  1087. {
  1088. $idno = strtoupper($idno);
  1089. $regx = '/(^\d{15}$)|(^\d{17}([0-9]|X)$)/';
  1090. $arr_split = array();
  1091. if (!preg_match($regx, $idno)) {
  1092. return false;
  1093. }
  1094. // 检查15位
  1095. if (15 == strlen($idno)) {
  1096. $regx = '/^(\d{6})+(\d{2})+(\d{2})+(\d{2})+(\d{3})$/';
  1097. @preg_match($regx, $idno, $arr_split);
  1098. $dtm_birth = "19" . $arr_split[2] . '/' . $arr_split[3] . '/' . $arr_split[4];
  1099. if (!strtotime($dtm_birth)) {
  1100. return false;
  1101. } else {
  1102. return true;
  1103. }
  1104. } else {
  1105. // 检查18位
  1106. $regx = '/^(\d{6})+(\d{4})+(\d{2})+(\d{2})+(\d{3})([0-9]|X)$/';
  1107. @preg_match($regx, $idno, $arr_split);
  1108. $dtm_birth = $arr_split[2] . '/' . $arr_split[3] . '/' . $arr_split[4];
  1109. // 检查生日日期是否正确
  1110. if (!strtotime($dtm_birth)) {
  1111. return false;
  1112. } else {
  1113. // 检验18位身份证的校验码是否正确。
  1114. // 校验位按照ISO 7064:1983.MOD 11-2的规定生成,X可以认为是数字10。
  1115. $arr_int = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
  1116. $arr_ch = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
  1117. $sign = 0;
  1118. for ($i = 0; $i < 17; $i++) {
  1119. $b = (int)$idno[$i];
  1120. $w = $arr_int[$i];
  1121. $sign += $b * $w;
  1122. }
  1123. $n = $sign % 11;
  1124. $val_num = $arr_ch[$n];
  1125. if ($val_num != substr($idno, 17, 1)) {
  1126. return false;
  1127. } else {
  1128. return true;
  1129. }
  1130. }
  1131. }
  1132. }
  1133. }
  1134. if (!function_exists('is_empty')) {
  1135. /**
  1136. * 判断是否为空
  1137. * @param $value 参数值
  1138. * @return bool 返回结果true或false
  1139. * @author laravel开发员
  1140. * @date 2019/6/5
  1141. */
  1142. function is_empty($value)
  1143. {
  1144. // 判断是否存在该值
  1145. if (!isset($value)) {
  1146. return true;
  1147. }
  1148. // 判断是否为empty
  1149. if (empty($value)) {
  1150. return true;
  1151. }
  1152. // 判断是否为null
  1153. if ($value === null) {
  1154. return true;
  1155. }
  1156. // 判断是否为空字符串
  1157. if (trim($value) === '') {
  1158. return true;
  1159. }
  1160. // 默认返回false
  1161. return false;
  1162. }
  1163. }
  1164. if (!function_exists('mkdirs')) {
  1165. /**
  1166. * 递归创建目录
  1167. * @param string $dir 需要创建的目录路径
  1168. * @param int $mode 权限值
  1169. * @return bool 返回结果true或false
  1170. * @author laravel开发员
  1171. * @date 2019/6/6
  1172. */
  1173. function mkdirs($dir, $mode = 0777)
  1174. {
  1175. if (is_dir($dir) || mkdir($dir, $mode, true)) {
  1176. return true;
  1177. }
  1178. if (!mkdirs(dirname($dir), $mode)) {
  1179. return false;
  1180. }
  1181. return mkdir($dir, $mode, true);
  1182. }
  1183. }
  1184. if (!function_exists('rmdirs')) {
  1185. /**
  1186. * 删除文件夹
  1187. * @param string $dir 文件夹路径
  1188. * @param bool $rmself 是否删除本身true或false
  1189. * @return bool 返回删除结果
  1190. * @author laravel开发员
  1191. * @date 2019/6/6
  1192. */
  1193. function rmdirs($dir, $rmself = true)
  1194. {
  1195. if (!is_dir($dir)) {
  1196. return false;
  1197. }
  1198. $files = new RecursiveIteratorIterator(
  1199. new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS),
  1200. RecursiveIteratorIterator::CHILD_FIRST
  1201. );
  1202. foreach ($files as $file) {
  1203. $todo = ($file->isDir() ? 'rmdir' : 'unlink');
  1204. $todo($file->getRealPath());
  1205. }
  1206. if ($rmself) {
  1207. @rmdir($dir);
  1208. }
  1209. return true;
  1210. }
  1211. }
  1212. if (!function_exists('copydirs')) {
  1213. /**
  1214. * 复制文件夹
  1215. * @param string $source 原文件夹路径
  1216. * @param string $dest 目的文件夹路径
  1217. * @author laravel开发员
  1218. * @date 2019/6/6
  1219. */
  1220. function copydirs($source, $dest)
  1221. {
  1222. if (!is_dir($dest)) {
  1223. mkdir($dest, 0755, true);
  1224. }
  1225. $iterator = new RecursiveIteratorIterator(
  1226. new RecursiveDirectoryIterator($source, RecursiveDirectoryIterator::SKIP_DOTS),
  1227. RecursiveIteratorIterator::SELF_FIRST
  1228. );
  1229. foreach ($iterator as $item) {
  1230. if ($item->isDir()) {
  1231. $sent_dir = $dest . "/" . $iterator->getSubPathName();
  1232. if (!is_dir($sent_dir)) {
  1233. mkdir($sent_dir, 0755, true);
  1234. }
  1235. } else {
  1236. copy($item, $dest . "/" . $iterator->getSubPathName());
  1237. }
  1238. }
  1239. }
  1240. }
  1241. if (!function_exists('lang')) {
  1242. function lang($msg, $replace = [], $local = '')
  1243. {
  1244. $local = $local ? $local : config('app.locale');
  1245. $data = __('api.' . $msg, $replace, $local);
  1246. if (strpos($data, 'api', 0) !== false) {
  1247. return __($msg, $replace, $local);
  1248. }
  1249. return __('api.' . $msg, $replace, $local);
  1250. }
  1251. }
  1252. if (!function_exists('web_lang')) {
  1253. function web_lang($msg, $replace = [], $local = '')
  1254. {
  1255. $local = $local? $local : session('locale_lang');
  1256. $local = $local ? $local : config('app.locale');
  1257. $data = __('api.' . $msg, $replace, $local);
  1258. if (strpos($data, 'api', 0) !== false) {
  1259. return __($msg, $replace, $local);
  1260. }
  1261. return __('api.' . $msg, $replace, $local);
  1262. }
  1263. }
  1264. if (!function_exists('message')) {
  1265. /**
  1266. * 消息数组
  1267. * @param string $msg 提示文字
  1268. * @param bool $success 是否成功true或false
  1269. * @param array $data 结果数据
  1270. * @param int $code 编码
  1271. * @return array 返回结果
  1272. * @author laravel开发员
  1273. * @date 2019/5/28
  1274. */
  1275. function message($msg = "操作成功", $success = true, $data = [], $code = 0, $type = 'json')
  1276. {
  1277. $result = ['success' => $success, 'msg' => lang($msg), 'data' => $data];
  1278. if ($success) {
  1279. $result['code'] = 0;
  1280. } else {
  1281. $result['code'] = $code ? $code : -1;
  1282. }
  1283. return $result;
  1284. }
  1285. }
  1286. if (!function_exists('showJson')) {
  1287. /**
  1288. * 消息数组
  1289. * @param string $msg 提示文字
  1290. * @param bool $success 是否成功true或false
  1291. * @param array $data 结果数据
  1292. * @param int $code 编码
  1293. * @return array 返回结果
  1294. * @author laravel开发员
  1295. * @date 2019/5/28
  1296. */
  1297. function showJson($msg = "操作成功", $success = true, $data = [], $code = 0, $type = 'json')
  1298. {
  1299. $result = ['success' => $success, 'msg' => web_lang($msg), 'data' => $data];
  1300. if ($success) {
  1301. $result['code'] = 0;
  1302. } else {
  1303. $result['code'] = $code ? $code : -1;
  1304. }
  1305. return $type=='json'?response()->json($result, 200, [])->setEncodingOptions(256):$result;
  1306. }
  1307. }
  1308. if (!function_exists('num2rmb')) {
  1309. /**
  1310. * 数字金额转大写
  1311. * @param float $num 金额
  1312. * @return string 返回大写金额
  1313. * @author laravel开发员
  1314. * @date 2019/6/6
  1315. */
  1316. function num2rmb($num)
  1317. {
  1318. $c1 = "零壹贰叁肆伍陆柒捌玖";
  1319. $c2 = "分角元拾佰仟万拾佰仟亿";
  1320. $num = round($num, 2);
  1321. $num = $num * 100;
  1322. if (strlen($num) > 10) {
  1323. return "oh,sorry,the number is too long!";
  1324. }
  1325. $i = 0;
  1326. $c = "";
  1327. while (1) {
  1328. if ($i == 0) {
  1329. $n = substr($num, strlen($num) - 1, 1);
  1330. } else {
  1331. $n = $num % 10;
  1332. }
  1333. $p1 = substr($c1, 3 * $n, 3);
  1334. $p2 = substr($c2, 3 * $i, 3);
  1335. if ($n != '0' || ($n == '0' && ($p2 == '亿' || $p2 == '万' || $p2 == '元'))) {
  1336. $c = $p1 . $p2 . $c;
  1337. } else {
  1338. $c = $p1 . $c;
  1339. }
  1340. $i = $i + 1;
  1341. $num = $num / 10;
  1342. $num = (int)$num;
  1343. if ($num == 0) {
  1344. break;
  1345. }
  1346. }
  1347. $j = 0;
  1348. $slen = strlen($c);
  1349. while ($j < $slen) {
  1350. $m = substr($c, $j, 6);
  1351. if ($m == '零元' || $m == '零万' || $m == '零亿' || $m == '零零') {
  1352. $left = substr($c, 0, $j);
  1353. $right = substr($c, $j + 3);
  1354. $c = $left . $right;
  1355. $j = $j - 3;
  1356. $slen = $slen - 3;
  1357. }
  1358. $j = $j + 3;
  1359. }
  1360. if (substr($c, strlen($c) - 3, 3) == '零') {
  1361. $c = substr($c, 0, strlen($c) - 3);
  1362. } // if there is a '0' on the end , chop it out
  1363. return $c . "整";
  1364. }
  1365. }
  1366. if (!function_exists('object_array')) {
  1367. /**
  1368. * 对象转数组
  1369. * @param $object 对象
  1370. * @return mixed 返回结果
  1371. * @author laravel开发员
  1372. * @date 2019/5/23
  1373. */
  1374. function object_array($object)
  1375. {
  1376. //先编码成json字符串,再解码成数组
  1377. return json_decode(json_encode($object), true);
  1378. }
  1379. }
  1380. if (!function_exists('parse_attr')) {
  1381. /**
  1382. * 配置值解析成数组
  1383. * @param string $value 参数值
  1384. * @return array 返回结果
  1385. * @author laravel开发员
  1386. * @date 2019/6/6
  1387. */
  1388. function parse_attr($value = '')
  1389. {
  1390. if (is_array($value)) {
  1391. return $value;
  1392. }
  1393. $array = preg_split('/[,;\r\n]+/', trim($value, ",;\r\n"));
  1394. if (strpos($value, ':')) {
  1395. $value = array();
  1396. foreach ($array as $val) {
  1397. list($k, $v) = explode(':', $val);
  1398. $value[$k] = $v;
  1399. }
  1400. } else {
  1401. $value = $array;
  1402. }
  1403. return $value;
  1404. }
  1405. }
  1406. if (!function_exists('strip_html_tags')) {
  1407. /**
  1408. * 去除HTML标签、图像等 仅保留文本
  1409. * @param string $str 字符串
  1410. * @param int $length 长度
  1411. * @return string 返回结果
  1412. * @author laravel开发员
  1413. * @date 2019/5/23
  1414. */
  1415. function strip_html_tags($str, $length = 0)
  1416. {
  1417. // 把一些预定义的 HTML 实体转换为字符
  1418. $str = htmlspecialchars_decode($str);
  1419. // 将空格替换成空
  1420. $str = str_replace("&nbsp;", "", $str);
  1421. // 函数剥去字符串中的 HTML、XML 以及 PHP 的标签,获取纯文本内容
  1422. $str = strip_tags($str);
  1423. $str = str_replace(array("\n", "\r\n", "\r"), ' ', $str);
  1424. $preg = '/<script[\s\S]*?<\/script>/i';
  1425. // 剥离JS代码
  1426. $str = preg_replace($preg, "", $str, -1);
  1427. if ($length == 2) {
  1428. // 返回字符串中的前100字符串长度的字符
  1429. $str = mb_substr($str, 0, $length, "utf-8");
  1430. }
  1431. return $str;
  1432. }
  1433. }
  1434. if (!function_exists('sub_str')) {
  1435. /**
  1436. * 字符串截取
  1437. * @param string $str 需要截取的字符串
  1438. * @param int $start 开始位置
  1439. * @param int $length 截取长度
  1440. * @param bool $suffix 截断显示字符
  1441. * @param string $charset 编码格式
  1442. * @return string 返回结果
  1443. * @author laravel开发员
  1444. * @date 2019/5/23
  1445. */
  1446. function sub_str($str, $start = 0, $length = 10, $suffix = true, $charset = "utf-8")
  1447. {
  1448. if (function_exists("mb_substr")) {
  1449. $slice = mb_substr($str, $start, $length, $charset);
  1450. } elseif (function_exists('iconv_substr')) {
  1451. $slice = iconv_substr($str, $start, $length, $charset);
  1452. } else {
  1453. $re['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
  1454. $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
  1455. $re['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
  1456. $re['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
  1457. preg_match_all($re[$charset], $str, $match);
  1458. $slice = join("", array_slice($match[0], $start, $length));
  1459. }
  1460. $omit = mb_strlen($str) >= $length ? '...' : '';
  1461. return $suffix ? $slice . $omit : $slice;
  1462. }
  1463. }
  1464. if (!function_exists('save_image')) {
  1465. /**
  1466. * 保存图片
  1467. * @param string $img_url 网络图片地址
  1468. * @param string $save_dir 图片保存目录
  1469. * @return string 返回路径
  1470. * @author laravel开发员
  1471. * @date 2019/5/23
  1472. */
  1473. function save_image($img_url, $save_dir = '/')
  1474. {
  1475. if (!$img_url) {
  1476. return false;
  1477. }
  1478. $save_dir = trim($save_dir, "/");
  1479. $imgExt = pathinfo($img_url, PATHINFO_EXTENSION);
  1480. // 是否是本站图片
  1481. if (strpos($img_url, IMG_URL) !== false) {
  1482. // 是否是临时文件
  1483. if (strpos($img_url, 'temp') === false) {
  1484. return str_replace(IMG_URL, "", $img_url);
  1485. }
  1486. $new_path = create_image_path($save_dir, $imgExt);
  1487. $old_path = str_replace(IMG_URL, ATTACHMENT_PATH, $img_url);
  1488. if (!file_exists($old_path)) {
  1489. return false;
  1490. }
  1491. rename($old_path, ATTACHMENT_PATH . $new_path);
  1492. return $new_path;
  1493. } else {
  1494. // 保存远程图片
  1495. $new_path = save_remote_image($img_url, $save_dir);
  1496. }
  1497. return $new_path;
  1498. }
  1499. }
  1500. if (!function_exists('create_image_path')) {
  1501. /**
  1502. * 创建图片存储目录
  1503. * @param string $save_dir 存储目录
  1504. * @param string $image_ext 图片后缀
  1505. * @param string $image_root 图片存储根目录路径
  1506. * @return string 返回文件目录
  1507. * @author laravel开发员
  1508. * @date 2019/5/23
  1509. */
  1510. function create_image_path($save_dir = "", $image_ext = "", $image_root = IMG_PATH)
  1511. {
  1512. $image_dir = date("/Ymd/");
  1513. if ($image_dir) {
  1514. $image_dir = ($save_dir ? "/" : '') . $save_dir . $image_dir;
  1515. }
  1516. // 未指定后缀默认使用JPG
  1517. if (!$image_ext) {
  1518. $image_ext = "jpg";
  1519. }
  1520. $image_path = $image_root . $image_dir;
  1521. if (!is_dir($image_path)) {
  1522. // 创建目录并赋予权限
  1523. mkdir($image_path, 0777, true);
  1524. }
  1525. $file_name = substr(md5(time() . rand(0, 999999)), 8, 16) . rand(100, 999) . ".{$image_ext}";
  1526. $file_path = str_replace(ATTACHMENT_PATH, "", IMG_PATH) . $image_dir . $file_name;
  1527. return $file_path;
  1528. }
  1529. }
  1530. if (!function_exists('save_remote_image')) {
  1531. /**
  1532. * 保存网络图片到本地
  1533. * @param string $img_url 网络图片地址
  1534. * @param string $save_dir 保存目录
  1535. * @return bool|string 图片路径
  1536. * @author laravel开发员
  1537. * @date 2019/5/23
  1538. */
  1539. function save_remote_image($img_url, $save_dir = '/')
  1540. {
  1541. $content = file_get_contents($img_url);
  1542. if (!$content) {
  1543. return false;
  1544. }
  1545. if ($content[0] . $content[1] == "\xff\xd8") {
  1546. $image_ext = 'jpg';
  1547. } elseif ($content[0] . $content[1] . $content[2] == "\x47\x49\x46") {
  1548. $image_ext = 'gif';
  1549. } elseif ($content[0] . $content[1] . $content[2] == "\x89\x50\x4e") {
  1550. $image_ext = 'png';
  1551. } else {
  1552. // 不是有效图片
  1553. return false;
  1554. }
  1555. $save_path = create_image_path($save_dir, $image_ext);
  1556. return file_put_contents(ATTACHMENT_PATH . $save_path, $content) ? $save_path : false;
  1557. }
  1558. }
  1559. if (!function_exists('save_image_content')) {
  1560. /**
  1561. * 富文本信息处理
  1562. * @param string $content 富文本内容
  1563. * @param bool $title 标题
  1564. * @param string $path 图片存储路径
  1565. * @return bool|int 返回结果
  1566. * @author laravel开发员
  1567. * @date 2020-04-21
  1568. */
  1569. function save_image_content(&$content, $title = false, $path = 'article')
  1570. {
  1571. // 图片处理
  1572. preg_match_all("/<img.*?src=[\"|\']?(.*?)[\"|\']?\s.*?>/i", str_ireplace("\\", "", $content), $match);
  1573. if ($match[1]) {
  1574. foreach ($match[1] as $id => $val) {
  1575. $save_image = save_image($val, $path);
  1576. if ($save_image) {
  1577. $content = str_replace($val, "[IMG_URL]" . $save_image, $content);
  1578. }
  1579. }
  1580. }
  1581. // 视频处理
  1582. preg_match_all("/<embed .*?src=[\"|\']?(.*?)[\"|\']?\s.*?>/i", str_ireplace("\\", "", $content), $match2);
  1583. if ($match2[1]) {
  1584. foreach ($match2[1] as $vo) {
  1585. $save_video = save_image($vo, $path);
  1586. if ($save_video) {
  1587. $content = str_replace($vo, "[IMG_URL]" . str_replace(ATTACHMENT_PATH, "", IMG_PATH) . $save_video, $content);
  1588. }
  1589. }
  1590. }
  1591. // 提示标签替换
  1592. if ((strpos($content, 'alt=\"\"') !== false) && $title) {
  1593. $content = str_replace('alt=\"\"', 'alt=\"' . $title . '\"', $content);
  1594. }
  1595. return true;
  1596. }
  1597. }
  1598. if (!function_exists('upload_image')) {
  1599. /**
  1600. * 上传图片
  1601. * @param $request 网络请求
  1602. * @param string $form_name 文件表单名
  1603. * @return array 返回结果
  1604. * @author laravel开发员
  1605. * @date 2019/5/23
  1606. */
  1607. function upload_image($request, $form_name = 'file', $file_path = '', $userId = 0)
  1608. {
  1609. // 检测请求中是否包含name=$form_name的上传文件
  1610. if (!$request->hasFile($form_name)) {
  1611. return message("请上传文件", false);
  1612. }
  1613. // 文件对象
  1614. $file = $request->file($form_name);
  1615. // 判断图片上传是否错误
  1616. if (!$file->isValid()) {
  1617. // 文件上传失败
  1618. return message("上传文件验证失败", false);
  1619. }
  1620. // 文件原名
  1621. $original_name = $file->getClientOriginalName();
  1622. // 文件扩展名(文件后缀)
  1623. $ext = $file->getClientOriginalExtension();
  1624. // 临时文件的绝对路径
  1625. $real_path = $file->getRealPath();
  1626. // 文件类型
  1627. $type = $file->getClientMimeType();
  1628. // 文件大小
  1629. $size = $file->getSize();
  1630. // 文件大小校验
  1631. if ($size > 5 * 1024 * 1024) {
  1632. return message("文件大小超过了5M", false);
  1633. }
  1634. // 文件后缀校验
  1635. $ext = strtolower($ext);
  1636. $ext_arr = array('jpg', 'jpeg', 'png', 'gif');
  1637. if (!in_array($ext, $ext_arr)) {
  1638. return message("文件格式不正确:" . $ext, false);
  1639. }
  1640. // 文件路径
  1641. $file_dir = ($file_path ? ATTACHMENT_PATH . '/images/' . $file_path : UPLOAD_TEMP_PATH) . "/" . date('Ymd');
  1642. // 检测文件路径是否存在,不存在则创建
  1643. if (!file_exists($file_dir)) {
  1644. mkdir($file_dir, 0777, true);
  1645. }
  1646. // 文件名称
  1647. $file_name = uniqid() . '.' . $ext;
  1648. // 重命名保存
  1649. $path = $file->move($file_dir, $file_name);
  1650. // 文件临时路径
  1651. $file_path = str_replace(ATTACHMENT_PATH, '', $file_dir) . '/' . $file_name;
  1652. // 返回结果
  1653. $result = [
  1654. 'img_original_name' => $original_name,
  1655. 'img_ext' => $ext,
  1656. 'img_real_path' => $real_path,
  1657. 'img_type' => $type,
  1658. 'img_size' => $size,
  1659. 'img_name' => $file_name,
  1660. 'img_path' => $file_path,
  1661. ];
  1662. return message(MESSAGE_OK, true, $result);
  1663. }
  1664. }
  1665. if (!function_exists('upload_file')) {
  1666. /**
  1667. * 上传文件
  1668. * @param $request 网络请求
  1669. * @param string $form_name 文件表单名
  1670. * @return array 返回结果
  1671. * @author laravel开发员
  1672. * @date 2019/5/23
  1673. */
  1674. function upload_file($request, $form_name = 'file', $file_path = '', $userId = 0)
  1675. {
  1676. // 检测请求中是否包含上传的文件
  1677. if (!$request->hasFile($form_name)) {
  1678. return message("请上传文件");
  1679. }
  1680. // 文件对象
  1681. $file = $request->file($form_name);
  1682. // 判断图片上传是否错误
  1683. if (!$file->isValid()) {
  1684. // 文件上传失败
  1685. return message("上传文件验证失败");
  1686. }
  1687. // 文件原名
  1688. $original_name = $file->getClientOriginalName();
  1689. // 文件扩展名(文件后缀)
  1690. $ext = $file->getClientOriginalExtension();
  1691. // 临时文件的绝对路径
  1692. $real_path = $file->getRealPath();
  1693. // 文件类型
  1694. $type = $file->getClientMimeType();
  1695. // 文件大小
  1696. $size = $file->getSize();
  1697. // 文件大小校验(MAX=5M)
  1698. $file_max_size = 5 * 1024 * 1024;
  1699. if ($size > $file_max_size) {
  1700. return message("您上传的文件过大,最大值为" . $file_max_size / 1024 / 1024 . "MB");
  1701. }
  1702. // 允许上传的文件后缀
  1703. $ext = strtolower($ext);
  1704. $file_exts = array('xls', 'xlsx', 'csv');
  1705. if (!in_array($ext, $file_exts)) {
  1706. return message("文件格式不正确:" . $ext);
  1707. }
  1708. // 文件路径
  1709. $file_dir = ($file_path ? ATTACHMENT_PATH . '/images/' . $file_path : UPLOAD_TEMP_PATH) . "/" . date('Ymd');
  1710. // 检测文件路径是否存在,不存在则创建
  1711. if (!file_exists($file_dir)) {
  1712. mkdir($file_dir, 0777, true);
  1713. }
  1714. // 文件名称
  1715. $file_name = uniqid() . '.' . $ext;
  1716. // 重命名保存
  1717. $path = $file->move($file_dir, $file_name);
  1718. // 文件临时路径
  1719. $file_path = str_replace(IMG_PATH, '', $file_dir) . '/' . $file_name;
  1720. // 返回结果
  1721. $result = [
  1722. 'file_original_name' => $original_name,
  1723. 'file_ext' => $ext,
  1724. 'file_real_path' => $real_path,
  1725. 'file_type' => $type,
  1726. 'file_size' => $size,
  1727. 'file_name' => $file_name,
  1728. 'file_path' => $file_path,
  1729. ];
  1730. return message(MESSAGE_OK, true, $result);
  1731. }
  1732. }
  1733. if (!function_exists('upload_video')) {
  1734. /**
  1735. * 上传视频文件
  1736. * @param $request 网络请求
  1737. * @param string $form_name 文件表单名
  1738. * @return array 返回结果
  1739. * @author laravel开发员
  1740. * @date 2019/5/23
  1741. */
  1742. function upload_video($request, $form_name = 'file', $file_path = '', $userId = 0)
  1743. {
  1744. // 检测请求中是否包含上传的文件
  1745. if (!$request->hasFile($form_name)) {
  1746. return message("请上传文件");
  1747. }
  1748. // 文件对象
  1749. $file = $request->file($form_name);
  1750. // 判断图片上传是否错误
  1751. if (!$file->isValid()) {
  1752. // 文件上传失败
  1753. return message("上传文件验证失败");
  1754. }
  1755. // 文件原名
  1756. $original_name = $file->getClientOriginalName();
  1757. // 文件扩展名(文件后缀)
  1758. $ext = $file->getClientOriginalExtension();
  1759. // 临时文件的绝对路径
  1760. $real_path = $file->getRealPath();
  1761. // 文件类型
  1762. $type = $file->getClientMimeType();
  1763. // 文件大小
  1764. $size = $file->getSize();
  1765. // 文件大小校验(MAX=100M)
  1766. $file_max_size = 50 * 1024 * 1024;
  1767. if ($size > $file_max_size) {
  1768. return message("您上传的文件过大,最大值为" . $file_max_size / 1024 / 1024 . "MB");
  1769. }
  1770. // 允许上传的文件后缀
  1771. $ext = strtolower($ext);
  1772. $file_exts = array('mp4', 'wmv', 'mov','flv');
  1773. if (!in_array($ext, $file_exts)) {
  1774. return message("视频文件格式不正确:" . $ext);
  1775. }
  1776. // 文件路径
  1777. $file_dir = ($file_path ? ATTACHMENT_PATH . '/videos/' . $file_path : ATTACHMENT_PATH . '/videos/files') . "/" . date('Ymd');
  1778. // 检测文件路径是否存在,不存在则创建
  1779. if (!file_exists($file_dir)) {
  1780. mkdir($file_dir, 0777, true);
  1781. }
  1782. // 文件名称
  1783. $file_name = uniqid() . '.' . $ext;
  1784. // 重命名保存
  1785. $path = $file->move($file_dir, $file_name);
  1786. // 文件临时路径
  1787. $file_path = str_replace(ATTACHMENT_PATH, '', $file_dir) . '/' . $file_name;
  1788. // 返回结果
  1789. $result = [
  1790. 'file_original_name' => $original_name,
  1791. 'file_ext' => $ext,
  1792. 'file_real_path' => $real_path,
  1793. 'file_type' => $type,
  1794. 'file_size' => $size,
  1795. 'file_name' => $file_name,
  1796. 'file_path' => $file_path,
  1797. ];
  1798. return message(MESSAGE_OK, true, $result);
  1799. }
  1800. }
  1801. if (!function_exists('upload_base64')) {
  1802. /**
  1803. * 上传文件
  1804. * @param $base64 base64内容
  1805. * @return array 返回结果
  1806. * @author laravel开发员
  1807. * @date 2019/5/23
  1808. */
  1809. function upload_base64($base64, $fileType='jpeg', $file_path = '', $userId = 0)
  1810. {
  1811. // 检测请求中是否包含上传的文件
  1812. if (empty($base64)) {
  1813. return false;
  1814. }
  1815. // 文件路径
  1816. $file_dir = ($file_path ? ATTACHMENT_PATH . '/base64/' . $file_path : ATTACHMENT_PATH . '/base64/files') . "/" . date('Ymd');
  1817. // 检测文件路径是否存在,不存在则创建
  1818. if (!file_exists($file_dir)) {
  1819. mkdir($file_dir, 0777, true);
  1820. }
  1821. // 文件名称
  1822. $file_name = uniqid() . '.' . $fileType;
  1823. // 保存文件内容
  1824. $base64 = str_replace('data:image/jpeg;base64,','',$base64);
  1825. file_put_contents($file_dir.'/'.$file_name,base64_decode($base64));
  1826. if(!file_exists($file_dir.'/'.$file_name)){
  1827. return false;
  1828. }
  1829. // 文件临时路径
  1830. $file_path = str_replace(ATTACHMENT_PATH, '', $file_dir) . '/' . $file_name;
  1831. // 返回结果
  1832. $result = [
  1833. 'file_ext' => $fileType,
  1834. 'file_type' => $fileType,
  1835. 'file_name' => $file_name,
  1836. 'file_path' => $file_path,
  1837. ];
  1838. return $result;
  1839. }
  1840. }
  1841. if (!function_exists('upload_remote_image')) {
  1842. /**
  1843. * 上传保存网络文件
  1844. * @param $request 网络请求
  1845. * @return array 返回结果
  1846. * @author laravel开发员
  1847. * @date 2019/5/23
  1848. */
  1849. function upload_remote_image($url, $fileType='jpg', $file_path = '', $userId = 0)
  1850. {
  1851. // 检测请求中是否包含上传的文件
  1852. if (empty($url)) {
  1853. return false;
  1854. }
  1855. // 文件路径
  1856. $file_dir = ($file_path ? ATTACHMENT_PATH . '/remotes/' . $file_path : ATTACHMENT_PATH . '/remotes/files') . "/" . date('Ymd');
  1857. // 检测文件路径是否存在,不存在则创建
  1858. if (!file_exists($file_dir)) {
  1859. mkdir($file_dir, 0777, true);
  1860. }
  1861. // 文件名称
  1862. $file_name = uniqid() . '.' . $fileType;
  1863. // 保存文件内容
  1864. $content = file_get_contents($url);
  1865. if(!$content){
  1866. return false;
  1867. }
  1868. file_put_contents($file_dir.'/'.$file_name, $content);
  1869. if(!file_exists($file_dir.'/'.$file_name)){
  1870. return false;
  1871. }
  1872. // 文件临时路径
  1873. $file_path = str_replace(ATTACHMENT_PATH, '', $file_dir) . '/' . $file_name;
  1874. // 返回结果
  1875. $result = [
  1876. 'file_ext' => $fileType,
  1877. 'file_type' => $fileType,
  1878. 'file_name' => $file_name,
  1879. 'file_path' => $file_path,
  1880. ];
  1881. return $result;
  1882. }
  1883. }
  1884. if (!function_exists('widget')) {
  1885. /**
  1886. * 加载系统组件,传入的名字会以目录和类名区别
  1887. * 如Home.Common就代表Widget目录下的Home/Common.php这个widget。
  1888. * @param $widgetName 组件名称
  1889. * @return bool|mixed
  1890. * @author laravel开发员
  1891. * @date 2019/5/23
  1892. */
  1893. function widget($widgetName)
  1894. {
  1895. $widgetNameEx = explode('.', $widgetName);
  1896. if (!isset($widgetNameEx[1])) {
  1897. return false;
  1898. }
  1899. $widgetClass = 'App\\Widget\\' . $widgetNameEx[0] . '\\' . $widgetNameEx[1];
  1900. if (app()->bound($widgetName)) {
  1901. return app()->make($widgetName);
  1902. }
  1903. app()->singleton($widgetName, function () use ($widgetClass) {
  1904. return new $widgetClass();
  1905. });
  1906. return app()->make($widgetName);
  1907. }
  1908. }
  1909. if (!function_exists('get_tree')) {
  1910. function get_tree($datas, $pid = 0, $tempArr = [])
  1911. {
  1912. if (empty($datas)) {
  1913. return false;
  1914. }
  1915. foreach ($datas as $key => $v) {
  1916. $data = [];
  1917. $id = isset($v['id']) ? $v['id'] : 0;
  1918. $currentPid = isset($v['parent_id']) ? $v['parent_id'] : 0;
  1919. $nickname = isset($v['nickname']) ? $v['nickname'] : '';
  1920. $username = isset($v['username']) ? $v['username'] : '';
  1921. $mobile = isset($v['mobile']) ? $v['mobile'] : '';
  1922. $data['id'] = $v['id'];
  1923. $data['parent_id'] = $v['parent_id'];
  1924. $data['mobile'] = $mobile;
  1925. $data['label'] = $nickname . ($mobile ? "({$mobile})" : "");
  1926. if ($currentPid && $currentPid == $pid) {
  1927. $data['children'] = get_tree($datas, $v['id'], []);
  1928. $tempArr[] = $data;
  1929. } else if ($currentPid == 0 && $pid == 0) {
  1930. $data['children'] = get_tree($datas, $v['id'], []);
  1931. $tempArr[$id] = $data;
  1932. }
  1933. }
  1934. return array_values($tempArr);
  1935. }
  1936. }
  1937. if (!function_exists('getSign')) {
  1938. function getSign($data, $key = '')
  1939. {
  1940. if (!is_array($data) || empty($data)) {
  1941. return false;
  1942. }
  1943. $str = arrayToStr($data);
  1944. // 拼接密钥
  1945. $str .= '&key=' . ($key ? $key : md5(env('APP_SIGN_KEY', 'dysapp')));
  1946. $str = htmlspecialchars_decode($str);
  1947. // MD5 运算
  1948. // var_dump($str);
  1949. $sign = md5($str);
  1950. $sign .= substr($sign, 6, 4);
  1951. $sign = strtoupper($sign);
  1952. return $sign;
  1953. }
  1954. }
  1955. // 获取图片数组预览
  1956. if (!function_exists('get_images_preview')) {
  1957. /**
  1958. * @param $urls 图片数组
  1959. * @param string $keyName 图片地址键名,空则无键名
  1960. * @return false
  1961. */
  1962. function get_images_preview($urls, $keyName = 'url', $domain='')
  1963. {
  1964. if (empty($urls)) {
  1965. return [];
  1966. }
  1967. $urls = is_array($urls)? $urls : json_decode($urls, true);
  1968. foreach ($urls as &$item) {
  1969. if ($keyName) {
  1970. $item[$keyName] = get_image_url($item[$keyName], $domain);
  1971. } else {
  1972. $item = get_image_url($item, $domain);
  1973. }
  1974. }
  1975. unset($item);
  1976. return $urls;
  1977. }
  1978. }
  1979. if (!function_exists('arrayToStr')) {
  1980. function arrayToStr($params)
  1981. {
  1982. $string = '';
  1983. if (!empty($params)) {
  1984. ksort($params, SORT_REGULAR);
  1985. $array = array();
  1986. foreach ($params as $key => &$value) {
  1987. if($key != 'sign'){
  1988. // 过滤无法匹配校验的参数类型
  1989. if ($value !== false && $value != 'undefined' && !is_array($value) && !preg_match("/^[0-9]{1,9}\.[0-9]{8,}$/", $value)) {
  1990. $array[] = $key . '=' . trim($value);
  1991. }
  1992. }
  1993. }
  1994. $string = implode("&", $array);
  1995. }
  1996. return $string;
  1997. }
  1998. }
  1999. if (!function_exists('arrayToUrl')) {
  2000. function arrayToUrl($params)
  2001. {
  2002. $string = '';
  2003. if (!empty($params)) {
  2004. ksort($params, SORT_REGULAR);
  2005. $array = array();
  2006. foreach ($params as $key => $value) {
  2007. if ($value != 'undefined') {
  2008. $array[] = $key . '=' . (is_array($value) ? json_encode($value,JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE) : $value);
  2009. }
  2010. }
  2011. $string = implode("&", $array);
  2012. }
  2013. return $string;
  2014. }
  2015. }
  2016. if (!function_exists('httpRequest')) {
  2017. /**
  2018. *
  2019. * 接口请求
  2020. * @param $url 接口地址
  2021. * @param $data
  2022. * @param $type
  2023. * @param int $timeout
  2024. * @return mixed
  2025. * @author wesmiler
  2026. */
  2027. function httpRequest($url, $data = '', $type = 'post', $cookie = '', $timeout = 60, $header=[],$dataType='array')
  2028. {
  2029. try {
  2030. set_time_limit($timeout);
  2031. $data = $data && is_array($data) ? http_build_query($data) : $data;
  2032. $url = strtolower($type) == 'get' ? $url . (strpos($url, '?') === false ? '?' : '&') . $data : $url;
  2033. $ch = curl_init($url);
  2034. if (!empty($cookie)) {
  2035. curl_setopt($ch, CURLOPT_COOKIE, $cookie);
  2036. }
  2037. if ($type == 'post') {
  2038. curl_setopt($ch, CURLOPT_POST, 1);
  2039. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  2040. }
  2041. if ($header) {
  2042. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  2043. } else {
  2044. $header = ['User-Agent:Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1'];
  2045. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  2046. }
  2047. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //禁止 cURL 验证对等证书
  2048. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); //是否检测服务器的域名与证书上的是否一致
  2049. curl_setopt($ch, CURLOPT_HEADER, 0);
  2050. curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
  2051. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  2052. curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
  2053. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  2054. $ret = curl_exec($ch);
  2055. $ret = trim($ret);
  2056. curl_close($ch);
  2057. if($dataType != 'array'){
  2058. return $ret;
  2059. }
  2060. if (!preg_match("/^{/", $ret)) {
  2061. return ['code' => 'err', 'msg' => $ret];
  2062. }
  2063. $retArr = $ret ? json_decode($ret, true) : ['code' => 'err', 'msg' => 'unknow error'];
  2064. if (empty($retArr)) {
  2065. $ret = iconv('gb2312', 'utf-8', $ret);
  2066. $retArr = json_decode($ret, true);
  2067. }
  2068. } catch (\Exception $exception) {
  2069. $retArr = ['code' => 'error', 'msg' => 'request error'];
  2070. }
  2071. return $retArr;
  2072. }
  2073. }
  2074. if (!function_exists('getVersion')) {
  2075. /**
  2076. * 获取版本号
  2077. * @param $version
  2078. * @return string|string[]
  2079. */
  2080. function getVersion($version)
  2081. {
  2082. return $version ? intval(str_replace('.', '', $version)) : '';
  2083. }
  2084. }
  2085. if (!function_exists('formatDistance')) {
  2086. /**
  2087. * 格式化距离
  2088. * @param $distance
  2089. * @return string
  2090. */
  2091. function formatDistance($distance)
  2092. {
  2093. return ($distance > 1000 ? round($distance / 1000, 2) . 'km' : $distance . 'm');
  2094. }
  2095. }
  2096. if (!function_exists('moneyFormat')) {
  2097. /**
  2098. * 金额格式化
  2099. * @param $money
  2100. * @param int $decimal 小数位数,默认2
  2101. * @param int $round 是否四舍五入
  2102. * @return string
  2103. */
  2104. function moneyFormat($money, $decimal = 2, $round = false, $charset = 'utf-8')
  2105. {
  2106. if ($round) {
  2107. $money = number_format($money, $decimal);
  2108. } else {
  2109. $money = round($money, $decimal + 1);
  2110. $data = explode('.', $money);
  2111. $money = isset($data[0]) ? $data[0] : 0;
  2112. $float = isset($data[1]) ? $data[1] : '';
  2113. $len = $float ? mb_strlen($float, $charset) : 0;
  2114. $decimal = $decimal >= 0 ? intval($decimal) : 2;
  2115. $num1 = isset($data[1]) ? $data[1] : '';
  2116. $float = $len >= $decimal ? mb_substr($num1, 0, $decimal, $charset) : $float . str_repeat('0', $decimal - $len);
  2117. $money = $money . '.' . $float;
  2118. $money = number_format($money, $decimal, '.', '');
  2119. }
  2120. return $money;
  2121. }
  2122. }
  2123. if(!function_exists('getChatKey')){
  2124. /**
  2125. * 获取聊天窗口KEY
  2126. * @param $fromUserId
  2127. * @param $toUserId
  2128. * @return false|string
  2129. */
  2130. function getChatKey($fromUserId, $toUserId)
  2131. {
  2132. if(empty($fromUserId) || empty($toUserId)){
  2133. return false;
  2134. }
  2135. $ids = [$fromUserId, $toUserId];
  2136. asort($ids);
  2137. return implode('_', $ids);
  2138. }
  2139. }
  2140. if(!function_exists('getDistance')) {
  2141. /**
  2142. * 计算地图坐标距离
  2143. * @param $lat
  2144. * @param $lng
  2145. * @param $toLat
  2146. * @param $toLng
  2147. * @return float
  2148. */
  2149. function getDistance($lat, $lng, $toLat, $toLng)
  2150. {
  2151. return ROUND(6378.138 * 2 * ASIN(
  2152. SQRT(
  2153. POW(
  2154. SIN(
  2155. ($lat * PI() / 180 - $toLat * PI() / 180
  2156. ) / 2
  2157. ),
  2158. 2
  2159. ) + COS($lat * PI() / 180) * COS($toLat * PI() / 180) * POW(
  2160. SIN(
  2161. ($lng * PI() / 180 - $toLng * PI() / 180
  2162. ) / 2
  2163. ), 2))) * 1000);
  2164. }
  2165. }
  2166. if(!function_exists('getParents')){
  2167. /**
  2168. * 获取分销上级用户ID
  2169. * @param $parents
  2170. * @return array|false
  2171. */
  2172. function getParents($parents, $level=3){
  2173. $parents = $parents? explode(',', $parents) : [];
  2174. $parents = array_filter($parents);
  2175. if(empty($parents)){
  2176. return false;
  2177. }
  2178. $parents = array_slice($parents, -$level);
  2179. return array_reverse($parents);
  2180. }
  2181. }
  2182. if(!function_exists('getMillSecondTime')){
  2183. /**
  2184. * 获取毫秒时间戳
  2185. * @return float
  2186. */
  2187. function getMillSecondTime() {
  2188. list($msec, $sec) = explode(' ', microtime());
  2189. return (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
  2190. }
  2191. }
  2192. if(!function_exists('isValidUrl')){
  2193. function isValidUrl($url) {
  2194. if (filter_var($url, FILTER_VALIDATE_URL)) {
  2195. return true;
  2196. } else {
  2197. return false;
  2198. }
  2199. }
  2200. }