<?php


namespace App\Models;


use Illuminate\Database\Eloquent\Model;

class AccountsIPAddress extends Model
{
    const TABLE = 'QPAccountsDB.dbo.AccountsIPAddress';
    protected $table = self::TABLE;

    // 获取IP城市
    public static function getCity($UserID, $ip)
    {
        $key = config('api.gaode')['key'];

        $url = "http://restapi.amap.com/v3/ip?key=$key&ip=" . $ip;

        $res = json_decode(file_get_contents($url));


        $ip_address = '';
        if (isset($res->status) && $res->status == 1) {
            $ip_address = !empty($res->province) ? $res->province : '';
            $ip_address .= !empty($res->city) ? '，'.$res->city : '';
        }

        self::updateOrInsert(['UserID' => $UserID], ['ip' => $ip, 'ip_address' => $ip_address]);
        return $ip_address;
    }
}

