Showing
8 changed files
with
233 additions
and
0 deletions
app/Http/Controllers/V1/UserController.php
0 → 100644
1 | +<?php | ||
2 | + | ||
3 | +namespace App\Http\Controllers\V1; | ||
4 | + | ||
5 | +use App\Http\Controllers\Controller; | ||
6 | +use App\Jobs\SendVerificationMessage; | ||
7 | +use App\Models\User; | ||
8 | +use Illuminate\Http\Request; | ||
9 | +use Illuminate\Support\Facades\Hash; | ||
10 | +use Illuminate\Support\Facades\Mail; | ||
11 | +use Illuminate\Support\Facades\Redis; | ||
12 | +use Illuminate\Support\Facades\Validator; | ||
13 | +use Jiannei\Response\Laravel\Support\Facades\Response; | ||
14 | + | ||
15 | +class UserController extends Controller | ||
16 | +{ | ||
17 | + // | ||
18 | + | ||
19 | + public function login(Request $request) | ||
20 | + { | ||
21 | + $validator = Validator::make($request->all(), [ | ||
22 | + 'email' => 'required|max:255', | ||
23 | + 'password' => 'required', | ||
24 | + ]); | ||
25 | + | ||
26 | + if ($validator->fails()){ | ||
27 | + return Response::fail('',500,$validator->errors()); | ||
28 | + } | ||
29 | + | ||
30 | + try{ | ||
31 | + $user = User::query()->where('email', $request->email)->first(); | ||
32 | + | ||
33 | + if (! $user || ! Hash::check($request->password, $user->password)) { | ||
34 | + Response::errorUnauthorized(); | ||
35 | + } | ||
36 | + | ||
37 | + $token = $user->createToken($request->email)->plainTextToken; | ||
38 | + | ||
39 | + $response = ['token' => $token]; | ||
40 | + | ||
41 | + return Response::success($response); | ||
42 | + }catch ( \Exception $exception ){ | ||
43 | + return Response::fail('账号或密码错误',500,$exception->getMessage()); | ||
44 | + } | ||
45 | + | ||
46 | + } | ||
47 | + | ||
48 | + public function register(Request $request) | ||
49 | + { | ||
50 | + $validator = Validator::make($request->all(), [ | ||
51 | + 'email' => 'required|unique:users|max:255', | ||
52 | + 'password' => 'required', | ||
53 | + 'verify_code' => 'required' | ||
54 | + ]); | ||
55 | + | ||
56 | + if ($validator->fails()){ | ||
57 | + return Response::fail('',500,$validator->errors()); | ||
58 | + } | ||
59 | + | ||
60 | + try{ | ||
61 | + $redis = Redis::connection(); | ||
62 | + | ||
63 | + if ($redis->get($request->email) !== $request->verify_code){ | ||
64 | + return Response::fail('verify code failed',500); | ||
65 | + } | ||
66 | + | ||
67 | + $user = User::query()->create($validator->validated()); | ||
68 | + | ||
69 | + $token = $user->createToken($user->email)->plainTextToken; | ||
70 | + | ||
71 | + $response = ['token' => $token]; | ||
72 | + | ||
73 | + return Response::success($response); | ||
74 | + }catch ( \Exception $exception ){ | ||
75 | + return Response::fail('register failed',500,$exception->getMessage()); | ||
76 | + } | ||
77 | + } | ||
78 | + | ||
79 | + public function verify(Request $request) | ||
80 | + { | ||
81 | + $validator = Validator::make($request->all(), [ | ||
82 | + 'email' => 'required|max:255', | ||
83 | + ]); | ||
84 | + | ||
85 | + if ($validator->fails()){ | ||
86 | + return Response::fail('',500,$validator->errors()); | ||
87 | + } | ||
88 | + | ||
89 | + SendVerificationMessage::dispatch($request->email); | ||
90 | + | ||
91 | + return Response::success('','发送成功'); | ||
92 | + } | ||
93 | +} |
app/Jobs/SendVerificationMessage.php
0 → 100644
1 | +<?php | ||
2 | + | ||
3 | +namespace App\Jobs; | ||
4 | + | ||
5 | +use App\Mail\SendVerifyCode; | ||
6 | +use Illuminate\Bus\Queueable; | ||
7 | +use Illuminate\Contracts\Queue\ShouldBeUnique; | ||
8 | +use Illuminate\Contracts\Queue\ShouldQueue; | ||
9 | +use Illuminate\Foundation\Bus\Dispatchable; | ||
10 | +use Illuminate\Queue\InteractsWithQueue; | ||
11 | +use Illuminate\Queue\SerializesModels; | ||
12 | +use Illuminate\Support\Facades\Cache; | ||
13 | +use Illuminate\Support\Facades\Mail; | ||
14 | +use Illuminate\Support\Facades\Redis; | ||
15 | + | ||
16 | +class SendVerificationMessage implements ShouldQueue | ||
17 | +{ | ||
18 | + use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; | ||
19 | + | ||
20 | + public $email; | ||
21 | + | ||
22 | + /** | ||
23 | + * Create a new job instance. | ||
24 | + * @param $email | ||
25 | + * @return void | ||
26 | + */ | ||
27 | + public function __construct($email) | ||
28 | + { | ||
29 | + // | ||
30 | + $this->email = $email; | ||
31 | + } | ||
32 | + | ||
33 | + /** | ||
34 | + * Execute the job. | ||
35 | + * | ||
36 | + * @return void | ||
37 | + */ | ||
38 | + public function handle() | ||
39 | + { | ||
40 | + $code = mt_rand(100000, 999999); | ||
41 | + | ||
42 | + $redis = Redis::connection(); | ||
43 | + | ||
44 | + $redis->setex($this->email,$code,1800); //过期时间30分钟 | ||
45 | + | ||
46 | + Mail::to($this->email)->send(new SendVerifyCode($code)); | ||
47 | + } | ||
48 | +} |
app/Mail/SendVerifyCode.php
0 → 100644
1 | +<?php | ||
2 | + | ||
3 | +namespace App\Mail; | ||
4 | + | ||
5 | +use Illuminate\Bus\Queueable; | ||
6 | +use Illuminate\Contracts\Queue\ShouldQueue; | ||
7 | +use Illuminate\Mail\Mailable; | ||
8 | +use Illuminate\Queue\SerializesModels; | ||
9 | + | ||
10 | +class SendVerifyCode extends Mailable | ||
11 | +{ | ||
12 | + use Queueable, SerializesModels; | ||
13 | + | ||
14 | + public $subject = "[Parlando] Register Verify"; | ||
15 | + | ||
16 | + public $code; | ||
17 | + | ||
18 | + /** | ||
19 | + * Create a new message instance. | ||
20 | + * @param $code | ||
21 | + * @return void | ||
22 | + */ | ||
23 | + public function __construct($code) | ||
24 | + { | ||
25 | + // | ||
26 | + $this->code = $code; | ||
27 | + } | ||
28 | + | ||
29 | + /** | ||
30 | + * Build the message. | ||
31 | + * | ||
32 | + * @return $this | ||
33 | + */ | ||
34 | + public function build() | ||
35 | + { | ||
36 | + return $this->from('15731208870@163.com', 'Parlando') | ||
37 | + ->view('mail.verify_code', ['code' => $this->code]); | ||
38 | + } | ||
39 | +} |
... | @@ -19,6 +19,7 @@ class User extends Authenticatable | ... | @@ -19,6 +19,7 @@ class User extends Authenticatable |
19 | protected $fillable = [ | 19 | protected $fillable = [ |
20 | 'nickname', | 20 | 'nickname', |
21 | 'mobile', | 21 | 'mobile', |
22 | + 'email', | ||
22 | 'password', | 23 | 'password', |
23 | 'avatar', | 24 | 'avatar', |
24 | 'gender', | 25 | 'gender', | ... | ... |
1 | +<?php | ||
2 | + | ||
3 | +use Illuminate\Database\Migrations\Migration; | ||
4 | +use Illuminate\Database\Schema\Blueprint; | ||
5 | +use Illuminate\Support\Facades\Schema; | ||
6 | + | ||
7 | +class AlterOneUsersTable extends Migration | ||
8 | +{ | ||
9 | + /** | ||
10 | + * Run the migrations. | ||
11 | + * | ||
12 | + * @return void | ||
13 | + */ | ||
14 | + public function up() | ||
15 | + { | ||
16 | + Schema::table('users', function (Blueprint $table) { | ||
17 | + | ||
18 | + $table->string('email')->after('mobile')->nullable()->comment('邮箱'); | ||
19 | + }); | ||
20 | + } | ||
21 | + | ||
22 | + /** | ||
23 | + * Reverse the migrations. | ||
24 | + * | ||
25 | + * @return void | ||
26 | + */ | ||
27 | + public function down() | ||
28 | + { | ||
29 | + Schema::dropColumns('users', ['email']); | ||
30 | + } | ||
31 | +} |
resources/views/mail/verify_code.blade.php
0 → 100644
... | @@ -15,6 +15,12 @@ use Illuminate\Routing\Router; | ... | @@ -15,6 +15,12 @@ use Illuminate\Routing\Router; |
15 | */ | 15 | */ |
16 | 16 | ||
17 | Route::prefix('v1')->namespace('App\Http\Controllers\V1')->group(function (Router $api){ | 17 | Route::prefix('v1')->namespace('App\Http\Controllers\V1')->group(function (Router $api){ |
18 | + /** 用户账密登录*/ | ||
19 | + $api->post('/login', 'UserController@login'); | ||
20 | + $api->post('/register', 'UserController@register'); | ||
21 | +}); | ||
22 | + | ||
23 | +Route::prefix('v1')->namespace('App\Http\Controllers\V1')->group(function (Router $api){ | ||
18 | /** 移动端微信用户登录*/ | 24 | /** 移动端微信用户登录*/ |
19 | $api->get('login/{service}/callback', 'AuthController@apiHandleProviderCallback'); | 25 | $api->get('login/{service}/callback', 'AuthController@apiHandleProviderCallback'); |
20 | }); | 26 | }); | ... | ... |
... | @@ -24,6 +24,10 @@ Route::get('/', function () { | ... | @@ -24,6 +24,10 @@ Route::get('/', function () { |
24 | return view('welcome'); | 24 | return view('welcome'); |
25 | }); | 25 | }); |
26 | 26 | ||
27 | +Route::get('/phpinfo', function () { | ||
28 | + phpinfo(); | ||
29 | +}); | ||
30 | + | ||
27 | 31 | ||
28 | Route::get('/create_overlay', function () { | 32 | Route::get('/create_overlay', function () { |
29 | header ('Content-Type: image/png'); | 33 | header ('Content-Type: image/png'); |
... | @@ -41,4 +45,10 @@ Route::get('/create_overlay', function () { | ... | @@ -41,4 +45,10 @@ Route::get('/create_overlay', function () { |
41 | imagedestroy($im); | 45 | imagedestroy($im); |
42 | 46 | ||
43 | dd($im); | 47 | dd($im); |
48 | +}); | ||
49 | + | ||
50 | +/** 预览邮件发送效果 */ | ||
51 | +Route::get('/verify_page',function (){ | ||
52 | + | ||
53 | + return new \App\Mail\SendVerifyCode('1234'); | ||
44 | }); | 54 | }); |
... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
-
Please register or login to post a comment