common.php 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384
  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 ? $host . '/' . ltrim($image_url, '/') : $image_url;
  765. }
  766. }
  767. if (!function_exists('get_image_path')) {
  768. /**
  769. * 获取图片相对路径
  770. * @param $image_url 图片地址
  771. * @param $https 是否有证书
  772. * @return string 返回图片网络地址
  773. * @author laravel开发员
  774. * @date 2019/6/6
  775. */
  776. function get_image_path($image_url, $https = true)
  777. {
  778. if (empty($image_url)) {
  779. return false;
  780. }
  781. $data = explode('/uploads', $image_url);
  782. return $data ? end($data) : $image_url;
  783. }
  784. }
  785. if (!function_exists('get_format_images')) {
  786. /**
  787. * 获取多图片相对路径数组或json
  788. * @param $images 图片地址
  789. * @return string 返回图片网络地址
  790. * @author laravel开发员
  791. * @date 2019/6/6
  792. */
  793. function get_format_images($images, $key = 'url', $dataType = 'json')
  794. {
  795. if (empty($images) || !is_array($images)) {
  796. return '';
  797. }
  798. $datas = [];
  799. foreach ($images as $v) {
  800. $url = isset($v[$key]) ? $v[$key] : '';
  801. if ($url) {
  802. $datas[] = [
  803. $key=> get_image_path($url)
  804. ];
  805. }
  806. }
  807. if ($datas) {
  808. return $dataType == 'json' ? json_encode($datas, 256) : '';
  809. } else {
  810. return $dataType == 'json' ? '' : [];
  811. }
  812. }
  813. }
  814. if(!function_exists('getImageContent')){
  815. /**
  816. * 获取图片内容
  817. * @param $url
  818. * @return false|string
  819. */
  820. function getImageContent($url){
  821. $imgUrl = $url;
  822. if(!preg_match("/^http/", $url)){
  823. $imgUrl = base_path().'/public/uploads/'.$url;
  824. }
  825. if(is_file($imgUrl) && file_exists($imgUrl)){
  826. $data =file_get_contents($imgUrl);
  827. return "data:image/jpeg;base64," . base64_encode($data);
  828. }else{
  829. return get_image_url($url);
  830. }
  831. }
  832. }
  833. if (!function_exists('get_web_url')) {
  834. /**
  835. * 获取预览地址
  836. * @param $url 地址
  837. * @return string 返回网络地址
  838. * @author laravel开发员
  839. * @date 2019/6/6
  840. */
  841. function get_web_url($url)
  842. {
  843. $host = request()->header('HOST');
  844. $host = ((strpos($host, '127') === false) && strpos($host, 'localhost') === false) ? env('WEB_URL') : 'http://' . $host;
  845. return strpos($url, 'http') === false ? $host . '/' . ltrim($url, '/') : $url;
  846. }
  847. }
  848. if (!function_exists('set_format_content')) {
  849. /**
  850. * 获取文章图片格式化后内容
  851. * @param $content
  852. * @return false|mixed|string
  853. */
  854. function set_format_content($content)
  855. {
  856. if (empty($content)) {
  857. return false;
  858. }
  859. $domain = request()->header('HOST');
  860. if(preg_match("/127/", $domain)){
  861. $host = env('IMG_URL','');
  862. if(empty($host)){
  863. $https = request()->secure();
  864. $host = ($https ? 'https://' : 'http://') . $host . '/uploads';
  865. }
  866. }else{
  867. $https = request()->secure();
  868. $host = ($https ? 'https://' : 'http://') . $domain . '/uploads';
  869. }
  870. $content = str_replace("{$host}",'/uploads', htmlspecialchars_decode($content));
  871. return $content;
  872. }
  873. }
  874. if (!function_exists('get_format_content')) {
  875. /**
  876. * 获取文章图片格式化后内容
  877. * @param $content
  878. * @return false|mixed|string
  879. */
  880. function get_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(["\"/uploads","'/uploads"],["\"{$host}","'{$host}"], htmlspecialchars_decode($content));
  897. return $content;
  898. }
  899. }
  900. if (!function_exists('get_hash')) {
  901. /**
  902. * 获取HASH值
  903. * @return string 返回hash字符串
  904. * @author laravel开发员
  905. * @date 2019/6/6
  906. */
  907. function get_hash()
  908. {
  909. $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()+-';
  910. $random = $chars[mt_rand(0, 73)] . $chars[mt_rand(0, 73)] . $chars[mt_rand(0, 73)]
  911. . $chars[mt_rand(0, 73)] . $chars[mt_rand(0, 73)];
  912. $content = uniqid() . $random;
  913. return sha1($content);
  914. }
  915. }
  916. if (!function_exists('get_server_ip')) {
  917. /**
  918. * 获取服务端IP地址
  919. * @return string 返回IP地址
  920. * @author laravel开发员
  921. * @date 2019/6/6
  922. */
  923. function get_server_ip()
  924. {
  925. if (isset($_SERVER)) {
  926. if ($_SERVER['SERVER_ADDR']) {
  927. $server_ip = $_SERVER['SERVER_ADDR'];
  928. } else {
  929. $server_ip = $_SERVER['LOCAL_ADDR'];
  930. }
  931. } else {
  932. $server_ip = getenv('SERVER_ADDR');
  933. }
  934. return $server_ip;
  935. }
  936. }
  937. if (!function_exists('get_client_ip')) {
  938. /**
  939. * 获取客户端IP地址
  940. * @param int $type 返回类型 0 返回IP地址 1 返回IPV4地址数字
  941. * @param bool $adv 否进行高级模式获取(有可能被伪装)
  942. * @return mixed 返回IP
  943. * @author laravel开发员
  944. * @date 2019/5/23
  945. */
  946. function get_client_ip($type = 0, $adv = false)
  947. {
  948. $type = $type ? 1 : 0;
  949. static $ip = null;
  950. if ($ip !== null) {
  951. return $ip[$type];
  952. }
  953. if ($adv) {
  954. if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  955. $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
  956. $pos = array_search('unknown', $arr);
  957. if (false !== $pos) {
  958. unset($arr[$pos]);
  959. }
  960. $ip = trim($arr[0]);
  961. } elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
  962. $ip = $_SERVER['HTTP_CLIENT_IP'];
  963. } elseif (isset($_SERVER['REMOTE_ADDR'])) {
  964. $ip = $_SERVER['REMOTE_ADDR'];
  965. }
  966. } elseif (isset($_SERVER['REMOTE_ADDR'])) {
  967. $ip = $_SERVER['REMOTE_ADDR'];
  968. }
  969. // IP地址合法验证
  970. $long = sprintf("%u", ip2long($ip));
  971. $ip = $long ? array($ip, $long) : array('0.0.0.0', 0);
  972. return $ip[$type];
  973. }
  974. }
  975. if (!function_exists('get_guid_v4')) {
  976. /**
  977. * 获取唯一性GUID
  978. * @param bool $trim 是否去除{}
  979. * @return string 返回GUID字符串
  980. * @author laravel开发员
  981. * @date 2019/6/6
  982. */
  983. function get_guid_v4($trim = true)
  984. {
  985. // Windows
  986. if (function_exists('com_create_guid') === true) {
  987. $charid = com_create_guid();
  988. return $trim == true ? trim($charid, '{}') : $charid;
  989. }
  990. // OSX/Linux
  991. if (function_exists('openssl_random_pseudo_bytes') === true) {
  992. $data = openssl_random_pseudo_bytes(16);
  993. $data[6] = chr(ord($data[6]) & 0x0f | 0x40); // set version to 0100
  994. $data[8] = chr(ord($data[8]) & 0x3f | 0x80); // set bits 6-7 to 10
  995. return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
  996. }
  997. // Fallback (PHP 4.2+)
  998. mt_srand((double)microtime() * 10000);
  999. $charid = strtolower(md5(uniqid(rand(), true)));
  1000. $hyphen = chr(45); // "-"
  1001. $lbrace = $trim ? "" : chr(123); // "{"
  1002. $rbrace = $trim ? "" : chr(125); // "}"
  1003. $guidv4 = $lbrace .
  1004. substr($charid, 0, 8) . $hyphen .
  1005. substr($charid, 8, 4) . $hyphen .
  1006. substr($charid, 12, 4) . $hyphen .
  1007. substr($charid, 16, 4) . $hyphen .
  1008. substr($charid, 20, 12) .
  1009. $rbrace;
  1010. return $guidv4;
  1011. }
  1012. }
  1013. if (!function_exists('is_email')) {
  1014. /**
  1015. * 判断是否为邮箱
  1016. * @param string $str 邮箱
  1017. * @return false 返回结果true或false
  1018. * @author laravel开发员
  1019. * @date 2019/5/23
  1020. */
  1021. function is_email($str)
  1022. {
  1023. return preg_match('/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/', $str);
  1024. }
  1025. }
  1026. if (!function_exists('is_mobile')) {
  1027. /**
  1028. * 判断是否为手机号
  1029. * @param string $num 手机号码
  1030. * @return false 返回结果true或false
  1031. * @author laravel开发员
  1032. * @date 2019/5/23
  1033. */
  1034. function is_mobile($num)
  1035. {
  1036. return preg_match('/^1(3|4|5|7|8)\d{9}$/', $num);
  1037. }
  1038. }
  1039. if (!function_exists('is_zipcode')) {
  1040. /**
  1041. * 验证邮编是否正确
  1042. * @param string $code 邮编
  1043. * @return false 返回结果true或false
  1044. * @author laravel开发员
  1045. * @date 2019/5/23
  1046. */
  1047. function is_zipcode($code)
  1048. {
  1049. return preg_match('/^[1-9][0-9]{5}$/', $code);
  1050. }
  1051. }
  1052. if (!function_exists('is_idcard')) {
  1053. /**
  1054. * 验证身份证是否正确
  1055. * @param string $idno 身份证号
  1056. * @return bool 返回结果true或false
  1057. * @author laravel开发员
  1058. * @date 2019/5/23
  1059. */
  1060. function is_idcard($idno)
  1061. {
  1062. $idno = strtoupper($idno);
  1063. $regx = '/(^\d{15}$)|(^\d{17}([0-9]|X)$)/';
  1064. $arr_split = array();
  1065. if (!preg_match($regx, $idno)) {
  1066. return false;
  1067. }
  1068. // 检查15位
  1069. if (15 == strlen($idno)) {
  1070. $regx = '/^(\d{6})+(\d{2})+(\d{2})+(\d{2})+(\d{3})$/';
  1071. @preg_match($regx, $idno, $arr_split);
  1072. $dtm_birth = "19" . $arr_split[2] . '/' . $arr_split[3] . '/' . $arr_split[4];
  1073. if (!strtotime($dtm_birth)) {
  1074. return false;
  1075. } else {
  1076. return true;
  1077. }
  1078. } else {
  1079. // 检查18位
  1080. $regx = '/^(\d{6})+(\d{4})+(\d{2})+(\d{2})+(\d{3})([0-9]|X)$/';
  1081. @preg_match($regx, $idno, $arr_split);
  1082. $dtm_birth = $arr_split[2] . '/' . $arr_split[3] . '/' . $arr_split[4];
  1083. // 检查生日日期是否正确
  1084. if (!strtotime($dtm_birth)) {
  1085. return false;
  1086. } else {
  1087. // 检验18位身份证的校验码是否正确。
  1088. // 校验位按照ISO 7064:1983.MOD 11-2的规定生成,X可以认为是数字10。
  1089. $arr_int = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
  1090. $arr_ch = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
  1091. $sign = 0;
  1092. for ($i = 0; $i < 17; $i++) {
  1093. $b = (int)$idno[$i];
  1094. $w = $arr_int[$i];
  1095. $sign += $b * $w;
  1096. }
  1097. $n = $sign % 11;
  1098. $val_num = $arr_ch[$n];
  1099. if ($val_num != substr($idno, 17, 1)) {
  1100. return false;
  1101. } else {
  1102. return true;
  1103. }
  1104. }
  1105. }
  1106. }
  1107. }
  1108. if (!function_exists('is_empty')) {
  1109. /**
  1110. * 判断是否为空
  1111. * @param $value 参数值
  1112. * @return bool 返回结果true或false
  1113. * @author laravel开发员
  1114. * @date 2019/6/5
  1115. */
  1116. function is_empty($value)
  1117. {
  1118. // 判断是否存在该值
  1119. if (!isset($value)) {
  1120. return true;
  1121. }
  1122. // 判断是否为empty
  1123. if (empty($value)) {
  1124. return true;
  1125. }
  1126. // 判断是否为null
  1127. if ($value === null) {
  1128. return true;
  1129. }
  1130. // 判断是否为空字符串
  1131. if (trim($value) === '') {
  1132. return true;
  1133. }
  1134. // 默认返回false
  1135. return false;
  1136. }
  1137. }
  1138. if (!function_exists('mkdirs')) {
  1139. /**
  1140. * 递归创建目录
  1141. * @param string $dir 需要创建的目录路径
  1142. * @param int $mode 权限值
  1143. * @return bool 返回结果true或false
  1144. * @author laravel开发员
  1145. * @date 2019/6/6
  1146. */
  1147. function mkdirs($dir, $mode = 0777)
  1148. {
  1149. if (is_dir($dir) || mkdir($dir, $mode, true)) {
  1150. return true;
  1151. }
  1152. if (!mkdirs(dirname($dir), $mode)) {
  1153. return false;
  1154. }
  1155. return mkdir($dir, $mode, true);
  1156. }
  1157. }
  1158. if (!function_exists('rmdirs')) {
  1159. /**
  1160. * 删除文件夹
  1161. * @param string $dir 文件夹路径
  1162. * @param bool $rmself 是否删除本身true或false
  1163. * @return bool 返回删除结果
  1164. * @author laravel开发员
  1165. * @date 2019/6/6
  1166. */
  1167. function rmdirs($dir, $rmself = true)
  1168. {
  1169. if (!is_dir($dir)) {
  1170. return false;
  1171. }
  1172. $files = new RecursiveIteratorIterator(
  1173. new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS),
  1174. RecursiveIteratorIterator::CHILD_FIRST
  1175. );
  1176. foreach ($files as $file) {
  1177. $todo = ($file->isDir() ? 'rmdir' : 'unlink');
  1178. $todo($file->getRealPath());
  1179. }
  1180. if ($rmself) {
  1181. @rmdir($dir);
  1182. }
  1183. return true;
  1184. }
  1185. }
  1186. if (!function_exists('copydirs')) {
  1187. /**
  1188. * 复制文件夹
  1189. * @param string $source 原文件夹路径
  1190. * @param string $dest 目的文件夹路径
  1191. * @author laravel开发员
  1192. * @date 2019/6/6
  1193. */
  1194. function copydirs($source, $dest)
  1195. {
  1196. if (!is_dir($dest)) {
  1197. mkdir($dest, 0755, true);
  1198. }
  1199. $iterator = new RecursiveIteratorIterator(
  1200. new RecursiveDirectoryIterator($source, RecursiveDirectoryIterator::SKIP_DOTS),
  1201. RecursiveIteratorIterator::SELF_FIRST
  1202. );
  1203. foreach ($iterator as $item) {
  1204. if ($item->isDir()) {
  1205. $sent_dir = $dest . "/" . $iterator->getSubPathName();
  1206. if (!is_dir($sent_dir)) {
  1207. mkdir($sent_dir, 0755, true);
  1208. }
  1209. } else {
  1210. copy($item, $dest . "/" . $iterator->getSubPathName());
  1211. }
  1212. }
  1213. }
  1214. }
  1215. if (!function_exists('lang')) {
  1216. function lang($msg, $replace = [], $local = '')
  1217. {
  1218. $local = $local ? $local : config('app.locale');
  1219. $data = __('api.' . $msg, $replace, $local);
  1220. if (strpos($data, 'api', 0) !== false) {
  1221. return __($msg, $replace, $local);
  1222. }
  1223. return __('api.' . $msg, $replace, $local);
  1224. }
  1225. }
  1226. if (!function_exists('message')) {
  1227. /**
  1228. * 消息数组
  1229. * @param string $msg 提示文字
  1230. * @param bool $success 是否成功true或false
  1231. * @param array $data 结果数据
  1232. * @param int $code 编码
  1233. * @return array 返回结果
  1234. * @author laravel开发员
  1235. * @date 2019/5/28
  1236. */
  1237. function message($msg = "操作成功", $success = true, $data = [], $code = 0, $type = 'json')
  1238. {
  1239. $result = ['success' => $success, 'msg' => lang($msg), 'data' => $data];
  1240. if ($success) {
  1241. $result['code'] = 0;
  1242. } else {
  1243. $result['code'] = $code ? $code : -1;
  1244. }
  1245. return $result;
  1246. }
  1247. }
  1248. if (!function_exists('showJson')) {
  1249. /**
  1250. * 消息数组
  1251. * @param string $msg 提示文字
  1252. * @param bool $success 是否成功true或false
  1253. * @param array $data 结果数据
  1254. * @param int $code 编码
  1255. * @return array 返回结果
  1256. * @author laravel开发员
  1257. * @date 2019/5/28
  1258. */
  1259. function showJson($msg = "操作成功", $success = true, $data = [], $code = 0, $type = 'json')
  1260. {
  1261. $result = ['success' => $success, 'msg' => lang($msg), 'data' => $data];
  1262. if ($success) {
  1263. $result['code'] = 0;
  1264. } else {
  1265. $result['code'] = $code ? $code : -1;
  1266. }
  1267. return $type=='json'?response()->json($result, 200, [])->setEncodingOptions(256):$result;
  1268. }
  1269. }
  1270. if (!function_exists('num2rmb')) {
  1271. /**
  1272. * 数字金额转大写
  1273. * @param float $num 金额
  1274. * @return string 返回大写金额
  1275. * @author laravel开发员
  1276. * @date 2019/6/6
  1277. */
  1278. function num2rmb($num)
  1279. {
  1280. $c1 = "零壹贰叁肆伍陆柒捌玖";
  1281. $c2 = "分角元拾佰仟万拾佰仟亿";
  1282. $num = round($num, 2);
  1283. $num = $num * 100;
  1284. if (strlen($num) > 10) {
  1285. return "oh,sorry,the number is too long!";
  1286. }
  1287. $i = 0;
  1288. $c = "";
  1289. while (1) {
  1290. if ($i == 0) {
  1291. $n = substr($num, strlen($num) - 1, 1);
  1292. } else {
  1293. $n = $num % 10;
  1294. }
  1295. $p1 = substr($c1, 3 * $n, 3);
  1296. $p2 = substr($c2, 3 * $i, 3);
  1297. if ($n != '0' || ($n == '0' && ($p2 == '亿' || $p2 == '万' || $p2 == '元'))) {
  1298. $c = $p1 . $p2 . $c;
  1299. } else {
  1300. $c = $p1 . $c;
  1301. }
  1302. $i = $i + 1;
  1303. $num = $num / 10;
  1304. $num = (int)$num;
  1305. if ($num == 0) {
  1306. break;
  1307. }
  1308. }
  1309. $j = 0;
  1310. $slen = strlen($c);
  1311. while ($j < $slen) {
  1312. $m = substr($c, $j, 6);
  1313. if ($m == '零元' || $m == '零万' || $m == '零亿' || $m == '零零') {
  1314. $left = substr($c, 0, $j);
  1315. $right = substr($c, $j + 3);
  1316. $c = $left . $right;
  1317. $j = $j - 3;
  1318. $slen = $slen - 3;
  1319. }
  1320. $j = $j + 3;
  1321. }
  1322. if (substr($c, strlen($c) - 3, 3) == '零') {
  1323. $c = substr($c, 0, strlen($c) - 3);
  1324. } // if there is a '0' on the end , chop it out
  1325. return $c . "整";
  1326. }
  1327. }
  1328. if (!function_exists('object_array')) {
  1329. /**
  1330. * 对象转数组
  1331. * @param $object 对象
  1332. * @return mixed 返回结果
  1333. * @author laravel开发员
  1334. * @date 2019/5/23
  1335. */
  1336. function object_array($object)
  1337. {
  1338. //先编码成json字符串,再解码成数组
  1339. return json_decode(json_encode($object), true);
  1340. }
  1341. }
  1342. if (!function_exists('parse_attr')) {
  1343. /**
  1344. * 配置值解析成数组
  1345. * @param string $value 参数值
  1346. * @return array 返回结果
  1347. * @author laravel开发员
  1348. * @date 2019/6/6
  1349. */
  1350. function parse_attr($value = '')
  1351. {
  1352. if (is_array($value)) {
  1353. return $value;
  1354. }
  1355. $array = preg_split('/[,;\r\n]+/', trim($value, ",;\r\n"));
  1356. if (strpos($value, ':')) {
  1357. $value = array();
  1358. foreach ($array as $val) {
  1359. list($k, $v) = explode(':', $val);
  1360. $value[$k] = $v;
  1361. }
  1362. } else {
  1363. $value = $array;
  1364. }
  1365. return $value;
  1366. }
  1367. }
  1368. if (!function_exists('strip_html_tags')) {
  1369. /**
  1370. * 去除HTML标签、图像等 仅保留文本
  1371. * @param string $str 字符串
  1372. * @param int $length 长度
  1373. * @return string 返回结果
  1374. * @author laravel开发员
  1375. * @date 2019/5/23
  1376. */
  1377. function strip_html_tags($str, $length = 0)
  1378. {
  1379. // 把一些预定义的 HTML 实体转换为字符
  1380. $str = htmlspecialchars_decode($str);
  1381. // 将空格替换成空
  1382. $str = str_replace("&nbsp;", "", $str);
  1383. // 函数剥去字符串中的 HTML、XML 以及 PHP 的标签,获取纯文本内容
  1384. $str = strip_tags($str);
  1385. $str = str_replace(array("\n", "\r\n", "\r"), ' ', $str);
  1386. $preg = '/<script[\s\S]*?<\/script>/i';
  1387. // 剥离JS代码
  1388. $str = preg_replace($preg, "", $str, -1);
  1389. if ($length == 2) {
  1390. // 返回字符串中的前100字符串长度的字符
  1391. $str = mb_substr($str, 0, $length, "utf-8");
  1392. }
  1393. return $str;
  1394. }
  1395. }
  1396. if (!function_exists('sub_str')) {
  1397. /**
  1398. * 字符串截取
  1399. * @param string $str 需要截取的字符串
  1400. * @param int $start 开始位置
  1401. * @param int $length 截取长度
  1402. * @param bool $suffix 截断显示字符
  1403. * @param string $charset 编码格式
  1404. * @return string 返回结果
  1405. * @author laravel开发员
  1406. * @date 2019/5/23
  1407. */
  1408. function sub_str($str, $start = 0, $length = 10, $suffix = true, $charset = "utf-8")
  1409. {
  1410. if (function_exists("mb_substr")) {
  1411. $slice = mb_substr($str, $start, $length, $charset);
  1412. } elseif (function_exists('iconv_substr')) {
  1413. $slice = iconv_substr($str, $start, $length, $charset);
  1414. } else {
  1415. $re['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
  1416. $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
  1417. $re['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
  1418. $re['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
  1419. preg_match_all($re[$charset], $str, $match);
  1420. $slice = join("", array_slice($match[0], $start, $length));
  1421. }
  1422. $omit = mb_strlen($str) >= $length ? '...' : '';
  1423. return $suffix ? $slice . $omit : $slice;
  1424. }
  1425. }
  1426. if (!function_exists('save_image')) {
  1427. /**
  1428. * 保存图片
  1429. * @param string $img_url 网络图片地址
  1430. * @param string $save_dir 图片保存目录
  1431. * @return string 返回路径
  1432. * @author laravel开发员
  1433. * @date 2019/5/23
  1434. */
  1435. function save_image($img_url, $save_dir = '/')
  1436. {
  1437. if (!$img_url) {
  1438. return false;
  1439. }
  1440. $save_dir = trim($save_dir, "/");
  1441. $imgExt = pathinfo($img_url, PATHINFO_EXTENSION);
  1442. // 是否是本站图片
  1443. if (strpos($img_url, IMG_URL) !== false) {
  1444. // 是否是临时文件
  1445. if (strpos($img_url, 'temp') === false) {
  1446. return str_replace(IMG_URL, "", $img_url);
  1447. }
  1448. $new_path = create_image_path($save_dir, $imgExt);
  1449. $old_path = str_replace(IMG_URL, ATTACHMENT_PATH, $img_url);
  1450. if (!file_exists($old_path)) {
  1451. return false;
  1452. }
  1453. rename($old_path, ATTACHMENT_PATH . $new_path);
  1454. return $new_path;
  1455. } else {
  1456. // 保存远程图片
  1457. $new_path = save_remote_image($img_url, $save_dir);
  1458. }
  1459. return $new_path;
  1460. }
  1461. }
  1462. if (!function_exists('create_image_path')) {
  1463. /**
  1464. * 创建图片存储目录
  1465. * @param string $save_dir 存储目录
  1466. * @param string $image_ext 图片后缀
  1467. * @param string $image_root 图片存储根目录路径
  1468. * @return string 返回文件目录
  1469. * @author laravel开发员
  1470. * @date 2019/5/23
  1471. */
  1472. function create_image_path($save_dir = "", $image_ext = "", $image_root = IMG_PATH)
  1473. {
  1474. $image_dir = date("/Ymd/");
  1475. if ($image_dir) {
  1476. $image_dir = ($save_dir ? "/" : '') . $save_dir . $image_dir;
  1477. }
  1478. // 未指定后缀默认使用JPG
  1479. if (!$image_ext) {
  1480. $image_ext = "jpg";
  1481. }
  1482. $image_path = $image_root . $image_dir;
  1483. if (!is_dir($image_path)) {
  1484. // 创建目录并赋予权限
  1485. mkdir($image_path, 0777, true);
  1486. }
  1487. $file_name = substr(md5(time() . rand(0, 999999)), 8, 16) . rand(100, 999) . ".{$image_ext}";
  1488. $file_path = str_replace(ATTACHMENT_PATH, "", IMG_PATH) . $image_dir . $file_name;
  1489. return $file_path;
  1490. }
  1491. }
  1492. if (!function_exists('save_remote_image')) {
  1493. /**
  1494. * 保存网络图片到本地
  1495. * @param string $img_url 网络图片地址
  1496. * @param string $save_dir 保存目录
  1497. * @return bool|string 图片路径
  1498. * @author laravel开发员
  1499. * @date 2019/5/23
  1500. */
  1501. function save_remote_image($img_url, $save_dir = '/')
  1502. {
  1503. $content = file_get_contents($img_url);
  1504. if (!$content) {
  1505. return false;
  1506. }
  1507. if ($content[0] . $content[1] == "\xff\xd8") {
  1508. $image_ext = 'jpg';
  1509. } elseif ($content[0] . $content[1] . $content[2] == "\x47\x49\x46") {
  1510. $image_ext = 'gif';
  1511. } elseif ($content[0] . $content[1] . $content[2] == "\x89\x50\x4e") {
  1512. $image_ext = 'png';
  1513. } else {
  1514. // 不是有效图片
  1515. return false;
  1516. }
  1517. $save_path = create_image_path($save_dir, $image_ext);
  1518. return file_put_contents(ATTACHMENT_PATH . $save_path, $content) ? $save_path : false;
  1519. }
  1520. }
  1521. if (!function_exists('save_image_content')) {
  1522. /**
  1523. * 富文本信息处理
  1524. * @param string $content 富文本内容
  1525. * @param bool $title 标题
  1526. * @param string $path 图片存储路径
  1527. * @return bool|int 返回结果
  1528. * @author laravel开发员
  1529. * @date 2020-04-21
  1530. */
  1531. function save_image_content(&$content, $title = false, $path = 'article')
  1532. {
  1533. // 图片处理
  1534. preg_match_all("/<img.*?src=[\"|\']?(.*?)[\"|\']?\s.*?>/i", str_ireplace("\\", "", $content), $match);
  1535. if ($match[1]) {
  1536. foreach ($match[1] as $id => $val) {
  1537. $save_image = save_image($val, $path);
  1538. if ($save_image) {
  1539. $content = str_replace($val, "[IMG_URL]" . $save_image, $content);
  1540. }
  1541. }
  1542. }
  1543. // 视频处理
  1544. preg_match_all("/<embed .*?src=[\"|\']?(.*?)[\"|\']?\s.*?>/i", str_ireplace("\\", "", $content), $match2);
  1545. if ($match2[1]) {
  1546. foreach ($match2[1] as $vo) {
  1547. $save_video = save_image($vo, $path);
  1548. if ($save_video) {
  1549. $content = str_replace($vo, "[IMG_URL]" . str_replace(ATTACHMENT_PATH, "", IMG_PATH) . $save_video, $content);
  1550. }
  1551. }
  1552. }
  1553. // 提示标签替换
  1554. if ((strpos($content, 'alt=\"\"') !== false) && $title) {
  1555. $content = str_replace('alt=\"\"', 'alt=\"' . $title . '\"', $content);
  1556. }
  1557. return true;
  1558. }
  1559. }
  1560. if (!function_exists('upload_image')) {
  1561. /**
  1562. * 上传图片
  1563. * @param $request 网络请求
  1564. * @param string $form_name 文件表单名
  1565. * @return array 返回结果
  1566. * @author laravel开发员
  1567. * @date 2019/5/23
  1568. */
  1569. function upload_image($request, $form_name = 'file', $file_path = '', $userId = 0)
  1570. {
  1571. // 检测请求中是否包含name=$form_name的上传文件
  1572. if (!$request->hasFile($form_name)) {
  1573. return message("请上传文件", false);
  1574. }
  1575. // 文件对象
  1576. $file = $request->file($form_name);
  1577. // 判断图片上传是否错误
  1578. if (!$file->isValid()) {
  1579. // 文件上传失败
  1580. return message("上传文件验证失败", false);
  1581. }
  1582. // 文件原名
  1583. $original_name = $file->getClientOriginalName();
  1584. // 文件扩展名(文件后缀)
  1585. $ext = $file->getClientOriginalExtension();
  1586. // 临时文件的绝对路径
  1587. $real_path = $file->getRealPath();
  1588. // 文件类型
  1589. $type = $file->getClientMimeType();
  1590. // 文件大小
  1591. $size = $file->getSize();
  1592. // 文件大小校验
  1593. if ($size > 5 * 1024 * 1024) {
  1594. return message("文件大小超过了5M", false);
  1595. }
  1596. // 文件后缀校验
  1597. $ext = strtolower($ext);
  1598. $ext_arr = array('jpg', 'jpeg', 'png', 'gif');
  1599. if (!in_array($ext, $ext_arr)) {
  1600. return message("文件格式不正确:" . $ext, false);
  1601. }
  1602. // 文件路径
  1603. $file_dir = ($file_path ? ATTACHMENT_PATH . '/images/' . $file_path : UPLOAD_TEMP_PATH) . "/" . date('Ymd');
  1604. // 检测文件路径是否存在,不存在则创建
  1605. if (!file_exists($file_dir)) {
  1606. mkdir($file_dir, 0777, true);
  1607. }
  1608. // 文件名称
  1609. $file_name = uniqid() . '.' . $ext;
  1610. // 重命名保存
  1611. $path = $file->move($file_dir, $file_name);
  1612. // 文件临时路径
  1613. $file_path = str_replace(ATTACHMENT_PATH, '', $file_dir) . '/' . $file_name;
  1614. // 返回结果
  1615. $result = [
  1616. 'img_original_name' => $original_name,
  1617. 'img_ext' => $ext,
  1618. 'img_real_path' => $real_path,
  1619. 'img_type' => $type,
  1620. 'img_size' => $size,
  1621. 'img_name' => $file_name,
  1622. 'img_path' => $file_path,
  1623. ];
  1624. return message(MESSAGE_OK, true, $result);
  1625. }
  1626. }
  1627. if (!function_exists('upload_file')) {
  1628. /**
  1629. * 上传文件
  1630. * @param $request 网络请求
  1631. * @param string $form_name 文件表单名
  1632. * @return array 返回结果
  1633. * @author laravel开发员
  1634. * @date 2019/5/23
  1635. */
  1636. function upload_file($request, $form_name = 'file', $file_path = '', $userId = 0)
  1637. {
  1638. // 检测请求中是否包含上传的文件
  1639. if (!$request->hasFile($form_name)) {
  1640. return message("请上传文件");
  1641. }
  1642. // 文件对象
  1643. $file = $request->file($form_name);
  1644. // 判断图片上传是否错误
  1645. if (!$file->isValid()) {
  1646. // 文件上传失败
  1647. return message("上传文件验证失败");
  1648. }
  1649. // 文件原名
  1650. $original_name = $file->getClientOriginalName();
  1651. // 文件扩展名(文件后缀)
  1652. $ext = $file->getClientOriginalExtension();
  1653. // 临时文件的绝对路径
  1654. $real_path = $file->getRealPath();
  1655. // 文件类型
  1656. $type = $file->getClientMimeType();
  1657. // 文件大小
  1658. $size = $file->getSize();
  1659. // 文件大小校验(MAX=5M)
  1660. $file_max_size = 5 * 1024 * 1024;
  1661. if ($size > $file_max_size) {
  1662. return message("您上传的文件过大,最大值为" . $file_max_size / 1024 / 1024 . "MB");
  1663. }
  1664. // 允许上传的文件后缀
  1665. $ext = strtolower($ext);
  1666. $file_exts = array('xls', 'xlsx', 'csv');
  1667. if (!in_array($ext, $file_exts)) {
  1668. return message("文件格式不正确:" . $ext);
  1669. }
  1670. // 文件路径
  1671. $file_dir = ($file_path ? ATTACHMENT_PATH . '/images/' . $file_path : UPLOAD_TEMP_PATH) . "/" . date('Ymd');
  1672. // 检测文件路径是否存在,不存在则创建
  1673. if (!file_exists($file_dir)) {
  1674. mkdir($file_dir, 0777, true);
  1675. }
  1676. // 文件名称
  1677. $file_name = uniqid() . '.' . $ext;
  1678. // 重命名保存
  1679. $path = $file->move($file_dir, $file_name);
  1680. // 文件临时路径
  1681. $file_path = str_replace(IMG_PATH, '', $file_dir) . '/' . $file_name;
  1682. // 返回结果
  1683. $result = [
  1684. 'file_original_name' => $original_name,
  1685. 'file_ext' => $ext,
  1686. 'file_real_path' => $real_path,
  1687. 'file_type' => $type,
  1688. 'file_size' => $size,
  1689. 'file_name' => $file_name,
  1690. 'file_path' => $file_path,
  1691. ];
  1692. return message(MESSAGE_OK, true, $result);
  1693. }
  1694. }
  1695. if (!function_exists('upload_video')) {
  1696. /**
  1697. * 上传视频文件
  1698. * @param $request 网络请求
  1699. * @param string $form_name 文件表单名
  1700. * @return array 返回结果
  1701. * @author laravel开发员
  1702. * @date 2019/5/23
  1703. */
  1704. function upload_video($request, $form_name = 'file', $file_path = '', $userId = 0)
  1705. {
  1706. // 检测请求中是否包含上传的文件
  1707. if (!$request->hasFile($form_name)) {
  1708. return message("请上传文件");
  1709. }
  1710. // 文件对象
  1711. $file = $request->file($form_name);
  1712. // 判断图片上传是否错误
  1713. if (!$file->isValid()) {
  1714. // 文件上传失败
  1715. return message("上传文件验证失败");
  1716. }
  1717. // 文件原名
  1718. $original_name = $file->getClientOriginalName();
  1719. // 文件扩展名(文件后缀)
  1720. $ext = $file->getClientOriginalExtension();
  1721. // 临时文件的绝对路径
  1722. $real_path = $file->getRealPath();
  1723. // 文件类型
  1724. $type = $file->getClientMimeType();
  1725. // 文件大小
  1726. $size = $file->getSize();
  1727. // 文件大小校验(MAX=100M)
  1728. $file_max_size = 100 * 1024 * 1024;
  1729. if ($size > $file_max_size) {
  1730. return message("您上传的文件过大,最大值为" . $file_max_size / 1024 / 1024 . "MB");
  1731. }
  1732. // 允许上传的文件后缀
  1733. $ext = strtolower($ext);
  1734. $file_exts = array('mp4', 'wmv', 'mov');
  1735. if (!in_array($ext, $file_exts)) {
  1736. return message("视频文件格式不正确:" . $ext);
  1737. }
  1738. // 文件路径
  1739. $file_dir = ($file_path ? ATTACHMENT_PATH . '/images/' . $file_path : UPLOAD_TEMP_PATH) . "/" . date('Ymd');
  1740. // 检测文件路径是否存在,不存在则创建
  1741. if (!file_exists($file_dir)) {
  1742. mkdir($file_dir, 0777, true);
  1743. }
  1744. // 文件名称
  1745. $file_name = uniqid() . '.' . $ext;
  1746. // 重命名保存
  1747. $path = $file->move($file_dir, $file_name);
  1748. // 文件临时路径
  1749. $file_path = str_replace(IMG_PATH, '', $file_dir) . '/' . $file_name;
  1750. // 返回结果
  1751. $result = [
  1752. 'file_original_name' => $original_name,
  1753. 'file_ext' => $ext,
  1754. 'file_real_path' => $real_path,
  1755. 'file_type' => $type,
  1756. 'file_size' => $size,
  1757. 'file_name' => $file_name,
  1758. 'file_path' => $file_path,
  1759. ];
  1760. return message(MESSAGE_OK, true, $result);
  1761. }
  1762. }
  1763. if (!function_exists('upload_base64')) {
  1764. /**
  1765. * 上传文件
  1766. * @param $base64 base64内容
  1767. * @return array 返回结果
  1768. * @author laravel开发员
  1769. * @date 2019/5/23
  1770. */
  1771. function upload_base64($base64, $fileType='jpg', $file_path = '', $userId = 0)
  1772. {
  1773. // 检测请求中是否包含上传的文件
  1774. if (empty($base64)) {
  1775. return false;
  1776. }
  1777. // 文件路径
  1778. $file_dir = ($file_path ? ATTACHMENT_PATH . '/base64/' . $file_path : ATTACHMENT_PATH . '/base64/files') . "/" . date('Ymd');
  1779. // 检测文件路径是否存在,不存在则创建
  1780. if (!file_exists($file_dir)) {
  1781. mkdir($file_dir, 0777, true);
  1782. }
  1783. // 文件名称
  1784. $file_name = uniqid() . '.' . $fileType;
  1785. // 保存文件内容
  1786. $base64 = str_replace('data:image/jpeg;base64,','',$base64);
  1787. file_put_contents($file_dir.$file_name,base64_decode($base64));
  1788. if(!file_exists($file_dir.$file_name)){
  1789. return false;
  1790. }
  1791. // 文件临时路径
  1792. $file_path = str_replace(ATTACHMENT_PATH, '', $file_dir) . '/' . $file_name;
  1793. // 返回结果
  1794. $result = [
  1795. 'file_ext' => $fileType,
  1796. 'file_type' => $fileType,
  1797. 'file_name' => $file_name,
  1798. 'file_path' => $file_path,
  1799. ];
  1800. return $result;
  1801. }
  1802. }
  1803. if (!function_exists('upload_remote_image')) {
  1804. /**
  1805. * 上传保存网络文件
  1806. * @param $request 网络请求
  1807. * @return array 返回结果
  1808. * @author laravel开发员
  1809. * @date 2019/5/23
  1810. */
  1811. function upload_remote_image($url, $fileType='jpg', $file_path = '', $userId = 0)
  1812. {
  1813. // 检测请求中是否包含上传的文件
  1814. if (empty($url)) {
  1815. return false;
  1816. }
  1817. // 文件路径
  1818. $file_dir = ($file_path ? ATTACHMENT_PATH . '/remotes/' . $file_path : ATTACHMENT_PATH . '/remotes/files') . "/" . date('Ymd');
  1819. // 检测文件路径是否存在,不存在则创建
  1820. if (!file_exists($file_dir)) {
  1821. mkdir($file_dir, 0777, true);
  1822. }
  1823. // 文件名称
  1824. $file_name = uniqid() . '.' . $fileType;
  1825. // 保存文件内容
  1826. $content = file_get_contents($url);
  1827. if(!$content){
  1828. return false;
  1829. }
  1830. file_put_contents($file_dir.$file_name, $content);
  1831. if(!file_exists($file_dir.$file_name)){
  1832. return false;
  1833. }
  1834. // 文件临时路径
  1835. $file_path = str_replace(ATTACHMENT_PATH, '', $file_dir) . '/' . $file_name;
  1836. // 返回结果
  1837. $result = [
  1838. 'file_ext' => $fileType,
  1839. 'file_type' => $fileType,
  1840. 'file_name' => $file_name,
  1841. 'file_path' => $file_path,
  1842. ];
  1843. return $result;
  1844. }
  1845. }
  1846. if (!function_exists('widget')) {
  1847. /**
  1848. * 加载系统组件,传入的名字会以目录和类名区别
  1849. * 如Home.Common就代表Widget目录下的Home/Common.php这个widget。
  1850. * @param $widgetName 组件名称
  1851. * @return bool|mixed
  1852. * @author laravel开发员
  1853. * @date 2019/5/23
  1854. */
  1855. function widget($widgetName)
  1856. {
  1857. $widgetNameEx = explode('.', $widgetName);
  1858. if (!isset($widgetNameEx[1])) {
  1859. return false;
  1860. }
  1861. $widgetClass = 'App\\Widget\\' . $widgetNameEx[0] . '\\' . $widgetNameEx[1];
  1862. if (app()->bound($widgetName)) {
  1863. return app()->make($widgetName);
  1864. }
  1865. app()->singleton($widgetName, function () use ($widgetClass) {
  1866. return new $widgetClass();
  1867. });
  1868. return app()->make($widgetName);
  1869. }
  1870. }
  1871. if (!function_exists('get_tree')) {
  1872. function get_tree($datas, $pid = 0, $tempArr = [])
  1873. {
  1874. if (empty($datas)) {
  1875. return false;
  1876. }
  1877. foreach ($datas as $key => $v) {
  1878. $data = [];
  1879. $id = isset($v['id']) ? $v['id'] : 0;
  1880. $currentPid = isset($v['parent_id']) ? $v['parent_id'] : 0;
  1881. $nickname = isset($v['nickname']) ? $v['nickname'] : '';
  1882. $username = isset($v['username']) ? $v['username'] : '';
  1883. $mobile = isset($v['mobile']) ? $v['mobile'] : '';
  1884. $data['id'] = $v['id'];
  1885. $data['parent_id'] = $v['parent_id'];
  1886. $data['mobile'] = $mobile;
  1887. $data['label'] = $nickname . ($mobile ? "({$mobile})" : "");
  1888. if ($currentPid && $currentPid == $pid) {
  1889. $data['children'] = get_tree($datas, $v['id'], []);
  1890. $tempArr[] = $data;
  1891. } else if ($currentPid == 0 && $pid == 0) {
  1892. $data['children'] = get_tree($datas, $v['id'], []);
  1893. $tempArr[$id] = $data;
  1894. }
  1895. }
  1896. return array_values($tempArr);
  1897. }
  1898. }
  1899. if (!function_exists('getSign')) {
  1900. function getSign($data, $key = '')
  1901. {
  1902. if (!is_array($data) || empty($data)) {
  1903. return false;
  1904. }
  1905. $str = arrayToStr($data);
  1906. // 拼接密钥
  1907. $str .= '&key=' . ($key ? $key : md5(env('APP_SIGN_KEY', 'dysapp')));
  1908. $str = htmlspecialchars_decode($str);
  1909. // MD5 运算
  1910. // var_dump($str);
  1911. $sign = md5($str);
  1912. $sign .= substr($sign, 6, 4);
  1913. $sign = strtoupper($sign);
  1914. return $sign;
  1915. }
  1916. }
  1917. // 获取图片数组预览
  1918. if (!function_exists('get_images_preview')) {
  1919. /**
  1920. * @param $urls 图片数组
  1921. * @param string $keyName 图片地址键名,空则无键名
  1922. * @return false
  1923. */
  1924. function get_images_preview($urls, $keyName = 'url')
  1925. {
  1926. if (empty($urls)) {
  1927. return [];
  1928. }
  1929. foreach ($urls as &$item) {
  1930. if ($keyName) {
  1931. $item[$keyName] = get_image_url($item[$keyName]);
  1932. } else {
  1933. $item = get_image_url($item);
  1934. }
  1935. }
  1936. unset($item);
  1937. return $urls;
  1938. }
  1939. }
  1940. if (!function_exists('arrayToStr')) {
  1941. function arrayToStr($params)
  1942. {
  1943. $string = '';
  1944. if (!empty($params)) {
  1945. ksort($params, SORT_REGULAR);
  1946. $array = array();
  1947. foreach ($params as $key => &$value) {
  1948. if($key != 'sign'){
  1949. // 过滤无法匹配校验的参数类型
  1950. if ($value !== false && $value != 'undefined' && !is_array($value) && !preg_match("/^[0-9]{1,9}\.[0-9]{8,}$/", $value)) {
  1951. $array[] = $key . '=' . trim($value);
  1952. }
  1953. }
  1954. }
  1955. $string = implode("&", $array);
  1956. }
  1957. return $string;
  1958. }
  1959. }
  1960. if (!function_exists('arrayToUrl')) {
  1961. function arrayToUrl($params)
  1962. {
  1963. $string = '';
  1964. if (!empty($params)) {
  1965. ksort($params, SORT_REGULAR);
  1966. $array = array();
  1967. foreach ($params as $key => $value) {
  1968. if ($value != 'undefined') {
  1969. $array[] = $key . '=' . (is_array($value) ? json_encode($value,JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE) : $value);
  1970. }
  1971. }
  1972. $string = implode("&", $array);
  1973. }
  1974. return $string;
  1975. }
  1976. }
  1977. if (!function_exists('httpRequest')) {
  1978. /**
  1979. *
  1980. * 接口请求
  1981. * @param $url 接口地址
  1982. * @param $data
  1983. * @param $type
  1984. * @param int $timeout
  1985. * @return mixed
  1986. * @author wesmiler
  1987. */
  1988. function httpRequest($url, $data = '', $type = 'post', $cookie = '', $timeout = 60, $header=[])
  1989. {
  1990. try {
  1991. set_time_limit($timeout);
  1992. $data = $data && is_array($data) ? http_build_query($data) : $data;
  1993. $url = strtolower($type) == 'get' ? $url . (strpos($url, '?') === false ? '?' : '&') . $data : $url;
  1994. $ch = curl_init($url);
  1995. if (!empty($cookie)) {
  1996. curl_setopt($ch, CURLOPT_COOKIE, $cookie);
  1997. }
  1998. if ($type == 'post') {
  1999. curl_setopt($ch, CURLOPT_POST, 1);
  2000. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  2001. }
  2002. if ($header) {
  2003. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  2004. } else {
  2005. $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'];
  2006. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  2007. }
  2008. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //禁止 cURL 验证对等证书
  2009. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); //是否检测服务器的域名与证书上的是否一致
  2010. curl_setopt($ch, CURLOPT_HEADER, 0);
  2011. curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
  2012. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  2013. curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
  2014. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  2015. $ret = curl_exec($ch);
  2016. $ret = trim($ret);
  2017. curl_close($ch);
  2018. if (!preg_match("/^{/", $ret)) {
  2019. return ['code' => 'err', 'msg' => $ret];
  2020. }
  2021. $retArr = $ret ? json_decode($ret, true) : ['code' => 'err', 'msg' => 'unknow error'];
  2022. if (empty($retArr)) {
  2023. $ret = iconv('gb2312', 'utf-8', $ret);
  2024. $retArr = json_decode($ret, true);
  2025. }
  2026. } catch (\Exception $exception) {
  2027. $retArr = ['code' => 'error', 'msg' => 'request error'];
  2028. }
  2029. return $retArr;
  2030. }
  2031. }
  2032. if (!function_exists('getVersion')) {
  2033. /**
  2034. * 获取版本号
  2035. * @param $version
  2036. * @return string|string[]
  2037. */
  2038. function getVersion($version)
  2039. {
  2040. return $version ? intval(str_replace('.', '', $version)) : '';
  2041. }
  2042. }
  2043. if (!function_exists('formatDistance')) {
  2044. /**
  2045. * 格式化距离
  2046. * @param $distance
  2047. * @return string
  2048. */
  2049. function formatDistance($distance)
  2050. {
  2051. return ($distance > 1000 ? round($distance / 1000, 2) . 'km' : $distance . 'm');
  2052. }
  2053. }
  2054. if (!function_exists('moneyFormat')) {
  2055. /**
  2056. * 金额格式化
  2057. * @param $money
  2058. * @param int $decimal 小数位数,默认2
  2059. * @param int $round 是否四舍五入
  2060. * @return string
  2061. */
  2062. function moneyFormat($money, $decimal = 2, $round = false, $charset = 'utf-8')
  2063. {
  2064. if ($round) {
  2065. $money = number_format($money, $decimal);
  2066. } else {
  2067. $money = round($money, $decimal + 1);
  2068. $data = explode('.', $money);
  2069. $money = isset($data[0]) ? $data[0] : 0;
  2070. $float = isset($data[1]) ? $data[1] : '';
  2071. $len = $float ? mb_strlen($float, $charset) : 0;
  2072. $decimal = $decimal >= 0 ? intval($decimal) : 2;
  2073. $num1 = isset($data[1]) ? $data[1] : '';
  2074. $float = $len >= $decimal ? mb_substr($num1, 0, $decimal, $charset) : $float . str_repeat('0', $decimal - $len);
  2075. $money = $money . '.' . $float;
  2076. $money = number_format($money, $decimal, '.', '');
  2077. }
  2078. return $money;
  2079. }
  2080. }
  2081. if(!function_exists('getChatKey')){
  2082. /**
  2083. * 获取聊天窗口KEY
  2084. * @param $fromUserId
  2085. * @param $toUserId
  2086. * @return false|string
  2087. */
  2088. function getChatKey($fromUserId, $toUserId)
  2089. {
  2090. if(empty($fromUserId) || empty($toUserId)){
  2091. return false;
  2092. }
  2093. $ids = [$fromUserId, $toUserId];
  2094. asort($ids);
  2095. return implode('_', $ids);
  2096. }
  2097. }
  2098. if(!function_exists('getDistance')) {
  2099. /**
  2100. * 计算地图坐标距离
  2101. * @param $lat
  2102. * @param $lng
  2103. * @param $toLat
  2104. * @param $toLng
  2105. * @return float
  2106. */
  2107. function getDistance($lat, $lng, $toLat, $toLng)
  2108. {
  2109. return ROUND(6378.138 * 2 * ASIN(
  2110. SQRT(
  2111. POW(
  2112. SIN(
  2113. ($lat * PI() / 180 - $toLat * PI() / 180
  2114. ) / 2
  2115. ),
  2116. 2
  2117. ) + COS($lat * PI() / 180) * COS($toLat * PI() / 180) * POW(
  2118. SIN(
  2119. ($lng * PI() / 180 - $toLng * PI() / 180
  2120. ) / 2
  2121. ), 2))) * 1000);
  2122. }
  2123. }
  2124. if(!function_exists('getParents')){
  2125. /**
  2126. * 获取分销上级用户ID
  2127. * @param $parents
  2128. * @return array|false
  2129. */
  2130. function getParents($parents, $level=3){
  2131. $parents = $parents? explode(',', $parents) : [];
  2132. $parents = array_filter($parents);
  2133. if(empty($parents)){
  2134. return false;
  2135. }
  2136. $parents = array_slice($parents, -$level);
  2137. return array_reverse($parents);
  2138. }
  2139. }
  2140. if(!function_exists('getMillSecondTime')){
  2141. /**
  2142. * 获取毫秒时间戳
  2143. * @return float
  2144. */
  2145. function getMillSecondTime() {
  2146. list($msec, $sec) = explode(' ', microtime());
  2147. return (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
  2148. }
  2149. }