test_lgq/niucloud/app/model/sys/SysUser.php
2024-01-24 17:36:08 +08:00

144 lines
3.4 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
// +----------------------------------------------------------------------
// | Niucloud-admin 企业快速开发的saas管理平台
// +----------------------------------------------------------------------
// | 官方网址https://www.niucloud-admin.com
// +----------------------------------------------------------------------
// | niucloud团队 版权所有 开源版本可自由商用
// +----------------------------------------------------------------------
// | Author: Niucloud Team
// +----------------------------------------------------------------------
namespace app\model\sys;
use app\dict\sys\RoleStatusDict;
use app\dict\sys\UserDict;
use core\base\BaseModel;
use think\model\concern\SoftDelete;
use think\model\relation\HasMany;
/**
* 系统用户模型
* Class SysUser
* @package app\model\sys
*/
class SysUser extends BaseModel
{
use SoftDelete;
protected $type = [
'last_time' => 'timestamp',
];
/**
* 数据表主键
* @var string
*/
protected $pk = 'uid';
/**
* 模型名称
* @var string
*/
protected $name = 'sys_user';
/**
* 定义软删除标记字段
* @var string
*/
protected $deleteTime = 'delete_time';
/**
* 定义软删除字段的默认值
* @var int
*/
protected $defaultSoftDelete = 0;
public function userrole()
{
return $this->hasOne(SysUserRole::class, 'uid', 'uid')->joinType('inner');
}
/**
* 权限组
* @return HasMany
*/
public function roles()
{
return $this->hasMany(SysUserRole::class, 'uid', 'uid');
}
public function getCreateTimeAttr($value, $data)
{
return $data['create_time'] ? get_date_by_time($data['create_time']) : '';
}
/**
* 账号搜索器
* @param $query
* @param $value
*/
public function searchUsernameAttr($query, $value)
{
if ($value) {
$query->whereLike('username', '%' . $value . '%');
}
}
/**
* 用户实际姓名搜索器
* @param $query
* @param $value
*/
public function searchRealnameAttr($query, $value)
{
if ($value) {
$query->whereLike('real_name', '%' . $value . '%');
}
}
/**
* 角色状态
* @param $value
* @param $data
* @return string
*/
public function getStatusNameAttr($value, $data)
{
if (empty($data['status'])) return '';
return RoleStatusDict::getStatus()[$data['status']] ?? '';
}
/**
* 是否删除搜索器
* @param $query
*/
public function searchIsDelAttr($query)
{
$query->where('is_del', 0);
}
/**
* 创建时间搜索器
* @param $query
* @param $value
* @param $data
*/
public function searchCreateTimeAttr($query, $value, $data)
{
$start_time = empty($value[0]) ? 0 : strtotime($value[0]);
$end_time = empty($value[1]) ? 0 : strtotime($value[1]);
if ($start_time > 0 && $end_time > 0) {
$query->whereBetweenTime('sys_user.create_time', $start_time, $end_time);
} else if ($start_time > 0 && $end_time == 0) {
$query->where([['sys_user.create_time', '>=', $start_time]]);
} else if ($start_time == 0 && $end_time > 0) {
$query->where([['sys_user.create_time', '<=', $end_time]]);
}
}
}