取消上传动态配置、完善上传接口

This commit is contained in:
wispx
2018-12-07 15:34:09 +08:00
parent 2727978c64
commit dc70718004
13 changed files with 143 additions and 45 deletions
+1
View File
@@ -32,6 +32,7 @@
安装需求
---
* PHP版本 ≥ 5.6(建议使用PHP7+)
* mysql版本 ≥ 5.5
* mysqli支持
* fileinfo拓展
* curl拓展
@@ -1,6 +1,6 @@
<?php
namespace app\index\controller\api;
namespace app\api\controller;
use app\common\model\Users;
use think\Controller;
@@ -20,16 +20,12 @@ class Base extends Controller
/**
* 构造方法
*
* @param bool $auth 是否认证
*
* @throws \think\Exception\DbException
*/
public function initialize($auth = true)
public function initialize()
{
parent::initialize();
$this->token = $this->param('token');
$configs = \app\common\model\Config::all();
foreach ($configs as $key => &$value) {
$this->config[$value->name] = $value->value;
@@ -39,31 +35,30 @@ class Base extends Controller
$this->response('API is not open yet.', 500);
}
$this->token = $this->request->header('token');
$this->auth($this->token);
$format = $this->param('format');
if ($format && in_array(strtolower($format), ['json', 'jsonp', 'xml'])) {
$this->format = $format;
}
$auth && $this->auth();
}
/**
* 权限认证
* 权限认证,成功设置成员属性user的数据,否则直接返回失败数据
*
* @param null $token
* @param $token
*
* @throws \think\Exception\DbException
*/
protected function auth($token = null)
protected function auth($token)
{
$token = $token ? $token : $this->token;
if ($token) {
$this->user = Users::get(['token' => $token]);
} else {
return $this->response('Token does not exist.', 500);
if (!$token) {
$this->response('Token does not exist.', 500);
}
$this->user = Users::get(['token' => $token]);
if (!$this->user) {
return $this->response('Authentication failed', 500);
$this->response('Authentication failed', 500);
}
}
@@ -72,15 +67,16 @@ class Base extends Controller
*
* @param string $msg 提示信息
* @param int $code 状态码
* @param array $data 数据
* @param null $data 数据
*
*/
protected function response($msg = '', $code = 200, $data = [])
protected function response($msg = '', $code = 200, $data = null)
{
$response = Response::create([
'code' => $code,
'msg' => $msg,
'data' => $data
'data' => $data,
'time' => time()
], $this->format, $code);
throw new HttpResponseException($response);
@@ -1,6 +1,6 @@
<?php
namespace app\index\controller\api;
namespace app\api\controller;
use app\common\model\Users;
use think\Exception;
@@ -6,21 +6,21 @@
* Link: https://github.com/wisp-x
*/
namespace app\index\controller\api;
namespace app\api\controller;
use think\Db;
use think\Exception;
class Upload extends Base
{
public function initialize($auth = false)
public function initialize()
{
parent::initialize($auth);
// 是否允许游客上传
if ($this->config['allowed_tourist_upload']) {
$this->token && $this->auth($this->token);
$token = $this->request->header('token');
if (!$this->config['allowed_tourist_upload']) {
$token && $this->auth($token);
} else {
$this->auth($this->token);
$this->auth($token);
}
}
@@ -32,7 +32,7 @@ class Upload extends Base
Db::startTrans();
try {
$data = (new \app\index\controller\Upload)->execute();
$data = (new \app\index\controller\Upload)->execute($this->user);
Db::commit();
} catch (Exception $e) {
+90
View File
@@ -0,0 +1,90 @@
<?php
/**
* User: Wisp X
* Date: 2018-12-07
* Time: 09:40
* Link: https://github.com/wisp-x
*/
namespace app\api\controller;
use think\Request;
class User extends Base
{
/**
* 显示资源列表
*
* @return \think\Response
*/
public function index()
{
return $this->response('success', 200, $this->user);
}
/**
* 显示创建资源表单页.
*
* @return \think\Response
*/
public function create()
{
//
}
/**
* 保存新建的资源
*
* @param \think\Request $request
* @return \think\Response
*/
public function save(Request $request)
{
//
}
/**
* 显示指定的资源
*
* @param int $id
* @return \think\Response
*/
public function read($id)
{
}
/**
* 显示编辑资源表单页.
*
* @param int $id
* @return \think\Response
*/
public function edit($id)
{
//
}
/**
* 保存更新的资源
*
* @param \think\Request $request
* @param int $id
* @return \think\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* 删除指定资源
*
* @param int $id
* @return \think\Response
*/
public function delete($id)
{
//
}
}
+3 -1
View File
@@ -19,6 +19,8 @@ class Users extends Model
protected $insert = ['reg_ip', 'quota', 'token'];
protected $append = ['use_quota'];
public function setPassWordAttr($password)
{
return md5($password);
@@ -41,7 +43,7 @@ class Users extends Model
public function getUseQuotaAttr()
{
return $this->hasMany('Images', 'user_id', 'id')->sum('size');
return sprintf("%.2f", $this->hasMany('Images', 'user_id', 'id')->sum('size'));
}
public static function login($account, $password)
@@ -4,7 +4,7 @@ namespace app\http\middleware;
use think\facade\Session;
class Auth
class WebAuth
{
/**
* 无需登录可访问的方法(除分层控制器)
+1 -1
View File
@@ -18,7 +18,7 @@ use think\facade\Env;
class Base extends Controller
{
protected $middleware = ['auth'];
protected $middleware = ['WebAuth'];
protected $user = null;
+12 -9
View File
@@ -9,6 +9,7 @@
namespace app\index\controller;
use app\common\model\Images;
use app\common\model\Users;
use GuzzleHttp\Client;
use think\Db;
use think\Exception;
@@ -22,7 +23,7 @@ class Upload extends Base
Db::startTrans();
try {
$data = $this->execute();
$data = $this->execute($this->user);
Db::commit();
} catch (Exception $e) {
@@ -37,12 +38,14 @@ class Upload extends Base
/**
* 执行上传,成功返回数据,否则直接抛出异常
*
* @param null|Users $user
*
* @return array
* @throws Exception
*/
public function execute()
public function execute($user = null)
{
if (!$this->config['allowed_tourist_upload'] && !$this->user) {
if (!$this->config['allowed_tourist_upload'] && !$user) {
throw new Exception('管理员关闭了游客上传!');
}
@@ -52,8 +55,8 @@ class Upload extends Base
$sha1 = $image->hash('sha1');
$md5 = $image->hash('md5');
if ($this->user) {
if (($this->user->use_quota + $size) > $this->user->quota) {
if ($user) {
if (($user->use_quota + $size) > $user->quota) {
throw new Exception('保存失败!您的储存容量不足,请联系管理员!');
}
}
@@ -87,7 +90,7 @@ class Upload extends Base
$client = new Client();
$response = $client->get("https://www.moderatecontent.com/api/v2?key={$this->config['audit_key']}&url={$url}");
if (200 == $response->getStatusCode()) {
$result = json_decode($response->getBody());
$result = json_decode($response->getBody()->getContents());
if (0 == $result->error_code) {
if ($result->rating_index >= $this->config['audit_index']) {
$strategy->delete($pathname);
@@ -101,7 +104,7 @@ class Upload extends Base
}
if (!Images::create([
'user_id' => $this->user ? $this->user->id : 0,
'user_id' => $user ? $user->id : 0,
'strategy' => $currentStrategy,
'path' => dirname($pathname),
'name' => basename($pathname),
@@ -120,8 +123,8 @@ class Upload extends Base
'url' => $url,
];
if ($this->user) {
$data['quota'] = sprintf('%.2f', (float)$this->user->quota);
$data['use_quota'] = sprintf('%.2f', (float)$this->user->use_quota);
$data['quota'] = sprintf('%.2f', (float) $user->quota);
$data['use_quota'] = sprintf('%.2f', (float) $user->use_quota);
}
return $data;
+1 -1
View File
@@ -13,5 +13,5 @@
// | 中间件配置
// +----------------------------------------------------------------------
return [
'auth' => app\http\middleware\Auth::class
'WebAuth' => app\http\middleware\WebAuth::class
];
@@ -85,7 +85,7 @@ return [
[
'name' => '{uid}',
'example' => '1',
'explain' => '用户ID(用户登录可用,否则为0)',
'explain' => '用户ID(用户从网页端登录可用,否则为0)',
'value' => $uid,
],
[
@@ -165,7 +165,7 @@ return [
[
'name' => '{uid}',
'example' => '1',
'explain' => '用户ID(用户登录可用,否则为0)',
'explain' => '用户ID(用户从网页端登录可用,否则为0)',
'value' => $uid,
],
[
+8 -2
View File
@@ -9,9 +9,15 @@
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
use think\facade\Route;
Route::view('compatibility', 'index@tpl/compatibility');
// [RESTFul Api route]
Route::resource('api.auth', 'index/api/auth');
// [RESTFul Api Route]
Route::group('api', function () {
Route::post('upload', 'api/Upload/index');
Route::resource('auth', 'api/Auth');
Route::resource('user', 'api/User');
});
return [];