diff --git a/app/api/controller/IndexController.php b/app/api/controller/IndexController.php index b1342c0..a4b5eb1 100644 --- a/app/api/controller/IndexController.php +++ b/app/api/controller/IndexController.php @@ -56,4 +56,11 @@ class IndexController extends Base public function getOssInfo(Request $request){ return $this->json(200,'ok',['bucketURL'=>getenv('HTTP')]); } + + public function logout(Request $request) + { + $session = $request->session(); + $session->delete('admin'); + return $this->json(200, '退出成功'); + } } diff --git a/app/api/controller/Project.php b/app/api/controller/Project.php index 7a1e26f..d3c0a39 100644 --- a/app/api/controller/Project.php +++ b/app/api/controller/Project.php @@ -16,7 +16,12 @@ class Project extends Base */ public function list(Request $request) { - $select = Db::name('Projects')->where('is_delete',-1)->order('id', 'desc')->paginate($request->get('limit', 10)); + $uid=$request->admin['id']; + if($uid!=1){ + $where[]= ['create_user_id', '=', $uid]; + } + $where[]= ['is_delete', '=', -1]; + $select = Db::name('Projects')->where($where)->order('id', 'desc')->paginate($request->get('limit', 10)); return json(['code' => 200, 'msg' => '获取成功', 'count' => $select->total(), 'data' => $select->items()]); } /** @@ -24,6 +29,11 @@ class Project extends Base */ public function edit(Request $request) { + $uid=$request->admin['id']; + if($uid!=1){ + $where[]= ['create_user_id', '=', $uid]; + } + $where[]= ['id', '=', $request->post('id')]; $update=[]; if(!empty($request->post('indexImage'))){ $update= ['indexImage' => $request->post('indexImage')]; @@ -31,7 +41,7 @@ class Project extends Base if(!empty($request->post('projectName'))){ $update= ['projectName'=> $request->post('projectName')]; } - Db::name('Projects')->where('id', $request->post('id'))->update($update); + Db::name('Projects')->where($where)->update($update); return json(['code' => 200, 'msg' => '更新成功']); } /** @@ -40,10 +50,12 @@ class Project extends Base public function create(Request $request) { $data = $request->post(); + $uid=$request->admin['id']; + $data['create_user_id']=$uid; $id = ProjectModel::insertGetId($data); $res = ProjectModel::where('id', $id)->first(); $res['CreateTime'] = $res['created_at']; - $res['CreateUserId'] = 1; + $res['CreateUserId'] = $res['create_user_id']; return $this->json(200, 'ok', $res->toArray()); } @@ -52,7 +64,12 @@ class Project extends Base */ public function publish(Request $request) { - $res=Db::name('Projects')->where('id', $request->post('id'))->update(['state' => $request->post('state')]); + $uid=$request->admin['id']; + if($uid!=1){ + $where[]= ['create_user_id', '=', $uid]; + } + $where[]= ['id', '=', $request->post('id')]; + $res=Db::name('Projects')->where($where)->update(['state' => $request->post('state')]); if ($res) { return $this->json(200, '操作成功'); } else { @@ -81,11 +98,16 @@ class Project extends Base public function data(Request $request) { $data = $request->post(); - $find = Db::name('projectdatas')->where('project_id', $data['projectId'])->find(); + $uid=$request->admin['id']; + if($uid!=1){ + $where[]= ['create_user_id', '=', $uid]; + } + $where[]= ['project_id', '=', $data['projectId']]; + $find = Db::name('projectdatas')->where($where)->find(); if ($find) { - Db::name('projectdatas')->where('project_id', $data['projectId'])->update(['content' => $data['content']]); + Db::name('projectdatas')->where($where)->update(['content' => $data['content']]); } else { - $id = Db::name('projectdatas')->insertGetId(['project_id' => $data['projectId'], 'content' => $data['content']]); + $id = Db::name('projectdatas')->insertGetId(['project_id' => $data['projectId'], 'content' => $data['content'],'create_user_id'=>$uid,'created_at'=>date('Y-m-d H:i:s')]); $find = Db::name('projectdatas')->where('id', $id)->find(); } return $this->json(200, 'ok', $find); @@ -94,7 +116,12 @@ class Project extends Base public function delete(Request $request) { $ids = $request->get('ids'); - $res = Db::name('projects')->where('id', $ids)->update(['is_delete' => 1]); + $uid=$request->admin['id']; + if($uid!=1){ + $where[]= ['create_user_id', '=', $uid]; + } + $where[]= ['id', '=', $ids]; + $res = Db::name('projects')->where($where)->update(['is_delete' => 1]); if ($res) { return $this->json(200, '删除成功'); } else { diff --git a/app/api/middleware/Auth.php b/app/api/middleware/Auth.php new file mode 100644 index 0000000..a197a8b --- /dev/null +++ b/app/api/middleware/Auth.php @@ -0,0 +1,45 @@ + + * @copyright walkor + * @link http://www.workerman.net/ + * @license http://www.opensource.org/licenses/mit-license.php MIT License + */ + +namespace app\api\middleware; + + +use Webman\MiddlewareInterface; +use Webman\Http\Response; +use Webman\Http\Request; + +class Auth implements MiddlewareInterface +{ + public function process(Request $request, callable $handler) : Response + { + // 如果是opitons请求则返回一个空的响应,否则继续向洋葱芯穿越,并得到一个响应 + $response = $request->method() == 'OPTIONS' ? response('') : $handler($request); + + // 给响应添加跨域相关的http头 + $response->withHeaders([ + 'Access-Control-Allow-Credentials' => 'true', + 'Access-Control-Allow-Origin' => $request->header('origin', '*'), + 'Access-Control-Allow-Methods' => $request->header('access-control-request-method', '*'), + 'Access-Control-Allow-Headers' => $request->header('access-control-request-headers', '*'), + ]); + $session = $request->session(); + $admin=$session->get('admin'); + if(!$admin){ + return json(['code'=>-1,'msg'=>'登录失效']); + } + $request->admin=$admin; + return $handler($request); + + } +} \ No newline at end of file diff --git a/composer.json b/composer.json index fb43210..dfe53bc 100644 --- a/composer.json +++ b/composer.json @@ -28,7 +28,8 @@ "workerman/webman-framework": "^1.5.0", "monolog/monolog": "^2.0", "webman/admin": "^0.6.13", - "webman/think-orm": "^1.1" + "webman/think-orm": "^1.1", + "vlucas/phpdotenv": "^5.5" }, "suggest": { "ext-event": "For better performance. " diff --git a/composer.lock b/composer.lock index df4aa73..5496f16 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "ede5c7067d13146d2b89685fc26d1ad4", + "content-hash": "d30f94cf75d6d6c6c07bb12adefb9a1f", "packages": [ { "name": "doctrine/inflector", @@ -97,6 +97,68 @@ ], "time": "2022-10-20T09:10:12+00:00" }, + { + "name": "graham-campbell/result-type", + "version": "v1.1.1", + "source": { + "type": "git", + "url": "https://github.com/GrahamCampbell/Result-Type.git", + "reference": "672eff8cf1d6fe1ef09ca0f89c4b287d6a3eb831" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/GrahamCampbell/Result-Type/zipball/672eff8cf1d6fe1ef09ca0f89c4b287d6a3eb831", + "reference": "672eff8cf1d6fe1ef09ca0f89c4b287d6a3eb831", + "shasum": "" + }, + "require": { + "php": "^7.2.5 || ^8.0", + "phpoption/phpoption": "^1.9.1" + }, + "require-dev": { + "phpunit/phpunit": "^8.5.32 || ^9.6.3 || ^10.0.12" + }, + "type": "library", + "autoload": { + "psr-4": { + "GrahamCampbell\\ResultType\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Graham Campbell", + "email": "hello@gjcampbell.co.uk", + "homepage": "https://github.com/GrahamCampbell" + } + ], + "description": "An Implementation Of The Result Type", + "keywords": [ + "Graham Campbell", + "GrahamCampbell", + "Result Type", + "Result-Type", + "result" + ], + "support": { + "issues": "https://github.com/GrahamCampbell/Result-Type/issues", + "source": "https://github.com/GrahamCampbell/Result-Type/tree/v1.1.1" + }, + "funding": [ + { + "url": "https://github.com/GrahamCampbell", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/graham-campbell/result-type", + "type": "tidelift" + } + ], + "time": "2023-02-25T20:23:15+00:00" + }, { "name": "guzzlehttp/guzzle", "version": "7.7.0", @@ -1301,6 +1363,81 @@ }, "time": "2018-02-13T20:26:39+00:00" }, + { + "name": "phpoption/phpoption", + "version": "1.9.1", + "source": { + "type": "git", + "url": "https://github.com/schmittjoh/php-option.git", + "reference": "dd3a383e599f49777d8b628dadbb90cae435b87e" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/schmittjoh/php-option/zipball/dd3a383e599f49777d8b628dadbb90cae435b87e", + "reference": "dd3a383e599f49777d8b628dadbb90cae435b87e", + "shasum": "" + }, + "require": { + "php": "^7.2.5 || ^8.0" + }, + "require-dev": { + "bamarni/composer-bin-plugin": "^1.8.2", + "phpunit/phpunit": "^8.5.32 || ^9.6.3 || ^10.0.12" + }, + "type": "library", + "extra": { + "bamarni-bin": { + "bin-links": true, + "forward-command": true + }, + "branch-alias": { + "dev-master": "1.9-dev" + } + }, + "autoload": { + "psr-4": { + "PhpOption\\": "src/PhpOption/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "Apache-2.0" + ], + "authors": [ + { + "name": "Johannes M. Schmitt", + "email": "schmittjoh@gmail.com", + "homepage": "https://github.com/schmittjoh" + }, + { + "name": "Graham Campbell", + "email": "hello@gjcampbell.co.uk", + "homepage": "https://github.com/GrahamCampbell" + } + ], + "description": "Option Type for PHP", + "keywords": [ + "language", + "option", + "php", + "type" + ], + "support": { + "issues": "https://github.com/schmittjoh/php-option/issues", + "source": "https://github.com/schmittjoh/php-option/tree/1.9.1" + }, + "funding": [ + { + "url": "https://github.com/GrahamCampbell", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/phpoption/phpoption", + "type": "tidelift" + } + ], + "time": "2023-02-25T19:38:58+00:00" + }, { "name": "psr/container", "version": "1.1.2", @@ -2757,6 +2894,90 @@ }, "time": "2023-04-20T14:27:51+00:00" }, + { + "name": "vlucas/phpdotenv", + "version": "v5.5.0", + "source": { + "type": "git", + "url": "https://github.com/vlucas/phpdotenv.git", + "reference": "1a7ea2afc49c3ee6d87061f5a233e3a035d0eae7" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/vlucas/phpdotenv/zipball/1a7ea2afc49c3ee6d87061f5a233e3a035d0eae7", + "reference": "1a7ea2afc49c3ee6d87061f5a233e3a035d0eae7", + "shasum": "" + }, + "require": { + "ext-pcre": "*", + "graham-campbell/result-type": "^1.0.2", + "php": "^7.1.3 || ^8.0", + "phpoption/phpoption": "^1.8", + "symfony/polyfill-ctype": "^1.23", + "symfony/polyfill-mbstring": "^1.23.1", + "symfony/polyfill-php80": "^1.23.1" + }, + "require-dev": { + "bamarni/composer-bin-plugin": "^1.4.1", + "ext-filter": "*", + "phpunit/phpunit": "^7.5.20 || ^8.5.30 || ^9.5.25" + }, + "suggest": { + "ext-filter": "Required to use the boolean validator." + }, + "type": "library", + "extra": { + "bamarni-bin": { + "bin-links": true, + "forward-command": true + }, + "branch-alias": { + "dev-master": "5.5-dev" + } + }, + "autoload": { + "psr-4": { + "Dotenv\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Graham Campbell", + "email": "hello@gjcampbell.co.uk", + "homepage": "https://github.com/GrahamCampbell" + }, + { + "name": "Vance Lucas", + "email": "vance@vancelucas.com", + "homepage": "https://github.com/vlucas" + } + ], + "description": "Loads environment variables from `.env` to `getenv()`, `$_ENV` and `$_SERVER` automagically.", + "keywords": [ + "dotenv", + "env", + "environment" + ], + "support": { + "issues": "https://github.com/vlucas/phpdotenv/issues", + "source": "https://github.com/vlucas/phpdotenv/tree/v5.5.0" + }, + "funding": [ + { + "url": "https://github.com/GrahamCampbell", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/vlucas/phpdotenv", + "type": "tidelift" + } + ], + "time": "2022-10-16T01:01:54+00:00" + }, { "name": "voku/portable-ascii", "version": "1.6.1", diff --git a/config/route.php b/config/route.php index c09fb5a..2826f21 100644 --- a/config/route.php +++ b/config/route.php @@ -19,6 +19,7 @@ Route::group('/api', function () { Route::group('/goview', function () { Route::group('/sys', function () { Route::any('/login',[app\api\controller\IndexController::class,'login']); + Route::any('/logout',[app\api\controller\IndexController::class,'logout']); Route::any('/getOssInfo',[app\api\controller\IndexController::class,'getOssInfo']); }); Route::group('/project', function () { @@ -30,7 +31,7 @@ Route::group('/api', function () { Route::any('/publish',[app\api\controller\Project::class,'publish']); Route::any('/list',[app\api\controller\Project::class,'list']); Route::any('/getData',[app\api\controller\Project::class,'getData']); - }); + })->middleware(app\api\middleware\Auth::class); }); });