118 lines
2.8 KiB
PHP
118 lines
2.8 KiB
PHP
<?php
|
|
/**
|
|
* This service file is part of item.
|
|
*
|
|
* @author ctexthuang
|
|
* @contact ctexthuang@qq.com
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Service\Admin;
|
|
|
|
use App\Constants\Admin\UserCode;
|
|
use App\Exception\AdminException;
|
|
use App\Extend\StringUtil;
|
|
use App\Lib\Crypto\CryptoFactory;
|
|
use App\Model\AdminUser;
|
|
use Exception;
|
|
use Hyperf\Di\Annotation\Inject;
|
|
use function Hyperf\Config\config;
|
|
|
|
class EmployeeService extends BaseService
|
|
{
|
|
/**
|
|
* 注入用户模型
|
|
* @var AdminUser $adminUserModel
|
|
*/
|
|
#[Inject]
|
|
protected AdminUser $adminUserModel;
|
|
|
|
/**
|
|
* 注入加密工厂
|
|
* @var CryptoFactory $cryptoFactory
|
|
*/
|
|
#[Inject]
|
|
protected CryptoFactory $cryptoFactory;
|
|
|
|
public function handle()
|
|
{
|
|
$limit = (int)$this->request->input('limit', 10);
|
|
|
|
|
|
return $this->return->success();
|
|
}
|
|
|
|
/**
|
|
* 添加
|
|
* @return array
|
|
* @throws Exception
|
|
*/
|
|
public function add(): array
|
|
{
|
|
$name = $this->request->input('chinese_name');
|
|
$account = $this->request->input('account');
|
|
|
|
$oldAccount = $this->adminUserModel->getAdminInfoByAccount($account);
|
|
$oldName = $this->adminUserModel->getAdminInfoByName($name);
|
|
|
|
if (!empty($oldName) && !empty($oldAccount)) throw new AdminException('账号或者员工已存在');
|
|
|
|
$salt = StringUtil::randStr(6);
|
|
$defaultPassword = config('system.admin_default_password');
|
|
|
|
$model = new AdminUser();
|
|
|
|
$model->username = $account;
|
|
$model->chinese_name = $name;
|
|
$model->salt = $salt;
|
|
$model->password = $this->cryptoFactory->cryptoClass('admin-password',$defaultPassword,$salt)->encrypt();
|
|
$model->status = $this->request->input('status', 1);
|
|
$model->avatar = $this->request->input('avatar',0);
|
|
$model->role_id = $this->request->input('role_id', 0);
|
|
|
|
if (!$model->save()) throw new AdminException('账号添加失败');
|
|
|
|
return $this->return->success();
|
|
}
|
|
|
|
public function edit()
|
|
{
|
|
|
|
return $this->return->success();
|
|
}
|
|
|
|
/**
|
|
* 删除
|
|
* @return array
|
|
*/
|
|
public function delete(): array
|
|
{
|
|
$id = (int)$this->request->input('id');
|
|
|
|
$info = $this->adminUserModel->getAdminInfoById($id);
|
|
|
|
if (empty($info)) throw new AdminException('员工不存在');
|
|
|
|
$info->is_del = UserCode::DISABLE;
|
|
|
|
if (!$info->save()) throw new AdminException('账号删除失败');
|
|
|
|
return $this->return->success();
|
|
}
|
|
|
|
/**
|
|
* 详情
|
|
* @return array
|
|
*/
|
|
public function get(): array
|
|
{
|
|
$id = (int)$this->request->input('id');
|
|
|
|
$info = $this->adminUserModel->getAdminInfoById($id);
|
|
|
|
if (empty($info)) throw new AdminException('员工不存在');
|
|
|
|
return $this->return->success('success', ['info' => $info->toArray()]);
|
|
}
|
|
} |