/**
* @notes 更新登录信息
* @param $userId
* @throws Exception
* @author 段誉
* @date 2022/9/20 19:46
*/
public static function updateLoginInfo($userId)
{
$user = User::findOrEmpty($userId);
if ($user->isEmpty()) {
throw new Exception('用户不存在');
}
$time = time();
$user->login_time = $time;
$user->login_ip = request()->ip();
$user->update_time = $time;
if(empty($user->city)){
$user->city = self::getAddressByIp();
}
$user->save();
}
// 腾讯地图api根据ip获取地址
public static function getAddressByIp()
{
$url = "https://apis.map.qq.com/ws/location/v1/ip?ip=". request()->ip() ."&key=DRHBZ-QZC6Z-2OFXB-7YX5T-LHXKF-2LFFD";
$response = self::https_request($url);
$response = json_decode($response, true);
// print_r($response);
return $response['result']['ad_info']['city'];
}
public static function https_request($url)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, (string)$url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($curl);
curl_close($curl);
return $output;
}