Skip to content

Commit

Permalink
增加阿里云OSS组件,增加附件上传功能,优化代码风格,删除文章模型读取监听,增加目录与用户填充,升级laravel
Browse files Browse the repository at this point in the history
  • Loading branch information
kyy1996 committed Jun 12, 2019
1 parent 224c73b commit e6906df
Show file tree
Hide file tree
Showing 29 changed files with 1,707 additions and 1,154 deletions.
13 changes: 13 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,16 @@ PUSHER_APP_CLUSTER=mt1

MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"

FILESYSTEM_DRIVER=local
FILESYSTEM_CLOUD=local
FILESYSTEM_ROOT=/web

OSS_ACCESS_ID=
OSS_ACCESS_KEY=
OSS_BUCKET=
OSS_ENDPOINT=
OSS_CDN_DOMAIN=
OSS_SSL=true
OSS_IS_CNAME=false
OSS_DEBUG=true
43 changes: 43 additions & 0 deletions Modules/Admin/Http/Controllers/Attachment/AttachmentController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Modules\Admin\Http\Controllers\Attachment;

use App\Model\Attachment;
use App\Model\Code;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Modules\Admin\Http\Controllers\AdminController;

/**
* 附件控制器
* Class AttachmentController
* @package Modules\Admin\Http\Controllers\Attachment
*/
class AttachmentController extends AdminController
{
protected static $rules = [
'postUpload' => [
'file' => 'required|file',
'directory' => 'string',
],
];

/**
* 上传文件
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
* @throws \Throwable
*/
public function postUpload(Request $request): Response
{
$this->checkValidate($request->all(), __FUNCTION__);
if (!$request->hasFile('file')) {
Code::setCode(Code::ERR_PARAMETER);
return $this->response();
}
$file = $request->file('file');
$directory = trim($request->input('directory', ''));
$attachment = Attachment::upload($file, $directory) ?: null;
return $this->response($attachment);
}
}
19 changes: 10 additions & 9 deletions Modules/Admin/Http/Controllers/User/BlacklistController.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,15 @@ class BlacklistController extends AdminController
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
* @throws \Exception
*/
public function getIndex(Request $request): Response
{
$data = $request->all();
$this->checkValidate($data, 'getIndex');
$this->checkValidate($data, __FUNCTION__);
$blacklist = new Blacklist();
if ($request->input('with_trashed')) {
$blacklist = $blacklist->withTrashed();
$blacklist = $blacklist::withTrashed();
}
if ($request->input('valid') !== null) {
$blacklist->where('valid', $request->input('valid'));
Expand All @@ -68,7 +69,7 @@ public function getIndex(Request $request): Response
public function postUpdate(Request $request): Response
{
$data = $request->all();
$this->checkValidate($data, 'postUpdate');
$this->checkValidate($data, __FUNCTION__);
$uid = $request->input('uid');
$blacklist = Blacklist::withTrashed()->whereUid($uid)->first();
if (!$blacklist) {
Expand All @@ -77,10 +78,10 @@ public function postUpdate(Request $request): Response
}
unset($data['id']);
$data['comment'] = $data['comment'] ?: '';
$data['valid'] = !!$data['valid'];
$data['valid'] = (bool)$data['valid'];
$blacklist->fill($data);
$blacklist->operator_uid = Auth::id();
$blacklist->restore();
$blacklist::restore();
$blacklist->saveOrFail();
return $this->response($blacklist);
}
Expand All @@ -93,9 +94,9 @@ public function postUpdate(Request $request): Response
*/
public function getShow(Request $request): Response
{
$this->checkValidate($request->all(), 'getShow');
$this->checkValidate($request->all(), __FUNCTION__);
$id = +$request->input('id');
$blacklist = Blacklist::findOrFail($id);
$blacklist = (new Blacklist())->findOrFail($id);
return $this->response($blacklist);
}

Expand All @@ -108,9 +109,9 @@ public function getShow(Request $request): Response
*/
public function deleteDelete(Request $request): Response
{
$this->checkValidate($request->all(), 'deleteDelete');
$this->checkValidate($request->all(), __FUNCTION__);
$id = +$request->input('id');
$blacklist = Blacklist::findOrFail($id);
$blacklist = (new Blacklist())->findOrFail($id);
$ret = $blacklist->delete();
if (!$ret) {
Code::setCode(Code::ERR_DB_FAIL);
Expand Down
119 changes: 45 additions & 74 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
## Website of WebmasterClub at Shanghai Dianji University
# Website of WebmasterClub at Shanghai Dianji University

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

### Instruction for deployment
## Instruction for deployment

#### Configure
### Configure

Copy the *environment configuration*

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

#### Install the infrastructure
### Install

1. Generate the key for the application
2. Migrate the database tables
3. Install the Laravel Passport used to support OAuth Server
Just run

```bash
./install.sh
```

It will execute the following instructions.

```bash
# 0. Install some dependencies
if hash pecl 2>/dev/null; then
pecl install igbinary redis xdebug
else
if hash apt-get 2>/dev/null; then
apt-get install php-igbinary php-redis php-xdebug php-bcmath php-mbstring
fi
fi

composer install

# 1. Generate the key for the application.
./artisan key:generate
./artisan migrate
# 2. Drop all existing tables in your specific database.
./artisan migrate:reset
# 2. Migrate the tables.
# 3. Fill up your database with init data.
./artisan migrate --force --seed
# 4. Install the Laravel Passport used to support OAuth Server.
./artisan passport:install --force
```

#### Fill up your database

Seed some base data to your database.
### Default User

```bash
./artisan db:seed
```
| Username | Mobile | Password | Role |
|:-----------------:|:---------:|:--------:|:-----------:|
| [email protected] |18181818181| 123456 | Super Admin |

**Make sure to run it once precisely or duplicated data will be added**
## Reinstall

### Reinstall
Reinstall like an new application according to *Instruction for deployment*

#### Clean your database
Just run

```bash
./artisan migrate:reset
```shell
./install.sh
```

#### Reinstall
## Uninstall

Reinstall like an new application according to *Instruction for deployment*
## Clean your database

```bash
./artisan migrate:reset
```

## Laravel

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

## About Laravel

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:

- [Simple, fast routing engine](https://laravel.com/docs/routing).
- [Powerful dependency injection container](https://laravel.com/docs/container).
- Multiple back-ends for [session](https://laravel.com/docs/session) and [cache](https://laravel.com/docs/cache) storage.
- Expressive, intuitive [database ORM](https://laravel.com/docs/eloquent).
- Database agnostic [schema migrations](https://laravel.com/docs/migrations).
- [Robust background job processing](https://laravel.com/docs/queues).
- [Real-time event broadcasting](https://laravel.com/docs/broadcasting).

Laravel is accessible, yet powerful, providing tools needed for large, robust applications.

## Learning Laravel

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.

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.

## Laravel Sponsors

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):

- **[Vehikl](https://vehikl.com/)**
- **[Tighten Co.](https://tighten.co)**
- **[Kirschbaum Development Group](https://kirschbaumdevelopment.com)**
- **[Cubet Techno Labs](https://cubettech.com)**
- **[British Software Development](https://www.britishsoftware.co)**
- **[Webdock, Fast VPS Hosting](https://www.webdock.io/en)**
- [UserInsights](https://userinsights.com)
- [Fragrantica](https://www.fragrantica.com)
- [SOFTonSOFA](https://softonsofa.com/)
- [User10](https://user10.com)
- [Soumettre.fr](https://soumettre.fr/)
- [CodeBrisk](https://codebrisk.com)
- [1Forge](https://1forge.com)
- [TECPRESSO](https://tecpresso.co.jp/)
- [Runtime Converter](http://runtimeconverter.com/)
- [WebL'Agence](https://weblagence.com/)
- [Invoice Ninja](https://www.invoiceninja.com)
- [iMi digital](https://www.imi-digital.de/)
- [Earthlink](https://www.earthlink.ro/)
- [Steadfast Collective](https://steadfastcollective.com/)

## Contributing

Thank you for considering contributing to the Laravel framework! The contribution guide can be found in the [Laravel documentation](https://laravel.com/docs/contributions).

## Security Vulnerabilities

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.

## License

The Laravel framework is open-sourced software licensed under the [MIT license](https://opensource.org/licenses/MIT).
The Website is open-sourced software licensed under the [MIT license](https://opensource.org/licenses/MIT).
32 changes: 17 additions & 15 deletions app/Http/Controllers/AppController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
namespace App\Http\Controllers;

use App\Common\Util;
use App\Http\Response\JsonResponse;
use App\Model\Code;
use App\Model\Menu;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
Expand All @@ -27,7 +29,7 @@ public function __construct()
{
//初始化分页
$page = request()->input('page');
if ($page !== null && is_integer($page) && +$page > 0) {
if ($page !== null && is_int($page) && +$page > 0) {
$this->page = $page;
}
$pageSize = request()->input('page_size') ?: request()->input('pageSize');
Expand All @@ -37,14 +39,14 @@ public function __construct()
}

protected static $rules = [
"default" => [
'default' => [
'page' => 'nullable|integer|min:1',
'pageSize' => 'nullable|integer|min:1',
],
];

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

protected static $rulesCodes = [
"default" => [
"Exists" => Code::ERR_MODEL_NOT_FOUND,
'default' => [
'Exists' => Code::ERR_MODEL_NOT_FOUND,
],
];

Expand All @@ -77,14 +79,15 @@ public function __construct()
* @param array $errors 错误
* @param int $code 错误码
*/
protected function checkValidate($data, $scene = 'default', &$errors = [], &$code = Code::ERR_PARAMETER)
protected function checkValidate($data, $scene = 'default', &$errors = [], &$code = Code::ERR_PARAMETER): void
{
$code = Code::ERR_PARAMETER;
$validator = Validator::make($data, $this->rules($scene), $this->rulesMessages($scene));
if ($validator->fails()) {
$rulesCode = $this->rulesCodes($scene);
$failed = $validator->failed();
$errors = Arr::flatten($validator->errors()->getMessages());
/** @noinspection LoopWhichDoesNotLoopInspection */
foreach ($failed as $para => $v) {
if (isset($rulesCode[$para])) {
$code = $rulesCode[$para];
Expand All @@ -98,7 +101,6 @@ protected function checkValidate($data, $scene = 'default', &$errors = [], &$cod
}
throw new InvalidParameterException(Util::toJson($errors), $code);
}

}
}

Expand Down Expand Up @@ -140,20 +142,20 @@ protected function rulesCodes($scene = 'default')
* @param array $headers
* @return \Illuminate\Http\Response
*/
function response($data = null, int $status = 200, array $headers = []): Response
public function response($data = null, int $status = 200, array $headers = []): Response
{
$extraFields = [];
if (@explode('/', request()->path())[1] === 'page' && (strtoupper(request()->method()) <=> 'GET') === 0) {
if ((strtoupper(request()->method()) <=> 'GET') === 0 && @explode('/', request()->path())[1] === 'page') {
//注入菜单、用户角色信息
$extraFields = [
'menu' => Menu::getMenuForUser(Auth::user())->map(function (Menu $item) {
'menu' => Menu::getMenuForUser(Auth::user())->map(static function (Menu $item) {
// return $item->toArray();
return Arr::only($item->toArray(), ['id', 'title', 'description', 'url', 'icon_class', 'sort', 'group', 'parent_id']);
})->sort(function ($a, $b) {
})->sort(static function ($a, $b) {
return $a['sort'] - $b['sort'];
})->groupBy('group')->map(function ($menus) {
})->groupBy('group')->map(static function ($menus) {
$menus = array_values(Util::list2tree($menus));
$menus = array_filter($menus, function ($menu) {
$menus = array_filter($menus, static function ($menu) {
//把没有子项的无用根节点删掉
return $menu['parent_id'] || $menu['url'] || @$menu['_child'];
});
Expand All @@ -162,7 +164,7 @@ function response($data = null, int $status = 200, array $headers = []): Respons
'user' => Auth::user(),
];
}
return new \App\Http\Response\JsonResponse($data, $status, $headers, $extraFields);
return new JsonResponse($data, $status, $headers, $extraFields);
}

/**
Expand All @@ -171,7 +173,7 @@ function response($data = null, int $status = 200, array $headers = []): Respons
* @param \Illuminate\Contracts\Pagination\LengthAwarePaginator $pagination
* @return array
*/
public function getPaginateResponse(\Illuminate\Contracts\Pagination\LengthAwarePaginator $pagination): array
public function getPaginateResponse(LengthAwarePaginator $pagination): array
{
return [
'items' => $pagination->items(),
Expand Down
Loading

0 comments on commit e6906df

Please sign in to comment.