'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() { // self::saved(function (Admin $admin) { // $admin->syncRoles($admin->type); // }); } 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' => '未知', ]); } public function shop() { return $this->belongsTo(Grade::class, 'shop_id'); } /** * 检查用户名是否存在 * @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 id2ShopId($admin_id) // { // return Cache::remember("model:Admin:id2ShopId:{$admin_id}", Carbon::now()->addDay(), function () use ($admin_id) { // return ModelAdmin::byAdminIdGetModelId($admin_id, Shop::class); // }); // } // public function byIdGetRoleDataPermission() // { // $role = $this->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 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(); } public function records() { return $this->hasMany(\App\Repositories\Models\Exam\Record::class, 'admin_id'); } }