common.php 65 KB

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