优化登录注册
parent
4d4da341d2
commit
ee9fd8f447
|
|
@ -4,6 +4,9 @@ namespace App\Constants;
|
||||||
|
|
||||||
class Device
|
class Device
|
||||||
{
|
{
|
||||||
public const PC = 'pc';
|
// 商城
|
||||||
public const UNIAPP = 'uniapp';
|
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\Constants\Device;
|
||||||
use App\Endpoint\Api\Http\Controllers\Controller;
|
use App\Endpoint\Api\Http\Controllers\Controller;
|
||||||
use App\Endpoint\Api\Http\Requests\LoginRequest;
|
|
||||||
use App\Exceptions\BizException;
|
use App\Exceptions\BizException;
|
||||||
use App\Models\User;
|
use App\Models\User;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
|
||||||
class LoginController extends Controller
|
class LoginController extends Controller
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* 登录
|
* 登录
|
||||||
*
|
*
|
||||||
* @param \App\Endpoint\Api\Http\Requests\LoginRequest $request
|
* @param \Illuminate\Http\Request $request
|
||||||
* @return \Illuminate\Http\JsonResponse
|
* @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'));
|
throw new BizException(__('Incorrect account or password'));
|
||||||
}
|
}
|
||||||
|
|
||||||
$user->update([
|
if ($user->old_password) {
|
||||||
'last_login_at' => now(),
|
$user->password = $validated['password'];
|
||||||
'last_login_ip' => $request->realIp(),
|
$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'));
|
throw new BizException(__('Registration failed, please try again'));
|
||||||
}
|
}
|
||||||
|
|
||||||
return response()->json(
|
$token = $user->createToken(Device::UNIAPP, ['mall']);
|
||||||
$user->createDeviceToken(Device::UNIAPP)
|
|
||||||
);
|
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',
|
'prefix' => 'merchant',
|
||||||
'middleware' => [
|
'middleware' => [
|
||||||
'guard:api',
|
'guard:api',
|
||||||
|
'ability:merchant',
|
||||||
],
|
],
|
||||||
], function () {
|
], function () {
|
||||||
Route::get('account', [Merchant\UserController::class, 'account']);
|
Route::get('account', [Merchant\UserController::class, 'account']);
|
||||||
|
|
|
||||||
|
|
@ -65,5 +65,6 @@ class Kernel extends HttpKernel
|
||||||
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
|
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
|
||||||
'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
|
'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
|
||||||
'admin.permissions' => \App\Admin\Middleware\Permissions::class,
|
'admin.permissions' => \App\Admin\Middleware\Permissions::class,
|
||||||
|
'ability' => \Laravel\Sanctum\Http\Middleware\CheckForAnyAbility::class,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,6 @@
|
||||||
|
|
||||||
namespace App\Models;
|
namespace App\Models;
|
||||||
|
|
||||||
use App\Constants\Device;
|
|
||||||
use Dcat\Admin\Traits\HasDateTimeFormatter;
|
use Dcat\Admin\Traits\HasDateTimeFormatter;
|
||||||
use Illuminate\Auth\Authenticatable;
|
use Illuminate\Auth\Authenticatable;
|
||||||
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
|
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\Database\Eloquent\Relations\HasOne;
|
||||||
use Illuminate\Foundation\Auth\Access\Authorizable;
|
use Illuminate\Foundation\Auth\Access\Authorizable;
|
||||||
use Illuminate\Support\Facades\Hash;
|
use Illuminate\Support\Facades\Hash;
|
||||||
|
use Illuminate\Support\Str;
|
||||||
use Laravel\Sanctum\HasApiTokens;
|
use Laravel\Sanctum\HasApiTokens;
|
||||||
|
|
||||||
class User extends Model implements AuthorizableContract, AuthenticatableContract
|
class User extends Model implements AuthorizableContract, AuthenticatableContract
|
||||||
|
|
@ -330,20 +330,12 @@ class User extends Model implements AuthorizableContract, AuthenticatableContrac
|
||||||
*/
|
*/
|
||||||
public function verifyPassword(string $password): bool
|
public function verifyPassword(string $password): bool
|
||||||
{
|
{
|
||||||
return $this->password && Hash::check($password, $this->password);
|
// 如果旧密码存在,则校验旧密码
|
||||||
}
|
if ($this->old_password) {
|
||||||
|
return $this->old_password === md5($password);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
return $this->password && Hash::check($password, $this->password);
|
||||||
* 创建设备授权令牌
|
|
||||||
*
|
|
||||||
* @param string $device
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function createDeviceToken(string $device = null): array
|
|
||||||
{
|
|
||||||
return [
|
|
||||||
'token' => $this->createToken($device ?: Device::PC)->plainTextToken,
|
|
||||||
];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -357,24 +349,19 @@ class User extends Model implements AuthorizableContract, AuthenticatableContrac
|
||||||
{
|
{
|
||||||
$user = static::query()->create($attributes);
|
$user = static::query()->create($attributes);
|
||||||
|
|
||||||
$user->userInfo()->create(
|
// 邀请人的深度
|
||||||
$inviter ? [
|
$depth = (int) $inviter?->userInfo?->depth;
|
||||||
'inviter_id' => $inviter->id,
|
|
||||||
'depth' => $inviter->userInfo->depth + 1,
|
$user->userInfo()->create([
|
||||||
'path' => $inviter->userInfo->path.$inviter->id.'/',
|
'inviter_id' => $inviter?->id,
|
||||||
] : [
|
'depth' => $depth + 1,
|
||||||
'inviter_id' => null,
|
'path' => Str::finish($inviter?->userInfo?->full_path, '/'),
|
||||||
'depth' => 1,
|
]);
|
||||||
'path' => '/',
|
|
||||||
]
|
|
||||||
);
|
|
||||||
|
|
||||||
//初始化钱包
|
//初始化钱包
|
||||||
$user->wallet()->create();
|
$user->wallet()->create();
|
||||||
//初始化余额
|
//初始化余额
|
||||||
$user->balance()->create();
|
$user->balance()->create();
|
||||||
//初始化绑定的银行卡
|
|
||||||
$user->bank()->create();
|
|
||||||
|
|
||||||
return $user;
|
return $user;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
namespace App\Models;
|
namespace App\Models;
|
||||||
|
|
||||||
|
use App\Helpers\Str as StrHelper;
|
||||||
use Dcat\Admin\Traits\HasDateTimeFormatter;
|
use Dcat\Admin\Traits\HasDateTimeFormatter;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Support\Str;
|
use Illuminate\Support\Str;
|
||||||
|
|
@ -126,7 +127,7 @@ class UserInfo extends Model
|
||||||
// 如果没有邀请码,则自动分配邀请码
|
// 如果没有邀请码,则自动分配邀请码
|
||||||
if ($userInfo->code === null) {
|
if ($userInfo->code === null) {
|
||||||
do {
|
do {
|
||||||
$userInfo->code = strtolower(Str::randomAlpha(6));
|
$userInfo->code = strtolower(StrHelper::randomAlpha(6));
|
||||||
} while (static::where('code', $userInfo->code)->exists());
|
} while (static::where('code', $userInfo->code)->exists());
|
||||||
} elseif ($userInfo->isDirty('code')) {
|
} elseif ($userInfo->isDirty('code')) {
|
||||||
$userInfo->code = strtolower($userInfo->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