112 lines
2.4 KiB
PHP
112 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace App\Domains\Account\Repositories;
|
|
|
|
use App\Core\Repository;
|
|
use App\Models\Account\Account as Model;
|
|
use App\Domains\Account\Services\AccountService;
|
|
use function App\account_avatar;
|
|
|
|
class AccountRepository extends Repository
|
|
{
|
|
/**
|
|
* 是否关闭缓存
|
|
*
|
|
* @var boolean
|
|
*/
|
|
protected $cacheSkip = false;
|
|
|
|
/**
|
|
* 是否开启数据转化
|
|
*
|
|
* @var bool
|
|
*/
|
|
protected $needTransform = true;
|
|
|
|
/**
|
|
* @var array
|
|
*/
|
|
protected $fieldSearchable = [
|
|
'id' => '=',
|
|
'username' => 'like',
|
|
'nickname' => 'like',
|
|
'status' => '=',
|
|
'roles.role_id' => '=',
|
|
];
|
|
|
|
public function model()
|
|
{
|
|
return Model::class;
|
|
}
|
|
|
|
/**
|
|
* 数据格式化
|
|
*
|
|
* @param mixed $result
|
|
*
|
|
* @return mixed
|
|
*/
|
|
public function transform($model)
|
|
{
|
|
$model->avatar = account_avatar($model->id, $model->created_at, 128);
|
|
return $model;
|
|
}
|
|
|
|
/**
|
|
* 获取数据
|
|
*
|
|
* @param string|int $key
|
|
* @return void
|
|
*/
|
|
public function fetch($key)
|
|
{
|
|
if (empty($key)) {
|
|
return null;
|
|
}
|
|
|
|
$column = 'id';
|
|
|
|
$map = [
|
|
'email' => filter_var($key, FILTER_VALIDATE_EMAIL),
|
|
'mobile' => validate_china_phone_number($key),
|
|
'username' => validate_username($key),
|
|
];
|
|
|
|
foreach ($map as $field => $value) {
|
|
if ($value) {
|
|
$column = $field;
|
|
}
|
|
}
|
|
|
|
return $this->model->where($column, $key)->first();
|
|
}
|
|
|
|
/**
|
|
* 查询条件
|
|
*
|
|
* @return void
|
|
*/
|
|
public function withConditions(array $conditions = [])
|
|
{
|
|
if (isset($conditions['ids'])) {
|
|
$this->model = $this->model->whereIn('id', $conditions['ids']);
|
|
}
|
|
|
|
if (isset($conditions['parent_id'])) {
|
|
$this->model = $this->model->where('parent_id', $conditions['parent_id']);
|
|
}
|
|
|
|
if (isset($conditions['status'])) {
|
|
$this->model = $this->model->where('status', $conditions['status']);
|
|
}
|
|
|
|
if (isset($conditions['role_id'])) {
|
|
$this->model = $this->model->whereHas('roles', function ($query) use ($conditions) {
|
|
$query->where('id', $conditions['role_id'])->orWhere('parent_id', $conditions['role_id']);
|
|
});
|
|
}
|
|
|
|
return $this;
|
|
}
|
|
}
|