Skip to content

Commit e6906df

Browse files
committed
增加阿里云OSS组件,增加附件上传功能,优化代码风格,删除文章模型读取监听,增加目录与用户填充,升级laravel
1 parent 224c73b commit e6906df

29 files changed

+1707
-1154
lines changed

.env.example

+13
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,16 @@ PUSHER_APP_CLUSTER=mt1
4141

4242
MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
4343
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"
44+
45+
FILESYSTEM_DRIVER=local
46+
FILESYSTEM_CLOUD=local
47+
FILESYSTEM_ROOT=/web
48+
49+
OSS_ACCESS_ID=
50+
OSS_ACCESS_KEY=
51+
OSS_BUCKET=
52+
OSS_ENDPOINT=
53+
OSS_CDN_DOMAIN=
54+
OSS_SSL=true
55+
OSS_IS_CNAME=false
56+
OSS_DEBUG=true
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace Modules\Admin\Http\Controllers\Attachment;
4+
5+
use App\Model\Attachment;
6+
use App\Model\Code;
7+
use Illuminate\Http\Request;
8+
use Illuminate\Http\Response;
9+
use Modules\Admin\Http\Controllers\AdminController;
10+
11+
/**
12+
* 附件控制器
13+
* Class AttachmentController
14+
* @package Modules\Admin\Http\Controllers\Attachment
15+
*/
16+
class AttachmentController extends AdminController
17+
{
18+
protected static $rules = [
19+
'postUpload' => [
20+
'file' => 'required|file',
21+
'directory' => 'string',
22+
],
23+
];
24+
25+
/**
26+
* 上传文件
27+
* @param \Illuminate\Http\Request $request
28+
* @return \Illuminate\Http\Response
29+
* @throws \Throwable
30+
*/
31+
public function postUpload(Request $request): Response
32+
{
33+
$this->checkValidate($request->all(), __FUNCTION__);
34+
if (!$request->hasFile('file')) {
35+
Code::setCode(Code::ERR_PARAMETER);
36+
return $this->response();
37+
}
38+
$file = $request->file('file');
39+
$directory = trim($request->input('directory', ''));
40+
$attachment = Attachment::upload($file, $directory) ?: null;
41+
return $this->response($attachment);
42+
}
43+
}

Modules/Admin/Http/Controllers/User/BlacklistController.php

