123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197 |
- <?php
- namespace App\Repositories\Models\Base;
- use App\Repositories\Enums\ModelStatusEnum;
- use App\Repositories\Models\Mentor\Grade;
- use App\Repositories\Models\Model;
- use Illuminate\Auth\Authenticatable;
- use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
- use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
- use Illuminate\Database\Eloquent\Factories\HasFactory;
- use Illuminate\Support\Facades\DB;
- use Laravel\Lumen\Auth\Authorizable;
- use Spatie\Permission\Traits\HasRoles;
- use Tymon\JWTAuth\Contracts\JWTSubject;
- class Admin extends Model implements AuthenticatableContract, AuthorizableContract, JWTSubject
- {
- use Authenticatable, Authorizable, HasFactory, HasRoles;
- /**
- * @var string
- */
- protected $table = 'base_admins';
- /**
- * The attributes that are mass assignable.
- *
- * @var array
- */
- protected $fillable = [
- 'name',
- 'username',
- 'mobile',
- 'email',
- 'headimg',
- 'sex',
- 'password',
- 'department_id',
- 'shop_id',
- 'company_id',
- 'is_view_user_info',
- 'job_id',
- 'roles_id',
- 'type',
- 'wechat_auth_id',
- 'extra_fields',
- 'status',
- 'last_login_ip',
- 'last_login_time',
- 'user_no',
- 'grade',
- 'class_name',
- ];
- // protected $guarded = [];
- /**
- * The attributes excluded from the model's JSON form.
- *
- * @var array
- */
- protected $hidden = [
- 'password'
- ];
- protected $casts = [
- 'extra_fields' => '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');
- }
- }
|