| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442 |
- <?php
- // +----------------------------------------------------------------------
- // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
- // +----------------------------------------------------------------------
- // | 版权所有 2017~2021 LARAVEL研发中心
- // +----------------------------------------------------------------------
- // | 官方网站: http://www.laravel.cn
- // +----------------------------------------------------------------------
- // | Author: laravel开发员 <laravel.qq.com>
- // +----------------------------------------------------------------------
- // 此文件为系统框架核心公共函数文件,为了系统的稳定与安全,未经允许不得擅自改动
- if (!function_exists('getValidatorError')) {
- /**
- * 错误验证
- * @param $error
- * @return mixed
- */
- function getValidatorError($validator)
- {
- $errors = $validator->errors()->all();
- return isset($errors[0]) ? $errors[0] : 1013;
- }
- }
- if (!function_exists('array_sort')) {
- /**
- * 数组排序
- * @param array $arr 数据源
- * @param $keys KEY
- * @param bool $desc 排序方式(默认:asc)
- * @return array 返回结果
- * @author laravel开发员
- * @date 2019/5/23
- */
- function array_sort($arr, $keys, $desc = false)
- {
- $key_value = $new_array = array();
- foreach ($arr as $k => $v) {
- $key_value[$k] = $v[$keys];
- }
- if ($desc) {
- arsort($key_value);
- } else {
- asort($key_value);
- }
- reset($key_value);
- foreach ($key_value as $k => $v) {
- $new_array[$k] = $arr[$k];
- }
- return $new_array;
- }
- }
- if (!function_exists('xml2array')) {
- /**
- * xml转数组
- * @param $xml xml文本
- * @return string
- * @author laravel开发员
- * @date 2019/6/6
- */
- function xml2array(&$xml)
- {
- $xml = "<xml>";
- foreach ($xml as $key => $val) {
- if (is_numeric($val)) {
- $xml .= "<" . $key . ">" . $val . "</" . $key . ">";
- } else {
- $xml .= "<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">";
- }
- }
- $xml .= "</xml>";
- return $xml;
- }
- }
- if (!function_exists('array2xml')) {
- /**
- * 数组转xml
- * @param $arr 原始数据(数组)
- * @param bool $ignore 是否忽视true或fasle
- * @param int $level 级别(默认:1)
- * @return string 返回结果
- * @author laravel开发员
- * @date 2019/6/6
- */
- function array2xml($arr, $ignore = true, $level = 1)
- {
- $s = $level == 1 ? "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\r\n<root>\r\n" : '';
- $space = str_repeat("\t", $level);
- foreach ($arr as $k => $v) {
- if (!is_array($v)) {
- $s .= $space . "<item id=\"$k\">" . ($ignore ? '<![CDATA[' : '') . $v . ($ignore ? ']]>' : '')
- . "</item>\r\n";
- } else {
- $s .= $space . "<item id=\"$k\">\r\n" . array2xml($v, $ignore, $level + 1) . $space . "</item>\r\n";
- }
- }
- $s = preg_replace("/([\x01-\x08\x0b-\x0c\x0e-\x1f])+/", ' ', $s);
- return $level == 1 ? $s . "</root>" : $s;
- }
- }
- if (!function_exists('array_merge_multiple')) {
- /**
- * 多为数组合并
- * @param array $array1 数组1
- * @param array $array2 数组2
- * @return array 返回合并后的数组
- * @author laravel开发员
- * @date 2019/6/6
- */
- function array_merge_multiple($array1, $array2)
- {
- $merge = $array1 + $array2;
- $data = [];
- foreach ($merge as $key => $val) {
- if (isset($array1[$key])
- && is_array($array1[$key])
- && isset($array2[$key])
- && is_array($array2[$key])
- ) {
- $data[$key] = array_merge_multiple($array1[$key], $array2[$key]);
- } else {
- $data[$key] = isset($array2[$key]) ? $array2[$key] : $array1[$key];
- }
- }
- return $data;
- }
- }
- if (!function_exists('array_key_value')) {
- /**
- * 获取数组中某个字段的所有值
- * @param array $arr 数据源
- * @param string $name 字段名
- * @return array 返回结果
- * @author laravel开发员
- * @date 2019/6/6
- */
- function array_key_value($arr, $name = "")
- {
- $result = [];
- if ($arr) {
- foreach ($arr as $key => $val) {
- if ($name) {
- $result[] = $val[$name];
- } else {
- $result[] = $key;
- }
- }
- }
- $result = array_unique($result);
- return $result;
- }
- }
- if (!function_exists('curl_url')) {
- /**
- * 获取当前访问的完整地址
- * @return string 返回结果
- * @author laravel开发员
- * @date 2019/6/6
- */
- function curl_url()
- {
- $page_url = 'http';
- if (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] === 'on') {
- $page_url .= "s";
- }
- $page_url .= "://";
- if ($_SERVER["SERVER_PORT"] != "80") {
- $page_url .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"];
- } else {
- $page_url .= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
- }
- return $page_url;
- }
- }
- if (!function_exists('curl_get')) {
- /**
- * curl请求(GET)
- * @param $url 请求地址
- * @param array $data 请求参数
- * @return bool|string 返回结果
- * @author laravel开发员
- * @date 2019/6/5
- */
- function curl_get($url, $data = [])
- {
- if (!empty($data)) {
- $url = $url . '?' . http_build_query($data);
- }
- // 初始化
- $ch = curl_init();
- // 设置抓取的url
- curl_setopt($ch, CURLOPT_URL, $url);
- // 设置头文件的信息作为数据流输出
- curl_setopt($ch, CURLOPT_HEADER, false);
- // 是否要求返回数据
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- // 是否检测服务器的证书是否由正规浏览器认证过的授权CA颁发的
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- // 执行命令
- $result = curl_exec($ch);
- // 关闭URL请求(释放句柄)
- curl_close($ch);
- return $result;
- }
- }
- if (!function_exists('curl_post')) {
- /**
- * curl请求(POST)
- * @param $url 请求地址
- * @param array $data 请求参数
- * @return bool|string 返回结果
- * @author laravel开发员
- * @date 2019/6/5
- */
- function curl_post($url, $data = [])
- {
- // 初始化
- $ch = curl_init();
- // 设置post方式提交
- curl_setopt($ch, CURLOPT_POST, 1);
- // 设置头文件的信息作为数据流输出
- curl_setopt($ch, CURLOPT_HEADER, 0);
- // 是否要求返回数据
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- // 设置抓取的url
- curl_setopt($ch, CURLOPT_URL, $url);
- // 提交的数据
- curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
- // 是否检测服务器的证书是否由正规浏览器认证过的授权CA颁发的
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- // 执行命令
- $result = curl_exec($ch);
- // 关闭URL请求(释放句柄)
- curl_close($ch);
- return $result;
- }
- }
- if (!function_exists('curl_request')) {
- /**
- * curl请求(支持get和post)
- * @param $url 请求地址
- * @param array $data 请求参数
- * @param string $type 请求类型(默认:post)
- * @param bool $https 是否https请求true或false
- * @return bool|string 返回请求结果
- * @author laravel开发员
- * @date 2019/6/6
- */
- function curl_request($url, $data = [], $type = 'post', $https = false)
- {
- // 初始化
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
- curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)');
- curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
- // 设置超时时间
- curl_setopt($ch, CURLOPT_TIMEOUT, 30);
- // 是否要求返回数据
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- if ($https) {
- // 对认证证书来源的检查
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- // 从证书中检查SSL加密算法是否存在
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
- }
- if (strtolower($type) == 'post') {
- // 设置post方式提交
- curl_setopt($ch, CURLOPT_POST, true);
- // 提交的数据
- curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
- } elseif (!empty($data) && is_array($data)) {
- // get网络请求
- $url = $url . '?' . http_build_query($data);
- }
- // 设置抓取的url
- curl_setopt($ch, CURLOPT_URL, $url);
- // 执行命令
- $result = curl_exec($ch);
- if ($result === false) {
- return false;
- }
- // 关闭URL请求(释放句柄)
- curl_close($ch);
- return $result;
- }
- }
- if (!function_exists('datetime')) {
- /**
- * 格式化日期函数
- * @param $time 时间戳
- * @param string $format 输出日期格式
- * @return string 返回格式化的日期
- * @author laravel开发员
- * @date 2019/5/23
- */
- function datetime($time, $format = 'Y-m-d H:i:s')
- {
- if($time == '0000-00-00 00:00:00'){
- return '';
- }
- $time = is_numeric($time) ? $time : strtotime($time);
- return date($format, $time);
- }
- }
- if (!function_exists('dateForWeek')) {
- /**
- * 返回星期格式时间
- * @param $time 时间戳
- * @return false|string
- */
- function dateForWeek($time)
- {
- if(empty($time)){
- return false;
- }
- $time = !is_int($time)? strtotime($time) : $time;
- $weeks = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];
- $week = date("w", $time);
- $cweek = date("w");
- if ($cweek - $week >= 0 && (time() - $time) <= 7 * 86400) {
- return isset($weeks[$week]) ? $weeks[$week] . ' ' . date('H:i', $time) : date('m月d日 H:i', $time);
- } else {
- return date('m月d日 H:i', $time);
- }
- }
- }
- if (!function_exists('dateFormat')) {
- /**
- * 格式化时间日期
- * @param $time 时间戳
- * @return false|string
- */
- function dateFormat($time, $format='m月d日')
- {
- if(empty($time)){
- return false;
- }
- $time = !is_int($time)? strtotime($time) : $time;
- $weeks = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];
- $week = date("w", $time);
- $cweek = date("w");
- $dateTime = strtotime(date('Y-m-d'));
- // 今日
- if ($time >= $dateTime) {
- return date('H:i', $time);
- } // 昨日
- else if ($time >= $dateTime - 86400) {
- return date('昨天 H:i', $time);
- } else if ($cweek - $week > 0 && (time() - $time) < 7 * 86400) {
- return isset($weeks[$week]) ? $weeks[$week] . ' ' . date('H:i', $time) : date('m月d日 H:i', $time);
- } else {
- return date($format, $time);
- }
- }
- }
- if (!function_exists('formatScientific')){
- /**
- * 格式化科学计数
- * @param $num 数字
- * @return false|string
- */
- function formatScientific($num, $dec=6)
- {
- if(empty($num)){
- return false;
- }
- $datas = explode('E', $num);
- $num = floatval($datas[0]);
- $decimal = intval($datas[1]);
- if($decimal>0){
- return number_format($num * pow(10, $decimal),$dec);
- }else{
- return number_format($num * pow(10, $decimal),$dec);
- }
- }
- }
- if (!function_exists('data_auth_sign')) {
- /**
- * 数据签名认证
- * @param $data 数据源
- * @return string
- * @author laravel开发员
- * @date 2019/6/6
- */
- function data_auth_sign($data)
- {
- // 数据类型检测
- if (!is_array($data)) {
- $data = (array)$data;
- }
- // 排序
- ksort($data);
- // url编码并生成query字符串
- $code = http_build_query($data);
- // 生成签名
- $sign = sha1($code);
- return $sign;
- }
- }
- if (!function_exists('decrypt')) {
- /**
- * DES解密
- * @param string $str 解密字符串
- * @param string $key 解密KEY
- * @return mixed
- * @author laravel开发员
- * @date 2019/6/6
- */
- function decrypt($str, $key = 'p@ssw0rd')
- {
- $str = base64_decode($str);
- $str = mcrypt_decrypt(MCRYPT_DES, $key, $str, MCRYPT_MODE_ECB);
- $block = mcrypt_get_block_size('des', 'ecb');
- $pad = ord($str[($len = strlen($str)) - 1]);
- if ($pad && $pad < $block && preg_match('/' . chr($pad) . '{' . $pad . '}$/', $str)) {
- $str = substr($str, 0, strlen($str) - $pad);
- }
- return unserialize($str);
- }
- }
- if (!function_exists('encrypt')) {
- /**
- *
- * @param string $str 加密字符串
- * @param string $key 加密KEY
- * @return string
- * @author laravel开发员
- * @date 2019/6/6
- */
- function encrypt($str, $key = 'p@ssw0rd')
- {
- $prep_code = serialize($str);
- $block = mcrypt_get_block_size('des', 'ecb');
- if (($pad = $block - (strlen($prep_code) % $block)) < $block) {
- $prep_code .= str_repeat(chr($pad), $pad);
- }
- $encrypt = mcrypt_encrypt(MCRYPT_DES, $key, $prep_code, MCRYPT_MODE_ECB);
- return base64_encode($encrypt);
- }
- }
- if (!function_exists('export_excel')) {
- /**
- * 导出Excel文件
- * @param string $file_name 文件名
- * @param array $title 标题
- * @param array $data 数据源
- * @author laravel开发员
- * @date 2019/6/6
- */
- function export_excel($file_name, $title = [], $data = [])
- {
- // 默认支持最大512M
- ini_set('memory_limit', '512M');
- ini_set('max_execution_time', 0);
- ob_end_clean();
- ob_start();
- header("Content-Type: text/csv");
- header("Content-Disposition:filename=" . $file_name);
- $fp = fopen('php://output', 'w');
- // 转码 防止乱码(比如微信昵称)
- fwrite($fp, chr(0xEF) . chr(0xBB) . chr(0xBF));
- fputcsv($fp, $title);
- $index = 0;
- foreach ($data as $item) {
- if ($index == 1000) {
- $index = 0;
- ob_flush();
- flush();
- }
- $index++;
- fputcsv($fp, $item);
- }
- ob_flush();
- flush();
- ob_end_clean();
- }
- }
- if (!function_exists('ecm_define')) {
- /**
- * 定义常量(读取数组或引用文件)
- * @param $value 数据源
- * @return bool
- * @author laravel开发员
- * @date 2019/6/6
- */
- function ecm_define($value)
- {
- if (is_string($value)) {
- /* 导入数组 */
- $value = include($value);
- }
- if (!is_array($value)) {
- /* 不是数组,无法定义 */
- return false;
- }
- foreach ($value as $key => $val) {
- if (is_string($val) || is_numeric($val) || is_bool($val) || is_null($val)) {
- // 判断是否已定义过,否则进行定义
- defined(strtoupper($key)) or define(strtoupper($key), $val);
- }
- }
- }
- }
- if (!function_exists('format_time')) {
- /**
- * 格式化时间段
- * @param int $time 时间戳
- * @return string 输出格式化时间
- * @author laravel开发员
- * @date 2019/5/23
- */
- function format_time($time)
- {
- $interval = time() - $time;
- $format = array(
- '31536000' => '年',
- '2592000' => '个月',
- '604800' => '星期',
- '86400' => '天',
- '3600' => '小时',
- '60' => '分钟',
- '1' => '秒',
- );
- foreach ($format as $key => $val) {
- $match = floor($interval / (int)$key);
- if (0 != $match) {
- return $match . $val . '前';
- }
- }
- return date('Y-m-d', $time);
- }
- }
- if (!function_exists('format_num')){
- /**
- * @param $num
- */
- function format_num($num, $max=10000, $text='w')
- {
- return $num > 10000? moneyFormat($num/$max,1).$text : $num;
- }
- }
- if (!function_exists('format_bytes')) {
- /**
- * 将字节转换为可读文本
- * @param int $size 字节大小
- * @param string $delimiter 分隔符
- * @return string 返回结果
- * @author laravel开发员
- * @date 2019/5/23
- */
- function format_bytes($size, $delimiter = '')
- {
- $units = array('B', 'KB', 'MB', 'GB', 'TB', 'PB');
- for ($i = 0; $size >= 1024 && $i < 6; $i++) {
- $size /= 1024;
- }
- return round($size, 2) . $delimiter . $units[$i];
- }
- }
- if (!function_exists('format_yuan')) {
- /**
- * 以分为单位的金额转换成元
- * @param int $money 金额
- * @return string 返回格式化的金额
- * @author laravel开发员
- * @date 2019/5/23
- */
- function format_yuan($money = 0)
- {
- if ($money > 0) {
- return number_format($money / 100, 2, ".", "");
- }
- return "0.00";
- }
- }
- if (!function_exists('format_cent')) {
- /**
- * 以元为单位的金额转化成分
- * @param $money 金额
- * @return string 返回格式化的金额
- * @author laravel开发员
- * @date 2019/5/23
- */
- function format_cent($money)
- {
- return (string)($money * 100);
- }
- }
- if (!function_exists('format_bank_card')) {
- /**
- * 银行卡格式转换
- * @param string $card_no 银行卡号
- * @param bool $is_format 是否格式化
- * @return string 输出结果
- * @author laravel开发员
- * @date 2019/5/23
- */
- function format_bank_card($card_no, $is_format = true, $hidden=true)
- {
- if($hidden){
- $format_card_no = '****'.substr($card_no, -4, 4);
- }else if ($is_format) {
- // 截取银行卡号前4位
- $prefix = substr($card_no, 0, 4);
- // 截取银行卡号后4位
- $suffix = substr($card_no, -4, 4);
- $format_card_no = $prefix . " **** **** **** " . $suffix;
- } else {
- // 4的意思就是每4个为一组
- $arr = str_split($card_no, 4);
- $format_card_no = implode(' ', $arr);
- }
- return $format_card_no;
- }
- }
- if (!function_exists('format_mobile')) {
- /**
- * 格式化手机号码
- * @param string $mobile 手机号码
- * @return string 返回结果
- * @author laravel开发员
- * @date 2019/5/23
- */
- function format_mobile($mobile)
- {
- return substr($mobile, 0, 3) . "****" . substr($mobile, -4, 4);
- }
- }
- if (!function_exists('get_random_code')) {
- /**
- * 生成唯一字符串
- * @param int $length
- * @param string $prefix
- * @param string $ext
- * @return string
- */
- function get_random_code($length = 8, $prefix = '', $ext = '')
- {
- $length = $length < 8 ? 8 : $length;
- $code = strtoupper(md5(uniqid(mt_rand(), true) . $ext . microtime()));
- $start = rand(1, strlen($code) - $length);
- return $prefix . mb_substr($code, $start, $length);
- }
- }
- if (!function_exists('get_username')) {
- /**
- * 生成唯一字符串
- * @param int $length
- * @param string $prefix
- * @param string $ext
- * @return string
- */
- function get_username($id, $prefix = 'XL')
- {
- $length = strlen($id);
- if ($length >= 8) {
- return $prefix . $id;
- }
- return $prefix . str_repeat('0', 2) . $id;
- }
- }
- if (!function_exists('get_password')) {
- /**
- * 获取双MD5加密密码
- * @param string $password 加密字符串
- * @return string 输出MD5加密字符串
- * @author laravel开发员
- * @date 2019/5/23
- */
- function get_password($password)
- {
- return md5(md5($password));
- }
- }
- if (!function_exists('get_order_num')) {
- /**
- * 生成订单号
- * @param string $prefix 订单前缀(如:JD-)
- * @return string 输出订单号字符串
- * @author laravel开发员
- * @date 2019/5/23
- */
- function get_order_num($prefix = '')
- {
- $micro = substr(microtime(), 2, 2);
- return $prefix . date("YmdHis") . $micro . rand(1000, 9999);
- }
- }
- if (!function_exists('getter')) {
- /**
- * 获取数组的下标值
- * @param array $data 数据源
- * @param string $field 字段名称
- * @param string $default 默认值
- * @return mixed|string 返回结果
- * @author laravel开发员
- * @date 2019/5/23
- */
- function getter($data, $field, $default = '')
- {
- $result = $default;
- if (isset($data[$field])) {
- $result = $data[$field];
- }
- return $result;
- }
- }
- if (!function_exists('get_zodiac_sign')) {
- /**
- * 根据月、日获取星座
- * @param $month 月份
- * @param $day 日期
- * @return string 返回结果
- * @author laravel开发员
- * @date 2019/5/23
- */
- function get_zodiac_sign($month, $day)
- {
- // 检查参数有效性
- if ($month < 1 || $month > 12 || $day < 1 || $day > 31) {
- return false;
- }
- // 星座名称以及开始日期
- $signs = array(
- array("20" => "水瓶座"),
- array("19" => "双鱼座"),
- array("21" => "白羊座"),
- array("20" => "金牛座"),
- array("21" => "双子座"),
- array("22" => "巨蟹座"),
- array("23" => "狮子座"),
- array("23" => "处女座"),
- array("23" => "天秤座"),
- array("24" => "天蝎座"),
- array("22" => "射手座"),
- array("22" => "摩羯座")
- );
- list($sign_start, $sign_name) = each($signs[(int)$month - 1]);
- if ($day < $sign_start) {
- list($sign_start, $sign_name) = each($signs[($month - 2 < 0) ? $month = 11 : $month -= 2]);
- }
- return $sign_name;
- }
- }
- if (!function_exists('get_image_url')) {
- /**
- * 获取图片地址
- * @param $image_url 图片地址
- * @return string $domain 域名
- * @author laravel开发员
- * @date 2019/6/6
- */
- function get_image_url($image_url, $domain = '')
- {
- if (empty($image_url)) {
- return '';
- }
- if($domain){
- $host = $domain.'/uploads';
- }else{
- $host = request()->header('HOST');
- $https = request()->secure();
- $host = ($https ? 'https://' : 'http://') . $host . '/uploads';
- }
- return strpos($image_url, 'http') === false && strpos($image_url, 'file://') === false ? $host . '/' . ltrim($image_url, '/') : $image_url;
- }
- }
- if (!function_exists('remove_image_url')) {
- /**
- * 获取图片地址
- * @param $image_url 图片地址
- * @return string $domain 域名
- * @author laravel开发员
- * @date 2019/6/6
- */
- function remove_image_url($image_url, $domain = '')
- {
- if (empty($image_url)) {
- return '';
- }
- if($domain){
- $host = $domain.'/uploads';
- }else{
- $host = request()->header('HOST');
- $https = request()->secure();
- $host = ($https ? 'https://' : 'http://') . $host . '/uploads';
- }
- if(strpos($image_url, $host) === true){
- return str_replace($host, "", $image_url);
- }
- return $image_url;
- }
- }
- if (!function_exists('get_image_path')) {
- /**
- * 获取图片相对路径
- * @param $image_url 图片地址
- * @param $https 是否有证书
- * @return string 返回图片网络地址
- * @author laravel开发员
- * @date 2019/6/6
- */
- function get_image_path($image_url, $https = true)
- {
- if (empty($image_url)) {
- return false;
- }
- $data = explode('/uploads', $image_url);
- return $data ? end($data) : $image_url;
- }
- }
- if (!function_exists('get_format_images')) {
- /**
- * 获取多图片相对路径数组或json
- * @param $images 图片地址
- * @return string 返回图片网络地址
- * @author laravel开发员
- * @date 2019/6/6
- */
- function get_format_images($images, $key = 'url', $dataType = 'json')
- {
- if (empty($images) || !is_array($images)) {
- return '';
- }
- $datas = [];
- foreach ($images as $v) {
- $url = isset($v[$key]) ? $v[$key] : $v;
- if ($url) {
- $datas[] = $key? [
- $key=> get_image_path($url)
- ] : get_image_path($url);
- }
- }
- if ($datas) {
- return $dataType == 'json' ? json_encode($datas, 256) : '';
- } else {
- return $dataType == 'json' ? '' : [];
- }
- }
- }
- if(!function_exists('getImageContent')){
- /**
- * 获取图片内容
- * @param $url
- * @return false|string
- */
- function getImageContent($url){
- $imgUrl = $url;
- if(!preg_match("/^http/", $url)){
- $imgUrl = base_path().'/public/uploads/'.$url;
- }
- if(is_file($imgUrl) && file_exists($imgUrl)){
- $data =file_get_contents($imgUrl);
- return "data:image/jpeg;base64," . base64_encode($data);
- }else{
- return get_image_url($url);
- }
- }
- }
- if (!function_exists('get_web_url')) {
- /**
- * 获取预览地址
- * @param $url 地址
- * @return string 返回网络地址
- * @author laravel开发员
- * @date 2019/6/6
- */
- function get_web_url($url)
- {
- $host = request()->header('HOST');
- $host = ((strpos($host, '127') === false) && strpos($host, 'localhost') === false) ? env('WEB_URL') : 'http://' . $host;
- return strpos($url, 'http') === false ? $host . '/' . ltrim($url, '/') : $url;
- }
- }
- if (!function_exists('set_format_content')) {
- /**
- * 获取文章图片格式化后内容
- * @param $content
- * @return false|mixed|string
- */
- function set_format_content($content)
- {
- if (empty($content)) {
- return false;
- }
- $domain = request()->header('HOST');
- if(preg_match("/127/", $domain)){
- $host = env('IMG_URL','');
- if(empty($host)){
- $https = request()->secure();
- $host = ($https ? 'https://' : 'http://') . $host . '/uploads';
- }
- }else{
- $https = request()->secure();
- $host = ($https ? 'https://' : 'http://') . $domain . '/uploads';
- }
- $content = str_replace("{$host}",'/uploads', htmlspecialchars_decode($content));
- return $content;
- }
- }
- if (!function_exists('get_format_content')) {
- /**
- * 获取文章图片格式化后内容
- * @param $content
- * @return false|mixed|string
- */
- function get_format_content($content)
- {
- if (empty($content)) {
- return false;
- }
- $domain = request()->header('HOST');
- if(preg_match("/127/", $domain)){
- $host = env('IMG_URL','');
- if(empty($host)){
- $https = request()->secure();
- $host = ($https ? 'https://' : 'http://') . $host . '/uploads';
- }
- }else{
- $https = request()->secure();
- $host = ($https ? 'https://' : 'http://') . $domain . '/uploads';
- }
- $content = str_replace(["\"/uploads","'/uploads"],["\"{$host}","'{$host}"], htmlspecialchars_decode($content));
- return $content;
- }
- }
- if (!function_exists('get_hash')) {
- /**
- * 获取HASH值
- * @return string 返回hash字符串
- * @author laravel开发员
- * @date 2019/6/6
- */
- function get_hash()
- {
- $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()+-';
- $random = $chars[mt_rand(0, 73)] . $chars[mt_rand(0, 73)] . $chars[mt_rand(0, 73)]
- . $chars[mt_rand(0, 73)] . $chars[mt_rand(0, 73)];
- $content = uniqid() . $random;
- return sha1($content);
- }
- }
- if (!function_exists('get_server_ip')) {
- /**
- * 获取服务端IP地址
- * @return string 返回IP地址
- * @author laravel开发员
- * @date 2019/6/6
- */
- function get_server_ip()
- {
- if (isset($_SERVER)) {
- if ($_SERVER['SERVER_ADDR']) {
- $server_ip = $_SERVER['SERVER_ADDR'];
- } else {
- $server_ip = $_SERVER['LOCAL_ADDR'];
- }
- } else {
- $server_ip = getenv('SERVER_ADDR');
- }
- return $server_ip;
- }
- }
- if (!function_exists('get_client_ip')) {
- /**
- * 获取客户端IP地址
- * @param int $type 返回类型 0 返回IP地址 1 返回IPV4地址数字
- * @param bool $adv 否进行高级模式获取(有可能被伪装)
- * @return mixed 返回IP
- * @author laravel开发员
- * @date 2019/5/23
- */
- function get_client_ip($type = 0, $adv = false)
- {
- $type = $type ? 1 : 0;
- static $ip = null;
- if ($ip !== null) {
- return $ip[$type];
- }
- if ($adv) {
- if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
- $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
- $pos = array_search('unknown', $arr);
- if (false !== $pos) {
- unset($arr[$pos]);
- }
- $ip = trim($arr[0]);
- } elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
- $ip = $_SERVER['HTTP_CLIENT_IP'];
- } elseif (isset($_SERVER['REMOTE_ADDR'])) {
- $ip = $_SERVER['REMOTE_ADDR'];
- }
- } elseif (isset($_SERVER['REMOTE_ADDR'])) {
- $ip = $_SERVER['REMOTE_ADDR'];
- }
- // IP地址合法验证
- $long = sprintf("%u", ip2long($ip));
- $ip = $long ? array($ip, $long) : array('0.0.0.0', 0);
- return $ip[$type];
- }
- }
- if (!function_exists('get_guid_v4')) {
- /**
- * 获取唯一性GUID
- * @param bool $trim 是否去除{}
- * @return string 返回GUID字符串
- * @author laravel开发员
- * @date 2019/6/6
- */
- function get_guid_v4($trim = true)
- {
- // Windows
- if (function_exists('com_create_guid') === true) {
- $charid = com_create_guid();
- return $trim == true ? trim($charid, '{}') : $charid;
- }
- // OSX/Linux
- if (function_exists('openssl_random_pseudo_bytes') === true) {
- $data = openssl_random_pseudo_bytes(16);
- $data[6] = chr(ord($data[6]) & 0x0f | 0x40); // set version to 0100
- $data[8] = chr(ord($data[8]) & 0x3f | 0x80); // set bits 6-7 to 10
- return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
- }
- // Fallback (PHP 4.2+)
- mt_srand((double)microtime() * 10000);
- $charid = strtolower(md5(uniqid(rand(), true)));
- $hyphen = chr(45); // "-"
- $lbrace = $trim ? "" : chr(123); // "{"
- $rbrace = $trim ? "" : chr(125); // "}"
- $guidv4 = $lbrace .
- substr($charid, 0, 8) . $hyphen .
- substr($charid, 8, 4) . $hyphen .
- substr($charid, 12, 4) . $hyphen .
- substr($charid, 16, 4) . $hyphen .
- substr($charid, 20, 12) .
- $rbrace;
- return $guidv4;
- }
- }
- if (!function_exists('is_email')) {
- /**
- * 判断是否为邮箱
- * @param string $str 邮箱
- * @return false 返回结果true或false
- * @author laravel开发员
- * @date 2019/5/23
- */
- function is_email($str)
- {
- return preg_match('/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/', $str);
- }
- }
- if (!function_exists('is_mobile')) {
- /**
- * 判断是否为手机号
- * @param string $num 手机号码
- * @return false 返回结果true或false
- * @author laravel开发员
- * @date 2019/5/23
- */
- function is_mobile($num)
- {
- return preg_match('/^1(3|4|5|7|8)\d{9}$/', $num);
- }
- }
- if (!function_exists('is_zipcode')) {
- /**
- * 验证邮编是否正确
- * @param string $code 邮编
- * @return false 返回结果true或false
- * @author laravel开发员
- * @date 2019/5/23
- */
- function is_zipcode($code)
- {
- return preg_match('/^[1-9][0-9]{5}$/', $code);
- }
- }
- if (!function_exists('is_idcard')) {
- /**
- * 验证身份证是否正确
- * @param string $idno 身份证号
- * @return bool 返回结果true或false
- * @author laravel开发员
- * @date 2019/5/23
- */
- function is_idcard($idno)
- {
- $idno = strtoupper($idno);
- $regx = '/(^\d{15}$)|(^\d{17}([0-9]|X)$)/';
- $arr_split = array();
- if (!preg_match($regx, $idno)) {
- return false;
- }
- // 检查15位
- if (15 == strlen($idno)) {
- $regx = '/^(\d{6})+(\d{2})+(\d{2})+(\d{2})+(\d{3})$/';
- @preg_match($regx, $idno, $arr_split);
- $dtm_birth = "19" . $arr_split[2] . '/' . $arr_split[3] . '/' . $arr_split[4];
- if (!strtotime($dtm_birth)) {
- return false;
- } else {
- return true;
- }
- } else {
- // 检查18位
- $regx = '/^(\d{6})+(\d{4})+(\d{2})+(\d{2})+(\d{3})([0-9]|X)$/';
- @preg_match($regx, $idno, $arr_split);
- $dtm_birth = $arr_split[2] . '/' . $arr_split[3] . '/' . $arr_split[4];
- // 检查生日日期是否正确
- if (!strtotime($dtm_birth)) {
- return false;
- } else {
- // 检验18位身份证的校验码是否正确。
- // 校验位按照ISO 7064:1983.MOD 11-2的规定生成,X可以认为是数字10。
- $arr_int = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
- $arr_ch = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
- $sign = 0;
- for ($i = 0; $i < 17; $i++) {
- $b = (int)$idno[$i];
- $w = $arr_int[$i];
- $sign += $b * $w;
- }
- $n = $sign % 11;
- $val_num = $arr_ch[$n];
- if ($val_num != substr($idno, 17, 1)) {
- return false;
- } else {
- return true;
- }
- }
- }
- }
- }
- if (!function_exists('is_empty')) {
- /**
- * 判断是否为空
- * @param $value 参数值
- * @return bool 返回结果true或false
- * @author laravel开发员
- * @date 2019/6/5
- */
- function is_empty($value)
- {
- // 判断是否存在该值
- if (!isset($value)) {
- return true;
- }
- // 判断是否为empty
- if (empty($value)) {
- return true;
- }
- // 判断是否为null
- if ($value === null) {
- return true;
- }
- // 判断是否为空字符串
- if (trim($value) === '') {
- return true;
- }
- // 默认返回false
- return false;
- }
- }
- if (!function_exists('mkdirs')) {
- /**
- * 递归创建目录
- * @param string $dir 需要创建的目录路径
- * @param int $mode 权限值
- * @return bool 返回结果true或false
- * @author laravel开发员
- * @date 2019/6/6
- */
- function mkdirs($dir, $mode = 0777)
- {
- if (is_dir($dir) || mkdir($dir, $mode, true)) {
- return true;
- }
- if (!mkdirs(dirname($dir), $mode)) {
- return false;
- }
- return mkdir($dir, $mode, true);
- }
- }
- if (!function_exists('rmdirs')) {
- /**
- * 删除文件夹
- * @param string $dir 文件夹路径
- * @param bool $rmself 是否删除本身true或false
- * @return bool 返回删除结果
- * @author laravel开发员
- * @date 2019/6/6
- */
- function rmdirs($dir, $rmself = true)
- {
- if (!is_dir($dir)) {
- return false;
- }
- $files = new RecursiveIteratorIterator(
- new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS),
- RecursiveIteratorIterator::CHILD_FIRST
- );
- foreach ($files as $file) {
- $todo = ($file->isDir() ? 'rmdir' : 'unlink');
- $todo($file->getRealPath());
- }
- if ($rmself) {
- @rmdir($dir);
- }
- return true;
- }
- }
- if (!function_exists('copydirs')) {
- /**
- * 复制文件夹
- * @param string $source 原文件夹路径
- * @param string $dest 目的文件夹路径
- * @author laravel开发员
- * @date 2019/6/6
- */
- function copydirs($source, $dest)
- {
- if (!is_dir($dest)) {
- mkdir($dest, 0755, true);
- }
- $iterator = new RecursiveIteratorIterator(
- new RecursiveDirectoryIterator($source, RecursiveDirectoryIterator::SKIP_DOTS),
- RecursiveIteratorIterator::SELF_FIRST
- );
- foreach ($iterator as $item) {
- if ($item->isDir()) {
- $sent_dir = $dest . "/" . $iterator->getSubPathName();
- if (!is_dir($sent_dir)) {
- mkdir($sent_dir, 0755, true);
- }
- } else {
- copy($item, $dest . "/" . $iterator->getSubPathName());
- }
- }
- }
- }
- if (!function_exists('lang')) {
- function lang($msg, $replace = [], $local = '')
- {
- $local = $local ? $local : config('app.locale');
- $data = __('api.' . $msg, $replace, $local);
- if (strpos($data, 'api', 0) !== false) {
- return __($msg, $replace, $local);
- }
- return __('api.' . $msg, $replace, $local);
- }
- }
- if (!function_exists('web_lang')) {
- function web_lang($msg, $replace = [], $local = '')
- {
- $local = $local? $local : session('locale_lang');
- $local = $local ? $local : config('app.locale');
- $data = __('api.' . $msg, $replace, $local);
- if (strpos($data, 'api', 0) !== false) {
- return __($msg, $replace, $local);
- }
- return __('api.' . $msg, $replace, $local);
- }
- }
- if (!function_exists('message')) {
- /**
- * 消息数组
- * @param string $msg 提示文字
- * @param bool $success 是否成功true或false
- * @param array $data 结果数据
- * @param int $code 编码
- * @return array 返回结果
- * @author laravel开发员
- * @date 2019/5/28
- */
- function message($msg = "操作成功", $success = true, $data = [], $code = 0, $type = 'json')
- {
- $result = ['success' => $success, 'msg' => lang($msg), 'data' => $data];
- if ($success) {
- $result['code'] = 0;
- } else {
- $result['code'] = $code ? $code : -1;
- }
- return $result;
- }
- }
- if (!function_exists('showJson')) {
- /**
- * 消息数组
- * @param string $msg 提示文字
- * @param bool $success 是否成功true或false
- * @param array $data 结果数据
- * @param int $code 编码
- * @return array 返回结果
- * @author laravel开发员
- * @date 2019/5/28
- */
- function showJson($msg = "操作成功", $success = true, $data = [], $code = 0, $type = 'json')
- {
- $result = ['success' => $success, 'msg' => web_lang($msg), 'data' => $data];
- if ($success) {
- $result['code'] = 0;
- } else {
- $result['code'] = $code ? $code : -1;
- }
- return $type=='json'?response()->json($result, 200, [])->setEncodingOptions(256):$result;
- }
- }
- if (!function_exists('num2rmb')) {
- /**
- * 数字金额转大写
- * @param float $num 金额
- * @return string 返回大写金额
- * @author laravel开发员
- * @date 2019/6/6
- */
- function num2rmb($num)
- {
- $c1 = "零壹贰叁肆伍陆柒捌玖";
- $c2 = "分角元拾佰仟万拾佰仟亿";
- $num = round($num, 2);
- $num = $num * 100;
- if (strlen($num) > 10) {
- return "oh,sorry,the number is too long!";
- }
- $i = 0;
- $c = "";
- while (1) {
- if ($i == 0) {
- $n = substr($num, strlen($num) - 1, 1);
- } else {
- $n = $num % 10;
- }
- $p1 = substr($c1, 3 * $n, 3);
- $p2 = substr($c2, 3 * $i, 3);
- if ($n != '0' || ($n == '0' && ($p2 == '亿' || $p2 == '万' || $p2 == '元'))) {
- $c = $p1 . $p2 . $c;
- } else {
- $c = $p1 . $c;
- }
- $i = $i + 1;
- $num = $num / 10;
- $num = (int)$num;
- if ($num == 0) {
- break;
- }
- }
- $j = 0;
- $slen = strlen($c);
- while ($j < $slen) {
- $m = substr($c, $j, 6);
- if ($m == '零元' || $m == '零万' || $m == '零亿' || $m == '零零') {
- $left = substr($c, 0, $j);
- $right = substr($c, $j + 3);
- $c = $left . $right;
- $j = $j - 3;
- $slen = $slen - 3;
- }
- $j = $j + 3;
- }
- if (substr($c, strlen($c) - 3, 3) == '零') {
- $c = substr($c, 0, strlen($c) - 3);
- } // if there is a '0' on the end , chop it out
- return $c . "整";
- }
- }
- if (!function_exists('object_array')) {
- /**
- * 对象转数组
- * @param $object 对象
- * @return mixed 返回结果
- * @author laravel开发员
- * @date 2019/5/23
- */
- function object_array($object)
- {
- //先编码成json字符串,再解码成数组
- return json_decode(json_encode($object), true);
- }
- }
- if (!function_exists('parse_attr')) {
- /**
- * 配置值解析成数组
- * @param string $value 参数值
- * @return array 返回结果
- * @author laravel开发员
- * @date 2019/6/6
- */
- function parse_attr($value = '')
- {
- if (is_array($value)) {
- return $value;
- }
- $array = preg_split('/[,;\r\n]+/', trim($value, ",;\r\n"));
- if (strpos($value, ':')) {
- $value = array();
- foreach ($array as $val) {
- list($k, $v) = explode(':', $val);
- $value[$k] = $v;
- }
- } else {
- $value = $array;
- }
- return $value;
- }
- }
- if (!function_exists('strip_html_tags')) {
- /**
- * 去除HTML标签、图像等 仅保留文本
- * @param string $str 字符串
- * @param int $length 长度
- * @return string 返回结果
- * @author laravel开发员
- * @date 2019/5/23
- */
- function strip_html_tags($str, $length = 0)
- {
- // 把一些预定义的 HTML 实体转换为字符
- $str = htmlspecialchars_decode($str);
- // 将空格替换成空
- $str = str_replace(" ", "", $str);
- // 函数剥去字符串中的 HTML、XML 以及 PHP 的标签,获取纯文本内容
- $str = strip_tags($str);
- $str = str_replace(array("\n", "\r\n", "\r"), ' ', $str);
- $preg = '/<script[\s\S]*?<\/script>/i';
- // 剥离JS代码
- $str = preg_replace($preg, "", $str, -1);
- if ($length == 2) {
- // 返回字符串中的前100字符串长度的字符
- $str = mb_substr($str, 0, $length, "utf-8");
- }
- return $str;
- }
- }
- if (!function_exists('sub_str')) {
- /**
- * 字符串截取
- * @param string $str 需要截取的字符串
- * @param int $start 开始位置
- * @param int $length 截取长度
- * @param bool $suffix 截断显示字符
- * @param string $charset 编码格式
- * @return string 返回结果
- * @author laravel开发员
- * @date 2019/5/23
- */
- function sub_str($str, $start = 0, $length = 10, $suffix = true, $charset = "utf-8")
- {
- if (function_exists("mb_substr")) {
- $slice = mb_substr($str, $start, $length, $charset);
- } elseif (function_exists('iconv_substr')) {
- $slice = iconv_substr($str, $start, $length, $charset);
- } else {
- $re['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
- $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
- $re['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
- $re['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
- preg_match_all($re[$charset], $str, $match);
- $slice = join("", array_slice($match[0], $start, $length));
- }
- $omit = mb_strlen($str) >= $length ? '...' : '';
- return $suffix ? $slice . $omit : $slice;
- }
- }
- if (!function_exists('save_image')) {
- /**
- * 保存图片
- * @param string $img_url 网络图片地址
- * @param string $save_dir 图片保存目录
- * @return string 返回路径
- * @author laravel开发员
- * @date 2019/5/23
- */
- function save_image($img_url, $save_dir = '/')
- {
- if (!$img_url) {
- return false;
- }
- $save_dir = trim($save_dir, "/");
- $imgExt = pathinfo($img_url, PATHINFO_EXTENSION);
- // 是否是本站图片
- if (strpos($img_url, IMG_URL) !== false) {
- // 是否是临时文件
- if (strpos($img_url, 'temp') === false) {
- return str_replace(IMG_URL, "", $img_url);
- }
- $new_path = create_image_path($save_dir, $imgExt);
- $old_path = str_replace(IMG_URL, ATTACHMENT_PATH, $img_url);
- if (!file_exists($old_path)) {
- return false;
- }
- rename($old_path, ATTACHMENT_PATH . $new_path);
- return $new_path;
- } else {
- // 保存远程图片
- $new_path = save_remote_image($img_url, $save_dir);
- }
- return $new_path;
- }
- }
- if (!function_exists('create_image_path')) {
- /**
- * 创建图片存储目录
- * @param string $save_dir 存储目录
- * @param string $image_ext 图片后缀
- * @param string $image_root 图片存储根目录路径
- * @return string 返回文件目录
- * @author laravel开发员
- * @date 2019/5/23
- */
- function create_image_path($save_dir = "", $image_ext = "", $image_root = IMG_PATH)
- {
- $image_dir = date("/Ymd/");
- if ($image_dir) {
- $image_dir = ($save_dir ? "/" : '') . $save_dir . $image_dir;
- }
- // 未指定后缀默认使用JPG
- if (!$image_ext) {
- $image_ext = "jpg";
- }
- $image_path = $image_root . $image_dir;
- if (!is_dir($image_path)) {
- // 创建目录并赋予权限
- mkdir($image_path, 0777, true);
- }
- $file_name = substr(md5(time() . rand(0, 999999)), 8, 16) . rand(100, 999) . ".{$image_ext}";
- $file_path = str_replace(ATTACHMENT_PATH, "", IMG_PATH) . $image_dir . $file_name;
- return $file_path;
- }
- }
- if (!function_exists('save_remote_image')) {
- /**
- * 保存网络图片到本地
- * @param string $img_url 网络图片地址
- * @param string $save_dir 保存目录
- * @return bool|string 图片路径
- * @author laravel开发员
- * @date 2019/5/23
- */
- function save_remote_image($img_url, $save_dir = '/')
- {
- $content = file_get_contents($img_url);
- if (!$content) {
- return false;
- }
- if ($content[0] . $content[1] == "\xff\xd8") {
- $image_ext = 'jpg';
- } elseif ($content[0] . $content[1] . $content[2] == "\x47\x49\x46") {
- $image_ext = 'gif';
- } elseif ($content[0] . $content[1] . $content[2] == "\x89\x50\x4e") {
- $image_ext = 'png';
- } else {
- // 不是有效图片
- return false;
- }
- $save_path = create_image_path($save_dir, $image_ext);
- return file_put_contents(ATTACHMENT_PATH . $save_path, $content) ? $save_path : false;
- }
- }
- if (!function_exists('save_image_content')) {
- /**
- * 富文本信息处理
- * @param string $content 富文本内容
- * @param bool $title 标题
- * @param string $path 图片存储路径
- * @return bool|int 返回结果
- * @author laravel开发员
- * @date 2020-04-21
- */
- function save_image_content(&$content, $title = false, $path = 'article')
- {
- // 图片处理
- preg_match_all("/<img.*?src=[\"|\']?(.*?)[\"|\']?\s.*?>/i", str_ireplace("\\", "", $content), $match);
- if ($match[1]) {
- foreach ($match[1] as $id => $val) {
- $save_image = save_image($val, $path);
- if ($save_image) {
- $content = str_replace($val, "[IMG_URL]" . $save_image, $content);
- }
- }
- }
- // 视频处理
- preg_match_all("/<embed .*?src=[\"|\']?(.*?)[\"|\']?\s.*?>/i", str_ireplace("\\", "", $content), $match2);
- if ($match2[1]) {
- foreach ($match2[1] as $vo) {
- $save_video = save_image($vo, $path);
- if ($save_video) {
- $content = str_replace($vo, "[IMG_URL]" . str_replace(ATTACHMENT_PATH, "", IMG_PATH) . $save_video, $content);
- }
- }
- }
- // 提示标签替换
- if ((strpos($content, 'alt=\"\"') !== false) && $title) {
- $content = str_replace('alt=\"\"', 'alt=\"' . $title . '\"', $content);
- }
- return true;
- }
- }
- if (!function_exists('upload_image')) {
- /**
- * 上传图片
- * @param $request 网络请求
- * @param string $form_name 文件表单名
- * @return array 返回结果
- * @author laravel开发员
- * @date 2019/5/23
- */
- function upload_image($request, $form_name = 'file', $file_path = '', $userId = 0)
- {
- // 检测请求中是否包含name=$form_name的上传文件
- if (!$request->hasFile($form_name)) {
- return message("请上传文件", false);
- }
- // 文件对象
- $file = $request->file($form_name);
- // 判断图片上传是否错误
- if (!$file->isValid()) {
- // 文件上传失败
- return message("上传文件验证失败", false);
- }
- // 文件原名
- $original_name = $file->getClientOriginalName();
- // 文件扩展名(文件后缀)
- $ext = $file->getClientOriginalExtension();
- // 临时文件的绝对路径
- $real_path = $file->getRealPath();
- // 文件类型
- $type = $file->getClientMimeType();
- // 文件大小
- $size = $file->getSize();
- // 文件大小校验
- if ($size > 5 * 1024 * 1024) {
- return message("文件大小超过了5M", false);
- }
- // 文件后缀校验
- $ext = strtolower($ext);
- $ext_arr = array('jpg', 'jpeg', 'png', 'gif');
- if (!in_array($ext, $ext_arr)) {
- return message("文件格式不正确:" . $ext, false);
- }
- // 文件路径
- $file_dir = ($file_path ? ATTACHMENT_PATH . '/images/' . $file_path : UPLOAD_TEMP_PATH) . "/" . date('Ymd');
- // 检测文件路径是否存在,不存在则创建
- if (!file_exists($file_dir)) {
- mkdir($file_dir, 0777, true);
- }
- // 文件名称
- $file_name = uniqid() . '.' . $ext;
- // 重命名保存
- $path = $file->move($file_dir, $file_name);
- // 文件临时路径
- $file_path = str_replace(ATTACHMENT_PATH, '', $file_dir) . '/' . $file_name;
- // 返回结果
- $result = [
- 'img_original_name' => $original_name,
- 'img_ext' => $ext,
- 'img_real_path' => $real_path,
- 'img_type' => $type,
- 'img_size' => $size,
- 'img_name' => $file_name,
- 'img_path' => $file_path,
- ];
- return message(MESSAGE_OK, true, $result);
- }
- }
- if (!function_exists('upload_file')) {
- /**
- * 上传文件
- * @param $request 网络请求
- * @param string $form_name 文件表单名
- * @return array 返回结果
- * @author laravel开发员
- * @date 2019/5/23
- */
- function upload_file($request, $form_name = 'file', $file_path = '', $userId = 0)
- {
- // 检测请求中是否包含上传的文件
- if (!$request->hasFile($form_name)) {
- return message("请上传文件");
- }
- // 文件对象
- $file = $request->file($form_name);
- // 判断图片上传是否错误
- if (!$file->isValid()) {
- // 文件上传失败
- return message("上传文件验证失败");
- }
- // 文件原名
- $original_name = $file->getClientOriginalName();
- // 文件扩展名(文件后缀)
- $ext = $file->getClientOriginalExtension();
- // 临时文件的绝对路径
- $real_path = $file->getRealPath();
- // 文件类型
- $type = $file->getClientMimeType();
- // 文件大小
- $size = $file->getSize();
- // 文件大小校验(MAX=5M)
- $file_max_size = 5 * 1024 * 1024;
- if ($size > $file_max_size) {
- return message("您上传的文件过大,最大值为" . $file_max_size / 1024 / 1024 . "MB");
- }
- // 允许上传的文件后缀
- $ext = strtolower($ext);
- $file_exts = array('xls', 'xlsx', 'csv');
- if (!in_array($ext, $file_exts)) {
- return message("文件格式不正确:" . $ext);
- }
- // 文件路径
- $file_dir = ($file_path ? ATTACHMENT_PATH . '/images/' . $file_path : UPLOAD_TEMP_PATH) . "/" . date('Ymd');
- // 检测文件路径是否存在,不存在则创建
- if (!file_exists($file_dir)) {
- mkdir($file_dir, 0777, true);
- }
- // 文件名称
- $file_name = uniqid() . '.' . $ext;
- // 重命名保存
- $path = $file->move($file_dir, $file_name);
- // 文件临时路径
- $file_path = str_replace(IMG_PATH, '', $file_dir) . '/' . $file_name;
- // 返回结果
- $result = [
- 'file_original_name' => $original_name,
- 'file_ext' => $ext,
- 'file_real_path' => $real_path,
- 'file_type' => $type,
- 'file_size' => $size,
- 'file_name' => $file_name,
- 'file_path' => $file_path,
- ];
- return message(MESSAGE_OK, true, $result);
- }
- }
- if (!function_exists('upload_video')) {
- /**
- * 上传视频文件
- * @param $request 网络请求
- * @param string $form_name 文件表单名
- * @return array 返回结果
- * @author laravel开发员
- * @date 2019/5/23
- */
- function upload_video($request, $form_name = 'file', $file_path = '', $userId = 0)
- {
- // 检测请求中是否包含上传的文件
- if (!$request->hasFile($form_name)) {
- return message("请上传文件");
- }
- // 文件对象
- $file = $request->file($form_name);
- // 判断图片上传是否错误
- if (!$file->isValid()) {
- // 文件上传失败
- return message("上传文件验证失败");
- }
- // 文件原名
- $original_name = $file->getClientOriginalName();
- // 文件扩展名(文件后缀)
- $ext = $file->getClientOriginalExtension();
- // 临时文件的绝对路径
- $real_path = $file->getRealPath();
- // 文件类型
- $type = $file->getClientMimeType();
- // 文件大小
- $size = $file->getSize();
- // 文件大小校验(MAX=100M)
- $file_max_size = 50 * 1024 * 1024;
- if ($size > $file_max_size) {
- return message("您上传的文件过大,最大值为" . $file_max_size / 1024 / 1024 . "MB");
- }
- // 允许上传的文件后缀
- $ext = strtolower($ext);
- $file_exts = array('mp4', 'wmv', 'mov','flv');
- if (!in_array($ext, $file_exts)) {
- return message("视频文件格式不正确:" . $ext);
- }
- // 文件路径
- $file_dir = ($file_path ? ATTACHMENT_PATH . '/videos/' . $file_path : ATTACHMENT_PATH . '/videos/files') . "/" . date('Ymd');
- // 检测文件路径是否存在,不存在则创建
- if (!file_exists($file_dir)) {
- mkdir($file_dir, 0777, true);
- }
- // 文件名称
- $file_name = uniqid() . '.' . $ext;
- // 重命名保存
- $path = $file->move($file_dir, $file_name);
- // 文件临时路径
- $file_path = str_replace(ATTACHMENT_PATH, '', $file_dir) . '/' . $file_name;
- // 返回结果
- $result = [
- 'file_original_name' => $original_name,
- 'file_ext' => $ext,
- 'file_real_path' => $real_path,
- 'file_type' => $type,
- 'file_size' => $size,
- 'file_name' => $file_name,
- 'file_path' => $file_path,
- ];
- return message(MESSAGE_OK, true, $result);
- }
- }
- if (!function_exists('upload_base64')) {
- /**
- * 上传文件
- * @param $base64 base64内容
- * @return array 返回结果
- * @author laravel开发员
- * @date 2019/5/23
- */
- function upload_base64($base64, $fileType='jpeg', $file_path = '', $userId = 0)
- {
- // 检测请求中是否包含上传的文件
- if (empty($base64)) {
- return false;
- }
- // 文件路径
- $file_dir = ($file_path ? ATTACHMENT_PATH . '/base64/' . $file_path : ATTACHMENT_PATH . '/base64/files') . "/" . date('Ymd');
- // 检测文件路径是否存在,不存在则创建
- if (!file_exists($file_dir)) {
- mkdir($file_dir, 0777, true);
- }
- // 文件名称
- $file_name = uniqid() . '.' . $fileType;
- // 保存文件内容
- $base64 = str_replace('data:image/jpeg;base64,','',$base64);
- file_put_contents($file_dir.'/'.$file_name,base64_decode($base64));
- if(!file_exists($file_dir.'/'.$file_name)){
- return false;
- }
- // 文件临时路径
- $file_path = str_replace(ATTACHMENT_PATH, '', $file_dir) . '/' . $file_name;
- // 返回结果
- $result = [
- 'file_ext' => $fileType,
- 'file_type' => $fileType,
- 'file_name' => $file_name,
- 'file_path' => $file_path,
- ];
- return $result;
- }
- }
- if (!function_exists('upload_remote_image')) {
- /**
- * 上传保存网络文件
- * @param $request 网络请求
- * @return array 返回结果
- * @author laravel开发员
- * @date 2019/5/23
- */
- function upload_remote_image($url, $fileType='jpg', $file_path = '', $userId = 0)
- {
- // 检测请求中是否包含上传的文件
- if (empty($url)) {
- return false;
- }
- // 文件路径
- $file_dir = ($file_path ? ATTACHMENT_PATH . '/remotes/' . $file_path : ATTACHMENT_PATH . '/remotes/files') . "/" . date('Ymd');
- // 检测文件路径是否存在,不存在则创建
- if (!file_exists($file_dir)) {
- mkdir($file_dir, 0777, true);
- }
- // 文件名称
- $file_name = uniqid() . '.' . $fileType;
- // 保存文件内容
- $content = file_get_contents($url);
- if(!$content){
- return false;
- }
- file_put_contents($file_dir.'/'.$file_name, $content);
- if(!file_exists($file_dir.'/'.$file_name)){
- return false;
- }
- // 文件临时路径
- $file_path = str_replace(ATTACHMENT_PATH, '', $file_dir) . '/' . $file_name;
- // 返回结果
- $result = [
- 'file_ext' => $fileType,
- 'file_type' => $fileType,
- 'file_name' => $file_name,
- 'file_path' => $file_path,
- ];
- return $result;
- }
- }
- if (!function_exists('widget')) {
- /**
- * 加载系统组件,传入的名字会以目录和类名区别
- * 如Home.Common就代表Widget目录下的Home/Common.php这个widget。
- * @param $widgetName 组件名称
- * @return bool|mixed
- * @author laravel开发员
- * @date 2019/5/23
- */
- function widget($widgetName)
- {
- $widgetNameEx = explode('.', $widgetName);
- if (!isset($widgetNameEx[1])) {
- return false;
- }
- $widgetClass = 'App\\Widget\\' . $widgetNameEx[0] . '\\' . $widgetNameEx[1];
- if (app()->bound($widgetName)) {
- return app()->make($widgetName);
- }
- app()->singleton($widgetName, function () use ($widgetClass) {
- return new $widgetClass();
- });
- return app()->make($widgetName);
- }
- }
- if (!function_exists('get_tree')) {
- function get_tree($datas, $pid = 0, $tempArr = [])
- {
- if (empty($datas)) {
- return false;
- }
- foreach ($datas as $key => $v) {
- $data = [];
- $id = isset($v['id']) ? $v['id'] : 0;
- $currentPid = isset($v['parent_id']) ? $v['parent_id'] : 0;
- $nickname = isset($v['nickname']) ? $v['nickname'] : '';
- $username = isset($v['username']) ? $v['username'] : '';
- $mobile = isset($v['mobile']) ? $v['mobile'] : '';
- $data['id'] = $v['id'];
- $data['parent_id'] = $v['parent_id'];
- $data['mobile'] = $mobile;
- $data['label'] = $nickname . ($mobile ? "({$mobile})" : "");
- if ($currentPid && $currentPid == $pid) {
- $data['children'] = get_tree($datas, $v['id'], []);
- $tempArr[] = $data;
- } else if ($currentPid == 0 && $pid == 0) {
- $data['children'] = get_tree($datas, $v['id'], []);
- $tempArr[$id] = $data;
- }
- }
- return array_values($tempArr);
- }
- }
- if (!function_exists('getSign')) {
- function getSign($data, $key = '')
- {
- if (!is_array($data) || empty($data)) {
- return false;
- }
- $str = arrayToStr($data);
- // 拼接密钥
- $str .= '&key=' . ($key ? $key : md5(env('APP_SIGN_KEY', 'dysapp')));
- $str = htmlspecialchars_decode($str);
- // MD5 运算
- // var_dump($str);
- $sign = md5($str);
- $sign .= substr($sign, 6, 4);
- $sign = strtoupper($sign);
- return $sign;
- }
- }
- // 获取图片数组预览
- if (!function_exists('get_images_preview')) {
- /**
- * @param $urls 图片数组
- * @param string $keyName 图片地址键名,空则无键名
- * @return false
- */
- function get_images_preview($urls, $keyName = 'url', $domain='')
- {
- if (empty($urls)) {
- return [];
- }
- $urls = is_array($urls)? $urls : json_decode($urls, true);
- foreach ($urls as &$item) {
- if ($keyName) {
- $item[$keyName] = get_image_url($item[$keyName], $domain);
- } else {
- $item = get_image_url($item, $domain);
- }
- }
- unset($item);
- return $urls;
- }
- }
- if (!function_exists('arrayToStr')) {
- function arrayToStr($params)
- {
- $string = '';
- if (!empty($params)) {
- ksort($params, SORT_REGULAR);
- $array = array();
- foreach ($params as $key => &$value) {
- if($key != 'sign'){
- // 过滤无法匹配校验的参数类型
- if ($value !== false && $value != 'undefined' && !is_array($value) && !preg_match("/^[0-9]{1,9}\.[0-9]{8,}$/", $value)) {
- $array[] = $key . '=' . trim($value);
- }
- }
- }
- $string = implode("&", $array);
- }
- return $string;
- }
- }
- if (!function_exists('arrayToUrl')) {
- function arrayToUrl($params)
- {
- $string = '';
- if (!empty($params)) {
- ksort($params, SORT_REGULAR);
- $array = array();
- foreach ($params as $key => $value) {
- if ($value != 'undefined') {
- $array[] = $key . '=' . (is_array($value) ? json_encode($value,JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE) : $value);
- }
- }
- $string = implode("&", $array);
- }
- return $string;
- }
- }
- if (!function_exists('httpRequest')) {
- /**
- *
- * 接口请求
- * @param $url 接口地址
- * @param $data
- * @param $type
- * @param int $timeout
- * @return mixed
- * @author wesmiler
- */
- function httpRequest($url, $data = '', $type = 'post', $cookie = '', $timeout = 60, $header=[],$dataType='array')
- {
- try {
- set_time_limit($timeout);
- $data = $data && is_array($data) ? http_build_query($data) : $data;
- $url = strtolower($type) == 'get' ? $url . (strpos($url, '?') === false ? '?' : '&') . $data : $url;
- $ch = curl_init($url);
- if (!empty($cookie)) {
- curl_setopt($ch, CURLOPT_COOKIE, $cookie);
- }
- if ($type == 'post') {
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
- }
- if ($header) {
- curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
- } else {
- $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'];
- curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
- }
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //禁止 cURL 验证对等证书
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); //是否检测服务器的域名与证书上的是否一致
- curl_setopt($ch, CURLOPT_HEADER, 0);
- curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
- curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
- $ret = curl_exec($ch);
- $ret = trim($ret);
- curl_close($ch);
- if($dataType != 'array'){
- return $ret;
- }
- if (!preg_match("/^{/", $ret)) {
- return ['code' => 'err', 'msg' => $ret];
- }
- $retArr = $ret ? json_decode($ret, true) : ['code' => 'err', 'msg' => 'unknow error'];
- if (empty($retArr)) {
- $ret = iconv('gb2312', 'utf-8', $ret);
- $retArr = json_decode($ret, true);
- }
- } catch (\Exception $exception) {
- $retArr = ['code' => 'error', 'msg' => 'request error'];
- }
- return $retArr;
- }
- }
- if (!function_exists('getVersion')) {
- /**
- * 获取版本号
- * @param $version
- * @return string|string[]
- */
- function getVersion($version)
- {
- return $version ? intval(str_replace('.', '', $version)) : '';
- }
- }
- if (!function_exists('formatDistance')) {
- /**
- * 格式化距离
- * @param $distance
- * @return string
- */
- function formatDistance($distance)
- {
- return ($distance > 1000 ? round($distance / 1000, 2) . 'km' : $distance . 'm');
- }
- }
- if (!function_exists('moneyFormat')) {
- /**
- * 金额格式化
- * @param $money
- * @param int $decimal 小数位数,默认2
- * @param int $round 是否四舍五入
- * @return string
- */
- function moneyFormat($money, $decimal = 2, $round = false, $charset = 'utf-8')
- {
- if ($round) {
- $money = number_format($money, $decimal);
- } else {
- $money = round($money, $decimal + 1);
- $data = explode('.', $money);
- $money = isset($data[0]) ? $data[0] : 0;
- $float = isset($data[1]) ? $data[1] : '';
- $len = $float ? mb_strlen($float, $charset) : 0;
- $decimal = $decimal >= 0 ? intval($decimal) : 2;
- $num1 = isset($data[1]) ? $data[1] : '';
- $float = $len >= $decimal ? mb_substr($num1, 0, $decimal, $charset) : $float . str_repeat('0', $decimal - $len);
- $money = $money . '.' . $float;
- $money = number_format($money, $decimal, '.', '');
- }
- return $money;
- }
- }
- if(!function_exists('getChatKey')){
- /**
- * 获取聊天窗口KEY
- * @param $fromUserId
- * @param $toUserId
- * @return false|string
- */
- function getChatKey($fromUserId, $toUserId)
- {
- if(empty($fromUserId) || empty($toUserId)){
- return false;
- }
- $ids = [$fromUserId, $toUserId];
- asort($ids);
- return implode('_', $ids);
- }
- }
- if(!function_exists('getDistance')) {
- /**
- * 计算地图坐标距离
- * @param $lat
- * @param $lng
- * @param $toLat
- * @param $toLng
- * @return float
- */
- function getDistance($lat, $lng, $toLat, $toLng)
- {
- return ROUND(6378.138 * 2 * ASIN(
- SQRT(
- POW(
- SIN(
- ($lat * PI() / 180 - $toLat * PI() / 180
- ) / 2
- ),
- 2
- ) + COS($lat * PI() / 180) * COS($toLat * PI() / 180) * POW(
- SIN(
- ($lng * PI() / 180 - $toLng * PI() / 180
- ) / 2
- ), 2))) * 1000);
- }
- }
- if(!function_exists('getParents')){
- /**
- * 获取分销上级用户ID
- * @param $parents
- * @return array|false
- */
- function getParents($parents, $level=3){
- $parents = $parents? explode(',', $parents) : [];
- $parents = array_filter($parents);
- if(empty($parents)){
- return false;
- }
- $parents = array_slice($parents, -$level);
- return array_reverse($parents);
- }
- }
- if(!function_exists('getMillSecondTime')){
- /**
- * 获取毫秒时间戳
- * @return float
- */
- function getMillSecondTime() {
- list($msec, $sec) = explode(' ', microtime());
- return (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
- }
- }
- if(!function_exists('isValidUrl')){
- function isValidUrl($url) {
- if (filter_var($url, FILTER_VALIDATE_URL)) {
- return true;
- } else {
- return false;
- }
- }
- }
|