generated from liutk/owl-admin-base
46 lines
973 B
PHP
46 lines
973 B
PHP
<?php
|
|
|
|
namespace App\Services\Api;
|
|
|
|
use App\Models\User;
|
|
|
|
/**
|
|
* @method User getModel()
|
|
* @method User|\Illuminate\Database\Query\Builder query()
|
|
*/
|
|
class UserService
|
|
{
|
|
public static function make(): static
|
|
{
|
|
return new static;
|
|
}
|
|
|
|
public function register($miniOpenid)
|
|
{
|
|
if(! $user = User::where('mini_openid', $miniOpenid)->first()){
|
|
$user = new User();
|
|
$user->mini_openid = $miniOpenid;
|
|
$user->save();
|
|
}
|
|
|
|
return $user;
|
|
}
|
|
|
|
public function login(User $user)
|
|
{
|
|
//记录当前登录时间、IP
|
|
$ip = request()->getClientIp();
|
|
$user->last_login_ip = ip2long($ip);
|
|
$user->last_login_at = now();
|
|
$user->save();
|
|
|
|
//撤销当前所有令牌;
|
|
$user->tokens()->delete();
|
|
|
|
return $user->createToken(
|
|
name: 'api',
|
|
expiresAt: now()->addDay(),
|
|
)->plainTextToken;
|
|
}
|
|
|
|
} |