123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- <?php
- namespace App\Servers\Common;
- use AlibabaCloud\Client\AlibabaCloud;
- use AlibabaCloud\Client\Exception\ClientException;
- use AlibabaCloud\Client\Exception\ServerException;
- /**
- * Redis数据缓存类
- */
- class CommonServer
- {
- /**
- * 错误信息
- * @var string
- */
- private $errorMsg = '';
- static private $server = null;
- private function __construct()
- {
- }
- /**
- * 创建对象
- * @return CommonServer
- */
- static function creatServer()
- {
- if (empty(self::$server)) self::$server = new CommonServer();
- return self::$server;
- }
- /**
- * 获取真实IP
- * @return mixed
- */
- function getClientIp()
- {
- $http_x_forwarded_for = request()->header('x-forwarded-for');
- if (empty($http_x_forwarded_for)) {
- return request()->getClientIp();
- } else {
- $http_x_forwarded_for = explode(',', $http_x_forwarded_for);
- return $http_x_forwarded_for[0];
- }
- }
- /**
- * 字符串去标签过滤及空格过滤
- * @param $str_name
- * @param $default
- * @return string
- */
- function filtrationStr($str_name, $default = '')
- {
- $str = request()->input($str_name, $default);
- if (empty($str)) return $str;
- if (!is_string($str)) {
- $str = json_encode($str);
- $str = strip_tags($str);
- $str = trim($str);
- $str = json_decode($str, true);
- } else {
- $str = strip_tags($str);
- $str = trim($str);
- }
- return $str;
- }
- /**
- * 验证手机号码格式
- * @param $phone
- * @return bool
- */
- public function verifyPhoneNumber($phone)
- {
- if (empty($phone)) return false;
- if (!preg_match('/^1[3-9]\d{9}$/', $phone)) {
- return false;
- }
- return true;
- }
- /**
- * 验证是否微信浏览器
- * @return bool
- */
- function isWeixin()
- {
- $http_user_agent = request()->header('user-agent');
- if (strpos($http_user_agent, 'MicroMessenger') !== false) {
- return true;
- }
- return false;
- }
- /**
- * 获取阿里云sts临时权限
- */
- function getAliSts()
- {
- $data = RedisDataServer::creatServer()->getData('sts_data');
- if ($data) {
- $data = json_decode($data, true);
- } else {
- $accessKeyId = env('ALI_OSS_ACCESS_ID');
- $accessSecret = env('ALI_OSS_ACCESS_KEY');
- AlibabaCloud::accessKeyClient($accessKeyId, $accessSecret)->regionId('cn-hangzhou')->asDefaultClient();
- //设置参数,发起请求。
- try {
- $result = AlibabaCloud::rpc()
- ->product('Sts')
- ->scheme('https') // https | http
- ->version('2015-04-01')
- ->action('AssumeRole')
- ->method('POST')
- ->host('sts.aliyuncs.com')
- ->options([
- 'query' => [
- 'RegionId' => "cn-chengdu",
- 'RoleArn' => "acs:ram::1470797691368660:role/oss-js",
- 'RoleSessionName' => "js-oss-serve",
- 'DurationSeconds' => 900,
- ],
- ])
- ->request();
- $result = $result->toArray();
- if (empty($result['Credentials'])) {
- return ['code' => 0, 'msg' => '获取token信息失败'];
- }
- $data = $result['Credentials'];
- unset($data['Expiration']);
- RedisDataServer::creatServer()->setData('sts_data', $data, 'json', 800);
- } catch (ClientException $e) {
- return ['code' => 0, 'msg' => $e->getErrorMessage()];
- } catch (ServerException $e) {
- return ['code' => 0, 'msg' => $e->getErrorMessage()];
- }
- }
- return ['code' => 1, 'msg' => '获取信息成功', 'data' => $data];
- }
- }
|