where(AdminMerchant::getMerchantWhere()) public function admin_user() { return $this->hasOne(AdminUser::class, 'id', 'admin_id'); } /** * 获取登录账号的商户id * @return int|mixed */ public static function getMchId() { $merchant_id = 0; if (Admin::user()->isRole(AdminRole::ADMINISTROTOR)) { // 技术 $merchant_id = 0; } else if (Admin::user()->isRole(AdminRole::MERCHANTSADMIN)) { // 商户 $merchant_id = AdminMerchant::query()->where(['admin_id' => Admin::user()->id])->first(['id'])['id']; } else { // 非商户 $merchant_id = Admin::user()->merchant_id; } return $merchant_id; } /** * 查询 merchant_id where 条件 数组 * @return array */ public static function getMerchantWhere() { $where = []; if (Admin::user()->isRole(AdminRole::ADMINISTROTOR)) { // 技术 $where = []; } else if (Admin::user()->isRole(AdminRole::MERCHANTSADMIN)) { // 商户 $merchant_id = \Session::get('sh_merchant_id'); if (empty($merchant_id)) { $merchant_id = AdminMerchant::query()->where(['admin_id' => Admin::user()->id])->first(['id'])['id']; \Session::put('sh_merchant_id', $merchant_id); } $where['merchant_id'] = $merchant_id; } else { // 非商户 $where['merchant_id'] = Admin::user()->merchant_id; } return $where; } /** * 获取登录着的商户id * @return int|mixed */ public static function putMerchantId() { if (Admin::user()->isRole(AdminRole::ADMINISTROTOR)) { // 技术 $merchant_id = 0; } else if (Admin::user()->isRole(AdminRole::MERCHANTSADMIN)) { // 商户 $merchant_id = AdminMerchant::query()->where(['admin_id' => Admin::user()->id])->first(['id'])['id']; } else { // 非商户 $merchant_id = Admin::user()->merchant_id; } return $merchant_id; } // 判断用户是否属于商户 public static function isUserToMch($user_id) { $merchant_id = self::putMerchantId(); if ($merchant_id == 0) { // 超级管理员 return false; } $user_merchant_id = User::query()->where(['id' => $user_id, 'merchant_id' => $merchant_id]); if ($user_merchant_id) { return false; } // 不存在提示错误 return true; } }