Common.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace App\Http\Models;
  3. use Illuminate\Database\Eloquent\Model;
  4. use Illuminate\Support\Facades\Redis;
  5. /**
  6. * 代理管理模型类
  7. */
  8. class Common extends Model
  9. {
  10. /**
  11. * 接口调用
  12. *
  13. * @access public
  14. * @param mixed $url 接口地址
  15. * @param mixed $where $params参数
  16. * @param mixed $timeout 请求时效
  17. * @return String
  18. */
  19. public function make_request($url, $params, $timeout=30)
  20. {
  21. set_time_limit(0);
  22. if (function_exists('curl_init') === true) {
  23. $ch = curl_init();
  24. $header = array(
  25. 'Accept-Language: zh-cn',
  26. 'Connection: Keep-Alive',
  27. 'Cache-Control: no-cache'
  28. );
  29. curl_setopt($ch, CURLOPT_POST, 1);
  30. curl_setopt($ch, CURLOPT_URL, $url);
  31. curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
  32. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  33. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  34. if ($timeout > 0) curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  35. $result = curl_exec($ch);
  36. $errno = curl_errno($ch);
  37. curl_close($ch);
  38. return $result;
  39. } else {
  40. $context = array(
  41. 'http' => array(
  42. 'method' => 'POST',
  43. 'header' => 'Content-type: application/x-www-form-urlencoded' . "\r\n" .
  44. 'Content-length: ' . strlen($params),
  45. 'content' => $params));
  46. if ($timeout > 0) $context['http']['timeout'] = $timeout;
  47. $contextid = stream_context_create($context);
  48. $sock = @fopen($url, 'r', false, $contextid);
  49. if ($sock) {
  50. $result = '';
  51. while (!feof($sock)) {
  52. $result .= fgets($sock, 8192);
  53. }
  54. fclose($sock);
  55. } else {
  56. return 'TimeOut';
  57. }
  58. }
  59. return $result;
  60. }//end make_request()
  61. /**
  62. * 接口调用
  63. *
  64. * @access public
  65. * @param mixed $paramsData 接口必要參數
  66. * @param mixed $url 接口地址
  67. * @return String
  68. */
  69. public function sportsAgentParams($paramsData, $url)
  70. {
  71. $paramsSting = '';
  72. $a = 0;
  73. foreach ($paramsData as $key => $value) {
  74. if ($a === 0) {
  75. $paramsSting .= $key . '=' . $value;
  76. } else {
  77. $paramsSting .= '&' . $key . '=' . $value;
  78. }
  79. $a++;
  80. }
  81. $params = base64_encode($paramsSting);
  82. $key = md5($params . session('sportsToken'));
  83. $loginUrl = $url . '?params=' . $params . '&Key=' . $key;
  84. return $loginUrl;
  85. }//end sportsAgentParams()
  86. /**
  87. * 添加系统通知
  88. *
  89. * @access public
  90. * @param array $msg 通知内容
  91. * @return
  92. */
  93. static public function addNotification(array $msg){
  94. foreach($msg as $k => $v){
  95. Redis::rpush('MSG_INDEX_QIPAI',$v);
  96. }
  97. }
  98. /**
  99. * 获取后删除栈内系统通知
  100. * @access public
  101. * @param array $msg 通知内容
  102. * @return
  103. */
  104. static public function getNotification(){
  105. $msg = Redis::lrange('MSG_INDEX_QIPAI',0,-1);
  106. if(!empty($msg)){
  107. Redis::ltrim('MSG_INDEX_QIPAI',1,0);
  108. return $msg;
  109. }
  110. return [];
  111. }
  112. }