菜谱项目

BaseController.php 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. namespace App\Api\V1\Controllers;
  3. use App\Error;
  4. use App\Http\Controllers\Controller;
  5. use Dingo\Api\Routing\Helpers;
  6. use Illuminate\Foundation\Auth\RegistersUsers;
  7. class BaseController extends Controller
  8. {
  9. use RegistersUsers;
  10. use Helpers;
  11. public static function returnValue($data, $errorNo = 0)
  12. {
  13. return ["rst" => $data, "errno" => $errorNo . '', "err" => Error::getError($errorNo) . '', "timestamp" => intval(microtime(true)) . ''];
  14. }
  15. public function _PAGE($totalNum, $pageId, $count)
  16. {
  17. $maxPage = ceil($totalNum / $count);
  18. $pageId < $maxPage ? $hasNext = true : $hasNext = false;
  19. $pageId > 1 ? $hasPrev = true : $hasPrev = false;
  20. $num = ($pageId == $maxPage ? $totalNum - ($maxPage - 1) * $count : $count);
  21. $pageInfo = array("totalNum" => $totalNum . '', "page" => $pageId . '', "maxPage" => $maxPage . '', "hasNext" => $hasNext, "hasPrev" => $hasPrev, "num" => $num . '');
  22. return $pageInfo;
  23. }
  24. public function checkDeviceType()
  25. {
  26. $device = 1;
  27. $agent = strtolower($_SERVER['HTTP_USER_AGENT']);
  28. //分别进行判断
  29. if (strpos($agent, 'iphone') || strpos($agent, 'ipad')) {
  30. $device = 2; //device:2 ios
  31. }
  32. if (strpos($agent, 'android')) {
  33. $device = 1; //device:1 android
  34. }
  35. return $device;
  36. }
  37. public static function formatData($ids, $list, $show_pic = false)
  38. {
  39. $data = array();
  40. foreach ($list as $item) {
  41. $item->icon = Config('constants.HOST_URL') . $item->icon;
  42. if ($show_pic) {
  43. $item->show_pic = Config('constants.HOST_URL') . $item->show_pic;
  44. }
  45. $data[$item->id] = $item;
  46. }
  47. if (empty($data)) {
  48. return array();
  49. }
  50. $res = array();
  51. foreach ($ids as $id) {
  52. if (isset($data[$id])) {
  53. $res[] = $data[$id];
  54. }
  55. }
  56. return $res;
  57. }
  58. public function curlRequest($method, $URL, $request_headers = array(), $fieldString = '', $log_name = false, $log_dir = false, $timeout = 5, $retries = 1)
  59. {
  60. global $http_code;
  61. $start_time = microtime(true);
  62. $ch = curl_init();
  63. curl_setopt($ch, CURLOPT_URL, $URL);
  64. if ($request_headers) {
  65. curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
  66. }
  67. if ($method == 'GET') {
  68. curl_setopt($ch, CURLOPT_HTTPGET, 1);
  69. } else if ($method == 'POST') {
  70. curl_setopt($ch, CURLOPT_POST, 1);
  71. curl_setopt($ch, CURLOPT_POSTFIELDS, $fieldString);
  72. } else if ($method == 'PUT') {
  73. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
  74. curl_setopt($ch, CURLOPT_POSTFIELDS, $fieldString);
  75. } else if ($method == 'DELETE') {
  76. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
  77. curl_setopt($ch, CURLOPT_POSTFIELDS, $fieldString);
  78. }
  79. // $cacert = 'oo/srca.cer'; //CA根证书
  80. $SSL = substr($URL, 0, 8) == "https://" ? true : false;
  81. $CA = false;
  82. if ($SSL && $CA) {
  83. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); // 只信任CA颁布的证书
  84. curl_setopt($ch, CURLOPT_CAINFO, $cacert); // CA根证书(用来验证的网站证书是否是CA颁布)
  85. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); // 检查证书中是否设置域名,并且是否与提供的主机名匹配
  86. } else if ($SSL && !$CA) {
  87. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 信任任何证书
  88. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1); // 检查证书中是否设置域名
  89. }
  90. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  91. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  92. $handles = false;
  93. while (($handles === false) && ($retries-- > 0)) {
  94. $handles = curl_exec($ch);
  95. $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  96. }
  97. curl_close($ch);
  98. if ($log_name) {
  99. $log = "---------------------------\n";
  100. $log .= date('Y-m-d H:i:s', $start_time) . " " . round(microtime(true) - $start_time, 3) . "\n" . $_SERVER['REQUEST_URI'];
  101. $log .= "\n$method $URL\n";
  102. $log .= "Head = " . var_export($request_headers, true) . "\n";
  103. $log .= "Body = " . $fieldString . "\n";
  104. $log .= "HTTP Code : " . $http_code . "\n";
  105. $log .= "HTTP Resp : $handles\n";
  106. @file_put_contents($log_dir . "{$log_name}_" . date('Ymd') . '.log', $log, FILE_APPEND);
  107. }
  108. return $handles;
  109. }
  110. }