mirror of
https://github.com/henry4682/finance_app.git
synced 2026-07-16 00:10:00 +00:00
feat: 前端分離+功能api化
1. 新增資產管理CRUD、googleOAuth登入 2. 更改原記帳controller
This commit is contained in:
48
app/Http/Controllers/Api/SecurityController.php
Normal file
48
app/Http/Controllers/Api/SecurityController.php
Normal file
@@ -0,0 +1,48 @@
|
||||
<?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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user