191 lines
5.7 KiB
PHP
191 lines
5.7 KiB
PHP
<?php
|
|
namespace App\Domains\Account\Services;
|
|
|
|
use App\Dicts;
|
|
use App\Core\Service;
|
|
use Illuminate\Support\Str;
|
|
use App\Events\FileUploadEvent;
|
|
use App\Models\Account\Account;
|
|
use Illuminate\Validation\Rule;
|
|
use Illuminate\Http\UploadedFile;
|
|
use Illuminate\Support\Facades\DB;
|
|
use App\Exceptions\NotExistException;
|
|
use Illuminate\Support\Facades\Event;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use App\Exceptions\InvalidArgumentException;
|
|
use App\Domains\Permission\Services\RoleService;
|
|
use App\Domains\Account\Repositories\AccountRepository;
|
|
use App\Domains\Permission\Repositories\RoleRepository;
|
|
use Dipper\JWTAuth\ServiceContract as JwtServiceContract;
|
|
|
|
class AccountService extends Service implements JwtServiceContract
|
|
{
|
|
protected $accountRepository;
|
|
protected $dicts;
|
|
protected $account;
|
|
|
|
/**
|
|
* 构造函数
|
|
*
|
|
* @return void
|
|
*/
|
|
public function __construct(AccountRepository $accountRepository, Dicts $dicts)
|
|
{
|
|
$this->accountRepository = $accountRepository;
|
|
$this->dicts = $dicts;
|
|
}
|
|
|
|
/**
|
|
* 获取用户
|
|
*
|
|
* @param string|int $key
|
|
* @param boolean $force
|
|
* @return void
|
|
*/
|
|
public function fetch($key, $force = false): ?Account
|
|
{
|
|
if ($force) {
|
|
$this->accountRepository->forgetCached();
|
|
}
|
|
|
|
$account = $this->accountRepository->fetch($key);
|
|
|
|
return $account;
|
|
}
|
|
|
|
/**
|
|
* 账号列表
|
|
*
|
|
* @param array $conditions
|
|
* @return mixed
|
|
*/
|
|
public function index(array $conditions = [])
|
|
{
|
|
$limit = $conditions['limit'] ?? 20;
|
|
|
|
$accounts = $this->accountRepository->withConditions($conditions)->applyConditions()->paginate($limit);
|
|
|
|
return $accounts;
|
|
}
|
|
|
|
/**
|
|
* 存储账号
|
|
*
|
|
* @param array $attributes
|
|
* @param Account $parent
|
|
* @return Account
|
|
*/
|
|
public function store(array $attributes = [])
|
|
{
|
|
$attributes = array_only($attributes, array_merge(app(Account::class)->getFillable(), ['parent_id', 'role_id', 'avatar']));
|
|
|
|
$rule = [
|
|
'username' => ['username', 'between:2,12', Rule::unique($this->accountRepository->getTable(), 'username')->ignore($attributes['id']), Rule::notIn(config('domain.account.reserved_account'))],
|
|
'nickname' => ['string', 'between:2,32'],
|
|
'mobile' => ['string', 'cn_phone', Rule::unique($this->accountRepository->getTable(), 'mobile')->ignore($attributes['id'])],
|
|
'email' => ['string', 'email', Rule::unique($this->accountRepository->getTable(), 'email')->ignore($attributes['id'])],
|
|
'password' => ['string'],
|
|
'avatar' => ['image'],
|
|
];
|
|
|
|
$message = [
|
|
'username.required' => '请输入用户名',
|
|
'username.between' => '用户名只能以非特殊字符和数字开头,不能包含特殊字符',
|
|
'username.display_length' => '用户名长度不合法',
|
|
'username.unique' => '用户名已经被其他用户所使用',
|
|
'username.not_in' => '系统保留用户名,禁止使用',
|
|
'nickname.display_length' => '昵称长度不合法',
|
|
'mobile.unique' => '手机号已被其他用户使用',
|
|
'password.required' => '请输入密码',
|
|
'password.string' => '密码格式不合法',
|
|
];
|
|
|
|
if (!$attributes['id']) {
|
|
$rule['password'][] = 'required';
|
|
$rule['username'][] = 'required';
|
|
}
|
|
|
|
Validator::validate($attributes, $rule, $message);
|
|
|
|
if ($attributes['parent_id']) {
|
|
$parent = $this->accountRepository->find($attributes['parent_id']);
|
|
}
|
|
|
|
DB::beginTransaction();
|
|
|
|
if ($attributes['password']) {
|
|
$attributes['salt'] = Str::random(6);
|
|
$attributes['password'] = md5($attributes['password'].$attributes['salt']);
|
|
}
|
|
|
|
if (!$attributes['id']) {
|
|
$attributes['status'] = $attributes['status'] ?? 1;
|
|
$node = $this->accountRepository->create($attributes, $parent);
|
|
}
|
|
|
|
if ($attributes['id']) {
|
|
unset($attributes['username']);
|
|
|
|
$node = $this->accountRepository->find($attributes['id']);
|
|
|
|
if (!$node) {
|
|
throw new NotExistException('用户不存在');
|
|
}
|
|
|
|
if ($parent && $parentid != $node->getParentId()) {
|
|
$node->appendToNode($parent);
|
|
}
|
|
|
|
$this->accountRepository->setModel($node)->update($attributes);
|
|
}
|
|
|
|
try {
|
|
if (($avatar = $attributes['avatar']) && $avatar instanceof UploadedFile) {
|
|
Event::fire(new FileUploadEvent($avatar, [
|
|
'filename' => $this->generateAvatarPath($node->id),
|
|
'type' => 'account/avatar',
|
|
'typeid' => $node->id,
|
|
'cover' => true,
|
|
], $disk = 'data'));
|
|
|
|
$node->avatar = account_avatar($node->id, $node->updated_at, 128);
|
|
}
|
|
|
|
if ($attributes['role_id']) {
|
|
$role = app(RoleRepository::class)->findById($attributes['role_id']);
|
|
|
|
app(RoleService::class)->syncRoles($node, $role);
|
|
}
|
|
|
|
DB::commit();
|
|
} catch (\Exception $e) {
|
|
DB::rollBack();
|
|
throw $e;
|
|
}
|
|
|
|
return $node;
|
|
}
|
|
|
|
/**
|
|
* 删除
|
|
*
|
|
* @return bool
|
|
*/
|
|
public function destroy($ids)
|
|
{
|
|
$ids = is_array($ids) ? $ids : [$ids];
|
|
|
|
$this->accountRepository->destroy($ids);
|
|
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* 头像路径
|
|
*/
|
|
public static function generateAvatarPath($accountId)
|
|
{
|
|
return '/account/avatar/' . sha1($accountId . 'avatar').'.jpg';
|
|
}
|
|
}
|