1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?php
namespace backend\controllers;
use common\models\psources\CoinImage;
use Yii;
use OSS\OssClient;
use yii\web\UploadedFile;
use yii\validators\ImageValidator;
class OssController extends BaseController
{
protected $accessKeyId = "LTAI4FxZ787zpBmjLmr6yMwA";
protected $accessKeySecret = "5OMu030RFIE2KP3fNJrhVRTlVqBLaE";
protected $endpoint = "http://oss-cn-shanghai.aliyuncs.com";
protected $bucket = "bqbwallet";
public function actionUpload()
{
Yii::$app->response->format = 'json';
$type = Yii::$app->request->post('image_type', '');
$uploaded_file = UploadedFile::getInstanceByName('file');
$object = md5(date('Y-m-d H:i:s') . $uploaded_file->tempName) . '.' . $uploaded_file->extension;
$filePath = $uploaded_file->tempName;
$ossClient = new OssClient($this->accessKeyId, $this->accessKeySecret, $this->endpoint);
$result = $ossClient->uploadFile($this->bucket, (empty($type) ? '' : $type . '/') . $object, $filePath);
if (!isset($result["info"]['url'])) {
return ['code' => 1, 'msg' => '上传失败'];
}
if (in_array($type, ['coin', 'banner', 'h5_banner', 'h5_icon', 'application', 'h5_application'])) {
$file_url = explode('http://bqbwallet.oss-cn-shanghai.aliyuncs.com', $result["info"]['url']);
$image_id = CoinImage::addOneRecord($file_url[1]);
}
return ['code' => 0, 'msg' => '上传成功', 'data' => ['src' => $result["info"]['url'], 'image_id' => !empty($image_id) ? $image_id : 0]];
}
}