Base.php 80 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556
  1. <?php
  2. namespace App\Module;
  3. use App\Model\DBCache;
  4. use Cache;
  5. use DB;
  6. use Exception;
  7. use Illuminate\Contracts\Pagination\LengthAwarePaginator;
  8. use Redirect;
  9. use Request;
  10. use Storage;
  11. class Base
  12. {
  13. /**
  14. * 获取版本号
  15. * @return string
  16. */
  17. public static function getVersion()
  18. {
  19. return Cache::remember("Base::version", now()->addSeconds(10), function () {
  20. $file = base_path('package.json');
  21. if (file_exists($file)) {
  22. $packageArray = json_decode(file_get_contents($file), true);
  23. return isset($packageArray['version']) ? $packageArray['version'] : '1.0.0';
  24. }
  25. return '1.0.0';
  26. });
  27. }
  28. /**
  29. * 获取数组或对象值
  30. * @param $obj
  31. * @param string $key
  32. * @param string $default
  33. * @return string
  34. */
  35. public static function val($obj, $key = '', $default = '')
  36. {
  37. if (is_object($obj)) $obj = Base::object2array($obj);
  38. if (is_int($key)) {
  39. if (isset($obj[$key])){
  40. $obj = $obj[$key];
  41. }else{
  42. $obj = "";
  43. }
  44. }elseif (!empty($key)){
  45. $arr = explode(".", str_replace("|", ".", $key));
  46. foreach ($arr as $val){
  47. if (isset($obj[$val])){
  48. $obj = $obj[$val];
  49. }else{
  50. $obj = ""; break;
  51. }
  52. }
  53. }
  54. if ($default && empty($obj)) $obj = $default;
  55. return $obj;
  56. }
  57. /**
  58. * 获取当前uri
  59. * @return string
  60. */
  61. public static function getUrl()
  62. {
  63. return Request::getRequestUri();
  64. }
  65. /**
  66. * 跳转
  67. * @param null $url
  68. * @return mixed
  69. */
  70. public static function goUrl($url = null)
  71. {
  72. if (empty($url)) {
  73. $url = Base::getUrl();
  74. }
  75. return Redirect::to($url, 301);
  76. }
  77. /**
  78. * 默认显示
  79. * @param $str
  80. * @param $val
  81. * @param $default
  82. * @return mixed
  83. */
  84. public static function nullShow($str, $val, $default = '')
  85. {
  86. return $str ? ($default ? $default : $str) : $val;
  87. }
  88. /**
  89. * 补零
  90. * @param $str
  91. * @param int $length 长度
  92. * @param bool $before 是否补在前面
  93. * @return string
  94. */
  95. public static function zeroFill($str, $length = 0, $before = true)
  96. {
  97. if (strlen($str) >= $length) {
  98. return $str;
  99. }
  100. $_str = '';
  101. for ($i = 0; $i < $length; $i++) {
  102. $_str .= '0';
  103. }
  104. if ($before) {
  105. $_ret = substr($_str . $str, $length * -1);
  106. } else {
  107. $_ret = substr($str . $_str, 0, $length);
  108. }
  109. return $_ret;
  110. }
  111. /**
  112. * 新建目录
  113. * @param $path
  114. * @return mixed
  115. */
  116. public static function makeDir($path)
  117. {
  118. try{
  119. Storage::makeDirectory($path);
  120. } catch (Exception $e) {}
  121. if (!file_exists($path)){
  122. self::makeDir(dirname($path));
  123. @mkdir($path, 0777);
  124. @chmod($path, 0777);
  125. }
  126. return $path;
  127. }
  128. /**
  129. * 删除目录
  130. * @param $path
  131. */
  132. public static function deleteDir($path)
  133. {
  134. Storage::deleteDirectory($path);
  135. }
  136. /**
  137. * 删除目录及目录下所有的文件
  138. * @param $dirName
  139. * @param bool $undeleteDir 不删除目录(只删除文件)
  140. */
  141. public static function deleteDirAndFile($dirName, $undeleteDir = false)
  142. {
  143. if ($handle = opendir($dirName)) {
  144. while (false !== ($item = readdir($handle))) {
  145. if ($item != "." && $item != "..") {
  146. if (is_dir($dirName . "/" . $item)) {
  147. self::deleteDirAndFile($dirName . "/" . $item);
  148. } else {
  149. @unlink($dirName . "/" . $item);
  150. }
  151. }
  152. }
  153. closedir($handle);
  154. if ($undeleteDir === false) {
  155. rmdir($dirName);
  156. }
  157. }
  158. }
  159. /**
  160. * 去除html
  161. * @param $text
  162. * @param int $length
  163. * @return mixed|string
  164. */
  165. public static function getHtml($text, $length = 255)
  166. {
  167. $text = Base::cutStr(strip_tags($text), $length, 0, "...");
  168. return $text;
  169. }
  170. /**
  171. *
  172. * 截取字符串
  173. * @param string $string 字符串
  174. * @param int $length 截取长度
  175. * @param int $start 何处开始
  176. * @param string $dot 超出尾部添加
  177. * @param string $charset 默认编码
  178. * @return mixed|string
  179. */
  180. public static function cutStr($string, $length, $start = 0, $dot = '', $charset = 'utf-8')
  181. {
  182. if (strtolower($charset) == 'utf-8') {
  183. if (Base::getStrlen($string) <= $length) return $string;
  184. $strcut = str_replace(array('&amp;', '&quot;', '&lt;', '&gt;'), array('&', '"', '<', '>'), $string);
  185. $strcut = Base::utf8Substr($strcut, $length, $start);
  186. $strcut = str_replace(array('&', '"', '<', '>'), array('&amp;', '&quot;', '&lt;', '&gt;'), $strcut);
  187. return $strcut . $dot;
  188. } else {
  189. $length = $length * 2;
  190. if (strlen($string) <= $length) return $string;
  191. $string = str_replace(array('&amp;', '&quot;', '&lt;', '&gt;'), array('&', '"', '<', '>'), $string);
  192. $strcut = '';
  193. for ($i = 0; $i < $length; $i++) {
  194. $strcut .= ord($string[$i]) > 127 ? $string[$i] . $string[++$i] : $string[$i];
  195. }
  196. $strcut = str_replace(array('&', '"', '<', '>'), array('&amp;', '&quot;', '&lt;', '&gt;'), $strcut);
  197. }
  198. return $strcut . $dot;
  199. }
  200. /**
  201. * PHP获取字符串中英文混合长度
  202. * @param string $str 字符串
  203. * @param string $charset 编码
  204. * @return float 返回长度,1中文=1位,2英文=1位
  205. */
  206. public static function getStrlen($str, $charset = 'utf-8')
  207. {
  208. if (strtolower($charset) == 'utf-8') {
  209. $str = iconv('utf-8', 'GBK//IGNORE', $str);
  210. }
  211. $num = strlen($str);
  212. $cnNum = 0;
  213. for ($i = 0; $i < $num; $i++) {
  214. if (ord(substr($str, $i + 1, 1)) > 127) {
  215. $cnNum++;
  216. $i++;
  217. }
  218. }
  219. $enNum = $num - ($cnNum * 2);
  220. $number = ($enNum / 2) + $cnNum;
  221. return ceil($number);
  222. }
  223. /**
  224. * PHP截取UTF-8字符串,解决半字符问题。
  225. * @param string $str 源字符串
  226. * @param int $len 左边的子串的长度
  227. * @param int $start 何处开始
  228. * @return string 取出的字符串, 当$len小于等于0时, 会返回整个字符串
  229. */
  230. public static function utf8Substr($str, $len, $start = 0)
  231. {
  232. $len = $len * 2;
  233. $new_str = [];
  234. for ($i = 0; $i < $len; $i++) {
  235. $temp_str = substr($str, 0, 1);
  236. if (ord($temp_str) > 127) {
  237. $i++;
  238. if ($i < $len) {
  239. $new_str[] = substr($str, 0, 3);
  240. $str = substr($str, 3);
  241. }
  242. } else {
  243. $new_str[] = substr($str, 0, 1);
  244. $str = substr($str, 1);
  245. }
  246. }
  247. return join(array_slice($new_str, $start));
  248. }
  249. /**
  250. * 将字符串转换为数组
  251. * @param string $data 字符串
  252. * @param array $default 为空时返回的默认数组
  253. * @return array 返回数组格式,如果,data为空,则返回$default
  254. */
  255. public static function string2array($data, $default = [])
  256. {
  257. if (is_array($data)) {
  258. return $data ? $data : $default;
  259. }
  260. $data = trim($data);
  261. if ($data == '') return $default;
  262. if (strpos(strtolower($data), 'array') === 0 && strtolower($data) !== 'array') {
  263. @ini_set('display_errors', 'on');
  264. @eval("\$array = $data;");
  265. @ini_set('display_errors', 'off');
  266. } else {
  267. if (strpos($data, '{\\') === 0) {
  268. $data = stripslashes($data);
  269. }
  270. $array = json_decode($data, true);
  271. }
  272. return isset($array) && is_array($array) && $data ? $array : $default;
  273. }
  274. /**
  275. * 将数组转换为字符串
  276. * @param array $data 数组
  277. * @param int $isformdata 如果为0,则不使用new_stripslashes处理,可选参数,默认为1
  278. * @return string 返回字符串,如果,data为空,则返回空
  279. */
  280. public static function array2string($data, $isformdata = 1)
  281. {
  282. if ($data == '' || empty($data)) return '';
  283. if ($isformdata) $data = Base::newStripslashes($data);
  284. if (version_compare(PHP_VERSION, '5.3.0', '<')) {
  285. return Base::newAddslashes(json_encode($data));
  286. } else {
  287. return Base::newAddslashes(json_encode($data, JSON_FORCE_OBJECT));
  288. }
  289. }
  290. /**
  291. * 将数组转换为字符串 (格式化)
  292. * @param array $data 数组
  293. * @param int $isformdata 如果为0,则不使用new_stripslashes处理,可选参数,默认为1
  294. * @return string 返回字符串,如果,data为空,则返回空
  295. */
  296. public static function array2string_discard($data, $isformdata = 1)
  297. {
  298. if ($data == '' || empty($data)) return '';
  299. if ($isformdata) $data = Base::newStripslashes($data);
  300. return var_export($data, TRUE);
  301. }
  302. /**
  303. * json字符串转换成array
  304. * @param $string
  305. * @return array|mixed
  306. */
  307. public static function json2array($string)
  308. {
  309. if (is_array($string)) {
  310. return $string;
  311. }
  312. try {
  313. return json_decode($string, true);
  314. } catch (Exception $e) {
  315. return [];
  316. }
  317. }
  318. /**
  319. * array转换成功json字符串
  320. * @param $array
  321. * @return string
  322. */
  323. public static function array2json($array)
  324. {
  325. if (!is_array($array)) {
  326. return $array;
  327. }
  328. try {
  329. return json_encode($array);
  330. } catch (Exception $e) {
  331. return '';
  332. }
  333. }
  334. /**
  335. * 叠加数组或对象
  336. * @param object|array $array
  337. * @param array $over
  338. * @return object|array
  339. */
  340. public static function array_over(&$array, $over = [])
  341. {
  342. if (is_array($over)) {
  343. foreach ($over AS $key=>$val) {
  344. if (is_array($array)) {
  345. $array[$key] = $val;
  346. }
  347. if (is_object($array)) {
  348. $array->$key = $val;
  349. }
  350. }
  351. }
  352. return $array;
  353. }
  354. /**
  355. * 获取数组第一个值
  356. * @param $array
  357. * @return mixed
  358. */
  359. public static function arrayFirst($array)
  360. {
  361. $val = '';
  362. if (is_array($array)) {
  363. foreach ($array AS $item) {
  364. $val = $item;
  365. break;
  366. }
  367. }
  368. return $val;
  369. }
  370. /**
  371. * 获取数组最后一个值
  372. * @param $array
  373. * @return mixed
  374. */
  375. public static function arrayLast($array)
  376. {
  377. $val = '';
  378. if (is_array($array)) {
  379. foreach (array_reverse($array) AS $item) {
  380. $val = $item;
  381. break;
  382. }
  383. }
  384. return $val;
  385. }
  386. /**
  387. * 数组指定值作为数组键
  388. * @param $array
  389. * @param $valkey
  390. * @return array
  391. */
  392. public static function arrayValkey($array, $valkey)
  393. {
  394. self::coll2array($array);
  395. if (is_array($array)) {
  396. $items = [];
  397. foreach ($array AS $item) {
  398. $items[$item[$valkey]] = $item;
  399. }
  400. $array = $items;
  401. }
  402. return $array;
  403. }
  404. /**
  405. * 对象转数组
  406. * @param $array
  407. * @return array
  408. */
  409. public static function object2array(&$array)
  410. {
  411. if (is_object($array)) {
  412. $array = (array)$array;
  413. }
  414. if (is_array($array)) {
  415. foreach ($array as $key => $value) {
  416. $array[$key] = Base::object2array($value);
  417. }
  418. }
  419. return $array;
  420. }
  421. /**
  422. * 数据库返回对象转数组
  423. * @param $array
  424. * @return array
  425. */
  426. public static function coll2array(&$array)
  427. {
  428. if (is_object($array) && $array) {
  429. $obj = $array;
  430. $array = [];
  431. foreach ($obj AS $key=>$item) {
  432. $array[$key] = $item;
  433. }
  434. Base::object2array($array);
  435. }
  436. return $array;
  437. }
  438. /**
  439. * 数据库返回对象转数组
  440. * @param $array
  441. * @return array
  442. */
  443. public static function DBC2A($array)
  444. {
  445. return Base::coll2array($array);
  446. }
  447. /**
  448. * 数据库自更新数组
  449. * @param $array
  450. * @return array|\Illuminate\Database\Query\Expression
  451. */
  452. public static function DBUP($array) {
  453. if (is_array($array)) {
  454. foreach ($array AS $key => $val) {
  455. $array[$key] = DB::raw(Base::DBRAW($key . ($val < 0 ? '-' : '+') . abs($val)));
  456. }
  457. } else {
  458. $array = DB::raw(Base::DBRAW($array));
  459. }
  460. return $array;
  461. }
  462. /**
  463. * 数据库更新或插入
  464. * @param $builder
  465. * @param $where
  466. * @param $update
  467. * @param array $insert
  468. * @return bool
  469. */
  470. public static function DBUPIN($builder, $where, $update, $insert = [])
  471. {
  472. if (is_string($builder)) {
  473. $builder = DB::table($builder);
  474. }
  475. if (!$builder->where($where)->exists()) {
  476. return $builder->insert(array_merge($where, $insert));
  477. }
  478. return (bool)$builder->take(1)->update($update);
  479. }
  480. /**
  481. * 处理数据库原生符号
  482. * @param string $value
  483. * @return mixed
  484. */
  485. public static function DBRAW($value = '')
  486. {
  487. if (env('DB_CONNECTION') === 'pgsql') {
  488. $value = str_replace("`", "\"", $value);
  489. }
  490. return $value;
  491. }
  492. /**
  493. * array转xml
  494. * @param $data
  495. * @param string $root 根节点
  496. * @return string
  497. */
  498. public static function array2xml($data, $root = '<xml>'){
  499. $str = "";
  500. if ($root) $str .= $root;
  501. foreach ($data as $key => $val) {
  502. if (is_array($val)) {
  503. $child = self::array2xml($val, false);
  504. $str .= "<$key>$child</$key>";
  505. } else {
  506. $str .= "<$key><![CDATA[$val]]></$key>";
  507. }
  508. }
  509. if ($root) $str .= '</xml>';
  510. return $str;
  511. }
  512. /**
  513. * xml转json
  514. * @param string $source 传的是文件,还是xml的string的判断
  515. * @return string
  516. */
  517. public static function xml2json($source)
  518. {
  519. if (is_file($source)) {
  520. $xml_array = @simplexml_load_file($source);
  521. } else {
  522. $xml_array = @simplexml_load_string($source, NULL, LIBXML_NOCDATA);
  523. }
  524. return json_encode($xml_array);
  525. }
  526. /**
  527. * 返回经stripslashes处理过的字符串或数组
  528. * @param array|string $string 需要处理的字符串或数组
  529. * @return mixed
  530. */
  531. public static function newStripslashes($string)
  532. {
  533. if (is_numeric($string)) {
  534. return $string;
  535. }elseif (!is_array($string)) {
  536. return stripslashes($string);
  537. }
  538. foreach ($string as $key => $val) $string[$key] = Base::newStripslashes($val);
  539. return $string;
  540. }
  541. /**
  542. * 返回经addslashes处理过的字符串或数组
  543. * @param array|string $string 需要处理的字符串或数组
  544. * @return mixed
  545. */
  546. public static function newAddslashes($string)
  547. {
  548. if (is_numeric($string)) {
  549. return $string;
  550. }elseif (!is_array($string)) {
  551. return addslashes($string);
  552. }
  553. foreach ($string as $key => $val) $string[$key] = Base::newAddslashes($val);
  554. return $string;
  555. }
  556. /**
  557. * 返回经trim处理过的字符串或数组
  558. * @param $string
  559. * @return array|string
  560. */
  561. public static function newTrim($string)
  562. {
  563. if (!is_array($string)) return trim($string);
  564. foreach ($string as $key => $val) $string[$key] = Base::newTrim($val);
  565. return $string;
  566. }
  567. /**
  568. * 返回经intval处理过的字符串或数组
  569. * @param $string
  570. * @return array|string
  571. */
  572. public static function newIntval($string)
  573. {
  574. if (!is_array($string)) return intval($string);
  575. foreach ($string as $key => $val) $string[$key] = Base::newIntval($val);
  576. return $string;
  577. }
  578. /**
  579. * 重MD5加密
  580. * @param $text
  581. * @param string $pass
  582. * @return string
  583. */
  584. public static function md52($text, $pass = '')
  585. {
  586. $_text = md5($text) . $pass;
  587. return md5($_text);
  588. }
  589. /**
  590. * 随机字符串
  591. * @param int $length 随机字符长度
  592. * @param string $type
  593. * @return string 1数字、2大小写字母、21小写字母、22大写字母、默认全部;
  594. */
  595. public static function generatePassword($length = 8, $type = '')
  596. {
  597. // 密码字符集,可任意添加你需要的字符
  598. switch ($type) {
  599. case '1':
  600. $chars = '0123456789';
  601. break;
  602. case '2':
  603. $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  604. break;
  605. case '21':
  606. $chars = 'abcdefghijklmnopqrstuvwxyz';
  607. break;
  608. case '22':
  609. $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  610. break;
  611. default:
  612. $chars = $type ? $type : 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
  613. break;
  614. }
  615. $passwordstr = '';
  616. $max = strlen($chars) - 1;
  617. for ($i = 0; $i < $length; $i++) {
  618. $passwordstr .= $chars[mt_rand(0, $max)];
  619. }
  620. return $passwordstr;
  621. }
  622. /**
  623. * 同 generate_password 默认获取纯数字
  624. * @param $length
  625. * @param string $chars
  626. * @return string
  627. */
  628. public static function strRandom($length, $chars = '0123456789')
  629. {
  630. return Base::generatePassword($length, $chars);
  631. }
  632. /**
  633. * 判断两个地址域名是否相同
  634. * @param string $var1
  635. * @param string $var2
  636. * @return bool
  637. */
  638. public static function hostContrast($var1, $var2)
  639. {
  640. $arr1 = parse_url($var1);
  641. $arr2 = parse_url($var2);
  642. //
  643. $host1 = $var1;
  644. if (isset($arr1['host'])) {
  645. $host1 = $arr1['host'];
  646. }
  647. //
  648. $host2 = $var2;
  649. if (isset($arr2['host'])) {
  650. $host2 = $arr2['host'];
  651. }
  652. return $host1== $host2;
  653. }
  654. /**
  655. * 获取url域名
  656. * @param string $var
  657. * @return mixed
  658. */
  659. public static function getHost($var = '')
  660. {
  661. if (empty($var)) {
  662. $var = url("/");
  663. }
  664. $arr = parse_url($var);
  665. return $arr['host'];
  666. }
  667. /**
  668. * 相对路径补全
  669. * @param string|array $str
  670. * @return string
  671. */
  672. public static function fillUrl($str = '')
  673. {
  674. if (is_array($str)) {
  675. foreach ($str AS $key=>$item) {
  676. $str[$key] = Base::fillUrl($item);
  677. }
  678. return $str;
  679. }
  680. if (empty($str)) {
  681. return $str;
  682. }
  683. if (substr($str, 0, 2) == "//" ||
  684. substr($str, 0, 7) == "http://" ||
  685. substr($str, 0, 8) == "https://" ||
  686. substr($str, 0, 6) == "ftp://" ||
  687. substr($str, 0, 1) == "/" ||
  688. substr(str_replace(' ', '', $str), 0, 11) == "data:image/"
  689. ) {
  690. return $str;
  691. } else {
  692. return url($str);
  693. }
  694. }
  695. /**
  696. * 反 fillUrl
  697. * @param string $str
  698. * @return array|string
  699. */
  700. public static function unFillUrl($str = '')
  701. {
  702. if (is_array($str)) {
  703. foreach ($str AS $key=>$item) {
  704. $str[$key] = Base::unFillUrl($item);
  705. }
  706. return $str;
  707. }
  708. return Base::leftDelete($str, url('') . '/');
  709. }
  710. /**
  711. * 格式化内容图片地址
  712. * @param $content
  713. * @return mixed
  714. */
  715. public static function formatContentImg($content)
  716. {
  717. $pattern = '/<img(.*?)src=("|\')(.*?)\2/is';
  718. if (preg_match($pattern, $content)) {
  719. preg_match_all($pattern, $content, $matchs);
  720. foreach ($matchs[3] AS $index => $value) {
  721. if (!(substr($value, 0, 7) == "http://" ||
  722. substr($value, 0, 8) == "https://" ||
  723. substr($value, 0, 6) == "ftp://" ||
  724. substr(str_replace(' ', '', $value), 0, 11) == "data:image/"
  725. )) {
  726. if (substr($value, 0, 2) == "//") {
  727. $value = "http:" . $value;
  728. }elseif (substr($value, 0, 1) == "/") {
  729. $value = substr($value, 1);
  730. }
  731. $newValue = "<img" . $matchs[1][$index] . "src=" . $matchs[2][$index] . self::fillUrl($value) . $matchs[2][$index];
  732. $content = str_replace($matchs[0][$index], $newValue, $content);
  733. }
  734. }
  735. }
  736. return $content;
  737. }
  738. /**
  739. * 打散字符串,只留为数字的项
  740. * @param $delimiter
  741. * @param $string
  742. * @return array
  743. */
  744. public static function explodeInt($delimiter, $string)
  745. {
  746. $array = is_array($string) ? $string : explode($delimiter, $string);
  747. foreach ($array AS $k => $v) {
  748. if (!is_numeric($v)) {
  749. unset($array[$k]);
  750. }
  751. }
  752. return array_values($array);
  753. }
  754. /**
  755. * 检测日期格式
  756. * @param string $str 需要检测的字符串
  757. * @return bool
  758. */
  759. public static function isDate($str)
  760. {
  761. $strArr = explode('-', $str);
  762. if (empty($strArr) || count($strArr) != 3) {
  763. return false;
  764. } else {
  765. list($year, $month, $day) = $strArr;
  766. if (checkdate($month, $day, $year)) {
  767. return true;
  768. } else {
  769. return false;
  770. }
  771. }
  772. }
  773. /**
  774. * 检测时间格式
  775. * @param string $str 需要检测的字符串
  776. * @return bool
  777. */
  778. public static function isTime($str)
  779. {
  780. $strArr = explode(':', $str);
  781. if (empty($strArr) || count($strArr) != 2) {
  782. return false;
  783. } else {
  784. list($hour, $minute) = $strArr;
  785. if (intval($hour) > 23 || intval($minute) > 59) {
  786. return false;
  787. } else {
  788. return true;
  789. }
  790. }
  791. }
  792. /**
  793. * 检测手机号码格式
  794. * @param string $str 需要检测的字符串
  795. * @return bool
  796. */
  797. public static function isMobile($str)
  798. {
  799. if (preg_match("/^1([3456789])\d{9}$/", $str)) {
  800. return true;
  801. }else{
  802. return false;
  803. }
  804. }
  805. /**
  806. * 检测邮箱格式
  807. * @param string $str 需要检测的字符串
  808. * @return int
  809. */
  810. public static function isMail($str)
  811. {
  812. $RegExp = '/^[a-z0-9][a-z\.0-9-_]+@[a-z0-9_-]+(?:\.[a-z]{0,3}\.[a-z]{0,2}|\.[a-z]{0,3}|\.[a-z]{0,2})$/i';
  813. return preg_match($RegExp, $str);
  814. }
  815. /**
  816. * 正则判断是否纯数字
  817. * @param $str
  818. * @return bool
  819. */
  820. public static function isNumber($str)
  821. {
  822. if (preg_match("/^\d*$/", $str)) {
  823. return true;
  824. } else {
  825. return false;
  826. }
  827. }
  828. /**
  829. * 判断身份证是否正确
  830. * @param $id
  831. * @return bool
  832. */
  833. public static function isIdcard($id)
  834. {
  835. $id = strtoupper($id);
  836. $regx = "/(^\d{15}$)|(^\d{17}([0-9]|X)$)/";
  837. $arr_split = array();
  838. if(!preg_match($regx, $id)) {
  839. return FALSE;
  840. }
  841. if(15==strlen($id)) {
  842. //检查15位
  843. $regx = "/^(\d{6})+(\d{2})+(\d{2})+(\d{2})+(\d{3})$/";
  844. @preg_match($regx, $id, $arr_split);
  845. //检查生日日期是否正确
  846. $dtm_birth = "19".$arr_split[2] . '/' . $arr_split[3]. '/' .$arr_split[4];
  847. if(!strtotime($dtm_birth)) {
  848. return FALSE;
  849. } else {
  850. return TRUE;
  851. }
  852. } else {
  853. //检查18位
  854. $regx = "/^(\d{6})+(\d{4})+(\d{2})+(\d{2})+(\d{3})([0-9]|X)$/";
  855. @preg_match($regx, $id, $arr_split);
  856. $dtm_birth = $arr_split[2] . '/' . $arr_split[3]. '/' .$arr_split[4];
  857. //检查生日日期是否正确
  858. if(!strtotime($dtm_birth)) {
  859. return FALSE;
  860. } else {
  861. //检验18位身份证的校验码是否正确。
  862. //校验位按照ISO 7064:1983.MOD 11-2的规定生成,X可以认为是数字10。
  863. $arr_int = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
  864. $arr_ch = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
  865. $sign = 0;
  866. for ( $i = 0; $i < 17; $i++ ) {
  867. $b = (int) $id[$i];
  868. $w = $arr_int[$i];
  869. $sign += $b * $w;
  870. }
  871. $n = $sign % 11;
  872. $val_num = $arr_ch[$n];
  873. if ($val_num != substr($id,17, 1)) {
  874. return FALSE;
  875. } else {
  876. return TRUE;
  877. }
  878. }
  879. }
  880. }
  881. /**
  882. * 阵列数组
  883. * @param $keys
  884. * @param $src
  885. * @param bool $default
  886. * @return array
  887. */
  888. public static function arrayElements($keys, $src, $default = FALSE)
  889. {
  890. $return = [];
  891. if (!is_array($keys)) {
  892. $keys = array($keys);
  893. }
  894. foreach ($keys as $key) {
  895. if (isset($src[$key])) {
  896. $return[$key] = $src[$key];
  897. } else {
  898. $return[$key] = $default;
  899. }
  900. }
  901. return $return;
  902. }
  903. /**
  904. * 判断字符串存在(包含)
  905. * @param string $string
  906. * @param string $find
  907. * @return bool
  908. */
  909. public static function strExists($string, $find)
  910. {
  911. if (!is_string($string) || !is_string($find)) {
  912. return false;
  913. }
  914. return !(strpos($string, $find) === FALSE);
  915. }
  916. /**
  917. * 判断字符串开头包含
  918. * @param string $string //原字符串
  919. * @param string $find //判断字符串
  920. * @param bool|false $lower //是否不区分大小写
  921. * @return int
  922. */
  923. public static function leftExists($string, $find, $lower = false)
  924. {
  925. if (!is_string($string) || !is_string($find)) {
  926. return false;
  927. }
  928. if ($lower) {
  929. $string = strtolower($string);
  930. $find = strtolower($find);
  931. }
  932. return (substr($string, 0, strlen($find)) == $find);
  933. }
  934. /**
  935. * 判断字符串结尾包含
  936. * @param string $string //原字符串
  937. * @param string $find //判断字符串
  938. * @param bool|false $lower //是否不区分大小写
  939. * @return int
  940. */
  941. public static function rightExists($string, $find, $lower = false)
  942. {
  943. if (!is_string($string) || !is_string($find)) {
  944. return false;
  945. }
  946. if ($lower) {
  947. $string = strtolower($string);
  948. $find = strtolower($find);
  949. }
  950. return (substr($string, strlen($find) * -1) == $find);
  951. }
  952. /**
  953. * 删除开头指定字符串
  954. * @param $string
  955. * @param $find
  956. * @param bool $lower
  957. * @return string
  958. */
  959. public static function leftDelete($string, $find, $lower = false)
  960. {
  961. if (Base::leftExists($string, $find, $lower)) {
  962. $string = substr($string, strlen($find));
  963. }
  964. return $string ? $string : '';
  965. }
  966. /**
  967. * 删除结尾指定字符串
  968. * @param $string
  969. * @param $find
  970. * @param bool $lower
  971. * @return string
  972. */
  973. public static function rightDelete($string, $find, $lower = false)
  974. {
  975. if (Base::rightExists($string, $find, $lower)) {
  976. $string = substr($string, 0, strlen($find) * -1);
  977. }
  978. return $string;
  979. }
  980. /**
  981. * 截取指定字符串
  982. * @param $str
  983. * @param string $ta
  984. * @param string $tb
  985. * @return string
  986. */
  987. public static function getMiddle($str, $ta = '', $tb = ''){
  988. if ($ta && strpos($str, $ta) !== false){
  989. $str = substr($str, strpos($str, $ta) + strlen($ta));
  990. }
  991. if ($tb && strpos($str, $tb) !== false){
  992. $str = substr($str, 0, strpos($str, $tb));
  993. }
  994. return $str;
  995. }
  996. /**
  997. * 自定义替换次数
  998. * @param $search
  999. * @param $replace
  1000. * @param $subject
  1001. * @param int $limit
  1002. * @return string|string[]|null
  1003. */
  1004. public static function strReplaceLimit($search, $replace, $subject, $limit = -1)
  1005. {
  1006. if (is_array($search)) {
  1007. foreach ($search as $k => $v) {
  1008. $search[$k] = '`' . preg_quote($search[$k], '`') . '`';
  1009. }
  1010. } else {
  1011. $search = '`' . preg_quote($search, '`') . '`';
  1012. }
  1013. return preg_replace($search, $replace, $subject, $limit);
  1014. }
  1015. /**
  1016. * 获取或设置
  1017. * @param $setname //配置名称
  1018. * @param bool $array //保存内容
  1019. * @param bool $isCache //读取缓存
  1020. * @return array
  1021. */
  1022. public static function setting($setname, $array = false, $isCache = false)
  1023. {
  1024. global $_A;
  1025. if (empty($setname)) {
  1026. return [];
  1027. }
  1028. if ($array === false && isset($_A["__static_setting_" . $setname])) {
  1029. return $_A["__static_setting_" . $setname];
  1030. }
  1031. $setting = [];
  1032. $setrow = DBCache::table('setting')->where('title', $setname)->cache($isCache)->first();
  1033. if (!empty($setrow)) {
  1034. $setting = Base::string2array($setrow['setting']);
  1035. } else {
  1036. DB::table('setting')->insert(['title' => $setname]);
  1037. }
  1038. if ($array !== false) {
  1039. $setting = $array;
  1040. DB::table('setting')->where('title', $setname)->update(['setting' => (is_array($array) ? Base::array2string($array) : $array)]);
  1041. DBCache::table('setting')->where('title', $setname)->removeCache()->first();
  1042. }
  1043. $_A["__static_setting_" . $setname] = $setting;
  1044. return $setting;
  1045. }
  1046. /**
  1047. * 获取设置值
  1048. * @param $setname
  1049. * @param $keyname
  1050. * @param $defaultVal
  1051. * @return mixed
  1052. */
  1053. public static function settingFind($setname, $keyname, $defaultVal = '')
  1054. {
  1055. $array = Base::setting($setname);
  1056. return isset($array[$keyname]) ? $array[$keyname] : $defaultVal;
  1057. }
  1058. /**
  1059. * 秒 (转) 年、天、时、分、秒
  1060. * @param $time
  1061. * @return array|bool
  1062. */
  1063. public static function sec2time($time)
  1064. {
  1065. if (is_numeric($time)) {
  1066. $value = array(
  1067. "years" => 0, "days" => 0, "hours" => 0,
  1068. "minutes" => 0, "seconds" => 0,
  1069. );
  1070. /*
  1071. if($time >= 31536000){
  1072. $value["years"] = floor($time/31536000);
  1073. $time = ($time%31536000);
  1074. }
  1075. */
  1076. if ($time >= 86400) {
  1077. $value["days"] = floor($time / 86400);
  1078. $time = ($time % 86400);
  1079. }
  1080. if ($time >= 3600) {
  1081. $value["hours"] = floor($time / 3600);
  1082. $time = ($time % 3600);
  1083. }
  1084. if ($time >= 60) {
  1085. $value["minutes"] = floor($time / 60);
  1086. $time = ($time % 60);
  1087. }
  1088. $value["seconds"] = floor($time);
  1089. return (array)$value;
  1090. } else {
  1091. return (bool)FALSE;
  1092. }
  1093. }
  1094. /**
  1095. * 年、天、时、分、秒 (转) 秒
  1096. * @param $value
  1097. * @return int
  1098. */
  1099. public static function time2sec($value)
  1100. {
  1101. $time = intval($value["seconds"]);
  1102. $time += intval($value["minutes"] * 60);
  1103. $time += intval($value["hours"] * 3600);
  1104. $time += intval($value["days"] * 86400);
  1105. $time += intval($value["years"] * 31536000);
  1106. return $time;
  1107. }
  1108. /**
  1109. * 阿拉伯数字转化为中文
  1110. * @param $num
  1111. * @return string
  1112. */
  1113. public static function chinaNum($num)
  1114. {
  1115. $china = array('零', '一', '二', '三', '四', '五', '六', '七', '八', '九');
  1116. $arr = str_split($num);
  1117. $txt = '';
  1118. for ($i = 0; $i < count($arr); $i++) {
  1119. $txt .= $china[$arr[$i]];
  1120. }
  1121. return $txt;
  1122. }
  1123. /**
  1124. * 阿拉伯数字转化为中文(用于星期,七改成日)
  1125. * @param $num
  1126. * @return string
  1127. */
  1128. public static function chinaNumZ($num)
  1129. {
  1130. return str_replace("七", "日", Base::chinaNum($num));
  1131. }
  1132. /**
  1133. * 获取(时间戳转)今天是星期几,只返回(几)
  1134. * @param string|number $unixTime
  1135. * @return string
  1136. */
  1137. public static function getTimeWeek($unixTime = '')
  1138. {
  1139. $unixTime = is_numeric($unixTime) ? $unixTime : Base::time();
  1140. $weekarray = ['日', '一', '二', '三', '四', '五', '六'];
  1141. return $weekarray[date('w', $unixTime)];
  1142. }
  1143. /**
  1144. * 获取(时间戳转)现在时间段:深夜、凌晨、早晨、上午.....
  1145. * @param string|number $unixTime
  1146. * @return string
  1147. */
  1148. public static function getTimeDayeSegment($unixTime = '')
  1149. {
  1150. $unixTime = is_numeric($unixTime) ? $unixTime : Base::time();
  1151. $H = date('H', $unixTime);
  1152. if ($H >= 19) {
  1153. return '晚上';
  1154. } elseif ($H >= 18) {
  1155. return '傍晚';
  1156. } elseif ($H >= 13) {
  1157. return '下午';
  1158. } elseif ($H >= 12) {
  1159. return '中午';
  1160. } elseif ($H >= 8) {
  1161. return '上午';
  1162. } elseif ($H >= 5) {
  1163. return '早晨';
  1164. } elseif ($H >= 1) {
  1165. return '凌晨';
  1166. } elseif ($H >= 0) {
  1167. return '深夜';
  1168. } else {
  1169. return '';
  1170. }
  1171. }
  1172. /**
  1173. * 国际化(替换国际语言)
  1174. * @param $val
  1175. * @return mixed
  1176. */
  1177. public static function Lang($val)
  1178. {
  1179. $repArray = [];
  1180. if (is_array($val)) {
  1181. if (self::strExists($val[0], '%') && count($val) > 1) {
  1182. $repArray = array_slice($val, 1);
  1183. }
  1184. $val = $val[0];
  1185. }
  1186. $data = self::langData();
  1187. if (isset($data[$val]) && $data[$val] !== null) {
  1188. $val = $data[$val];
  1189. }
  1190. if ($repArray) {
  1191. foreach ($repArray AS $item) {
  1192. $val = self::strReplaceLimit('%', $item, $val, 1);
  1193. }
  1194. }
  1195. return $val;
  1196. }
  1197. /**
  1198. * 加载语言数据
  1199. * @param bool $refresh
  1200. * @return array|mixed
  1201. */
  1202. public static function langData($refresh = false)
  1203. {
  1204. global $_A;
  1205. if (!isset($_A["__static_langdata"]) || $refresh === true) {
  1206. $_A["__static_langdata"] = [];
  1207. $language = trim(Request::header('language'));
  1208. $langpath = resource_path('lang/' . $language . '/general.php');
  1209. if (file_exists($langpath)) {
  1210. $data = include $langpath;
  1211. if (is_array($data)) {
  1212. $_A["__static_langdata"] = $data;
  1213. }
  1214. }
  1215. }
  1216. return $_A["__static_langdata"];
  1217. }
  1218. /**
  1219. * JSON返回
  1220. * @param $param
  1221. * @return string
  1222. */
  1223. public static function jsonEcho($param)
  1224. {
  1225. global $_GPC;
  1226. //
  1227. $json = json_encode($param);
  1228. $callback = $_GPC['callback'];
  1229. if ($callback) {
  1230. return $callback . '(' . $json . ')';
  1231. } else {
  1232. return $json;
  1233. }
  1234. }
  1235. /**
  1236. * 数组返回 正确
  1237. * @param $msg
  1238. * @param array $data
  1239. * @param int $ret
  1240. * @return array
  1241. */
  1242. public static function retSuccess($msg, $data = [], $ret = 1)
  1243. {
  1244. return array(
  1245. 'ret' => $ret,
  1246. 'msg' => self::Lang($msg),
  1247. 'data' => $data
  1248. );
  1249. }
  1250. /**
  1251. * 数组返回 错误
  1252. * @param $msg
  1253. * @param array $data
  1254. * @param int $ret
  1255. * @return array
  1256. */
  1257. public static function retError($msg, $data = [], $ret = 0)
  1258. {
  1259. return array(
  1260. 'ret' => $ret,
  1261. 'msg' => self::Lang($msg),
  1262. 'data' => $data
  1263. );
  1264. }
  1265. /**
  1266. * Ajax 错误返回
  1267. * @param $msg
  1268. * @param array $data
  1269. * @param int $ret
  1270. * @param int $abortCode
  1271. * @return array|void
  1272. */
  1273. public static function ajaxError($msg, $data = [], $ret = 0, $abortCode = 404)
  1274. {
  1275. if (Request::input('__Access-Control-Allow-Origin') || Request::header('Content-Type') === 'application/json') {
  1276. return Base::retError($msg, $data, $ret);
  1277. }else{
  1278. return abort($abortCode, $msg);
  1279. }
  1280. }
  1281. /**
  1282. * JSON返回 正确
  1283. * @param $msg
  1284. * @param array $data
  1285. * @param int $ret
  1286. * @return string
  1287. */
  1288. public static function jsonSuccess($msg, $data = [], $ret = 1)
  1289. {
  1290. return Base::jsonEcho(Base::retSuccess($msg, $data, $ret));
  1291. }
  1292. /**
  1293. * JSON返回 错误
  1294. * @param $msg
  1295. * @param array $data
  1296. * @param int $ret
  1297. * @return string
  1298. */
  1299. public static function jsonError($msg, $data = [], $ret = 0)
  1300. {
  1301. return Base::jsonEcho(Base::retError($msg, $data, $ret));
  1302. }
  1303. /**
  1304. * 是否错误
  1305. * @param $param
  1306. * @return bool
  1307. */
  1308. public static function isError($param)
  1309. {
  1310. return intval($param['ret']) <= 0;
  1311. }
  1312. /**
  1313. * 异常模板
  1314. * @param $msg
  1315. * @param bool $catch
  1316. * @return bool
  1317. */
  1318. public static function Exce($msg, $catch = false)
  1319. {
  1320. if ($catch === false) {
  1321. return "{{ExceMsg:".$msg.":ExceMsg}}";
  1322. }else{
  1323. if (Base::strExists($msg . "", "{{ExceMsg:") && Base::strExists($msg . "", ":ExceMsg}}")) {
  1324. $msg = Base::getMiddle($msg, "{{ExceMsg:", ":ExceMsg}}");
  1325. $catch = $msg?$msg:$catch;
  1326. }
  1327. return $catch;
  1328. }
  1329. }
  1330. /**
  1331. * 获取数组的第几个值
  1332. * @param $arr
  1333. * @param int $i
  1334. * @return array
  1335. */
  1336. public static function getArray($arr, $i = 1)
  1337. {
  1338. $array = [];
  1339. $j = 1;
  1340. foreach ($arr AS $item) {
  1341. $array[] = $item;
  1342. if ($i >= $j) {
  1343. break;
  1344. }
  1345. $j++;
  1346. }
  1347. return $array;
  1348. }
  1349. /**
  1350. * 小时转天/小时
  1351. * @param $hour
  1352. * @return string
  1353. */
  1354. public static function forumHourDay($hour)
  1355. {
  1356. $hour = intval($hour);
  1357. if ($hour > 24) {
  1358. $day = floor($hour / 24);
  1359. $hour-= $day * 24;
  1360. return $day.'天'.$hour.'小时';
  1361. }
  1362. return $hour.'小时';
  1363. }
  1364. /**
  1365. * 时间格式化
  1366. * @param $date
  1367. * @return false|string
  1368. */
  1369. public static function forumDate($date)
  1370. {
  1371. $dur = time() - $date;
  1372. if ($date > strtotime(date("Y-m-d"))) {
  1373. //今天
  1374. if ($dur < 60) {
  1375. return max($dur, 1) . '秒前';
  1376. } elseif ($dur < 3600) {
  1377. return floor($dur / 60) . '分钟前';
  1378. } elseif ($dur < 86400) {
  1379. return floor($dur / 3600) . '小时前';
  1380. } else {
  1381. return date("H:i", $date);
  1382. }
  1383. } elseif ($date > strtotime(date("Y-m-d", strtotime("-1 day")))) {
  1384. //昨天
  1385. return '昨天';
  1386. } elseif ($date > strtotime(date("Y-m-d", strtotime("-2 day")))) {
  1387. //前天
  1388. return '前天';
  1389. } elseif ($dur > 86400) {
  1390. //x天前
  1391. return floor($dur / 86400) . '天前';
  1392. }
  1393. return date("Y-m-d", $date);
  1394. }
  1395. /**
  1396. * 获取时间戳今天的第一秒时间戳
  1397. * @param $time
  1398. * @return false|int
  1399. */
  1400. public static function dayTimeF($time)
  1401. {
  1402. return strtotime(date("Y-m-d 00:00:00", self::isNumber($time) ? $time : strtotime($time)));
  1403. }
  1404. /**
  1405. * 获取时间戳今天的最后一秒时间戳
  1406. * @param $time
  1407. * @return false|int
  1408. */
  1409. public static function dayTimeE($time)
  1410. {
  1411. return strtotime(date("Y-m-d 23:59:59", self::isNumber($time) ? $time : strtotime($time)));
  1412. }
  1413. /**
  1414. * 用户名、邮箱、手机账号、银行卡号中间字符串以*隐藏
  1415. * @param $str
  1416. * @return mixed|string
  1417. */
  1418. public static function cardFormat($str)
  1419. {
  1420. if (strpos($str, '@')) {
  1421. $email_array = explode("@", $str);
  1422. $prevfix = (strlen($email_array[0]) < 4) ? "" : substr($str, 0, 3); //邮箱前缀
  1423. $count = 0;
  1424. $str = preg_replace('/([\d\w+_-]{0,100})@/', '***@', $str, -1, $count);
  1425. return $prevfix . $str;
  1426. }
  1427. if (Base::isMobile($str)) {
  1428. return substr($str, 0, 3) . "****" . substr($str, -4);
  1429. }
  1430. $pattern = '/([\d]{4})([\d]{4})([\d]{4})([\d]{4})([\d]{0,})?/i';
  1431. if (preg_match($pattern, $str)) {
  1432. return preg_replace($pattern, '$1 **** **** **** $5', $str);
  1433. }
  1434. $pattern = '/([\d]{4})([\d]{4})([\d]{4})([\d]{0,})?/i';
  1435. if (preg_match($pattern, $str)) {
  1436. return preg_replace($pattern, '$1 **** **** $4', $str);
  1437. }
  1438. $pattern = '/([\d]{4})([\d]{4})([\d]{0,})?/i';
  1439. if (preg_match($pattern, $str)) {
  1440. return preg_replace($pattern, '$1 **** $3', $str);
  1441. }
  1442. return substr($str, 0, 3) . "***" . substr($str, -1);
  1443. }
  1444. /**
  1445. * 数字每4位加一空格
  1446. * @param $str
  1447. * @param string $interval
  1448. * @return string
  1449. */
  1450. public static function fourFormat($str, $interval = " ")
  1451. {
  1452. if (!is_numeric($str)) return $str;
  1453. //
  1454. $text = '';
  1455. for ($i = 0; $i < strlen($str); $i++) {
  1456. $text .= $str[$i];
  1457. if ($i % 4 == 3) {
  1458. $text .= $interval;
  1459. }
  1460. }
  1461. return $text;
  1462. }
  1463. /**
  1464. * 保留两位小数点
  1465. * @param $str
  1466. * @param bool $float
  1467. * @return float
  1468. */
  1469. public static function twoFloat($str, $float = false)
  1470. {
  1471. $str = sprintf("%.2f", floatval($str));
  1472. if ($float === true) {
  1473. $str = floatval($str);
  1474. }
  1475. return $str;
  1476. }
  1477. /**
  1478. * 获取时间戳
  1479. * @param bool $refresh
  1480. * @return int
  1481. */
  1482. public static function time($refresh = false)
  1483. {
  1484. global $_A;
  1485. if (!isset($_A["__static_time"]) || $refresh === true) {
  1486. $_A["__static_time"] = time();
  1487. }
  1488. return $_A["__static_time"];
  1489. }
  1490. /**
  1491. * 获取毫秒时间戳
  1492. * @return float
  1493. */
  1494. public static function msecTime()
  1495. {
  1496. list($msec, $sec) = explode(' ', microtime());
  1497. $time = explode(".", $sec . ($msec * 1000));
  1498. return $time[0];
  1499. }
  1500. /**
  1501. * 时间差(不够1个小时算一个小时)
  1502. * @param int $s 开始时间戳
  1503. * @param int $e 结束时间戳
  1504. * @return string
  1505. */
  1506. public static function timeDiff($s, $e)
  1507. {
  1508. $d = $e - $s;
  1509. if ($d > 86400) {
  1510. $day = floor($d / 86400);
  1511. $hour = ceil(($d - ($day * 86400)) / 3600);
  1512. if ($hour > 0) {
  1513. return $day . '天' . $hour . '小时';
  1514. } else {
  1515. return $day . '天';
  1516. }
  1517. } elseif ($d > 3600) {
  1518. return ceil($d / 3600) . '小时';
  1519. } elseif ($d > 60) {
  1520. return ceil($d / 60) . '分钟';
  1521. } elseif ($d > 1) {
  1522. return '1分钟内';
  1523. } else {
  1524. return '0秒';
  1525. }
  1526. }
  1527. /**
  1528. * 获取IP地址
  1529. * @return string
  1530. */
  1531. public static function getIp()
  1532. {
  1533. global $_A;
  1534. if (!isset($_A["__static_ip"])) {
  1535. if (getenv('HTTP_CLIENT_IP') and strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) {
  1536. $onlineip = getenv('HTTP_CLIENT_IP');
  1537. } elseif (getenv('HTTP_X_FORWARDED_FOR') and strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) {
  1538. $onlineip = getenv('HTTP_X_FORWARDED_FOR');
  1539. } elseif (getenv('REMOTE_ADDR') and strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) {
  1540. $onlineip = getenv('REMOTE_ADDR');
  1541. } elseif (isset($_SERVER['REMOTE_ADDR']) and $_SERVER['REMOTE_ADDR'] and strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) {
  1542. $onlineip = $_SERVER['REMOTE_ADDR'];
  1543. } elseif (Request::header('X-Real-IP')) {
  1544. $onlineip = Request::header('X-Real-IP');
  1545. } else {
  1546. $onlineip = '0,0,0,0';
  1547. }
  1548. preg_match("/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/", $onlineip, $match);
  1549. $_A["__static_ip"] = $match[0] ? $match[0] : 'unknown';
  1550. }
  1551. return $_A["__static_ip"];
  1552. }
  1553. /**
  1554. * 获取IP地址
  1555. * @param string $ip
  1556. * @return array|mixed
  1557. */
  1558. public static function getIpInfo($ip = '')
  1559. {
  1560. if (empty($ip)) {
  1561. $ip = self::getIp();
  1562. }
  1563. $cacheKey = "getIpInfo::" . md5($ip);
  1564. $result = Cache::rememberForever($cacheKey, function() use ($ip) {
  1565. return Ihttp::ihttp_request("http://ip.taobao.com/service/getIpInfo.php?accessKey=alibaba-inc&ip=" . $ip, [], [], 8);
  1566. });
  1567. if (Base::isError($result)) {
  1568. return $result;
  1569. }
  1570. $data = json_decode($result['data'], true);
  1571. if (!is_array($data) || intval($data['code']) != 0) {
  1572. Cache::forget($cacheKey);
  1573. return Base::retError("error ip: -1");
  1574. }
  1575. $data = $data['data'];
  1576. if (!is_array($data) || !isset($data['country'])) {
  1577. return Base::retError("error ip: -2");
  1578. }
  1579. $data['text'] = $data['country'];
  1580. $data['textSmall'] = $data['country'];
  1581. if ($data['region'] && $data['region'] != $data['country'] && $data['region'] != "XX") {
  1582. $data['text'].= " " . $data['region'];
  1583. $data['textSmall'] = $data['region'];
  1584. }
  1585. if ($data['city'] && $data['city'] != $data['region'] && $data['city'] != "XX") {
  1586. $data['text'].= " " . $data['city'];
  1587. $data['textSmall'].= " " . $data['city'];
  1588. }
  1589. if ($data['county'] && $data['county'] != $data['city'] && $data['county'] != "XX") {
  1590. $data['text'].= " " . $data['county'];
  1591. $data['textSmall'].= " " . $data['county'];
  1592. }
  1593. return Base::retSuccess("success", $data);
  1594. }
  1595. /**
  1596. * 是否是中国IP:-1错误、1是、0否
  1597. * @param string $ip
  1598. * @return int
  1599. */
  1600. public static function isCnIp($ip = '')
  1601. {
  1602. if (empty($ip)) {
  1603. $ip = self::getIp();
  1604. }
  1605. $cacheKey = "isCnIp::" . md5($ip);
  1606. //
  1607. $result = Cache::remember($cacheKey, now()->addMinutes(10), function () use ($ip) {
  1608. $file = dirname(__FILE__) . '/ip/all_cn.txt';
  1609. if (!file_exists($file)) {
  1610. return -1;
  1611. }
  1612. $in = false;
  1613. $myFile = fopen($file, "r");
  1614. $i = 0;
  1615. while (!feof($myFile)) {
  1616. $i++;
  1617. $cidr = trim(fgets($myFile));
  1618. if (Base::ipInRange($ip, $cidr)) {
  1619. $in = true;
  1620. break;
  1621. }
  1622. }
  1623. fclose($myFile);
  1624. return $in ? 1 : 0;
  1625. });
  1626. if ($result === -1) {
  1627. Cache::forget($cacheKey);
  1628. }
  1629. //
  1630. return intval($result);
  1631. }
  1632. /**
  1633. * 验证IP地址范围
  1634. * $range 支持多种写法
  1635. * - Wildcard: 1.2.3.*
  1636. * - CIRD:1.2.3/24 或者 1.2.3.4/255.255.255.0
  1637. * - Start-End: 1.2.3.0-1.2.3.255
  1638. * @param $ip
  1639. * @param $range
  1640. * @return bool
  1641. */
  1642. public static function ipInRange($ip, $range)
  1643. {
  1644. if (substr_count($ip, '.') == 3 && $ip == $range) {
  1645. return true;
  1646. }
  1647. if (strpos($range, '/') !== false) {
  1648. list($range, $netmask) = explode('/', $range, 2);
  1649. if (strpos($netmask, '.') !== false) {
  1650. $netmask = str_replace('*', '0', $netmask);
  1651. $netmask_dec = ip2long($netmask);
  1652. return ((ip2long($ip) & $netmask_dec) == (ip2long($range) & $netmask_dec));
  1653. } else {
  1654. $x = explode('.', $range);
  1655. while (count($x) < 4) {
  1656. $x[] = '0';
  1657. }
  1658. list($a, $b, $c, $d) = $x;
  1659. $range = sprintf("%u.%u.%u.%u", empty($a) ? '0' : $a, empty($b) ? '0' : $b, empty($c) ? '0' : $c, empty($d) ? '0' : $d);
  1660. $range_dec = ip2long($range);
  1661. $ip_dec = ip2long($ip);
  1662. $wildcard_dec = pow(2, (32 - $netmask)) - 1;
  1663. $netmask_dec = ~$wildcard_dec;
  1664. return (($ip_dec & $netmask_dec) == ($range_dec & $netmask_dec));
  1665. }
  1666. } else {
  1667. if (strpos($range, '*') !== false) {
  1668. $lower = str_replace('*', '0', $range);
  1669. $upper = str_replace('*', '255', $range);
  1670. $range = "$lower-$upper";
  1671. }
  1672. if (strpos($range, '-') !== false) {
  1673. list($lower, $upper) = explode('-', $range, 2);
  1674. $lower_dec = (float)sprintf("%u", ip2long($lower));
  1675. $upper_dec = (float)sprintf("%u", ip2long($upper));
  1676. $ip_dec = (float)sprintf("%u", ip2long($ip));
  1677. return (($ip_dec >= $lower_dec) && ($ip_dec <= $upper_dec));
  1678. }
  1679. return false;
  1680. }
  1681. }
  1682. /**
  1683. * php://input 字符串解析到变量并获取指定值
  1684. * @param $key
  1685. * @return array
  1686. */
  1687. public static function getContentsParse($key)
  1688. {
  1689. parse_str(Request::getContent(), $input);
  1690. if ($key) {
  1691. $input = isset($input[$key]) ? $input[$key] : array();
  1692. }
  1693. return is_array($input) ? $input : array($input);
  1694. }
  1695. /**
  1696. * php://input 字符串解析到变量并获取指定值
  1697. * @param $key
  1698. * @param null $default
  1699. * @return mixed|null
  1700. */
  1701. public static function getContentValue($key, $default = null)
  1702. {
  1703. global $_A;
  1704. if (!isset($_A["__static_input_content"])) {
  1705. parse_str(Request::getContent(), $input);
  1706. $_A["__static_input_content"] = $input;
  1707. }
  1708. return isset($_A["__static_input_content"][$key]) ? $_A["__static_input_content"][$key] : $default;
  1709. }
  1710. public static function getPostValue($key, $default = null)
  1711. {
  1712. return self::getContentValue($key, $default);
  1713. }
  1714. /**
  1715. * 多维 array_values
  1716. * @param $array
  1717. * @param string $keyName
  1718. * @param string $valName
  1719. * @return array
  1720. */
  1721. public static function array_values_recursive($array, $keyName = 'key', $valName = 'item') {
  1722. if (is_array($array) && count($array) > 0) {
  1723. $temp = [];
  1724. foreach ($array as $key => $value) {
  1725. $continue = false;
  1726. if (is_array($value) && count($value) > 0) {
  1727. $continue = true;
  1728. foreach ($value AS $item) {
  1729. if (!is_array($item)) {
  1730. $continue = false;
  1731. break;
  1732. }
  1733. }
  1734. }
  1735. $temp[] = [
  1736. $keyName => $key,
  1737. $valName => $continue ? self::array_values_recursive($value, $keyName, $valName) : $value,
  1738. ];
  1739. }
  1740. return $temp;
  1741. }
  1742. return $array;
  1743. }
  1744. /**
  1745. * 获取tonken
  1746. * @return string
  1747. */
  1748. public static function getToken()
  1749. {
  1750. global $_A;
  1751. if (!isset($_A["__static_token"])) {
  1752. $_A["__static_token"] = Base::nullShow(Request::header('token'), Request::input('token'));
  1753. }
  1754. return $_A["__static_token"];
  1755. }
  1756. /**
  1757. * 设置tonken
  1758. * @param $token
  1759. */
  1760. public static function setToken($token)
  1761. {
  1762. global $_A;
  1763. $_A["__static_token"] = $token;
  1764. }
  1765. /**
  1766. * 是否微信
  1767. * @return bool
  1768. */
  1769. public static function isWechat()
  1770. {
  1771. return strpos(Request::server('HTTP_USER_AGENT'), 'MicroMessenger') !== false;
  1772. }
  1773. /**
  1774. * 获取浏览器类型
  1775. * @return string
  1776. */
  1777. public static function browser()
  1778. {
  1779. $user_agent = Request::server('HTTP_USER_AGENT');
  1780. if (strpos($user_agent, 'AlipayClient') !== false) {
  1781. return 'alipay';
  1782. }elseif (strpos($user_agent, 'MicroMessenger') !== false) {
  1783. return 'weixin';
  1784. }else{
  1785. return 'none';
  1786. }
  1787. }
  1788. /**
  1789. * 返回根据距离sql排序语句
  1790. * @param $lat
  1791. * @param $lng
  1792. * @param string $latName
  1793. * @param string $lngName
  1794. * @return string
  1795. */
  1796. public static function acos($lat , $lng, $latName = 'lat', $lngName = 'lng') {
  1797. $lat = floatval($lat);
  1798. $lng = floatval($lng);
  1799. return 'ACOS(
  1800. SIN(('.$lat.' * 3.1415) / 180) * SIN(('.$latName.' * 3.1415) / 180) + COS(('.$lat.' * 3.1415) / 180) * COS(('.$latName.' * 3.1415) / 180) * COS(
  1801. ('.$lng.' * 3.1415) / 180 - ('.$lngName.' * 3.1415) / 180
  1802. )
  1803. ) * 6380';
  1804. }
  1805. /**
  1806. * 获取分页详细信息
  1807. * @param LengthAwarePaginator $lists
  1808. * @param bool $getTotal
  1809. * @return array
  1810. */
  1811. public static function getPageInfo(LengthAwarePaginator $lists, $getTotal = true)
  1812. {
  1813. return [
  1814. "currentPage" => $lists->currentPage(),
  1815. "firstItem" => $lists->firstItem(),
  1816. "hasMorePages" => $lists->hasMorePages(),
  1817. "lastItem" => $lists->lastItem(),
  1818. "lastPage" => $lists->lastPage(),
  1819. "nextPageUrl" => $lists->nextPageUrl(),
  1820. "previousPageUrl" => $lists->previousPageUrl(),
  1821. "perPage" => $lists->perPage(),
  1822. "total" => $getTotal === true ? $lists->total() : -1,
  1823. ];
  1824. }
  1825. /**
  1826. * 获取分页数据
  1827. * @param $lists
  1828. * @param bool $getTotal
  1829. * @return array
  1830. */
  1831. public static function getPageList($lists, $getTotal = true)
  1832. {
  1833. $data = Base::getPageInfo($lists, $getTotal);
  1834. $data['lists'] = Base::coll2array($lists);
  1835. return $data;
  1836. }
  1837. /**
  1838. * 获取每页数量
  1839. * @param $max
  1840. * @param $default
  1841. * @param string $inputName
  1842. * @return mixed
  1843. */
  1844. public static function getPaginate($max, $default, $inputName = 'pagesize') {
  1845. return Min(Max(Base::nullShow(Request::input($inputName), $default), 1), $max);
  1846. }
  1847. /**
  1848. * image64图片保存
  1849. * @param array $param [ image64=带前缀的base64, path=>文件路径, fileName=>文件名称, scale=>[压缩原图宽,高, 压缩方式] ]
  1850. * @return array [name=>文件名, size=>文件大小(单位KB),file=>绝对地址, path=>相对地址, url=>全路径地址, ext=>文件后缀名]
  1851. */
  1852. public static function image64save($param)
  1853. {
  1854. $imgBase64 = $param['image64'];
  1855. if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $imgBase64, $res)) {
  1856. $extension = $res[2];
  1857. if (!in_array($extension, ['png', 'jpg', 'jpeg', 'gif'])) {
  1858. return Base::retError('图片格式错误!');
  1859. }
  1860. $scaleName = "";
  1861. if ($param['fileName']) {
  1862. $fileName = $param['fileName'];
  1863. }else{
  1864. if ($param['scale'] && is_array($param['scale'])) {
  1865. list($width, $height) = $param['scale'];
  1866. if ($width > 0 || $height > 0) {
  1867. $scaleName = "_{WIDTH}x{HEIGHT}";
  1868. if (isset($param['scale'][2])) {
  1869. $scaleName.= $param['scale'][2];
  1870. }
  1871. }
  1872. }
  1873. $fileName = 'paste_' . md5($imgBase64) . '.' . $extension;
  1874. $scaleName = md5_file($imgBase64) . $scaleName . '.' . $extension;
  1875. }
  1876. $fileDir = $param['path'];
  1877. $filePath = public_path($fileDir);
  1878. Base::makeDir($filePath);
  1879. if (file_put_contents($filePath . $fileName, base64_decode(str_replace($res[1], '', $imgBase64)))) {
  1880. $fileSize = filesize($filePath . $fileName);
  1881. $array = [
  1882. "name" => $fileName, //原文件名
  1883. "size" => Base::twoFloat($fileSize / 1024, true), //大小KB
  1884. "file" => $filePath . $fileName, //目录的完整路径 "D:\www....KzZ.jpg"
  1885. "path" => $fileDir . $fileName, //相对路径 "uploads/pic....KzZ.jpg"
  1886. "url" => Base::fillUrl($fileDir . $fileName), //完整的URL "https://.....hhsKzZ.jpg"
  1887. "thumb" => '', //缩略图(预览图) "https://.....hhsKzZ.jpg_thumb.jpg"
  1888. "width" => -1, //图片宽度
  1889. "height" => -1, //图片高度
  1890. "ext" => $extension, //文件后缀名
  1891. ];
  1892. if (in_array($extension, ['png', 'jpg', 'jpeg', 'gif'])) {
  1893. //图片尺寸
  1894. $paramet = getimagesize($array['file']);
  1895. $array['width'] = $paramet[0];
  1896. $array['height'] = $paramet[1];
  1897. //原图压缩
  1898. if ($param['scale'] && is_array($param['scale'])) {
  1899. list($width, $height) = $param['scale'];
  1900. if (($width > 0 && $array['width'] > $width) || ($height > 0 && $array['height'] > $height)) {
  1901. $cut = ($width > 0 && $height > 0) ? 1 : -1;
  1902. $cut = $param['scale'][2] ?? $cut;
  1903. //图片压缩
  1904. $tmpFile = $array['file'] . '_tmp.jpg';
  1905. if (Base::imgThumb($array['file'], $tmpFile, $width, $height, $cut)) {
  1906. $tmpSize = filesize($tmpFile);
  1907. if ($tmpSize > $fileSize) {
  1908. @unlink($tmpFile);
  1909. }else{
  1910. @unlink($array['file']);
  1911. rename($tmpFile, $array['file']);
  1912. }
  1913. }
  1914. //图片尺寸
  1915. $paramet = getimagesize($array['file']);
  1916. $array['width'] = $paramet[0];
  1917. $array['height'] = $paramet[1];
  1918. //重命名
  1919. if ($scaleName) {
  1920. $scaleName = str_replace(['{WIDTH}', '{HEIGHT}'], [$array['width'], $array['height']], $scaleName);
  1921. if (rename($array['file'], Base::rightDelete($array['file'], $fileName) . $scaleName)) {
  1922. $array['file'] = Base::rightDelete($array['file'], $fileName) . $scaleName;
  1923. $array['path'] = Base::rightDelete($array['path'], $fileName) . $scaleName;
  1924. $array['url'] = Base::rightDelete($array['url'], $fileName) . $scaleName;
  1925. }
  1926. }
  1927. }
  1928. }
  1929. //生成缩略图
  1930. $array['thumb'] = $array['path'];
  1931. if (Base::imgThumb($array['file'], $array['file']."_thumb.jpg", 180, 0)) {
  1932. $array['thumb'].= "_thumb.jpg";
  1933. }
  1934. $array['thumb'] = Base::fillUrl($array['thumb']);
  1935. }
  1936. return Base::retSuccess('success', $array);
  1937. }
  1938. }
  1939. return Base::retError('图片保存失败!');
  1940. }
  1941. /**
  1942. * 上传文件
  1943. * @param array $param [ type=[文件类型], file=>Request::file, path=>文件路径, fileName=>文件名称, scale=>[压缩原图宽,高, 压缩方式], size=>限制大小KB, autoThumb=>false不要自动生成缩略图 ]
  1944. * @return array [name=>原文件名, size=>文件大小(单位KB),file=>绝对地址, path=>相对地址, url=>全路径地址, ext=>文件后缀名]
  1945. */
  1946. public static function upload($param)
  1947. {
  1948. $file = $param['file'];
  1949. if (empty($file)) {
  1950. return Base::retError("您没有选择要上传的文件!");
  1951. }
  1952. if($file->isValid()){
  1953. Base::makeDir(public_path($param['path']));
  1954. //
  1955. switch ($param['type']) {
  1956. case 'png':
  1957. $type = ['png'];
  1958. break;
  1959. case 'png+jpg':
  1960. $type = ['jpg', 'jpeg', 'png'];
  1961. break;
  1962. case 'image':
  1963. $type = ['jpg', 'jpeg', 'gif', 'png'];
  1964. break;
  1965. case 'video':
  1966. $type = ['rm', 'rmvb', 'wmv', 'avi', 'mpg', 'mpeg', 'mp4'];
  1967. break;
  1968. case 'audio':
  1969. $type = ['mp3', 'wma', 'wav', 'amr'];
  1970. break;
  1971. case 'excel':
  1972. $type = ['xls', 'xlsx'];
  1973. break;
  1974. case 'app':
  1975. $type = ['apk'];
  1976. break;
  1977. case 'zip':
  1978. $type = ['zip'];
  1979. break;
  1980. case 'file':
  1981. $type = ['jpg', 'jpeg', 'png', 'gif', 'doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'txt', 'esp', 'pdf', 'rar', 'zip', 'gz', 'ai', 'avi', 'bmp', 'cdr', 'eps', 'mov', 'mp3', 'mp4', 'pr', 'psd', 'svg', 'tif'];
  1982. break;
  1983. default:
  1984. return Base::retError('错误的类型参数');
  1985. }
  1986. $extension = strtolower($file->getClientOriginalExtension());
  1987. if ($type && is_array($type) && !in_array($extension, $type)) {
  1988. return Base::retError(['文件格式错误,限制类型:%!', implode(",", $type)]);
  1989. }
  1990. try {
  1991. $fileSize = $file->getSize();
  1992. if ($param['size'] > 0 && $fileSize > $param['size'] * 1024) {
  1993. return Base::retError(['文件大小超限,最大限制:%KB!', $param['size']]);
  1994. }
  1995. } catch (Exception $e) {
  1996. $fileSize = 0;
  1997. }
  1998. $scaleName = "";
  1999. if ($param['fileName']) {
  2000. $fileName = $param['fileName'];
  2001. }else{
  2002. if ($param['scale'] && is_array($param['scale'])) {
  2003. list($width, $height) = $param['scale'];
  2004. if ($width > 0 || $height > 0) {
  2005. $scaleName = "_{WIDTH}x{HEIGHT}";
  2006. if (isset($param['scale'][2])) {
  2007. $scaleName.= $param['scale'][2];
  2008. }
  2009. }
  2010. }
  2011. $fileName = md5_file($file) . '.' . $extension;
  2012. $scaleName = md5_file($file) . $scaleName . '.' . $extension;
  2013. }
  2014. //
  2015. $file->move(public_path($param['path']), $fileName);
  2016. //
  2017. $array = [
  2018. "name" => $file->getClientOriginalName(), //原文件名
  2019. "size" => Base::twoFloat($fileSize / 1024, true), //大小KB
  2020. "file" => public_path($param['path'].$fileName), //目录的完整路径 "D:\www....KzZ.jpg"
  2021. "path" => $param['path'].$fileName, //相对路径 "uploads/pic....KzZ.jpg"
  2022. "url" => Base::fillUrl($param['path'].$fileName), //完整的URL "https://.....hhsKzZ.jpg"
  2023. "thumb" => '', //缩略图(预览图) "https://.....hhsKzZ.jpg_thumb.jpg"
  2024. "width" => -1, //图片宽度
  2025. "height" => -1, //图片高度
  2026. "ext" => $extension, //文件后缀名
  2027. ];
  2028. if (!is_file($array['file'])) {
  2029. return Base::retError('上传失败!');
  2030. }
  2031. //iOS照片颠倒处理
  2032. if (in_array($extension, ['jpg', 'jpeg']) && function_exists('exif_read_data')) {
  2033. $data = imagecreatefromstring(file_get_contents($array['file']));
  2034. $exif = @exif_read_data($array['file']);
  2035. if (!empty($exif['Orientation'])) {
  2036. switch ($exif['Orientation']) {
  2037. case 8:
  2038. $data = imagerotate($data, 90, 0);
  2039. break;
  2040. case 3:
  2041. $data = imagerotate($data, 180, 0);
  2042. break;
  2043. case 6:
  2044. $data = imagerotate($data, -90, 0);
  2045. break;
  2046. default:
  2047. $data = null;
  2048. break;
  2049. }
  2050. if ($data !== null) {
  2051. imagejpeg($data, $array['file']);
  2052. }
  2053. }
  2054. }
  2055. //
  2056. if (in_array($extension, ['jpg', 'jpeg', 'gif', 'png'])) {
  2057. //图片尺寸
  2058. $paramet = getimagesize($array['file']);
  2059. $array['width'] = $paramet[0];
  2060. $array['height'] = $paramet[1];
  2061. //原图压缩
  2062. if ($param['scale'] && is_array($param['scale'])) {
  2063. list($width, $height) = $param['scale'];
  2064. if (($width > 0 && $array['width'] > $width) || ($height > 0 && $array['height'] > $height)) {
  2065. $cut = ($width > 0 && $height > 0) ? 1 : -1;
  2066. $cut = $param['scale'][2] ?? $cut;
  2067. //图片压缩
  2068. $tmpFile = $array['file'] . '_tmp.jpg';
  2069. if (Base::imgThumb($array['file'], $tmpFile, $width, $height, $cut)) {
  2070. $tmpSize = filesize($tmpFile);
  2071. if ($tmpSize > $fileSize) {
  2072. @unlink($tmpFile);
  2073. }else{
  2074. @unlink($array['file']);
  2075. rename($tmpFile, $array['file']);
  2076. }
  2077. }
  2078. //图片尺寸
  2079. $paramet = getimagesize($array['file']);
  2080. $array['width'] = $paramet[0];
  2081. $array['height'] = $paramet[1];
  2082. //重命名
  2083. if ($scaleName) {
  2084. $scaleName = str_replace(['{WIDTH}', '{HEIGHT}'], [$array['width'], $array['height']], $scaleName);
  2085. if (rename($array['file'], Base::rightDelete($array['file'], $fileName) . $scaleName)) {
  2086. $array['file'] = Base::rightDelete($array['file'], $fileName) . $scaleName;
  2087. $array['path'] = Base::rightDelete($array['path'], $fileName) . $scaleName;
  2088. $array['url'] = Base::rightDelete($array['url'], $fileName) . $scaleName;
  2089. }
  2090. }
  2091. }
  2092. }
  2093. //生成缩略图
  2094. $array['thumb'] = $array['path'];
  2095. if ($param['autoThumb'] === "false") $param['autoThumb'] = false;
  2096. if ($param['autoThumb'] !== false) {
  2097. if (Base::imgThumb($array['file'], $array['file']."_thumb.jpg", 180, 0)) {
  2098. $array['thumb'].= "_thumb.jpg";
  2099. }
  2100. }
  2101. $array['thumb'] = Base::fillUrl($array['thumb']);
  2102. }
  2103. //
  2104. return Base::retSuccess('success', $array);
  2105. }else{
  2106. return Base::retError($file->getErrorMessage());
  2107. }
  2108. }
  2109. /**
  2110. * 生成缩略图
  2111. * @param string $src_img 源图绝对完整地址{带文件名及后缀名}
  2112. * @param string $dst_img 目标图绝对完整地址{带文件名及后缀名}
  2113. * @param int $width 缩略图宽{0:此时目标高度不能为0,目标宽度为源图宽*(目标高度/源图高)}
  2114. * @param int $height 缩略图高{0:此时目标宽度不能为0,目标高度为源图高*(目标宽度/源图宽)}
  2115. * @param int $cut 是否裁切{宽,高必须非0}:1是、0否、-1或'auto'保持等比
  2116. * @param int $proportion 缩放{0:不缩放, 0<this<1:缩放到相应比例(此时宽高限制和裁切均失效)}
  2117. * @return bool
  2118. */
  2119. public static function imgThumb($src_img, $dst_img, $width = 75, $height = 75, $cut = 0, $proportion = 0)
  2120. {
  2121. if (!is_file($src_img)) {
  2122. return false;
  2123. }
  2124. if (empty($dst_img)) {
  2125. $dst_img = $src_img;
  2126. }
  2127. $st = pathinfo($src_img, PATHINFO_EXTENSION);
  2128. if (!in_array(strtolower($st), array('jpg', 'jpeg', 'png', 'gif', 'bmp'))) {
  2129. return false;
  2130. }
  2131. $ot = pathinfo($dst_img, PATHINFO_EXTENSION);
  2132. $otfunc = 'image' . ($ot == 'jpg' ? 'jpeg' : $ot);
  2133. $srcinfo = getimagesize($src_img);
  2134. $src_w = $srcinfo[0];
  2135. $src_h = $srcinfo[1];
  2136. $type = strtolower(substr(image_type_to_extension($srcinfo[2]), 1));
  2137. if (empty($type)) {
  2138. return false;
  2139. }
  2140. $createfun = 'imagecreatefrom' . ($type == 'jpg' ? 'jpeg' : $type);
  2141. $dst_h = $height;
  2142. $dst_w = $width;
  2143. $x = $y = 0;
  2144. /**
  2145. * 缩略图不超过源图尺寸(前提是宽或高只有一个)
  2146. */
  2147. if (($width > $src_w && $height > $src_h) || ($height > $src_h && $width == 0) || ($width > $src_w && $height == 0)) {
  2148. $proportion = 1;
  2149. }
  2150. if ($width > $src_w) {
  2151. $dst_w = $width = $src_w;
  2152. }
  2153. if ($height > $src_h) {
  2154. $dst_h = $height = $src_h;
  2155. }
  2156. if (!$width && !$height && !$proportion) {
  2157. return false;
  2158. }
  2159. if (!$proportion) {
  2160. if ($cut == 'auto' || $cut == -1) {
  2161. if ($dst_w && $dst_h) {
  2162. $wB = $dst_w / $src_w;
  2163. $hB = $dst_h / $src_h;
  2164. if ($wB > $hB) {
  2165. $dst_w = 0;
  2166. }else{
  2167. $dst_h = 0;
  2168. }
  2169. }
  2170. $cut = 0;
  2171. }
  2172. if ($cut == 0) {
  2173. if ($dst_w && $dst_h) {
  2174. if ($dst_w / $src_w > $dst_h / $src_h) {
  2175. $dst_w = $src_w * ($dst_h / $src_h);
  2176. $x = 0 - ($dst_w - $width) / 2;
  2177. } else {
  2178. $dst_h = $src_h * ($dst_w / $src_w);
  2179. $y = 0 - ($dst_h - $height) / 2;
  2180. }
  2181. } else if ($dst_w xor $dst_h) {
  2182. if ($dst_w && !$dst_h) //有宽无高
  2183. {
  2184. $propor = $dst_w / $src_w;
  2185. $height = $dst_h = $src_h * $propor;
  2186. } else if (!$dst_w && $dst_h) //有高无宽
  2187. {
  2188. $propor = $dst_h / $src_h;
  2189. $width = $dst_w = $src_w * $propor;
  2190. }
  2191. }
  2192. } else {
  2193. if (!$dst_h) //裁剪时无高
  2194. {
  2195. $height = $dst_h = $dst_w;
  2196. }
  2197. if (!$dst_w) //裁剪时无宽
  2198. {
  2199. $width = $dst_w = $dst_h;
  2200. }
  2201. $propor = min(max($dst_w / $src_w, $dst_h / $src_h), 1);
  2202. $dst_w = (int)round($src_w * $propor);
  2203. $dst_h = (int)round($src_h * $propor);
  2204. $x = ($width - $dst_w) / 2;
  2205. $y = ($height - $dst_h) / 2;
  2206. }
  2207. } else {
  2208. $proportion = min($proportion, 1);
  2209. $height = $dst_h = $src_h * $proportion;
  2210. $width = $dst_w = $src_w * $proportion;
  2211. }
  2212. if (!function_exists($createfun)) {
  2213. return false;
  2214. }
  2215. $src = $createfun($src_img);
  2216. $dst = imagecreatetruecolor($width ? $width : $dst_w, $height ? $height : $dst_h);
  2217. try {
  2218. $white = imagecolorallocate($dst, 255, 255, 255);
  2219. imagefill($dst, 0, 0, $white);
  2220. } catch (Exception $e) {
  2221. }
  2222. if (function_exists('imagecopyresampled')) {
  2223. imagecopyresampled($dst, $src, $x, $y, 0, 0, $dst_w, $dst_h, $src_w, $src_h);
  2224. } else {
  2225. imagecopyresized($dst, $src, $x, $y, 0, 0, $dst_w, $dst_h, $src_w, $src_h);
  2226. }
  2227. $otfunc($dst, $dst_img);
  2228. imagedestroy($dst);
  2229. imagedestroy($src);
  2230. return true;
  2231. }
  2232. /**
  2233. * 排列组合(无重复)
  2234. * @param $arr
  2235. * @param $m
  2236. * @return array
  2237. */
  2238. public static function getCombinationToString($arr, $m)
  2239. {
  2240. $result = [];
  2241. if ($m == 1) {
  2242. return $arr;
  2243. }
  2244. if ($m == count($arr)) {
  2245. $result[] = implode(',', $arr);
  2246. return $result;
  2247. }
  2248. $temp_firstelement = $arr[0];
  2249. unset($arr[0]);
  2250. $arr = array_values($arr);
  2251. $temp_list1 = self::getCombinationToString($arr, ($m - 1));
  2252. foreach ($temp_list1 as $s) {
  2253. $s = $temp_firstelement . ',' . $s;
  2254. $result[] = $s;
  2255. }
  2256. unset($temp_list1);
  2257. $temp_list2 = self::getCombinationToString($arr, $m);
  2258. foreach ($temp_list2 as $s) {
  2259. $result[] = $s;
  2260. }
  2261. unset($temp_list2);
  2262. return $result;
  2263. }
  2264. /**
  2265. * 不同元素交叉组合(多个数组)
  2266. * @return array
  2267. */
  2268. public static function getNewArray()
  2269. {
  2270. $args = func_get_args();
  2271. $pailie = function ($arr1, $arr2) {
  2272. $arr = [];
  2273. $k = 0;
  2274. foreach ($arr1 as $k1 => $v1) {
  2275. foreach ($arr2 as $k2 => $v2) {
  2276. $arr[$k] = $v1 . "," . $v2;
  2277. $k++;
  2278. }
  2279. }
  2280. return $arr;
  2281. };
  2282. $arr = [];
  2283. foreach ($args as $k => $v) {
  2284. if (isset($args[$k + 1]) && $args[$k + 1]) {
  2285. switch ($k) {
  2286. case 0:
  2287. $arr[$k] = $pailie($v, $args[$k + 1]);
  2288. break;
  2289. default:
  2290. $arr[$k] = $pailie($arr[$k - 1], $args[$k + 1]);
  2291. break;
  2292. }
  2293. }
  2294. }
  2295. $key = count($arr) - 1;
  2296. return array_values($arr[$key]);
  2297. }
  2298. /**
  2299. * 获取当前是本月第几个星期
  2300. * @return false|float
  2301. */
  2302. public static function getMonthWeek()
  2303. {
  2304. $time = strtotime(date("Y-m-01"));
  2305. $w = date('w', $time);
  2306. $j = date("j");
  2307. return ceil(($j + $w) / 7);
  2308. }
  2309. /**
  2310. * 把返回的数据集转换成Tree
  2311. * @param array $list 要转换的数据集
  2312. * @param string $pk id标记字段
  2313. * @param string $pid parent标记字段
  2314. * @param string $child 生成子类字段
  2315. * @param int $root
  2316. * @return array
  2317. */
  2318. public static function list2Tree($list, $pk='id', $pid = 'pid', $child = 'children', $root = 0) {
  2319. if (!is_array($list)) {
  2320. return [];
  2321. }
  2322. // 创建基于主键的数组引用
  2323. $aRefer = [];
  2324. foreach ($list as $key => $data) {
  2325. $list[$key][$child] = [];
  2326. $aRefer[$data[$pk]] = & $list[$key];
  2327. }
  2328. $tree = [];
  2329. foreach ($list as $key => $data) {
  2330. // 判断是否存在parent
  2331. $parentId = $data[$pid];
  2332. if ($root === $parentId) {
  2333. $tree[] = & $list[$key];
  2334. } else {
  2335. if (isset($aRefer[$parentId])) {
  2336. $parent = & $aRefer[$parentId];
  2337. $parent[$child][] = & $list[$key];
  2338. }
  2339. }
  2340. }
  2341. return $tree;
  2342. }
  2343. /**
  2344. * 遍历获取文件
  2345. * @param $dir
  2346. * @return array
  2347. */
  2348. public static function readDir($dir)
  2349. {
  2350. $files = array();
  2351. $dir_list = scandir($dir);
  2352. foreach ($dir_list as $file) {
  2353. if ($file != '..' && $file != '.') {
  2354. if (is_dir($dir . '/' . $file)) {
  2355. $files = array_merge($files, self::readDir($dir . '/' . $file));
  2356. } else {
  2357. $files[] = $dir . "/" . $file;
  2358. }
  2359. }
  2360. }
  2361. return $files;
  2362. }
  2363. /**
  2364. * 获取中文字符拼音首字母
  2365. * @param $str
  2366. * @return string
  2367. */
  2368. public static function getFirstCharter($str)
  2369. {
  2370. if (empty($str)) {
  2371. return '';
  2372. }
  2373. $fchar = ord($str[0]);
  2374. if ($fchar >= ord('A') && $fchar <= ord('z')) return strtoupper($str[0]);
  2375. $s1 = iconv('UTF-8', 'gb2312', $str);
  2376. $s2 = iconv('gb2312', 'UTF-8', $s1);
  2377. $s = $s2 == $str ? $s1 : $str;
  2378. $asc = ord($s[0]) * 256 + ord($s[1]) - 65536;
  2379. if ($asc >= -20319 && $asc <= -20284) return 'A';
  2380. if ($asc >= -20283 && $asc <= -19776) return 'B';
  2381. if ($asc >= -19775 && $asc <= -19219) return 'C';
  2382. if ($asc >= -19218 && $asc <= -18711) return 'D';
  2383. if ($asc >= -18710 && $asc <= -18527) return 'E';
  2384. if ($asc >= -18526 && $asc <= -18240) return 'F';
  2385. if ($asc >= -18239 && $asc <= -17923) return 'G';
  2386. if ($asc >= -17922 && $asc <= -17418) return 'H';
  2387. if ($asc >= -17417 && $asc <= -16475) return 'J';
  2388. if ($asc >= -16474 && $asc <= -16213) return 'K';
  2389. if ($asc >= -16212 && $asc <= -15641) return 'L';
  2390. if ($asc >= -15640 && $asc <= -15166) return 'M';
  2391. if ($asc >= -15165 && $asc <= -14923) return 'N';
  2392. if ($asc >= -14922 && $asc <= -14915) return 'O';
  2393. if ($asc >= -14914 && $asc <= -14631) return 'P';
  2394. if ($asc >= -14630 && $asc <= -14150) return 'Q';
  2395. if ($asc >= -14149 && $asc <= -14091) return 'R';
  2396. if ($asc >= -14090 && $asc <= -13319) return 'S';
  2397. if ($asc >= -13318 && $asc <= -12839) return 'T';
  2398. if ($asc >= -12838 && $asc <= -12557) return 'W';
  2399. if ($asc >= -12556 && $asc <= -11848) return 'X';
  2400. if ($asc >= -11847 && $asc <= -11056) return 'Y';
  2401. if ($asc >= -11055 && $asc <= -10247) return 'Z';
  2402. return '#';
  2403. }
  2404. /**
  2405. * 缓存数据
  2406. * @param $title
  2407. * @param null $value
  2408. * @return mixed|null
  2409. */
  2410. public static function cacheData($title, $value = null)
  2411. {
  2412. $title = "cacheData::" . $title;
  2413. $tmp = DB::table('tmp')->where('title', $title)->select('value')->first();
  2414. if ($value !== null ) {
  2415. if (empty($tmp)) {
  2416. DB::table('tmp')->insert(['title'=>$title, 'value'=>$value]);
  2417. }else{
  2418. DB::table('tmp')->where('title', $title)->update(['value'=>$value]);
  2419. }
  2420. return $value;
  2421. }else{
  2422. return $tmp->value;
  2423. }
  2424. }
  2425. }