84 lines
2.2 KiB
PHP
Raw Normal View History

2019-12-03 21:43:37 +08:00
<?php
namespace catchAdmin\user\model;
2020-01-13 21:23:45 +08:00
use catchAdmin\permissions\model\Department;
2020-01-12 09:30:56 +08:00
use catchAdmin\permissions\model\HasJobsTrait;
2019-12-09 16:22:00 +08:00
use catchAdmin\permissions\model\HasRolesTrait;
2019-12-11 20:59:59 +08:00
use catcher\base\CatchModel;
2019-12-03 21:43:37 +08:00
2019-12-11 20:59:59 +08:00
class Users extends CatchModel
2019-12-03 21:43:37 +08:00
{
2019-12-09 16:22:00 +08:00
use HasRolesTrait;
2020-01-12 09:30:56 +08:00
use HasJobsTrait;
2020-01-13 21:23:45 +08:00
use UserSearch;
2019-12-03 21:43:37 +08:00
protected $name = 'users';
2019-12-07 17:31:38 +08:00
2019-12-03 21:43:37 +08:00
protected $field = [
'id', //
'username', // 用户名
'password', // 用户密码
'email', // 邮箱 登录
2020-01-13 21:23:45 +08:00
'creator_id', // 创建者ID
'department_id', // 部门ID
2019-12-03 21:43:37 +08:00
'status', // 用户状态 1 正常 2 禁用
'last_login_ip', // 最后登录IP
'last_login_time', // 最后登录时间
'created_at', // 创建时间
'updated_at', // 更新时间
2019-12-07 17:31:38 +08:00
'deleted_at', // 删除状态0未删除 >0 已删除
2019-12-03 21:43:37 +08:00
2019-12-07 17:31:38 +08:00
];
/**
* set password
*
* @time 2019年12月07日
* @param $value
* @return false|string
*/
public function setPasswordAttr($value)
{
return password_hash($value, PASSWORD_DEFAULT);
}
2019-12-09 16:22:00 +08:00
/**
* 用户列表
*
* @time 2019年12月08日
* @param $search
* @throws \think\db\exception\DbException
* @return \think\Paginator
*/
public function getList($search): \think\Paginator
2019-12-07 17:31:38 +08:00
{
2020-01-19 15:50:15 +08:00
return $this->withoutField(['updated_at'], true)
->catchSearch()
2020-01-13 21:23:45 +08:00
->catchLeftJoin(Department::class, 'id', 'department_id', ['department_name'])
->order('users.id', 'desc')
2020-01-21 17:55:54 +08:00
->paginate($search['limit'] ?? parent::LIMIT);
2019-12-07 17:31:38 +08:00
}
2019-12-12 18:52:24 +08:00
/**
* 获取权限
*
* @time 2019年12月12日
* @param $uid
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @return array
*/
public function getPermissionsBy($uid = 0): array
{
$roles = $uid ? $this->findBy($uid)->getRoles() : $this->getRoles();
$permissionIds = [];
foreach ($roles as $role) {
$permissionIds = array_merge($permissionIds, $role->getPermissions()->column('id'));
}
return array_unique($permissionIds);
}
2019-12-24 18:21:57 +08:00
}