common.php 68 KB

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