v2board/app/Http/Controllers/Passport/CommController.php

63 lines
1.8 KiB
PHP
Raw Normal View History

2019-10-29 15:33:36 +08:00
<?php
namespace App\Http\Controllers\Passport;
use App\Http\Requests\Passport\CommSendEmailVerify;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Support\Facades\Mail;
2020-01-12 00:49:13 +08:00
use App\Utils\Helper;
2020-01-12 01:27:36 +08:00
use Illuminate\Support\Facades\Cache;
2019-10-29 15:33:36 +08:00
class CommController extends Controller
{
2020-01-11 13:36:52 +08:00
public function config()
{
2019-11-25 18:14:49 +08:00
return response([
2019-11-25 18:22:38 +08:00
'data' => [
'isEmailVerify' => (int)config('v2board.email_verify', 0) ? 1 : 0,
2019-11-25 18:23:43 +08:00
'isInviteForce' => (int)config('v2board.invite_force', 0) ? 1 : 0
2019-11-25 18:22:38 +08:00
]
2019-11-25 18:14:49 +08:00
]);
}
2020-01-11 13:36:52 +08:00
private function isEmailVerify()
{
2019-10-29 15:33:36 +08:00
return response([
2019-11-18 19:52:30 +08:00
'data' => (int)config('v2board.email_verify', 0) ? 1 : 0
2019-10-29 15:33:36 +08:00
]);
}
2020-01-11 13:36:52 +08:00
public function sendEmailVerify(CommSendEmailVerify $request)
{
2019-10-29 15:33:36 +08:00
$email = $request->input('email');
2020-01-09 12:58:01 +08:00
$cacheKey = 'sendEmailVerify:' . $email;
if (Cache::get($cacheKey)) {
2019-10-29 15:33:36 +08:00
abort(500, '验证码已发送,请过一会在请求');
}
2020-01-12 00:49:13 +08:00
$code = Helper::randomChar(6);
2019-11-18 19:52:30 +08:00
$subject = config('v2board.app_name', 'V2Board') . '邮箱验证码';
2019-12-26 23:47:01 +08:00
Mail::send(
2020-01-11 13:36:52 +08:00
'mail.sendEmailVerify',
2019-12-26 23:47:01 +08:00
[
'code' => $code,
2019-12-30 21:05:48 +08:00
'name' => config('v2board.app_name', 'V2Board'),
'url' => config('v2board.app_url')
2019-12-26 23:47:01 +08:00
],
2020-01-11 13:36:52 +08:00
function ($message) use ($email, $subject) {
$message->to($email)->subject($subject);
2019-12-26 23:47:01 +08:00
}
);
if (count(Mail::failures()) >= 1) {
// 发送失败
abort(500, '发送失败');
}
2020-01-18 18:41:02 +08:00
Cache::put($cacheKey, $code, 60);
2019-10-29 15:33:36 +08:00
return response([
'data' => true
]);
}
}