Api.php 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. namespace App\Common;
  3. use App\Http\Requests;
  4. use Illuminate\Support\Facades\Log;
  5. class Api{
  6. /**
  7. * 接口返回信息json转化
  8. * @param null $code
  9. * @param null $msg
  10. * @param array $data
  11. * @return string
  12. */
  13. public static function json($code=NULL, $msg=NULL, $data=array()){
  14. if(isset($code)){
  15. $res['error'] = $code;
  16. }
  17. if($msg){
  18. $res['message'] = $msg;
  19. }
  20. if(empty($data)){
  21. $res['data'] = [];
  22. } else {
  23. $res['data'] = $data;
  24. }
  25. return json_encode($res);
  26. }
  27. /**
  28. * 分页数据格式化
  29. * @param $info
  30. * @return array
  31. */
  32. public static function page($info){
  33. $info = collect($info)->toArray();
  34. $data = [];
  35. $data['total'] = $info['total'];
  36. $data['total_page'] = $info['last_page'];
  37. $data['page'] = $info['current_page'];
  38. $data['page_size'] = $info['per_page'];
  39. $data['data'] = $info['data'];
  40. return $data;
  41. }
  42. /**
  43. * 接口数据数组化
  44. * @param $code
  45. * @param $msg
  46. * @param string $data
  47. * @return array
  48. */
  49. public static function arr($code, $msg, $data=''){
  50. $res = [];
  51. if(isset($code)){
  52. $res['code'] = $code;
  53. }
  54. if($msg){
  55. $res['msg'] = $msg;
  56. }
  57. $res['data'] = $data;
  58. return $res;
  59. }
  60. /**
  61. * 日期时间数据格式化
  62. * @param $data
  63. * @return mixed
  64. */
  65. public static function dateFormat($data){
  66. collect($data)->toArray();
  67. if(isset($data['update_time']) && !empty($data['update_time'])){
  68. $data['update_time'] = date('Y-m-d H:i:s', $data['update_time']);
  69. }
  70. if(isset($data['create_time']) && !empty($data['create_time'])){
  71. $data['create_time'] = date('Y-m-d H:i:s', $data['create_time']);
  72. }
  73. if(isset($data['start_time']) && !empty($data['start_time'])){
  74. $data['start_time'] = date('Y-m-d H:i:s', $data['start_time']);
  75. }
  76. if(isset($data['end_time']) && !empty($data['end_time'])){
  77. $data['end_time'] = date('Y-m-d H:i:s', $data['end_time']);
  78. }
  79. return $data;
  80. }
  81. /**
  82. * 请求外部链接
  83. * @param $url
  84. * @param string $post
  85. */
  86. public static function curl($url, $post=''){
  87. $ch = curl_init();
  88. $timeout = 5;
  89. curl_setopt($ch, CURLOPT_URL, $url);
  90. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  91. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  92. if($post){
  93. curl_setopt($ch, CURLOPT_POST,true);
  94. }
  95. $contents = curl_exec($ch);
  96. curl_close($ch);
  97. return $contents;
  98. }
  99. /**
  100. * 去除字符串中的空格
  101. * @param $str
  102. * @return mixed
  103. */
  104. public static function trimAll($str){
  105. $null_str=array(" "," ","\t","\n","\r");
  106. $replace_str=array("","","","","");
  107. return str_replace($null_str,$replace_str,$str);
  108. }
  109. /**
  110. * 服务器错误处理
  111. * @param $e
  112. * @return string
  113. */
  114. public static function serviceError($e){
  115. Log::info($e);
  116. return self::json(config('code.service_error'), trans('msg.service_error'));
  117. }
  118. /**
  119. * unicode转码为utf-8
  120. * 用于去除电话号码中隐藏的unicode编码
  121. * @param $name
  122. * @return string
  123. */
  124. public static function unicode_decode($ostr)
  125. {
  126. $json = '{"str":"'.$ostr.'"}';
  127. $arr = json_decode($json,true);
  128. if(empty($arr)) return '';
  129. preg_match_all('/[\x{FF00}-\x{FFEF}|\x{0000}-\x{00ff}|\x{4e00}-\x{9fff}]+/u', $ostr, $matches);
  130. $str = join('', $matches[0]);
  131. if($str==''){ //含有特殊字符需要逐個處理
  132. $returnstr = '';
  133. $i = 0;
  134. $str_length = strlen($ostr);
  135. while ($i<=$str_length){
  136. $temp_str = substr($ostr, $i, 1);
  137. $ascnum = Ord($temp_str);
  138. if ($ascnum>=224){
  139. $returnstr = $returnstr.substr($ostr, $i, 3);
  140. $i = $i + 3;
  141. }elseif ($ascnum>=192){
  142. $returnstr = $returnstr.substr($ostr, $i, 2);
  143. $i = $i + 2;
  144. }elseif ($ascnum>=65 && $ascnum<=90){
  145. $returnstr = $returnstr.substr($ostr, $i, 1);
  146. $i = $i + 1;
  147. }elseif ($ascnum>=128 && $ascnum<=191){ // 特殊字符
  148. $i = $i + 1;
  149. }else{
  150. $returnstr = $returnstr.substr($ostr, $i, 1);
  151. $i = $i + 1;
  152. }
  153. }
  154. $str = $returnstr;
  155. preg_match_all('/[\x{FF00}-\x{FFEF}|\x{0000}-\x{00ff}|\x{4e00}-\x{9fff}]+/u', $str, $matches);
  156. $str = join('', $matches[0]);
  157. }
  158. return $str;
  159. //return $arr['str'];
  160. }
  161. /**
  162. * 替换不见unicode字符
  163. * 用于去除guid和部门导入时的特殊字符
  164. * 添加保留符号时在中括号中添加
  165. * @param $str
  166. * @return mixed
  167. */
  168. public static function trimUnicode($str){
  169. $str = self::setFilter($str);
  170. $res = preg_replace('#[^\x{4e00}-\x{9fa5}A-Za-z0-9\-\_\(\)\[\]\{\}\<\>\*\=\+]#u','',$str);
  171. return $res;
  172. }
  173. /**
  174. * 全半角转换
  175. * @param $html
  176. * @return string
  177. */
  178. public static function setFilter($html){
  179. $arr = array('0' => '0', '1' => '1', '2' => '2', '3' => '3', '4' => '4',
  180. '5' => '5', '6' => '6', '7' => '7', '8' => '8', '9' => '9',
  181. 'A' => 'A', 'B' => 'B', 'C' => 'C', 'D' => 'D', 'E' => 'E',
  182. 'F' => 'F', 'G' => 'G', 'H' => 'H', 'I' => 'I', 'J' => 'J',
  183. 'K' => 'K', 'L' => 'L', 'M' => 'M', 'N' => 'N', 'O' => 'O',
  184. 'P' => 'P', 'Q' => 'Q', 'R' => 'R', 'S' => 'S', 'T' => 'T',
  185. 'U' => 'U', 'V' => 'V', 'W' => 'W', 'X' => 'X', 'Y' => 'Y',
  186. 'Z' => 'Z', 'a' => 'a', 'b' => 'b', 'c' => 'c', 'd' => 'd',
  187. 'e' => 'e', 'f' => 'f', 'g' => 'g', 'h' => 'h', 'i' => 'i',
  188. 'j' => 'j', 'k' => 'k', 'l' => 'l', 'm' => 'm', 'n' => 'n',
  189. 'o' => 'o', 'p' => 'p', 'q' => 'q', 'r' => 'r', 's' => 's',
  190. 't' => 't', 'u' => 'u', 'v' => 'v', 'w' => 'w', 'x' => 'x',
  191. 'y' => 'y', 'z' => 'z',
  192. '(' => '(', ')' => ')', '〔' => '[', '〕' => ']', '【' => '[',
  193. '】' => ']', '〖' => '[', '〗' => ']', '“' => '[', '”' => ']',
  194. '‘' => '[', '’' => ']', '{' => '{', '}' => '}', '《' => '<',
  195. '》' => '>',
  196. '%' => '%', '+' => '+', '—' => '-', '-' => '-', '~' => '-',
  197. ':' => ':', '。' => '.', '、' => ',', ',' => '.', '、' => '.',
  198. ';' => ',', '?' => '?', '!' => '!', '…' => '-', '‖' => '|',
  199. '”' => '"', '’' => '`', '‘' => '`', '|' => '|', '〃' => '"',
  200. ' ' => ' ');
  201. return strtr($html, $arr);
  202. }
  203. }?>