170 lines
5.1 KiB
PHP
170 lines
5.1 KiB
PHP
<?php
|
|
namespace App\Domains\User\Services;
|
|
|
|
use App\Dicts;
|
|
use App\Core\Service;
|
|
use App\Models\User\User;
|
|
use Illuminate\Support\Str;
|
|
use App\Events\FileUploadEvent;
|
|
use Illuminate\Validation\Rule;
|
|
use Illuminate\Http\UploadedFile;
|
|
use App\Exceptions\NotExistException;
|
|
use Illuminate\Support\Facades\Event;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use App\Domains\App\Repositories\AppRepository;
|
|
use App\Domains\User\Repositories\UserRepository;
|
|
use Dipper\JWTAuth\ServiceContract as JwtServiceContract;
|
|
|
|
class UserService extends Service implements JwtServiceContract
|
|
{
|
|
protected $appRepository;
|
|
protected $userRepository;
|
|
protected $dicts;
|
|
|
|
/**
|
|
* 构造函数
|
|
*
|
|
* @return void
|
|
*/
|
|
public function __construct(AppRepository $appRepository, UserRepository $userRepository, Dicts $dicts)
|
|
{
|
|
$this->appRepository = $appRepository;
|
|
$this->userRepository = $userRepository;
|
|
$this->dicts = $dicts;
|
|
$this->application = app('dipper')->application;
|
|
}
|
|
|
|
|
|
/**
|
|
* 获取用户
|
|
*
|
|
* @param string|int $key
|
|
* @param boolean $force
|
|
* @return void
|
|
*/
|
|
public function fetch($key, $force = false): ?User
|
|
{
|
|
if ($force) {
|
|
$this->userRepository->forgetCached();
|
|
}
|
|
|
|
return $this->userRepository->fetch($this->application->id, $key);
|
|
}
|
|
|
|
/**
|
|
* 账号列表
|
|
*
|
|
* @param array $conditions
|
|
* @return mixed
|
|
*/
|
|
public function index(array $conditions = [])
|
|
{
|
|
$limit = $conditions['limit'] ?? 20;
|
|
|
|
if ($this->application && $this->application->id) {
|
|
$this->userRepository = $this->userRepository->where('appid', $this->application->id);
|
|
}
|
|
|
|
return $this->userRepository->applyConditions()->paginate($limit);
|
|
}
|
|
|
|
/**
|
|
* 存储账号
|
|
*
|
|
* @param array $attributes
|
|
* @param Account $parent
|
|
* @return Account
|
|
*/
|
|
public function store(array $attributes = [], $parent = null)
|
|
{
|
|
$attributes = array_only($attributes, array_merge(app(User::class)->getFillable(), ['id', 'avatar']));
|
|
|
|
$rule = [
|
|
'appid' => ['required', 'exists:'.$this->appRepository->getTable().',id'],
|
|
'username' => ['username', 'display_length:2,12', Rule::unique($this->userRepository->getTable(), 'username')->ignore($attributes['id']), Rule::notIn(config('domain.user.reserved_username'))],
|
|
'email' => ['email', Rule::unique($this->userRepository->getTable(), 'email')->ignore($attributes['id'])],
|
|
'mobile' => ['required', 'cn_phone', Rule::unique($this->userRepository->getTable(), 'mobile')->ignore($attributes['id'])],
|
|
'password' => ['string'],
|
|
'nickname' => ['display_length:36'],
|
|
];
|
|
|
|
$message = [
|
|
'appid.required' => '应用ID不能为空',
|
|
'appid.exists' => '应用不存在',
|
|
'username.username' => '用户名只能以非特殊字符和数字开头,不能包含特殊字符',
|
|
'username.display_length' => '用户名长度不合法',
|
|
'username.unique' => '用户名已经被其他用户所使用',
|
|
'username.not_in' => '系统保留用户名,禁止使用',
|
|
'password.string' => '密码格式不合法',
|
|
'email.email' => '邮箱格式不合法',
|
|
'mobile.cn_phone' => '手机号格式不合法',
|
|
];
|
|
|
|
if (!$attributes['id']) {
|
|
$rule['username'][] = 'required';
|
|
$rule['mobile'][] = 'required';
|
|
$rule['password'][] = 'required';
|
|
}
|
|
|
|
Validator::validate($attributes, $rule, $message);
|
|
|
|
if ($attributes['password']) {
|
|
$attributes['salt'] = Str::random(6);
|
|
$attributes['password'] = md5(md5($attributes['password']).$attributes['salt']);
|
|
}
|
|
|
|
if (!$attributes['id']) {
|
|
$user = $this->userRepository->create($attributes);
|
|
}
|
|
|
|
if ($attributes['id']) {
|
|
unset($attributes['username']);
|
|
|
|
$user = $this->userRepository->find($attributes['id']);
|
|
|
|
if (!$user) {
|
|
throw new NotExistException('用户不存在');
|
|
}
|
|
|
|
$this->userRepository->setModel($user)->update($attributes);
|
|
}
|
|
|
|
if (($avatar = $attributes['avatar']) && $avatar instanceof UploadedFile) {
|
|
Event::fire(new FileUploadEvent($avatar, [
|
|
'filename' => $this->generateAvatarPath($user->id),
|
|
'type' => 'user/avatar',
|
|
'typeid' => $user->id,
|
|
'cover' => true,
|
|
], $disk = 'data'));
|
|
|
|
$user->avatar = avatar($user->id, $user->updated_at, 128);
|
|
}
|
|
|
|
return $user;
|
|
}
|
|
|
|
/**
|
|
* 删除
|
|
*
|
|
* @return bool
|
|
*/
|
|
public function destroy($ids)
|
|
{
|
|
$ids = is_array($ids) ? $ids : [$ids];
|
|
|
|
// $users = $this->userRepository->whereIn('id', $ids)->get();
|
|
|
|
$this->userRepository->destroy($ids);
|
|
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* 头像路径
|
|
*/
|
|
public static function generateAvatarPath($uid)
|
|
{
|
|
return '/user/avatar/' . sha1($uid . 'avatar').'.jpg';
|
|
}
|
|
}
|