文件上传、图片上传对接阿里云OSS
This commit is contained in:
77
Server/application/common/util/AliyunOSS.php
Normal file
77
Server/application/common/util/AliyunOSS.php
Normal file
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
namespace app\common\util;
|
||||
|
||||
use OSS\OssClient;
|
||||
use OSS\Core\OssException;
|
||||
|
||||
class AliyunOSS
|
||||
{
|
||||
// OSS配置信息
|
||||
const ACCESS_KEY_ID = 'your_access_key_id';
|
||||
const ACCESS_KEY_SECRET = 'your_access_key_secret';
|
||||
const ENDPOINT = 'oss-cn-hangzhou.aliyuncs.com';
|
||||
const BUCKET = 'your_bucket_name';
|
||||
|
||||
/**
|
||||
* 获取OSS客户端实例
|
||||
* @return OssClient
|
||||
* @throws OssException
|
||||
*/
|
||||
public static function getClient()
|
||||
{
|
||||
try {
|
||||
return new OssClient(
|
||||
self::ACCESS_KEY_ID,
|
||||
self::ACCESS_KEY_SECRET,
|
||||
self::ENDPOINT
|
||||
);
|
||||
} catch (OssException $e) {
|
||||
throw new OssException('创建OSS客户端失败:' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传文件到OSS
|
||||
* @param string $filePath 本地文件路径
|
||||
* @param string $objectName OSS对象名称
|
||||
* @return array
|
||||
* @throws OssException
|
||||
*/
|
||||
public static function uploadFile($filePath, $objectName)
|
||||
{
|
||||
try {
|
||||
$client = self::getClient();
|
||||
|
||||
// 上传文件
|
||||
$result = $client->uploadFile(self::BUCKET, $objectName, $filePath);
|
||||
|
||||
// 获取文件访问URL
|
||||
$url = $client->signUrl(self::BUCKET, $objectName, 3600);
|
||||
|
||||
return [
|
||||
'success' => true,
|
||||
'url' => $url,
|
||||
'object_name' => $objectName,
|
||||
'size' => filesize($filePath),
|
||||
'mime_type' => mime_content_type($filePath)
|
||||
];
|
||||
} catch (OssException $e) {
|
||||
return [
|
||||
'success' => false,
|
||||
'error' => $e->getMessage()
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成OSS对象名称
|
||||
* @param string $originalName 原始文件名
|
||||
* @return string
|
||||
*/
|
||||
public static function generateObjectName($originalName)
|
||||
{
|
||||
$ext = pathinfo($originalName, PATHINFO_EXTENSION);
|
||||
$name = md5(uniqid(mt_rand(), true));
|
||||
return date('Y/m/d/') . $name . '.' . $ext;
|
||||
}
|
||||
}
|
||||
65
Server/application/common/util/AliyunSMS.php
Normal file
65
Server/application/common/util/AliyunSMS.php
Normal file
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\util;
|
||||
|
||||
use Darabonba\OpenApi\Models\Config;
|
||||
use AlibabaCloud\SDK\Dysmsapi\V20170525\Dysmsapi;
|
||||
use AlibabaCloud\SDK\Dysmsapi\V20170525\Models\SendSmsRequest;
|
||||
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
|
||||
|
||||
class AliyunSMS {
|
||||
|
||||
const ACCESS_KEY_ID = 'LTAI5tFjVRYAFmo6fayvv2Te';
|
||||
const ACCESS_KEY_SECRET = 'mdc7KETPGVon8PiA2kfM47rAOpJne8';
|
||||
const SIGN_NAME = '广州宏科网络';
|
||||
|
||||
const TC_VCODE = 'SMS_464445466';
|
||||
|
||||
static public function createClient() {
|
||||
$config = new Config([
|
||||
// AccessKey ID
|
||||
'accessKeyId' => static::ACCESS_KEY_ID,
|
||||
// AccessKey Secret
|
||||
'accessKeySecret' => static::ACCESS_KEY_SECRET
|
||||
]);
|
||||
|
||||
// 访问的域名
|
||||
$config->endpoint = 'dysmsapi.aliyuncs.com';
|
||||
|
||||
return new Dysmsapi($config);
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送验证码
|
||||
*
|
||||
* @param $phoneNumbers
|
||||
* @param $templateCode
|
||||
* @param array $templateParam
|
||||
* @return bool
|
||||
*/
|
||||
static public function send($phoneNumbers, $templateCode, array $templateParam = array()) {
|
||||
$client = static::createClient();
|
||||
$sendSmsRequest = new SendSmsRequest([
|
||||
'phoneNumbers' => $phoneNumbers,
|
||||
'signName' => static::SIGN_NAME,
|
||||
'templateCode' => $templateCode,
|
||||
'templateParam' => json_encode($templateParam)
|
||||
]);
|
||||
$runtime = new RuntimeOptions([]);
|
||||
$logFile = ROOT_PATH . DS . 'aliyun-sms.txt';
|
||||
try {
|
||||
$data = $client->sendSmsWithOptions($sendSmsRequest, $runtime);
|
||||
if ($data->body->code === 'OK') {
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
$logData = print_r($data, TRUE);
|
||||
} catch (\Exception $ex) {
|
||||
$logData = print_r($ex, TRUE);
|
||||
}
|
||||
|
||||
file_put_contents($logFile, '[' . date('Y-m-d H:i:s') . ']' . PHP_EOL . $logData . PHP_EOL . PHP_EOL, FILE_APPEND);
|
||||
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user