CommonServer.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. namespace App\Servers\Common;
  3. use AlibabaCloud\Client\AlibabaCloud;
  4. use AlibabaCloud\Client\Exception\ClientException;
  5. use AlibabaCloud\Client\Exception\ServerException;
  6. /**
  7. * Redis数据缓存类
  8. */
  9. class CommonServer
  10. {
  11. /**
  12. * 错误信息
  13. * @var string
  14. */
  15. private $errorMsg = '';
  16. static private $server = null;
  17. private function __construct()
  18. {
  19. }
  20. /**
  21. * 创建对象
  22. * @return CommonServer
  23. */
  24. static function creatServer()
  25. {
  26. if (empty(self::$server)) self::$server = new CommonServer();
  27. return self::$server;
  28. }
  29. /**
  30. * 获取真实IP
  31. * @return mixed
  32. */
  33. function getClientIp()
  34. {
  35. $http_x_forwarded_for = request()->header('x-forwarded-for');
  36. if (empty($http_x_forwarded_for)) {
  37. return request()->getClientIp();
  38. } else {
  39. $http_x_forwarded_for = explode(',', $http_x_forwarded_for);
  40. return $http_x_forwarded_for[0];
  41. }
  42. }
  43. /**
  44. * 字符串去标签过滤及空格过滤
  45. * @param $str_name
  46. * @param $default
  47. * @return string
  48. */
  49. function filtrationStr($str_name, $default = '')
  50. {
  51. $str = request()->input($str_name, $default);
  52. if (empty($str)) return $str;
  53. if (!is_string($str)) {
  54. $str = json_encode($str);
  55. $str = strip_tags($str);
  56. $str = trim($str);
  57. $str = json_decode($str, true);
  58. } else {
  59. $str = strip_tags($str);
  60. $str = trim($str);
  61. }
  62. return $str;
  63. }
  64. /**
  65. * 验证手机号码格式
  66. * @param $phone
  67. * @return bool
  68. */
  69. public function verifyPhoneNumber($phone)
  70. {
  71. if (empty($phone)) return false;
  72. if (!preg_match('/^1[3-9]\d{9}$/', $phone)) {
  73. return false;
  74. }
  75. return true;
  76. }
  77. /**
  78. * 验证是否微信浏览器
  79. * @return bool
  80. */
  81. function isWeixin()
  82. {
  83. $http_user_agent = request()->header('user-agent');
  84. if (strpos($http_user_agent, 'MicroMessenger') !== false) {
  85. return true;
  86. }
  87. return false;
  88. }
  89. /**
  90. * 获取阿里云sts临时权限
  91. */
  92. function getAliSts()
  93. {
  94. $data = RedisDataServer::creatServer()->getData('sts_data');
  95. if ($data) {
  96. $data = json_decode($data, true);
  97. } else {
  98. $accessKeyId = env('ALI_OSS_ACCESS_ID');
  99. $accessSecret = env('ALI_OSS_ACCESS_KEY');
  100. AlibabaCloud::accessKeyClient($accessKeyId, $accessSecret)->regionId('cn-hangzhou')->asDefaultClient();
  101. //设置参数,发起请求。
  102. try {
  103. $result = AlibabaCloud::rpc()
  104. ->product('Sts')
  105. ->scheme('https') // https | http
  106. ->version('2015-04-01')
  107. ->action('AssumeRole')
  108. ->method('POST')
  109. ->host('sts.aliyuncs.com')
  110. ->options([
  111. 'query' => [
  112. 'RegionId' => "cn-chengdu",
  113. 'RoleArn' => "acs:ram::1470797691368660:role/oss-js",
  114. 'RoleSessionName' => "js-oss-serve",
  115. 'DurationSeconds' => 900,
  116. ],
  117. ])
  118. ->request();
  119. $result = $result->toArray();
  120. if (empty($result['Credentials'])) {
  121. return ['code' => 0, 'msg' => '获取token信息失败'];
  122. }
  123. $data = $result['Credentials'];
  124. unset($data['Expiration']);
  125. RedisDataServer::creatServer()->setData('sts_data', $data, 'json', 800);
  126. } catch (ClientException $e) {
  127. return ['code' => 0, 'msg' => $e->getErrorMessage()];
  128. } catch (ServerException $e) {
  129. return ['code' => 0, 'msg' => $e->getErrorMessage()];
  130. }
  131. }
  132. return ['code' => 1, 'msg' => '获取信息成功', 'data' => $data];
  133. }
  134. }