| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764 |
- <?php
- // +----------------------------------------------------------------------
- // | Laravel框架 [ Laravel ]
- // +----------------------------------------------------------------------
- // | 版权所有 2017~2021 Laravel研发中心
- // +----------------------------------------------------------------------
- // | 官方网站: http://www.laravel.cn
- // +----------------------------------------------------------------------
- // | Author: wesmiler <12345678@qq.com>
- // +----------------------------------------------------------------------
- // 此文件为系统框架核心公共函数文件,为了系统的稳定与安全,未经允许不得擅自改动
- if (!function_exists('array_sort')) {
- /**
- * 数组排序
- * @param array $arr 数据源
- * @param $keys KEY
- * @param bool $desc 排序方式(默认:asc)
- * @return array 返回结果
- * @author wesmiler
- * @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 wesmiler
- * @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 wesmiler
- * @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 wesmiler
- * @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 wesmiler
- * @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 wesmiler
- * @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 wesmiler
- * @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 wesmiler
- * @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 wesmiler
- * @date 2019/6/6
- */
- function curl_request($url, $data = [], $type = 'post', $timeout=10, $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, $timeout);
- // 设置超时时间
- curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
- // 是否要求返回数据
- 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('httpRequest')) {
- /**
- *
- * 接口请求
- * @author wesmiler
- * @param $url 接口地址
- * @param $data
- * @param $type
- * @param int $timeout
- * @return mixed
- */
- function httpRequest($url, $data = '', $type = 'post', $cookie = '', $timeout = 60)
- {
- 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);
- }
- 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);
- curl_close($ch);
- if (!preg_match("/^{/", $ret)) {
- return ['code' => 'err', 'msg' => $ret];
- }
- $ret = $ret ? json_decode($ret, true) : ['code' => 'err', 'msg' => 'unknow error'];
- } catch (\Exception $exception) {
- $ret = ['code' => 'error', 'msg' => 'request error'];
- }
- return $ret;
- }
- }
- /**
- * HTTP请求
- * @param $url 链接
- * @param $data 提交数据
- * @param string $type 请求类型:get post
- * @param string $dataType 返回数据类型 array json
- * @param int $timeout
- * @return mixed
- */
- function httpHeaderRequest($url, $data=[], $type='post', $headers=[], $dataType='array', $timeout=60){
- $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($headers){
- curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
- }
- curl_setopt($ch, CURLOPT_POST, 1);
- 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_SSL_VERIFYPEER, 0); // 对认证证书来源的检查,0-规避ssl的证书检查
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
- curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
- $ret = curl_exec($ch);
- curl_close($ch);
- if(strtolower($dataType) == 'array' && !is_array($ret)){
- $ret = json_decode($ret, true);
- }
- return $ret;
- }
- if (!function_exists('datetime')) {
- /**
- * 格式化日期函数
- * @param $time 时间戳
- * @param string $format 输出日期格式
- * @return string 返回格式化的日期
- * @author wesmiler
- * @date 2019/5/23
- */
- function datetime($time, $format = 'Y-m-d H:i:s')
- {
- $time = is_numeric($time) ? $time : strtotime($time);
- return date($format, $time);
- }
- }
- if (!function_exists('data_auth_sign')) {
- /**
- * 数据签名认证
- * @param $data 数据源
- * @return string
- * @author wesmiler
- * @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 wesmiler
- * @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 wesmiler
- * @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 wesmiler
- * @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 wesmiler
- * @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 wesmiler
- * @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_bytes')) {
- /**
- * 将字节转换为可读文本
- * @param int $size 字节大小
- * @param string $delimiter 分隔符
- * @return string 返回结果
- * @author wesmiler
- * @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 wesmiler
- * @date 2019/5/23
- */
- function format_yuan($money = 0)
- {
- if ($money > 0) {
- return number_format($money / 100, 2, ".", "");
- }
- return "0.00";
- }
- }
- if(!function_exists('makeUniqueCode')){
- /**
- * 生成邀请码
- * @param int $len 长度
- * @return false|string
- */
- function makeUniqueCode($str='',$len=6){
- return substr(base_convert(md5(uniqid(md5(microtime(true).$str),true)), 16, 10), 0, $len);
- }
- }
- if (!function_exists('format_cent')) {
- /**
- * 以元为单位的金额转化成分
- * @param $money 金额
- * @return string 返回格式化的金额
- * @author wesmiler
- * @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 wesmiler
- * @date 2019/5/23
- */
- function format_bank_card($card_no, $is_format = true)
- {
- 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 wesmiler
- * @date 2019/5/23
- */
- function format_mobile($mobile)
- {
- return substr($mobile, 0, 5) . "****" . substr($mobile, 9, 2);
- }
- }
- if (!function_exists('get_random_code')) {
- /**
- * 获取指定位数的随机码
- * @param int $num 随机码长度
- * @return string 返回字符串
- * @author wesmiler
- * @date 2019/5/23
- */
- function get_random_code($num = 12)
- {
- $codeSeeds = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
- $codeSeeds .= "abcdefghijklmnopqrstuvwxyz";
- $codeSeeds .= "0123456789_";
- $len = strlen($codeSeeds);
- $code = "";
- for ($i = 0; $i < $num; $i++) {
- $rand = rand(0, $len - 1);
- $code .= $codeSeeds[$rand];
- }
- return $code;
- }
- }
- if (!function_exists('get_password')) {
- /**
- * 获取双MD5加密密码
- * @param string $password 加密字符串
- * @return string 输出MD5加密字符串
- * @author wesmiler
- * @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 wesmiler
- * @date 2019/5/23
- */
- function get_order_num($prefix = '')
- {
- $micro = substr(microtime(), 2, 3);
- return $prefix . date("YmdHis") . $micro . rand(100, 999);
- }
- }
- if (!function_exists('getter')) {
- /**
- * 获取数组的下标值
- * @param array $data 数据源
- * @param string $field 字段名称
- * @param string $default 默认值
- * @return mixed|string 返回结果
- * @author wesmiler
- * @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 wesmiler
- * @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 返回图片网络地址
- * @author wesmiler
- * @date 2019/6/6
- */
- function get_image_url($image_url)
- {
- return strpos($image_url,'http') === false? IMG_URL . $image_url : $image_url;
- }
- }
- if (!function_exists('get_file_url')) {
- /**
- * 获取文件地址
- * @param $file_url 文件地址
- * @return string 返回图片网络地址
- * @author wesmiler
- * @date 2019/6/6
- */
- function get_file_url($file_url)
- {
- return strpos($file_url,'http') === false? IMG_URL . $file_url : $file_url;
- }
- }
- if (!function_exists('get_hash')) {
- /**
- * 获取HASH值
- * @return string 返回hash字符串
- * @author wesmiler
- * @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 wesmiler
- * @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 wesmiler
- * @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('formatName')) {
- /**
- * 格式化账号
- * @param $name
- */
- function formatName($name)
- {
- if(mb_strlen($name,'utf-8')<=6){
- return mb_substr($name, 0, 1,'utf-8') . '****' . mb_substr($name, -1, 1,'utf-8');
- }else{
- return mb_substr($name, 0, 2,'utf-8') . '****' . mb_substr($name, -2, 2,'utf-8');
- }
- }
- }
- if (!function_exists('moneyFormat')) {
- /**
- * 金钱格式化小数点
- * @param $money
- * @param int $decimal 小数点位数,默认系统配置2位
- * @return float
- */
- function moneyFormat($money, $decimal = null)
- {
- $formatConfig = [];
- $charset = !empty($formatConfig['charset']) ? trim($formatConfig['charset']) : 'utf-8';
- if ($decimal === null) {
- $decimal = isset($formatConfig['moneyDecimal']) ? intval($formatConfig['moneyDecimal']) : 2;
- }
- $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;
- return number_format($money, $decimal, '.', '');
- }
- }
- if (!function_exists('get_guid_v4')) {
- /**
- * 获取唯一性GUID
- * @param bool $trim 是否去除{}
- * @return string 返回GUID字符串
- * @author wesmiler
- * @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 wesmiler
- * @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 wesmiler
- * @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 wesmiler
- * @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 wesmiler
- * @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 wesmiler
- * @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 wesmiler
- * @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 wesmiler
- * @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 wesmiler
- * @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('message')) {
- /**
- * 消息数组
- * @param string $msg 提示文字
- * @param bool $success 是否成功true或false
- * @param array $data 结果数据
- * @param int $code 编码
- * @return array 返回结果
- * @author wesmiler
- * @date 2019/5/28
- */
- function message($msg = "操作成功", $success = true, $data = [], $code = 0)
- {
- $msg = is_int($msg)? lang($msg, 'api') : $msg;
- $result = ['success' => $success, 'msg' => $msg, 'data' => $data];
- if ($success) {
- $result['code'] = 0;
- } else {
- $result['code'] = $code ? $code : -1;
- }
- return $result;
- }
- }
- if (!function_exists('getValidatorError')) {
- /**
- * 错误验证
- * @param $error
- * @return mixed
- */
- function getValidatorError($validator)
- {
- $errors = $validator->errors()->all();
- return isset($errors[0]) ? $errors[0] : 'validator error';
- }
- }
- if (!function_exists('getDateText')) {
- /**
- * 获取实际日期格式
- * @param $error
- * @return mixed
- */
- function getDateText($date, $format='Y-m-d')
- {
- $dateText = str_replace(['年','月','日'],['-','-',''], $date);
- if(!preg_match("/年/", $date)){
- $dateText = date('Y-').$dateText;
- }
- return date($format, strtotime($dateText));
- }
- }
- if (!function_exists('lang')) {
- /**
- * 语言转换
- * @param $value
- * @return array|\Illuminate\Contracts\Translation\Translator|null|string
- */
- function lang($value, $app = 'api')
- {
- $value = $app ? $app . '.' . $value : $value;
- return trim(trans($value), $app . '.');
- }
- }
- if (!function_exists('num2rmb')) {
- /**
- * 数字金额转大写
- * @param float $num 金额
- * @return string 返回大写金额
- * @author wesmiler
- * @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 wesmiler
- * @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 wesmiler
- * @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 wesmiler
- * @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 wesmiler
- * @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 wesmiler
- * @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 wesmiler
- * @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 wesmiler
- * @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(IMG_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 wesmiler
- * @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 wesmiler
- * @date 2019/5/23
- */
- function upload_image($request, $form_name = 'file')
- {
- // 检测请求中是否包含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_arr = array('jpg', 'jpeg', 'png', 'gif');
- if (!in_array($ext, $ext_arr)) {
- return message("文件格式不正确", false);
- }
- // 文件路径
- $file_dir = 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 wesmiler
- * @date 2019/5/23
- */
- function upload_file($request, $form_name = 'file')
- {
- // 检测请求中是否包含上传的文件
- 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();
- // 文件大小校验(MAX=5M)
- $file_max_size = 50 * 1024 * 1024;
- if ($size > $file_max_size) {
- return message("您上传的文件过大,最大值为" . $file_max_size / 1024 / 1024 . "MB", false);
- }
- // 允许上传的文件后缀
- $file_exts = array('xls', 'xlsx', 'csv','mp3','mp4','WMA','MPEG');
- if (!in_array($ext, $file_exts)) {
- return message("文件格式不正确", false);
- }
- // 文件路径
- $file_dir = UPLOAD_FILE_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 = [
- '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('widget')) {
- /**
- * 加载系统组件,传入的名字会以目录和类名区别
- * 如Home.Common就代表Widget目录下的Home/Common.php这个widget。
- * @param $widgetName 组件名称
- * @return bool|mixed
- * @author wesmiler
- * @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);
- }
- }
|