diff --git a/app/Model/AdminUser.php b/app/Model/AdminUser.php index b68702c..905218c 100644 --- a/app/Model/AdminUser.php +++ b/app/Model/AdminUser.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace App\Model; +use App\Constants\Admin\UserCode; use Hyperf\Database\Model\Builder; use Hyperf\DbConnection\Model\Model; @@ -47,20 +48,29 @@ class AdminUser extends Model const UPDATED_AT = 'update_time'; /** - * @param $account + * @param string $account * @return Builder|\Hyperf\Database\Model\Model|null */ - public function getAdminInfoByAccount($account): \Hyperf\Database\Model\Model|Builder|null + public function getAdminInfoByAccount(string $account): \Hyperf\Database\Model\Model|Builder|null { - return $this->where('username', $account)->where('is_del',1)->first(); + return $this->where('username', $account)->where('is_del',UserCode::ENABLE)->first(); } /** - * @param $name + * @param string $name * @return \Hyperf\Database\Model\Model|Builder|null */ - public function getAdminInfoByName($name): \Hyperf\Database\Model\Model|Builder|null + public function getAdminInfoByName(string $name): \Hyperf\Database\Model\Model|Builder|null { - return $this->where('chinese_name', $name)->where('is_del',1)->first(); + return $this->where('chinese_name', $name)->where('is_del',UserCode::ENABLE)->first(); + } + + /** + * @param int $id + * @return \Hyperf\Database\Model\Model|Builder|null + */ + public function getAdminInfoById(int $id): \Hyperf\Database\Model\Model|Builder|null + { + return $this->where('id', $id)->where('is_del',UserCode::ENABLE)->first(); } } diff --git a/app/Service/Admin/EmployeeService.php b/app/Service/Admin/EmployeeService.php index 23015cc..642f8ce 100644 --- a/app/Service/Admin/EmployeeService.php +++ b/app/Service/Admin/EmployeeService.php @@ -10,10 +10,14 @@ 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 { @@ -24,12 +28,27 @@ class EmployeeService extends BaseService #[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(); } - public function add() + /** + * 添加 + * @return array + * @throws Exception + */ + public function add(): array { $name = $this->request->input('chinese_name'); $account = $this->request->input('account'); @@ -40,34 +59,60 @@ class EmployeeService extends BaseService 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 = + $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->save(); + if (!$model->save()) throw new AdminException('账号添加失败'); return $this->return->success(); } public function edit() { + return $this->return->success(); } - public function delete() + /** + * 删除 + * @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(); } - public function get() + /** + * 详情 + * @return array + */ + public function get(): array { - return $this->return->success(); + $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()]); } } \ No newline at end of file diff --git a/config/autoload/system.php b/config/autoload/system.php index 59a0ee3..79253fb 100644 --- a/config/autoload/system.php +++ b/config/autoload/system.php @@ -19,4 +19,6 @@ return [ 'jwt_expire' => env('JWT_EXPIRE',86400 * 30), // admin jwt 过期时间 'admin_jwt_expire' => env('ADMIN_JWT_EXPIRE',86400 * 30), + // admin 默认密码 + 'admin_default_password' => '123456' ]; \ No newline at end of file