mirror of
https://github.com/henry4682/finance_app.git
synced 2026-07-16 08:20:00 +00:00
48 lines
1.5 KiB
PHP
48 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Security;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Http;
|
|
|
|
class SecurityController extends Controller
|
|
{
|
|
public function index(Request $request)
|
|
{
|
|
$validated = $request->validate([
|
|
'type' => ['required', 'in:stock,etf,fund'],
|
|
'keyword' => ['required', 'string', 'min:1'],
|
|
]);
|
|
|
|
$results = Security::query()
|
|
->where('type', $validated['type'])
|
|
->where(function ($query) use ($validated) {
|
|
$query->where('code', 'ilike', "%{$validated['keyword']}%")
|
|
->orWhere('name', 'ilike', "%{$validated['keyword']}%");
|
|
})
|
|
->orderBy('code')
|
|
->limit(10)
|
|
->get(['code', 'name']);
|
|
|
|
if ($results->isEmpty()) {
|
|
if ($validated['type'] === 'fund') {
|
|
$res = Http::post("https://www.anuefund.com/anuefundApi/Search/Light", [
|
|
'keyword' => $validated['keyword'],
|
|
]);
|
|
if ($res->ok()) {
|
|
$data = $res->json();
|
|
foreach ($data['data']['result'] as $item) {
|
|
$results->push(new Security([
|
|
'code' => $item['fundID'],
|
|
'name' => $item['fundName'],
|
|
]));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return response()->json($results);
|
|
}
|
|
} |