Files
xk-api-old-laravel/app/Http/Controllers/Member/MemberBaseController.php
2025-01-11 14:16:20 +08:00

93 lines
1.9 KiB
PHP

<?php
namespace App\Http\Controllers\Member;
use App\Http\Controllers\Controller;
use App\Models\YiiModels\Drugstore;
use App\Models\YiiModels\Store;
use App\Models\YiiModels\User;
use Illuminate\Support\Facades\Auth;
/**
* 用户端基类
*/
class MemberBaseController extends Controller
{
protected ?Store $store = null;
protected ?Drugstore $drugstore = null;
protected ?int $store_id = 0;
protected ?int $drugstore_id = 0;
/**
* @comment 用户
* @return \App\Models\YiiModels\User|\Illuminate\Contracts\Auth\Authenticatable|null
*/
public function user()
{
return Auth::guard(User::GUARD)->user();
}
/**
* @comment 用户id
* @return int
*/
public function userId(): int
{
return $this->user()->id;
}
/**
* 当前仓库ID
*
* @return int
*/
public function getDrugstoreId(): int
{
if (!$this->drugstore_id) {
$this->drugstore_id = $this->getStore()->drugstore_id;
}
return $this->drugstore_id;
}
/**
* 当前仓库
*
* @return Drugstore
*/
public function getDrugstore(): Drugstore
{
$this->drugstore = Drugstore::where('id', $this->getDrugstoreId())->first();
if (!$this->drugstore) {
json_error('药房信息获取失败');
}
return $this->drugstore;
}
/**
* 获取门店ID
*
* @return int
*/
public function getStoreId(): int
{
if (!$this->store_id) {
$this->store_id = (int)request()->input('store_id');
}
return $this->store_id;
}
/**
* 获取门店
*
* @return Store
*/
public function getStore(): Store
{
$this->store = Store::where('id', $this->getStoreId())->first();
if (!$this->store) {
json_error('门店信息获取失败');
}
return $this->store;
}
}