CountryRegionController.php
1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
namespace App\Http\Container\AppSection\Controllers;
use App\Helpers\JsonResource;
use App\Models\CountryRegion;
use App\Support\Controller;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Torann\GeoIP\Facades\GeoIP;
class CountryRegionController extends Controller
{
/**
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\JsonResponse
*/
public function index(Request $request): JsonResponse
{
$filter = [
'parent_id' => $request->get('parent_id', 0),
'status' => 1
];
$data = CountryRegion::filter($filter)
->with('children:id,parent_id,identifier,name')
->latest('weight')->get(['id', 'name', 'identifier']);
return JsonResource::success(JsonResource::SUCCESS, $data);
}
/**
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\JsonResponse
*/
public function ip(Request $request): JsonResponse
{
$result = GeoIP::getLocation($request->get('ip', $request->ip()));
$data = [
'ip' => $result->getAttribute('ip'),
'country' => $result->getAttribute('country') ?? '',
'region' => $result->getAttribute('state_name') ?? '',
'city' => $result->getAttribute('city') ?? ''
];
if (in_array($result->getAttribute('iso_code'), ['HK', 'MO', 'TW'], true)) {
$data['country'] = '中国';
$data['region'] = $result->getAttribute('country') ?? '';
$data['city'] = $result->getAttribute('city') ?? '';
}
return JsonResource::success(JsonResource::SUCCESS, $data);
}
}