65 lines
2.1 KiB
PHP
65 lines
2.1 KiB
PHP
<?php
|
||
|
||
namespace App\Http\Controllers\Mini;
|
||
|
||
use App\Exceptions\RepositoryException;
|
||
use App\Models\StoreModel;
|
||
use App\Models\SupplierModel;
|
||
use App\Models\UserModel;
|
||
use Illuminate\Http\Request;
|
||
use Modules\Common\Http\Controllers\BaseController;
|
||
|
||
/**
|
||
* 小程序端控制器基类
|
||
*
|
||
* 无 #[RequestAttribute],不会被 AnnoRoute 注册为路由。
|
||
* 提供当前用户获取与门店/供应商绑定前置校验。
|
||
*/
|
||
abstract class BaseMiniController extends BaseController
|
||
{
|
||
/**
|
||
* 当前小程序用户(auth:sanctum 注入的 tokenable)
|
||
*/
|
||
protected function currentUser(Request $request): UserModel
|
||
{
|
||
$user = UserModel::find($request->user()->id);
|
||
if ($user === null || $user->status === UserModel::STATUS_DISABLED) {
|
||
throw new RepositoryException('账号不存在或已被停用');
|
||
}
|
||
|
||
return $user;
|
||
}
|
||
|
||
/**
|
||
* 门店端前置校验:type=门店 且 store_id>0 且门店正常
|
||
*/
|
||
protected function ensureStoreBound(UserModel $user): StoreModel
|
||
{
|
||
if ($user->type !== UserModel::TYPE_STORE || $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;
|
||
}
|
||
|
||
/**
|
||
* 供应商端前置校验:type=供应商 且 supplier_id>0 且供应商正常
|
||
*/
|
||
protected function ensureSupplierBound(UserModel $user): SupplierModel
|
||
{
|
||
if ($user->type !== UserModel::TYPE_SUPPLIER || $user->supplier_id <= 0) {
|
||
throw new RepositoryException('尚未绑定供应商,请联系客服处理');
|
||
}
|
||
$supplier = SupplierModel::find($user->supplier_id);
|
||
if ($supplier === null || $supplier->status !== SupplierModel::STATUS_NORMAL) {
|
||
throw new RepositoryException('供应商不存在或已停用,请联系客服处理');
|
||
}
|
||
|
||
return $supplier;
|
||
}
|
||
}
|