修改查询条件

This commit is contained in:
神楽坂 白 2023-10-31 09:44:59 +08:00
parent 2a4ea5a3d8
commit 31e4e3e660

View File

@ -18,7 +18,7 @@ public function index(Request $request)
$domain_list = config('mail.domain_list'); $domain_list = config('mail.domain_list');
$block_prefix = config('mail.block_prefix'); $block_prefix = config('mail.block_prefix');
$rand_prefix = Str::random(8); $rand_prefix = Str::random(8);
$key = Crypt::encryptString(Carbon::now()->format('Y-m-d H:i:s')); $key = Crypt::encryptString(json_encode(['id' => 0, 'time' => time()]));
return view('welcome', [ return view('welcome', [
'domain_list' => $domain_list, 'domain_list' => $domain_list,
'block_prefix' => $block_prefix, 'block_prefix' => $block_prefix,
@ -63,32 +63,23 @@ function ($attribute, $value, $fail) {
return abort(400); return abort(400);
} }
$where = [];
$to_hash = hash('sha256', $email);
$where[] = ['to_hash', '=', $to_hash];
try { try {
$key = Crypt::decryptString($key); $key = Crypt::decryptString($key);
$key = Carbon::parse($key); $key = json_decode($key, true);
if (time() - $key['time'] < 300 && !empty($key['id'])) {
$where[] = ['id', '>', $key['id']];
} else {
$where[] = ['received_at', '>', Carbon::now()];
}
} catch (\Throwable $th) { } catch (\Throwable $th) {
return abort(400); return abort(400);
} }
// $now_time = Carbon::now(); $new_email_list = Mail::where($where)
// if ($key->diffInMinutes($now_time) > 5) {
// $key = $now_time;
// }
$to_hash = hash('sha256', $email);
$max_received_at = Mail::where([
['to_hash', '=', $to_hash],
['received_at', '>', $key],
])
->orderBy('received_at', 'desc')
->value("received_at") ?? $key;
$new_email_list = Mail::where([
['to_hash', '=', $to_hash],
['received_at', '>', $key],
])
->select([ ->select([
'id as key', 'id as key',
'from', 'from',
@ -100,7 +91,12 @@ function ($attribute, $value, $fail) {
->orderBy('received_at', 'asc') ->orderBy('received_at', 'asc')
->get(); ->get();
foreach ($new_email_list as $key => $value) { $key = ['id' => $key['id'], 'time' => time()];
if ($new_email_list->count() > 0) {
$key['id'] = $new_email_list->last()->key;
}
foreach ($new_email_list as $value) {
$value->key = Crypt::encryptString($value->key); $value->key = Crypt::encryptString($value->key);
} }
@ -108,8 +104,8 @@ function ($attribute, $value, $fail) {
'code' => 1, 'code' => 1,
'msg' => '', 'msg' => '',
'data' => [ 'data' => [
'list' => $new_email_list, 'list' => $new_email_list,
'new_key' => Crypt::encryptString(Carbon::parse($max_received_at)->format('Y-m-d H:i:s')), 'new_key' => Crypt::encryptString(json_encode($key)),
], ],
]); ]);
} }
@ -157,6 +153,10 @@ public function info(Request $request, $key)
return abort(400); return abort(400);
} }
if (empty(intval($id))) {
return abort(400);
}
$mail_info = Mail::find($id); $mail_info = Mail::find($id);
if (empty($mail_info)) { if (empty($mail_info)) {
return abort(404); return abort(404);