CommonServer.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. * 获取阿里云sts临时权限
  79. */
  80. function getAliSts()
  81. {
  82. $data = RedisDataServer::creatServer()->getData('sts_data');
  83. if ($data) {
  84. $data = json_decode($data, true);
  85. } else {
  86. $accessKeyId = env('ALI_OSS_ACCESS_ID');
  87. $accessSecret = env('ALI_OSS_ACCESS_KEY');
  88. AlibabaCloud::accessKeyClient($accessKeyId, $accessSecret)->regionId('cn-hangzhou')->asDefaultClient();
  89. //设置参数,发起请求。
  90. try {
  91. $result = AlibabaCloud::rpc()
  92. ->product('Sts')
  93. ->scheme('https') // https | http
  94. ->version('2015-04-01')
  95. ->action('AssumeRole')
  96. ->method('POST')
  97. ->host('sts.aliyuncs.com')
  98. ->options([
  99. 'query' => [
  100. 'RegionId' => "cn-chengdu",
  101. 'RoleArn' => "acs:ram::1470797691368660:role/oss-js",
  102. 'RoleSessionName' => "js-oss-serve",
  103. 'DurationSeconds' => 900,
  104. ],
  105. ])
  106. ->request();
  107. $result = $result->toArray();
  108. if (empty($result['Credentials'])) {
  109. return ['code' => 0, 'msg' => '获取token信息失败'];
  110. }
  111. $data = $result['Credentials'];
  112. unset($data['Expiration']);
  113. RedisDataServer::creatServer()->setData('sts_data', $data, 'json', 800);
  114. } catch (ClientException $e) {
  115. return ['code' => 0, 'msg' => $e->getErrorMessage()];
  116. } catch (ServerException $e) {
  117. return ['code' => 0, 'msg' => $e->getErrorMessage()];
  118. }
  119. }
  120. return ['code' => 1, 'msg' => '获取信息成功', 'data' => $data];
  121. }
  122. }