小程序用户改用门店登录
This commit is contained in:
@@ -4,7 +4,6 @@ namespace App\Http\Controllers\Mini;
|
||||
|
||||
use App\Exceptions\RepositoryException;
|
||||
use App\Models\StoreModel;
|
||||
use App\Models\UserModel;
|
||||
use Illuminate\Http\Request;
|
||||
use Modules\Common\Http\Controllers\BaseController;
|
||||
use Modules\SystemUser\Models\SysAccessToken;
|
||||
@@ -13,71 +12,39 @@ use Modules\SystemUser\Models\SysAccessToken;
|
||||
* 小程序端控制器基类
|
||||
*
|
||||
* 无 #[RequestAttribute],不会被 AnnoRoute 注册为路由。
|
||||
* 提供当前用户获取与门店绑定前置校验。
|
||||
* 门店即用户:登录主体就是门店(store 表),提供当前门店获取与状态校验。
|
||||
*/
|
||||
abstract class BaseMiniController extends BaseController
|
||||
{
|
||||
/**
|
||||
* 当前小程序用户(auth:sanctum 注入的 tokenable)
|
||||
* 当前登录门店(auth:sanctum 注入的 tokenable)
|
||||
*/
|
||||
protected function currentUser(Request $request): UserModel
|
||||
protected function currentStore(Request $request): StoreModel
|
||||
{
|
||||
$user = UserModel::find($request->user()->id);
|
||||
if ($user === null || $user->status === UserModel::STATUS_DISABLED) {
|
||||
$store = StoreModel::find($request->user()->id);
|
||||
if ($store === null || $store->status === StoreModel::STATUS_DISABLED) {
|
||||
throw new RepositoryException('账号不存在或已被停用');
|
||||
}
|
||||
return $user;
|
||||
return $store;
|
||||
}
|
||||
|
||||
/**
|
||||
* 可选登录场景手动识别当前用户(路由关闭 authorize 时使用)
|
||||
* 可选登录场景手动识别当前门店(路由关闭 authorize 时使用)
|
||||
*
|
||||
* 手动解析 Bearer token;未携带 token、token 无效或非小程序用户时返回 null(不抛错)。
|
||||
* 手动解析 Bearer token;未携带 token、token 无效或非门店账号时返回 null(不抛错)。
|
||||
*/
|
||||
protected function optionalUser(Request $request): ?UserModel
|
||||
protected function optionalStore(Request $request): ?StoreModel
|
||||
{
|
||||
$token = $request->bearerToken();
|
||||
if (empty($token)) {
|
||||
return null;
|
||||
}
|
||||
$accessToken = SysAccessToken::findToken($token);
|
||||
if ($accessToken === null || $accessToken->tokenable_type !== UserModel::class) {
|
||||
if ($accessToken === null || $accessToken->tokenable_type !== StoreModel::class) {
|
||||
return null;
|
||||
}
|
||||
$user = UserModel::find($accessToken->tokenable_id);
|
||||
if ($user === null || $user->status === UserModel::STATUS_DISABLED) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $user;
|
||||
}
|
||||
|
||||
/**
|
||||
* 门店端前置校验
|
||||
*/
|
||||
protected function ensureStoreBound(UserModel $user): StoreModel
|
||||
{
|
||||
if ($user->store_id <= 0) {
|
||||
throw new RepositoryException('尚未绑定门店,请联系客服处理');
|
||||
}
|
||||
$store = StoreModel::find($user->store_id);
|
||||
if ($store === null || $store->status !== StoreModel::STATUS_NORMAL) {
|
||||
throw new RepositoryException('门店不存在或已停用,请联系客服处理');
|
||||
}
|
||||
|
||||
return $store;
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户当前绑定的正常门店(未绑定/已停用返回 null,不抛错)
|
||||
*/
|
||||
protected function boundStore(UserModel $user): ?StoreModel
|
||||
{
|
||||
if ($user->store_id <= 0) {
|
||||
return null;
|
||||
}
|
||||
$store = StoreModel::find($user->store_id);
|
||||
if ($store === null || $store->status !== StoreModel::STATUS_NORMAL) {
|
||||
$store = StoreModel::find($accessToken->tokenable_id);
|
||||
if ($store === null || $store->status === StoreModel::STATUS_DISABLED) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user