generated from liutk/owl-admin-base
59 lines
1.4 KiB
PHP
59 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Api;
|
|
|
|
use App\Models\User;
|
|
use Illuminate\Http\Response;
|
|
|
|
/**
|
|
* @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;
|
|
}
|
|
|
|
public function bindPhone(User $user, $phone)
|
|
{
|
|
if(User::where('phone', $phone)->where('id', '<>', $user->id)->exists()){
|
|
return response()->json(['data'=>[], 'code'=> Response::HTTP_BAD_REQUEST, 'message' => '该手机号已被其他微信号绑定,请更换手机号绑定']);
|
|
}
|
|
|
|
return $user->update([
|
|
'phone' => $phone,
|
|
'bind_phone_at' => now()
|
|
]);
|
|
}
|
|
|
|
} |