97 lines
1.9 KiB
PHP
97 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace App\Domains\User\Repositories;
|
|
|
|
use App\Core\Repository;
|
|
use Illuminate\Support\Arr;
|
|
use App\Models\User\User as Model;
|
|
|
|
class UserRepository extends Repository
|
|
{
|
|
/**
|
|
* 是否关闭缓存
|
|
*
|
|
* @var boolean
|
|
*/
|
|
protected $cacheSkip = false;
|
|
|
|
/**
|
|
* 是否开启数据转化
|
|
*
|
|
* @var bool
|
|
*/
|
|
protected $needTransform = true;
|
|
|
|
/**
|
|
* @var array
|
|
*/
|
|
protected $fieldSearchable = [
|
|
'id' => '=',
|
|
'username' => 'like',
|
|
'nickname' => 'like',
|
|
'status' => '=',
|
|
];
|
|
|
|
public function model()
|
|
{
|
|
return Model::class;
|
|
}
|
|
|
|
/**
|
|
* 数据格式化
|
|
*
|
|
* @param mixed $result
|
|
*
|
|
* @return mixed
|
|
*/
|
|
public function transform($model)
|
|
{
|
|
$model->avatar = avatar($model->id, $model->created_at, 128);
|
|
return $model;
|
|
}
|
|
|
|
/**
|
|
* 获取数据
|
|
*
|
|
* @param string|int $key
|
|
* @return void
|
|
*/
|
|
public function fetch($appid, $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('appid', $appid)->where($column, $key)->first();
|
|
}
|
|
|
|
/**
|
|
* 查询条件
|
|
*
|
|
* @return void
|
|
*/
|
|
public function withConditions(array $conditions = [])
|
|
{
|
|
if (isset($conditions['ids'])) {
|
|
$conditions['ids'] = Arr::wrap($conditions['ids']);
|
|
$this->model = $this->model->whereIn('id', $conditions['ids']);
|
|
}
|
|
|
|
return $this;
|
|
}
|
|
}
|