Admin.php 4.3 KB
<?php
/**
+-----------------------------------------------------------------------------------------------------------------------
 * 模型层:管理端用户 模型类
+-----------------------------------------------------------------------------------------------------------------------
 *
 * PHP version 7
 *
 * @category  App\Models\User
 * @package   App\Models\User
 * @author    Richer <yangzi1028@163.com>
 * @date      2021年6月16日10:41:08
 * @copyright 2020-2022 Richer (http://www.Richer.com/)
 * @license   http://www.Richer.com/ License
 * @link      http://www.Richer.com/
 */
namespace App\Models\User;

use App\Models\Traits\UserTrait;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Support\Arr;
use Tymon\JWTAuth\Contracts\JWTSubject;

/**
 * Class User
 *
 * @category  App\Models\User
 * @package   App\Models\User
 * @author    Richer <yangzi1028@163.com>
 * @date      2021年6月16日10:41:08
 * @copyright 2020-2022 Richer (http://www.Richer.com/)
 * @license   http://www.Richer.com/ License
 * @link      http://www.Richer.com/
 */

class Admin extends Authenticatable implements JWTSubject
{
    use UserTrait;

    // 指定数据库表
    const TABLE = 'admin_users';
    protected $table = self::TABLE;

    // 指定对象显示名称:方便系统统一查询和做其他处理
    const OBJ_NAME      = 'admin';
    const OBJ_NAME_ZH   = '管理用户';

    /**
     * 用户状态业务常量配置
     */
    const ENABLE    = 1;// 有效
    const DISABLE   = 2;// 禁用

    /**
     * 用户状态业务常量配置[1 => '有效', 2 => '禁用']
     */
    const STATUS_OPTIONS = [
        self::ENABLE    => '有效',
        self::DISABLE   => '禁用',
    ];

    /**
     * 用户状态颜色业务常量配置[1 => '待支付', 2 => '已完成', 3 => '已取消']
     */
    const STATUS_COLOR_OPTIONS = [
        self::ENABLE    => 'success',
        self::DISABLE   => 'danger',
    ];

    // 用于laravel-admin switch 控件
    const STATUS_STATES = [
        'on'  => [
            'value' => self::ENABLE,
            'text'  => self::STATUS_OPTIONS[self::ENABLE],
            'color' => 'success'
        ],
        'off' => [
            'value' => self::DISABLE,
            'text'  => self::STATUS_OPTIONS[self::DISABLE],
            'color' => 'default'
        ],
    ];

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'id','username', 'password','name','avatar','remember_token', 'status','role'
    ];

    /**
     * 访问器被附加到模型数组的形式。
     *
     * @var array
     */
    protected $appends = ['status_show', 'role'];

    /**
     * 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'];
    }

    /**
     * A user has and belongs to many roles.
     *
     * @return BelongsToMany
     */
    public function roles(): BelongsToMany
    {
        $pivotTable = config('admin.database.role_users_table');

        $relatedModel = config('admin.database.roles_model');

        return $this->belongsToMany($relatedModel, $pivotTable, 'user_id', 'role_id');
    }

    /**
     * 获取角色
     *
     * @return mixed
     */
    public function getRoleAttribute()
    {
        // 只获取最新的角色
        $role = $this->roles()->first();
        return optional($role)->id;
        return $this->roles()->get();
    }

//    /**
//     * 获取角色
//     *
//     * @return mixed
//     */
//    public function getRoleIdAttribute()
//    {
//        // 只获取最新的角色
//        $role = $this->roles()->first();
//        return optional($role)->id;
//        return $this->roles()->get();
//    }

    /**
     * 获取状态的中文显示
     *
     * @return mixed
     */
    public function getStatusShowAttribute()
    {
        return Arr::get(self::STATUS_OPTIONS, $this->status ? : self::ENABLE);
    }
}