123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225 |
- <?php
- namespace App\Common;
-
- use App\Http\Requests;
- use Illuminate\Support\Facades\Log;
-
- class Api{
-
- /**
- * 接口返回信息json转化
- * @param null $code
- * @param null $msg
- * @param array $data
- * @return string
- */
- public static function json($code=NULL, $msg=NULL, $data=array()){
- if(isset($code)){
- $res['error'] = $code;
- }
- if($msg){
- $res['message'] = $msg;
- }
-
- if(empty($data)){
- $res['data'] = [];
- } else {
- $res['data'] = $data;
- }
-
-
- return json_encode($res);
- }
-
- /**
- * 分页数据格式化
- * @param $info
- * @return array
- */
- public static function page($info){
- $info = collect($info)->toArray();
-
- $data = [];
- $data['total'] = $info['total'];
- $data['total_page'] = $info['last_page'];
- $data['page'] = $info['current_page'];
- $data['page_size'] = $info['per_page'];
- $data['data'] = $info['data'];
-
- return $data;
- }
-
- /**
- * 接口数据数组化
- * @param $code
- * @param $msg
- * @param string $data
- * @return array
- */
- public static function arr($code, $msg, $data=''){
- $res = [];
- if(isset($code)){
- $res['code'] = $code;
- }
- if($msg){
- $res['msg'] = $msg;
- }
- $res['data'] = $data;
- return $res;
- }
-
- /**
- * 日期时间数据格式化
- * @param $data
- * @return mixed
- */
- public static function dateFormat($data){
- collect($data)->toArray();
-
- if(isset($data['update_time']) && !empty($data['update_time'])){
- $data['update_time'] = date('Y-m-d H:i:s', $data['update_time']);
- }
- if(isset($data['create_time']) && !empty($data['create_time'])){
- $data['create_time'] = date('Y-m-d H:i:s', $data['create_time']);
- }
- if(isset($data['start_time']) && !empty($data['start_time'])){
- $data['start_time'] = date('Y-m-d H:i:s', $data['start_time']);
- }
- if(isset($data['end_time']) && !empty($data['end_time'])){
- $data['end_time'] = date('Y-m-d H:i:s', $data['end_time']);
- }
- return $data;
- }
-
- /**
- * 请求外部链接
- * @param $url
- * @param string $post
- */
- public static function curl($url, $post=''){
- $ch = curl_init();
- $timeout = 5;
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
-
- if($post){
- curl_setopt($ch, CURLOPT_POST,true);
- }
-
- $contents = curl_exec($ch);
- curl_close($ch);
-
- return $contents;
- }
-
- /**
- * 去除字符串中的空格
- * @param $str
- * @return mixed
- */
- public static function trimAll($str){
- $null_str=array(" "," ","\t","\n","\r");
- $replace_str=array("","","","","");
- return str_replace($null_str,$replace_str,$str);
- }
-
- /**
- * 服务器错误处理
- * @param $e
- * @return string
- */
- public static function serviceError($e){
- Log::info($e);
- return self::json(config('code.service_error'), trans('msg.service_error'));
- }
-
- /**
- * unicode转码为utf-8
- * 用于去除电话号码中隐藏的unicode编码
- * @param $name
- * @return string
- */
- public static function unicode_decode($ostr)
- {
- $json = '{"str":"'.$ostr.'"}';
- $arr = json_decode($json,true);
- if(empty($arr)) return '';
-
- preg_match_all('/[\x{FF00}-\x{FFEF}|\x{0000}-\x{00ff}|\x{4e00}-\x{9fff}]+/u', $ostr, $matches);
- $str = join('', $matches[0]);
- if($str==''){ //含有特殊字符需要逐個處理
- $returnstr = '';
- $i = 0;
- $str_length = strlen($ostr);
- while ($i<=$str_length){
- $temp_str = substr($ostr, $i, 1);
- $ascnum = Ord($temp_str);
- if ($ascnum>=224){
- $returnstr = $returnstr.substr($ostr, $i, 3);
- $i = $i + 3;
- }elseif ($ascnum>=192){
- $returnstr = $returnstr.substr($ostr, $i, 2);
- $i = $i + 2;
- }elseif ($ascnum>=65 && $ascnum<=90){
- $returnstr = $returnstr.substr($ostr, $i, 1);
- $i = $i + 1;
- }elseif ($ascnum>=128 && $ascnum<=191){ // 特殊字符
- $i = $i + 1;
- }else{
- $returnstr = $returnstr.substr($ostr, $i, 1);
- $i = $i + 1;
- }
- }
- $str = $returnstr;
- preg_match_all('/[\x{FF00}-\x{FFEF}|\x{0000}-\x{00ff}|\x{4e00}-\x{9fff}]+/u', $str, $matches);
- $str = join('', $matches[0]);
- }
- return $str;
- //return $arr['str'];
- }
-
- /**
- * 替换不见unicode字符
- * 用于去除guid和部门导入时的特殊字符
- * 添加保留符号时在中括号中添加
- * @param $str
- * @return mixed
- */
- public static function trimUnicode($str){
- $str = self::setFilter($str);
- $res = preg_replace('#[^\x{4e00}-\x{9fa5}A-Za-z0-9\-\_\(\)\[\]\{\}\<\>\*\=\+]#u','',$str);
- return $res;
- }
-
- /**
- * 全半角转换
- * @param $html
- * @return string
- */
- public static function setFilter($html){
- $arr = array('0' => '0', '1' => '1', '2' => '2', '3' => '3', '4' => '4',
- '5' => '5', '6' => '6', '7' => '7', '8' => '8', '9' => '9',
- 'A' => 'A', 'B' => 'B', 'C' => 'C', 'D' => 'D', 'E' => 'E',
- 'F' => 'F', 'G' => 'G', 'H' => 'H', 'I' => 'I', 'J' => 'J',
- 'K' => 'K', 'L' => 'L', 'M' => 'M', 'N' => 'N', 'O' => 'O',
- 'P' => 'P', 'Q' => 'Q', 'R' => 'R', 'S' => 'S', 'T' => 'T',
- 'U' => 'U', 'V' => 'V', 'W' => 'W', 'X' => 'X', 'Y' => 'Y',
- 'Z' => 'Z', 'a' => 'a', 'b' => 'b', 'c' => 'c', 'd' => 'd',
- 'e' => 'e', 'f' => 'f', 'g' => 'g', 'h' => 'h', 'i' => 'i',
- 'j' => 'j', 'k' => 'k', 'l' => 'l', 'm' => 'm', 'n' => 'n',
- 'o' => 'o', 'p' => 'p', 'q' => 'q', 'r' => 'r', 's' => 's',
- 't' => 't', 'u' => 'u', 'v' => 'v', 'w' => 'w', 'x' => 'x',
- 'y' => 'y', 'z' => 'z',
- '(' => '(', ')' => ')', '〔' => '[', '〕' => ']', '【' => '[',
- '】' => ']', '〖' => '[', '〗' => ']', '“' => '[', '”' => ']',
- '‘' => '[', '’' => ']', '{' => '{', '}' => '}', '《' => '<',
- '》' => '>',
- '%' => '%', '+' => '+', '—' => '-', '-' => '-', '~' => '-',
- ':' => ':', '。' => '.', '、' => ',', ',' => '.', '、' => '.',
- ';' => ',', '?' => '?', '!' => '!', '…' => '-', '‖' => '|',
- '”' => '"', '’' => '`', '‘' => '`', '|' => '|', '〃' => '"',
- ' ' => ' ');
- return strtr($html, $arr);
- }
- }?>
|