+10-9
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,15 @@ class BlacklistController extends AdminController
3939
*
4040
* @param \Illuminate\Http\Request $request
4141
* @return \Illuminate\Http\Response
42+
* @throws \Exception
4243
*/
4344
public function getIndex(Request $request): Response
4445
{
4546
$data = $request->all();
46-
$this->checkValidate($data, 'getIndex');
47+
$this->checkValidate($data, __FUNCTION__);
4748
$blacklist = new Blacklist();
4849
if ($request->input('with_trashed')) {
49-
$blacklist = $blacklist->withTrashed();
50+
$blacklist = $blacklist::withTrashed();
5051
}
5152
if ($request->input('valid') !== null) {
5253
$blacklist->where('valid', $request->input('valid'));
@@ -68,7 +69,7 @@ public function getIndex(Request $request): Response
6869
public function postUpdate(Request $request): Response
6970
{
7071
$data = $request->all();
71-
$this->checkValidate($data, 'postUpdate');
72+
$this->checkValidate($data, __FUNCTION__);
7273
$uid = $request->input('uid');
7374
$blacklist = Blacklist::withTrashed()->whereUid($uid)->first();
7475
if (!$blacklist) {
@@ -77,10 +78,10 @@ public function postUpdate(Request $request): Response
7778
}
7879
unset($data['id']);
7980
$data['comment'] = $data['comment'] ?: '';
80-
$data['valid'] = !!$data['valid'];
81+
$data['valid'] = (bool)$data['valid'];
8182
$blacklist->fill($data);
8283
$blacklist->operator_uid = Auth::id();
83-
$blacklist->restore();
84+
$blacklist::restore();
8485
$blacklist->saveOrFail();
8586
return $this->response($blacklist);
8687
}
@@ -93,9 +94,9 @@ public function postUpdate(Request $request): Response
9394
*/
9495
public function getShow(Request $request): Response
9596
{
96-
$this->checkValidate($request->all(), 'getShow');
97+
$this->checkValidate($request->all(), __FUNCTION__);
9798
$id = +$request->input('id');
98-
$blacklist = Blacklist::findOrFail($id);
99+
$blacklist = (new Blacklist())->findOrFail($id);
99100
return $this->response($blacklist);
100101
}
101102

@@ -108,9 +109,9 @@ public function getShow(Request $request): Response
108109
*/
109110
public function deleteDelete(Request $request): Response
110111
{
111-
$this->checkValidate($request->all(), 'deleteDelete');
112+
$this->checkValidate($request->all(), __FUNCTION__);
112113
$id = +$request->input('id');
113-
$blacklist = Blacklist::findOrFail($id);
114+
$blacklist = (new Blacklist())->findOrFail($id);
114115
$ret = $blacklist->delete();
115116
if (!$ret) {
116117
Code::setCode(Code::ERR_DB_FAIL);

README.md

+45-74
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
## Website of WebmasterClub at Shanghai Dianji University
1+
# Website of WebmasterClub at Shanghai Dianji University
22

33
[https://www.djwebclub.com](https://www.djwebclub.com)
44

5-
### Instruction for deployment
5+
## Instruction for deployment
66

7-
#### Configure
7+
### Configure
88

99
Copy the *environment configuration*
1010

@@ -18,39 +18,63 @@ and add your *database connection* and *SMTP Server* info to it.
1818
vim .env
1919
```
2020

21-
#### Install the infrastructure
21+
### Install
2222

23-
1. Generate the key for the application
24-
2. Migrate the database tables
25-
3. Install the Laravel Passport used to support OAuth Server
23+
Just run
2624

2725
```bash
26+
./install.sh
27+
```
28+
29+
It will execute the following instructions.
30+
31+
```bash
32+
# 0. Install some dependencies
33+
if hash pecl 2>/dev/null; then
34+
pecl install igbinary redis xdebug
35+
else
36+
if hash apt-get 2>/dev/null; then
37+
apt-get install php-igbinary php-redis php-xdebug php-bcmath php-mbstring
38+
fi
39+
fi
40+
41+
composer install
42+
43+
# 1. Generate the key for the application.
2844
./artisan key:generate
29-
./artisan migrate
45+
# 2. Drop all existing tables in your specific database.
46+
./artisan migrate:reset
47+
# 2. Migrate the tables.
48+
# 3. Fill up your database with init data.
49+
./artisan migrate --force --seed
50+
# 4. Install the Laravel Passport used to support OAuth Server.
3051
./artisan passport:install --force
3152
```
3253

33-
#### Fill up your database
3454

35-
Seed some base data to your database.
55+
### Default User
3656

37-
```bash
38-
./artisan db:seed
39-
```
57+
| Username | Mobile | Password | Role |
58+
|:-----------------:|:---------:|:--------:|:-----------:|
59+
| [email protected] |18181818181| 123456 | Super Admin |
4060

41-
**Make sure to run it once precisely or duplicated data will be added**
61+
## Reinstall
4262

43-
### Reinstall
63+
Reinstall like an new application according to *Instruction for deployment*
4464

45-
#### Clean your database
65+
Just run
4666

47-
```bash
48-
./artisan migrate:reset
67+
```shell
68+
./install.sh
4969
```
5070

51-
#### Reinstall
71+
## Uninstall
5272

53-
Reinstall like an new application according to *Instruction for deployment*
73+
## Clean your database
74+
75+
```bash
76+
./artisan migrate:reset
77+
```
5478

5579
## Laravel
5680

@@ -63,59 +87,6 @@ Reinstall like an new application according to *Instruction for deployment*
6387
<a href="https://packagist.org/packages/laravel/framework"><img src="https://poser.pugx.org/laravel/framework/license.svg" alt="License"></a>
6488
</p>
6589

66-
## About Laravel
67-
68-
Laravel is a web application framework with expressive, elegant syntax. We believe development must be an enjoyable and creative experience to be truly fulfilling. Laravel attempts to take the pain out of development by easing common tasks used in the majority of web projects, such as:
69-
70-
- [Simple, fast routing engine](https://laravel.com/docs/routing).
71-
- [Powerful dependency injection container](https://laravel.com/docs/container).
72-
- Multiple back-ends for [session](https://laravel.com/docs/session) and [cache](https://laravel.com/docs/cache) storage.
73-
- Expressive, intuitive [database ORM](https://laravel.com/docs/eloquent).
74-
- Database agnostic [schema migrations](https://laravel.com/docs/migrations).
75-
- [Robust background job processing](https://laravel.com/docs/queues).
76-
- [Real-time event broadcasting](https://laravel.com/docs/broadcasting).
77-
78-
Laravel is accessible, yet powerful, providing tools needed for large, robust applications.
79-
80-
## Learning Laravel
81-
82-
Laravel has the most extensive and thorough [documentation](https://laravel.com/docs) and video tutorial library of any modern web application framework, making it a breeze to get started learning the framework.
83-
84-
If you're not in the mood to read, [Laracasts](https://laracasts.com) contains over 1100 video tutorials on a range of topics including Laravel, modern PHP, unit testing, JavaScript, and more. Boost the skill level of yourself and your entire team by digging into our comprehensive video library.
85-
86-
## Laravel Sponsors
87-
88-
We would like to extend our thanks to the following sponsors for helping fund on-going Laravel development. If you are interested in becoming a sponsor, please visit the Laravel [Patreon page](https://patreon.com/taylorotwell):
89-
90-
- **[Vehikl](https://vehikl.com/)**
91-
- **[Tighten Co.](https://tighten.co)**
92-
- **[Kirschbaum Development Group](https://kirschbaumdevelopment.com)**
93-
- **[Cubet Techno Labs](https://cubettech.com)**
94-
- **[British Software Development](https://www.britishsoftware.co)**
95-
- **[Webdock, Fast VPS Hosting](https://www.webdock.io/en)**
96-
- [UserInsights](https://userinsights.com)
97-
- [Fragrantica](https://www.fragrantica.com)
98-
- [SOFTonSOFA](https://softonsofa.com/)
99-
- [User10](https://user10.com)
100-
- [Soumettre.fr](https://soumettre.fr/)
101-
- [CodeBrisk](https://codebrisk.com)
102-
- [1Forge](https://1forge.com)
103-
- [TECPRESSO](https://tecpresso.co.jp/)
104-
- [Runtime Converter](http://runtimeconverter.com/)
105-
- [WebL'Agence](https://weblagence.com/)
106-
- [Invoice Ninja](https://www.invoiceninja.com)
107-
- [iMi digital](https://www.imi-digital.de/)
108-
- [Earthlink](https://www.earthlink.ro/)
109-
- [Steadfast Collective](https://steadfastcollective.com/)
110-
111-
## Contributing
112-
113-
Thank you for considering contributing to the Laravel framework! The contribution guide can be found in the [Laravel documentation](https://laravel.com/docs/contributions).
114-
115-
## Security Vulnerabilities
116-
117-
If you discover a security vulnerability within Laravel, please send an e-mail to Taylor Otwell via [[email protected]](mailto:[email protected]). All security vulnerabilities will be promptly addressed.
118-
11990
## License
12091

121-
The Laravel framework is open-sourced software licensed under the [MIT license](https://opensource.org/licenses/MIT).
92+
The Website is open-sourced software licensed under the [MIT license](https://opensource.org/licenses/MIT).

app/Http/Controllers/AppController.php

+17-15
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
namespace App\Http\Controllers;
44

55
use App\Common\Util;
6+
use App\Http\Response\JsonResponse;
67
use App\Model\Code;
78
use App\Model\Menu;
9+
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
810
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
911
use Illuminate\Foundation\Bus\DispatchesJobs;
1012
use Illuminate\Foundation\Validation\ValidatesRequests;
@@ -27,7 +29,7 @@ public function __construct()
2729
{
2830
//初始化分页
2931
$page = request()->input('page');
30-
if ($page !== null && is_integer($page) && +$page > 0) {
32+
if ($page !== null && is_int($page) && +$page > 0) {
3133
$this->page = $page;
3234
}
3335
$pageSize = request()->input('page_size') ?: request()->input('pageSize');
@@ -37,14 +39,14 @@ public function __construct()
3739
}
3840

3941
protected static $rules = [
40-
"default" => [
42+
'default' => [
4143
'page' => 'nullable|integer|min:1',
4244
'pageSize' => 'nullable|integer|min:1',
4345
],
4446
];
4547

4648
protected static $rulesMessages = [
47-
"default" => [
49+
'default' => [
4850
'required' => '缺少必填参数[:attribute]',
4951
'filled' => '[:attribute]不能为空',
5052
'string' => '格式错误,[:attribute]应该为字符串',
@@ -64,8 +66,8 @@ public function __construct()
6466
];
6567

6668
protected static $rulesCodes = [
67-
"default" => [
68-
"Exists" => Code::ERR_MODEL_NOT_FOUND,
69+
'default' => [
70+
'Exists' => Code::ERR_MODEL_NOT_FOUND,
6971
],
7072
];
7173

@@ -77,14 +79,15 @@ public function __construct()
7779
* @param array $errors 错误
7880
* @param int $code 错误码
7981
*/
80-
protected function checkValidate($data, $scene = 'default', &$errors = [], &$code = Code::ERR_PARAMETER)
82+
protected function checkValidate($data, $scene = 'default', &$errors = [], &$code = Code::ERR_PARAMETER): void
8183
{
8284
$code = Code::ERR_PARAMETER;
8385
$validator = Validator::make($data, $this->rules($scene), $this->rulesMessages($scene));
8486
if ($validator->fails()) {
8587
$rulesCode = $this->rulesCodes($scene);
8688
$failed = $validator->failed();
8789
$errors = Arr::flatten($validator->errors()->getMessages());
90+
/** @noinspection LoopWhichDoesNotLoopInspection */
8891
foreach ($failed as $para => $v) {
8992
if (isset($rulesCode[$para])) {
9093
$code = $rulesCode[$para];
@@ -98,7 +101,6 @@ protected function checkValidate($data, $scene = 'default', &$errors = [], &$cod
98101
}
99102
throw new InvalidParameterException(Util::toJson($errors), $code);
100103
}
101-
102104
}
103105
}
104106

@@ -140,20 +142,20 @@ protected function rulesCodes($scene = 'default')
140142
* @param array $headers
141143
* @return \Illuminate\Http\Response
142144
*/
143-
function response($data = null, int $status = 200, array $headers = []): Response
145+
public function response($data = null, int $status = 200, array $headers = []): Response
144146
{
145147
$extraFields = [];
146-
if (@explode('/', request()->path())[1] === 'page' && (strtoupper(request()->method()) <=> 'GET') === 0) {
148+
if ((strtoupper(request()->method()) <=> 'GET') === 0 && @explode('/', request()->path())[1] === 'page') {
147149
//注入菜单、用户角色信息
148150
$extraFields = [
149-
'menu' => Menu::getMenuForUser(Auth::user())->map(function (Menu $item) {
151+
'menu' => Menu::getMenuForUser(Auth::user())->map(static function (Menu $item) {
150152
// return $item->toArray();
151153
return Arr::only($item->toArray(), ['id', 'title', 'description', 'url', 'icon_class', 'sort', 'group', 'parent_id']);
152-
})->sort(function ($a, $b) {
154+
})->sort(static function ($a, $b) {
153155
return $a['sort'] - $b['sort'];
154-
})->groupBy('group')->map(function ($menus) {
156+
})->groupBy('group')->map(static function ($menus) {
155157
$menus = array_values(Util::list2tree($menus));
156-
$menus = array_filter($menus, function ($menu) {
158+
$menus = array_filter($menus, static function ($menu) {
157159
//把没有子项的无用根节点删掉
158160
return $menu['parent_id'] || $menu['url'] || @$menu['_child'];
159161
});
@@ -162,7 +164,7 @@ function response($data = null, int $status = 200, array $headers = []): Respons
162164
'user' => Auth::user(),
163165
];
164166
}
165-
return new \App\Http\Response\JsonResponse($data, $status, $headers, $extraFields);
167+
return new JsonResponse($data, $status, $headers, $extraFields);
166168
}
167169

168170
/**
@@ -171,7 +173,7 @@ function response($data = null, int $status = 200, array $headers = []): Respons
171173
* @param \Illuminate\Contracts\Pagination\LengthAwarePaginator $pagination
172174
* @return array
173175
*/
174-
public function getPaginateResponse(\Illuminate\Contracts\Pagination\LengthAwarePaginator $pagination): array
176+
public function getPaginateResponse(LengthAwarePaginator $pagination): array
175177
{
176178
return [
177179
'items' => $pagination->items(),

0 commit comments

Comments
 (0)