'json', ]; /** * Get the identifier that will be stored in the subject claim of the JWT. * * @return mixed */ public function getJWTIdentifier() { return $this->getKey(); } /** * Return a key value array, containing any custom claims to be added to the JWT. * * @return array */ public function getJWTCustomClaims() { return ['role' => 'admin']; } public function isSuperAdmin() { return in_array($this->attributes['id'], config('site.superAdmin_ids', [])); } protected static function booted() { parent::booted(); // TODO: Change the autogenerated stub self::updated(function (Admin $model) { if ($model->isDirty('status')) { //退出登录 if ($model->status == ModelStatusEnum::PAUSE) { SingleLoginLimit::delToken('admins', $model->id); } } }); self::deleted(function (Admin $model) { SingleLoginLimit::delToken('admins', $model->id); }); } public function department() { return $this->belongsTo(Department::class)->select(['id', 'name'])->withDefault([ 'id' => 0, 'name' => '未知', ]); } public function company() { return $this->belongsTo(Department::class)->select(['id', 'name'])->withDefault([ 'id' => 0, 'name' => '未知', ]); } public function job() { return $this->belongsTo(Job::class)->select(['id', 'name'])->withDefault([ 'id' => 0, 'name' => '未知', ]); } /** * 检查用户名是否存在 * @param $username * @param $ignore_id * @return bool */ public static function checkUsernameIsUnique($username, $ignore_id = 0) { return self::query()->where('username', $username)->where('id', '<>', $ignore_id)->exists(); } /** * 用户ID转shopId[门店] * @param $admin_id * @return */ public static function byIdGetRoleDataPermission($id) { $admin = self::query()->where('id', $id)->first(); $role = $admin->roles()->orderBy('data_permission_type', 'asc')->first(); if ($role->data_permission_type == Role::DATA_TYPE_CUSTOM) { return [ 'type' => $role->data_permission_type, 'ids' => $role->departments->pluck('id')->toArray(), ]; } return [ 'type' => $role->data_permission_type, ]; } public function hasMenuBtn($mid, $admin = false) { if (!$admin) $admin = login_admin(); if ($admin->isSuperAdmin()) return true; $roles = $admin->roles()->pluck('id'); return DB::table('base_roles_menus')->whereIn('role_id', $roles)->where('menu_id', $mid)->exists(); } }