| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- <?php
- namespace app\common\library\ps;
- use app\common\library\Auth as FrontendAuth;
- use think\Request;
- /**
- * 当前请求的工厂上下文。超管后台默认不注入工厂条件。
- */
- class FactoryContext
- {
- /**
- * 是否跳过工厂隔离(超管后台)
- * @return bool
- */
- public static function bypass()
- {
- if (defined('IS_CLI') && IS_CLI) {
- return true;
- }
- $module = Request::instance()->module();
- return $module === 'admin';
- }
- protected static $forcedId = 0;
- /**
- * Open API 绑定当前工厂(无会员登录)
- * @param int $factoryId
- */
- public static function bindFactory($factoryId)
- {
- self::$forcedId = (int)$factoryId;
- }
- /**
- * 当前会员所属工厂 ID
- * @return int
- */
- public static function id()
- {
- if (self::$forcedId > 0) {
- return self::$forcedId;
- }
- $auth = FrontendAuth::instance();
- if (!$auth || !$auth->isLogin()) {
- return 0;
- }
- return (int)$auth->factory_id;
- }
- /**
- * 当前操作员会员 ID
- * @return int
- */
- public static function userId()
- {
- $auth = FrontendAuth::instance();
- if (!$auth || !$auth->isLogin()) {
- return 0;
- }
- return (int)$auth->id;
- }
- /**
- * 是否工厂管理员
- * @return bool
- */
- public static function isOwner()
- {
- $auth = FrontendAuth::instance();
- return $auth && $auth->isLogin() && $auth->factory_role === 'owner';
- }
- /**
- * Open API 已按 Key 绑定工厂(无会员登录)
- * @return bool
- */
- public static function isApiBound()
- {
- return self::$forcedId > 0;
- }
- /**
- * 未入厂则抛业务异常给 API 基类处理
- * @return int
- */
- public static function requireId()
- {
- $id = self::id();
- if ($id <= 0 && !self::bypass()) {
- throw new \think\Exception('账号未绑定工厂');
- }
- return $id;
- }
- }
|