1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- <?php
- namespace app\api\controller;
- use app\common\controller\Api;
- use think\Db;
- class Upload extends Api
- {
- protected $noNeedLogin = ['gmt_iso8601','signedUrl','callBack'];
- protected $noNeedRight = ['*'];
- public function gmt_iso8601($time) {
- $expiration = date(DATE_ISO8601,$time);
- $pos = strpos($expiration, '+');
- $expiration = substr($expiration, 0, $pos);
- return $expiration."Z";
- }
-
- public function signedUrl(){
- $config = config("oss");
- $id= $config["secretId"];
- $key= $config["secretKey"];
-
- $host = $config["bucket"].".".$config["endPoint"];
-
- $callbackUrl = config('website_url').'/api/upload/callBack';
- $dir = 'images/';
- $dir = 'uploads/'.date('Ymd').'/';
- $callback_param = array('callbackUrl'=>$callbackUrl,
- 'callbackBody'=>'filename=${object}&size=${size}&mimeType=${mimeType}&height=${imageInfo.height}&width=${imageInfo.width}',
- 'callbackBodyType'=>"application/x-www-form-urlencoded");
- $callback_string = json_encode($callback_param);
- $base64_callback_body = base64_encode($callback_string);
- $now = time();
- $expire = 3000;
- $end = $now + $expire;
- $expiration = $this->gmt_iso8601($end);
-
- $condition = array(0=>'content-length-range', 1=>0, 2=>1048576000);
- $conditions[] = $condition;
-
- $start = array(0=>'starts-with', 1=>'$key', 2=>$dir);
- $conditions[] = $start;
- $arr = array('expiration'=>$expiration,'conditions'=>$conditions);
- $policy = json_encode($arr);
- $base64_policy = base64_encode($policy);
- $string_to_sign = $base64_policy;
- $signature = base64_encode(hash_hmac('sha1', $string_to_sign, $key, true));
- $response = [];
- $response['accessid'] = $id;
- $response['policy'] = $base64_policy;
- $response['signature'] = $signature;
- $response['bucket'] = $config["url"];
- $response['fileurl'] = 'uploads/'.date('Ymd').'/';
- $this->success("获取成功!",$response);
- }
-
- public function callBack(){
- header("Content-Type: application/json");
- $data = array("Status"=>"Ok",'data'=>$_POST);
- echo json_encode($data);
- }
- }
|