Files
hyperf_service/app/Service/Admin/User/EmployeeService.php
2024-11-07 16:20:07 +08:00

183 lines
5.3 KiB
PHP

<?php
/**
* This service file is part of item.
*
* @author ctexthuang
* @contact ctexthuang@qq.com
*/
declare(strict_types=1);
namespace App\Service\Admin\User;
use App\Constants\Admin\UserCode;
use App\Exception\AdminException;
use App\Extend\StringUtil;
use App\Lib\Crypto\CryptoFactory;
use App\Model\AdminUser;
use App\Service\Admin\BaseService;
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;
/**
* @var array|string[]
*/
private array $filed = ['id','username','avatar','chinese_name','mobile','status','last_login_ip','last_login_time','role_id'];
/**
* 列表
* @return array
*/
public function handle(): array
{
$limit = (int)$this->request->input('limit', 10);
$list = $this->adminUserModel->where('is_del',UserCode::ENABLE)->paginate($limit,$this->filed)->toArray();
return $this->return->success('success', ['list' => $list]);
}
/**
* 添加
* @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->mobile = $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);
$model->section_id = $this->request->input('section_id', 0);
if (!$model->save()) throw new AdminException('账号添加失败');
return $this->return->success();
}
/**
* 修改
* @return array
*/
public function edit(): array
{
$id = (int)$this->request->input('id');
$name = $this->request->input('chinese_name');
$account = $this->request->input('account');
$info = $this->adminUserModel->getAdminInfoById($id);
if (empty($info)) throw new AdminException('数据不存在');
$oldAccount = $this->adminUserModel->getAdminInfoByAccount($account);
$oldName = $this->adminUserModel->getAdminInfoByName($name);
if (!empty($oldName) && $oldName->id != $info->id) throw new AdminException('员工已存在');
if (!empty($oldAccount) && $oldAccount->id != $info->id) throw new AdminException('账号已存在');
$info->username = $account;
$info->mobile = $account;
$info->chinese_name = $name;
$info->status = $this->request->input('status', 1);
$info->avatar = $this->request->input('avatar',0);
$info->role_id = $this->request->input('role_id', 0);
$info->section_id = $this->request->input('section_id', 0);
if (!$info->save()) throw new AdminException('账号修改失败');
return $this->return->success();
}
/**
* 删除
* @return array
*/
public function delete(): array
{
$id = (int)$this->request->input('id');
if ($this->adminId == $id) throw new AdminException('不可删除自己');
$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->where('id', $id)->where('is_del',UserCode::ENABLE)->first($this->filed);
if (empty($info)) throw new AdminException('员工不存在');
return $this->return->success('success', ['info' => $info->toArray()]);
}
/**
* 重置密码
* @return array
* @throws Exception
*/
public function resetPassword(): array
{
$id = (int)$this->request->input('id');
$info = $this->adminUserModel->getAdminInfoById($id);
if (empty($info)) throw new AdminException('员工不存在');
$salt = StringUtil::randStr(6);
$defaultPassword = config('system.admin_default_password');
$info->salt = $salt;
$info->password = $this->cryptoFactory->cryptoClass('admin-password',$defaultPassword,$salt)->encrypt();
if (!$info->save()) throw new AdminException('密码重置失败');
return $this->return->success();
}
}