123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- <?php
- namespace App\Api\V1\Controllers;
- use App\Error;
- use App\Http\Controllers\Controller;
- use Dingo\Api\Routing\Helpers;
- use Illuminate\Foundation\Auth\RegistersUsers;
- class BaseController extends Controller
- {
- use RegistersUsers;
- use Helpers;
- public static function returnValue($data, $errorNo = 0)
- {
- return ["rst" => $data, "errno" => $errorNo . '', "err" => Error::getError($errorNo) . '', "timestamp" => intval(microtime(true)) . ''];
- }
- public function _PAGE($totalNum, $pageId, $count)
- {
- $maxPage = ceil($totalNum / $count);
- $pageId < $maxPage ? $hasNext = true : $hasNext = false;
- $pageId > 1 ? $hasPrev = true : $hasPrev = false;
- $num = ($pageId == $maxPage ? $totalNum - ($maxPage - 1) * $count : $count);
- $pageInfo = array("totalNum" => $totalNum . '', "page" => $pageId . '', "maxPage" => $maxPage . '', "hasNext" => $hasNext, "hasPrev" => $hasPrev, "num" => $num . '');
- return $pageInfo;
- }
- public function checkDeviceType()
- {
- $device = 1;
- $agent = strtolower($_SERVER['HTTP_USER_AGENT']);
- //分别进行判断
- if (strpos($agent, 'iphone') || strpos($agent, 'ipad')) {
- $device = 2; //device:2 ios
- }
- if (strpos($agent, 'android')) {
- $device = 1; //device:1 android
- }
- return $device;
- }
- public static function formatData($ids, $list, $show_pic = false)
- {
- $data = array();
- foreach ($list as $item) {
- $item->icon = Config('constants.HOST_URL') . $item->icon;
- if ($show_pic) {
- $item->show_pic = Config('constants.HOST_URL') . $item->show_pic;
- }
- $data[$item->id] = $item;
- }
- if (empty($data)) {
- return array();
- }
- $res = array();
- foreach ($ids as $id) {
- if (isset($data[$id])) {
- $res[] = $data[$id];
- }
- }
- return $res;
- }
- public function curlRequest($method, $URL, $request_headers = array(), $fieldString = '', $log_name = false, $log_dir = false, $timeout = 5, $retries = 1)
- {
- global $http_code;
- $start_time = microtime(true);
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $URL);
- if ($request_headers) {
- curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
- }
- if ($method == 'GET') {
- curl_setopt($ch, CURLOPT_HTTPGET, 1);
- } else if ($method == 'POST') {
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $fieldString);
- } else if ($method == 'PUT') {
- curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
- curl_setopt($ch, CURLOPT_POSTFIELDS, $fieldString);
- } else if ($method == 'DELETE') {
- curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
- curl_setopt($ch, CURLOPT_POSTFIELDS, $fieldString);
- }
- // $cacert = 'oo/srca.cer'; //CA根证书
- $SSL = substr($URL, 0, 8) == "https://" ? true : false;
- $CA = false;
- if ($SSL && $CA) {
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); // 只信任CA颁布的证书
- curl_setopt($ch, CURLOPT_CAINFO, $cacert); // CA根证书(用来验证的网站证书是否是CA颁布)
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); // 检查证书中是否设置域名,并且是否与提供的主机名匹配
- } else if ($SSL && !$CA) {
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 信任任何证书
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1); // 检查证书中是否设置域名
- }
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
- $handles = false;
- while (($handles === false) && ($retries-- > 0)) {
- $handles = curl_exec($ch);
- $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
- }
- curl_close($ch);
- if ($log_name) {
- $log = "---------------------------\n";
- $log .= date('Y-m-d H:i:s', $start_time) . " " . round(microtime(true) - $start_time, 3) . "\n" . $_SERVER['REQUEST_URI'];
- $log .= "\n$method $URL\n";
- $log .= "Head = " . var_export($request_headers, true) . "\n";
- $log .= "Body = " . $fieldString . "\n";
- $log .= "HTTP Code : " . $http_code . "\n";
- $log .= "HTTP Resp : $handles\n";
- @file_put_contents($log_dir . "{$log_name}_" . date('Ymd') . '.log', $log, FILE_APPEND);
- }
- return $handles;
- }
- }
|