From dc707180040a04b1dfb2935fed5e5ccb53839f91 Mon Sep 17 00:00:00 2001 From: wispx <1591788658@qq.com> Date: Fri, 7 Dec 2018 15:34:09 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=96=E6=B6=88=E4=B8=8A=E4=BC=A0=E5=8A=A8?= =?UTF-8?q?=E6=80=81=E9=85=8D=E7=BD=AE=E3=80=81=E5=AE=8C=E5=96=84=E4=B8=8A?= =?UTF-8?q?=E4=BC=A0=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + .../api => api/controller}/Base.php | 36 ++++---- .../api => api/controller}/Token.php | 2 +- .../api => api/controller}/Upload.php | 14 +-- application/api/controller/User.php | 90 +++++++++++++++++++ application/common/model/Users.php | 4 +- .../http/middleware/{Auth.php => WebAuth.php} | 2 +- application/index/controller/Base.php | 2 +- application/index/controller/Upload.php | 21 +++-- config/middleware.php | 2 +- .../index/config => config}/naming.php | 4 +- .../index/config => config}/strategy.php | 0 route/route.php | 10 ++- 13 files changed, 143 insertions(+), 45 deletions(-) rename application/{index/controller/api => api/controller}/Base.php (76%) rename application/{index/controller/api => api/controller}/Token.php (96%) rename application/{index/controller/api => api/controller}/Upload.php (70%) create mode 100644 application/api/controller/User.php rename application/http/middleware/{Auth.php => WebAuth.php} (98%) rename {application/index/config => config}/naming.php (96%) rename {application/index/config => config}/strategy.php (100%) diff --git a/README.md b/README.md index 35274fd0..852607f5 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,7 @@ 安装需求 --- * PHP版本 ≥ 5.6(建议使用PHP7+) +* mysql版本 ≥ 5.5 * mysqli支持 * fileinfo拓展 * curl拓展 diff --git a/application/index/controller/api/Base.php b/application/api/controller/Base.php similarity index 76% rename from application/index/controller/api/Base.php rename to application/api/controller/Base.php index 7075df1d..14c5e6cb 100644 --- a/application/index/controller/api/Base.php +++ b/application/api/controller/Base.php @@ -1,6 +1,6 @@ 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); diff --git a/application/index/controller/api/Token.php b/application/api/controller/Token.php similarity index 96% rename from application/index/controller/api/Token.php rename to application/api/controller/Token.php index febb41f3..7425da17 100644 --- a/application/index/controller/api/Token.php +++ b/application/api/controller/Token.php @@ -1,6 +1,6 @@ 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) { diff --git a/application/api/controller/User.php b/application/api/controller/User.php new file mode 100644 index 00000000..8f82c547 --- /dev/null +++ b/application/api/controller/User.php @@ -0,0 +1,90 @@ +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) + { + // + } +} diff --git a/application/common/model/Users.php b/application/common/model/Users.php index 5cbaad52..45424a1b 100644 --- a/application/common/model/Users.php +++ b/application/common/model/Users.php @@ -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) diff --git a/application/http/middleware/Auth.php b/application/http/middleware/WebAuth.php similarity index 98% rename from application/http/middleware/Auth.php rename to application/http/middleware/WebAuth.php index dc1c6874..9f311595 100644 --- a/application/http/middleware/Auth.php +++ b/application/http/middleware/WebAuth.php @@ -4,7 +4,7 @@ namespace app\http\middleware; use think\facade\Session; -class Auth +class WebAuth { /** * 无需登录可访问的方法(除分层控制器) diff --git a/application/index/controller/Base.php b/application/index/controller/Base.php index d743d816..c273499c 100644 --- a/application/index/controller/Base.php +++ b/application/index/controller/Base.php @@ -18,7 +18,7 @@ use think\facade\Env; class Base extends Controller { - protected $middleware = ['auth']; + protected $middleware = ['WebAuth']; protected $user = null; diff --git a/application/index/controller/Upload.php b/application/index/controller/Upload.php index bd55332d..ff066ef3 100644 --- a/application/index/controller/Upload.php +++ b/application/index/controller/Upload.php @@ -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; diff --git a/config/middleware.php b/config/middleware.php index e9978f79..0ac680c0 100644 --- a/config/middleware.php +++ b/config/middleware.php @@ -13,5 +13,5 @@ // | 中间件配置 // +---------------------------------------------------------------------- return [ - 'auth' => app\http\middleware\Auth::class + 'WebAuth' => app\http\middleware\WebAuth::class ]; diff --git a/application/index/config/naming.php b/config/naming.php similarity index 96% rename from application/index/config/naming.php rename to config/naming.php index 1147f7ca..134e62bd 100644 --- a/application/index/config/naming.php +++ b/config/naming.php @@ -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, ], [ diff --git a/application/index/config/strategy.php b/config/strategy.php similarity index 100% rename from application/index/config/strategy.php rename to config/strategy.php diff --git a/route/route.php b/route/route.php index 9cbca03c..338f3b29 100644 --- a/route/route.php +++ b/route/route.php @@ -9,9 +9,15 @@ // | Author: liu21st // +---------------------------------------------------------------------- +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 [];