Skip to content

Commit 764dc66

Browse files
author
qiang.sun
committed
文件上传
1 parent 0992492 commit 764dc66

File tree

14 files changed

+908
-485
lines changed

14 files changed

+908
-485
lines changed

app/Contracts/BillerInterface.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: sunqiang
5+
* Date: 2018/11/15
6+
* Time: 5:45 PM
7+
*/
8+
9+
namespace App\Contracts;
10+
11+
12+
interface BillerInterface
13+
{
14+
public function bill(array $user, $amount);
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: sunqiang
5+
* Date: 2018/11/15
6+
* Time: 5:46 PM
7+
*/
8+
9+
namespace App\Contracts;
10+
11+
12+
interface BillingNotifierInterface
13+
{
14+
public function notify(array $user, $amount);
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: sunqiang
5+
* Date: 2018/11/15
6+
* Time: 4:43 PM
7+
*/
8+
9+
namespace App\Contracts;
10+
11+
12+
interface UserRepositoryInterface
13+
{
14+
public function all(): array;
15+
}

app/Http/Controllers/RequestController.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace App\Http\Controllers;
44

55
use Illuminate\Http\Request;
6+
use Illuminate\Support\Facades\Storage;
67

78
class RequestController extends Controller
89
{
@@ -25,4 +26,38 @@ public function form(Request $request, $id)
2526
dump($id == $request->segment(2));
2627
}
2728

29+
public function formPage()
30+
{
31+
return view('request.form');
32+
}
33+
34+
public function fileUpload(Request $request)
35+
{
36+
if ($request->hasFile('picture')) {
37+
$picture = $request->file('picture');
38+
if (!$picture->isValid()) {
39+
abort(400, '无效的上传文件');
40+
}
41+
// 文件扩展名
42+
$extension = $picture->getClientOriginalExtension();
43+
// 文件名
44+
$fileName = $picture->getClientOriginalName();
45+
// 生成新的统一格式的文件名
46+
$newFileName = md5($fileName . time() . mt_rand(1, 10000)) . '.' . $extension;
47+
// 图片保存路径
48+
$savePath = 'images/' . $newFileName;
49+
// Web 访问路径
50+
$webPath = '/storage/' . $savePath;
51+
// 将文件保存到 storage/app/public/images 目录下,先判断同名文件是否已经存在
52+
if (Storage::disk('public')->has($savePath)) {
53+
return response()->json(['path' => $webPath]);
54+
}
55+
if ($picture->storePubliclyAs('images', $newFileName, ['disk' => 'public'])) {
56+
return response()->json(['path' => $webPath]);
57+
}
58+
abort(500, '文件上传失败');
59+
} else {
60+
abort(400, '请选择要上传的文件');
61+
}
62+
}
2863
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
5+
use App\Contracts\UserRepositoryInterface;
6+
use Illuminate\Http\Request;
7+
8+
class UserController extends Controller
9+
{
10+
private $users;
11+
12+
public function __construct(UserRepositoryInterface $users)
13+
{
14+
$this->users = $users;
15+
}
16+
17+
public function getIndex()
18+
{
19+
$users = $this->users->all();
20+
return view('user.index', compact('users'));
21+
}
22+
}

app/Repositories/UserRepository.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: sunqiang
5+
* Date: 2018/11/15
6+
* Time: 4:43 PM
7+
*/
8+
9+
namespace App\Repositories;
10+
11+
use App\Contracts\UserRepositoryInterface;
12+
use App\User;
13+
14+
class UserRepository implements UserRepositoryInterface
15+
{
16+
17+
public function all(): array
18+
{
19+
return User::all()->toArray();
20+
}
21+
}

app/Services/StripeBiller.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: sunqiang
5+
* Date: 2018/11/15
6+
* Time: 5:49 PM
7+
*/
8+
9+
namespace App\Services;
10+
11+
12+
use App\Contracts\BillerInterface;
13+
use App\Contracts\BillingNotifierInterface;
14+
15+
class StripeBiller implements BillerInterface
16+
{
17+
private $notifier;
18+
19+
public function __construct(BillingNotifierInterface $notifier)
20+
{
21+
$this->notifier = $notifier;
22+
}
23+
24+
public function bill(array $user, $amount)
25+
{
26+
// Bill the user via Stripe...
27+
$this->notifier->notify($user, $amount);
28+
}
29+
}

0 commit comments

Comments
 (0)