Files
xin-school/modules/Merchant/Http/Controllers/Portal/MerchantAuthTrait.php
T
2026-06-17 12:41:49 +08:00

47 lines
1.1 KiB
PHP

<?php
namespace Modules\Merchant\Http\Controllers\Portal;
use Illuminate\Support\Facades\Auth;
use Modules\Merchant\Models\MerchantStoreModel;
trait MerchantAuthTrait
{
protected ?int $_merchantId = null;
/**
* 获取当前登录商户的 ID
* 通过 sys_user.id -> merchants.user_id 关联获取
*/
public function getMerchantId(): int
{
if ($this->_merchantId === null) {
$userId = Auth::id();
$merchant = MerchantStoreModel::where('user_id', $userId)->first(['id']);
if (!$merchant) {
$this->throwError('商户信息不存在,请联系管理员');
}
$this->_merchantId = $merchant->id;
}
return $this->_merchantId;
}
/**
* 获取当前登录商户的完整模型
*/
public function getMerchant(): MerchantStoreModel
{
$userId = Auth::id();
$merchant = MerchantStoreModel::where('user_id', $userId)->first();
if (!$merchant) {
$this->throwError('商户信息不存在,请联系管理员');
}
return $merchant;
}
}