优化登录注册
parent
4d4da341d2
commit
ee9fd8f447
|
|
@ -4,6 +4,9 @@ namespace App\Constants;
|
|||
|
||||
class Device
|
||||
{
|
||||
public const PC = 'pc';
|
||||
// 商城
|
||||
public const UNIAPP = 'uniapp';
|
||||
|
||||
// 商户端
|
||||
public const MERCHANT = 'merchant';
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,35 +4,61 @@ namespace App\Endpoint\Api\Http\Controllers\Auth;
|
|||
|
||||
use App\Constants\Device;
|
||||
use App\Endpoint\Api\Http\Controllers\Controller;
|
||||
use App\Endpoint\Api\Http\Requests\LoginRequest;
|
||||
use App\Exceptions\BizException;
|
||||
use App\Models\User;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class LoginController extends Controller
|
||||
{
|
||||
/**
|
||||
* 登录
|
||||
*
|
||||
* @param \App\Endpoint\Api\Http\Requests\LoginRequest $request
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return \Illuminate\Http\JsonResponse
|
||||
*/
|
||||
public function __invoke(LoginRequest $request)
|
||||
public function __invoke(Request $request)
|
||||
{
|
||||
$user = User::where('phone', $request->input('phone'))->first();
|
||||
$validated = $request->validate([
|
||||
'phone' => ['bail', 'required', 'string'],
|
||||
'password' => ['bail', 'required', 'string'],
|
||||
]);
|
||||
|
||||
if (! $user?->verifyPassword($request->input('password'))) {
|
||||
$user = User::where('phone', $validated['phone'])->first();
|
||||
|
||||
if (! $user?->verifyPassword($validated['password'])) {
|
||||
throw new BizException(__('Incorrect account or password'));
|
||||
}
|
||||
|
||||
$user->update([
|
||||
'last_login_at' => now(),
|
||||
'last_login_ip' => $request->realIp(),
|
||||
if ($user->old_password) {
|
||||
$user->password = $validated['password'];
|
||||
$user->old_password = null;
|
||||
}
|
||||
$user->last_login_at = now();
|
||||
$user->last_login_ip = $request->realIp();
|
||||
$user->save();
|
||||
|
||||
// 获取登录设备
|
||||
$device = $request->header('client-app', Device::UNIAPP);
|
||||
|
||||
switch ($device) {
|
||||
case Device::MERCHANT:
|
||||
// 清理此用户的商户端令牌
|
||||
$user->tokens()->where('name', $device)->delete();
|
||||
// 颁发新的商户端令牌
|
||||
$token = $user->createToken($device);
|
||||
break;
|
||||
|
||||
default:
|
||||
$device = Device::UNIAPP;
|
||||
// 清理此用户的商城端令牌
|
||||
$user->tokens()->where('name', $device)->delete();
|
||||
// 颁发新的商城端令牌
|
||||
$token = $user->createToken($device, ['mall']);
|
||||
break;
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'token' => $token->plainTextToken,
|
||||
]);
|
||||
|
||||
$user->tokens()->delete();
|
||||
|
||||
return response()->json(
|
||||
$user->createDeviceToken(Device::UNIAPP)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -59,9 +59,11 @@ class RegisterController extends Controller
|
|||
throw new BizException(__('Registration failed, please try again'));
|
||||
}
|
||||
|
||||
return response()->json(
|
||||
$user->createDeviceToken(Device::UNIAPP)
|
||||
);
|
||||
$token = $user->createToken(Device::UNIAPP, ['mall']);
|
||||
|
||||
return response()->json([
|
||||
'token' => $token->plainTextToken,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -1,31 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace App\Endpoint\Api\Http\Requests;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class LoginRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function authorize()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
'phone' => ['bail', 'required', 'string'],
|
||||
'password' => ['bail', 'required', 'string'],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
@ -179,6 +179,7 @@ Route::group([
|
|||
'prefix' => 'merchant',
|
||||
'middleware' => [
|
||||
'guard:api',
|
||||
'ability:merchant',
|
||||
],
|
||||
], function () {
|
||||
Route::get('account', [Merchant\UserController::class, 'account']);
|
||||
|
|
|
|||
|
|
@ -65,5 +65,6 @@ class Kernel extends HttpKernel
|
|||
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
|
||||
'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
|
||||
'admin.permissions' => \App\Admin\Middleware\Permissions::class,
|
||||
'ability' => \Laravel\Sanctum\Http\Middleware\CheckForAnyAbility::class,
|
||||
];
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,7 +2,6 @@
|
|||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Constants\Device;
|
||||
use Dcat\Admin\Traits\HasDateTimeFormatter;
|
||||
use Illuminate\Auth\Authenticatable;
|
||||
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
|
||||
|
|
@ -12,6 +11,7 @@ use Illuminate\Database\Eloquent\Model;
|
|||
use Illuminate\Database\Eloquent\Relations\HasOne;
|
||||
use Illuminate\Foundation\Auth\Access\Authorizable;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Illuminate\Support\Str;
|
||||
use Laravel\Sanctum\HasApiTokens;
|
||||
|
||||
class User extends Model implements AuthorizableContract, AuthenticatableContract
|
||||
|
|
@ -330,20 +330,12 @@ class User extends Model implements AuthorizableContract, AuthenticatableContrac
|
|||
*/
|
||||
public function verifyPassword(string $password): bool
|
||||
{
|
||||
return $this->password && Hash::check($password, $this->password);
|
||||
}
|
||||
// 如果旧密码存在,则校验旧密码
|
||||
if ($this->old_password) {
|
||||
return $this->old_password === md5($password);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建设备授权令牌
|
||||
*
|
||||
* @param string $device
|
||||
* @return array
|
||||
*/
|
||||
public function createDeviceToken(string $device = null): array
|
||||
{
|
||||
return [
|
||||
'token' => $this->createToken($device ?: Device::PC)->plainTextToken,
|
||||
];
|
||||
return $this->password && Hash::check($password, $this->password);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -357,24 +349,19 @@ class User extends Model implements AuthorizableContract, AuthenticatableContrac
|
|||
{
|
||||
$user = static::query()->create($attributes);
|
||||
|
||||
$user->userInfo()->create(
|
||||
$inviter ? [
|
||||
'inviter_id' => $inviter->id,
|
||||
'depth' => $inviter->userInfo->depth + 1,
|
||||
'path' => $inviter->userInfo->path.$inviter->id.'/',
|
||||
] : [
|
||||
'inviter_id' => null,
|
||||
'depth' => 1,
|
||||
'path' => '/',
|
||||
]
|
||||
);
|
||||
// 邀请人的深度
|
||||
$depth = (int) $inviter?->userInfo?->depth;
|
||||
|
||||
$user->userInfo()->create([
|
||||
'inviter_id' => $inviter?->id,
|
||||
'depth' => $depth + 1,
|
||||
'path' => Str::finish($inviter?->userInfo?->full_path, '/'),
|
||||
]);
|
||||
|
||||
//初始化钱包
|
||||
$user->wallet()->create();
|
||||
//初始化余额
|
||||
$user->balance()->create();
|
||||
//初始化绑定的银行卡
|
||||
$user->bank()->create();
|
||||
|
||||
return $user;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Helpers\Str as StrHelper;
|
||||
use Dcat\Admin\Traits\HasDateTimeFormatter;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Str;
|
||||
|
|
@ -126,7 +127,7 @@ class UserInfo extends Model
|
|||
// 如果没有邀请码,则自动分配邀请码
|
||||
if ($userInfo->code === null) {
|
||||
do {
|
||||
$userInfo->code = strtolower(Str::randomAlpha(6));
|
||||
$userInfo->code = strtolower(StrHelper::randomAlpha(6));
|
||||
} while (static::where('code', $userInfo->code)->exists());
|
||||
} elseif ($userInfo->isDirty('code')) {
|
||||
$userInfo->code = strtolower($userInfo->code);
|
||||
|
|
|
|||
|
|
@ -0,0 +1,32 @@
|
|||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class AddNameIndexToPersonalAccessTokensTable extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::table('personal_access_tokens', function (Blueprint $table) {
|
||||
$table->index('name');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::table('personal_access_tokens', function (Blueprint $table) {
|
||||
$table->dropIndex(['name']);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class AddOldPasswordToUsersTable extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::table('users', function (Blueprint $table) {
|
||||
$table->string('old_password')->nullable()->comment('旧密码');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::table('users', function (Blueprint $table) {
|
||||
$table->dropColumn(['old_password']);
|
||||
});
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue