feat : user

This commit is contained in:
2024-10-29 18:00:12 +08:00
parent ed9f874293
commit 5dad242446
3 changed files with 70 additions and 13 deletions

View File

@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace App\Model; namespace App\Model;
use App\Constants\Admin\UserCode;
use Hyperf\Database\Model\Builder; use Hyperf\Database\Model\Builder;
use Hyperf\DbConnection\Model\Model; use Hyperf\DbConnection\Model\Model;
@@ -47,20 +48,29 @@ class AdminUser extends Model
const UPDATED_AT = 'update_time'; const UPDATED_AT = 'update_time';
/** /**
* @param $account * @param string $account
* @return Builder|\Hyperf\Database\Model\Model|null * @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 * @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();
} }
} }

View File

@@ -10,10 +10,14 @@ declare(strict_types=1);
namespace App\Service\Admin; namespace App\Service\Admin;
use App\Constants\Admin\UserCode;
use App\Exception\AdminException; use App\Exception\AdminException;
use App\Extend\StringUtil; use App\Extend\StringUtil;
use App\Lib\Crypto\CryptoFactory;
use App\Model\AdminUser; use App\Model\AdminUser;
use Exception;
use Hyperf\Di\Annotation\Inject; use Hyperf\Di\Annotation\Inject;
use function Hyperf\Config\config;
class EmployeeService extends BaseService class EmployeeService extends BaseService
{ {
@@ -24,12 +28,27 @@ class EmployeeService extends BaseService
#[Inject] #[Inject]
protected AdminUser $adminUserModel; protected AdminUser $adminUserModel;
/**
* 注入加密工厂
* @var CryptoFactory $cryptoFactory
*/
#[Inject]
protected CryptoFactory $cryptoFactory;
public function handle() public function handle()
{ {
$limit = (int)$this->request->input('limit', 10);
return $this->return->success(); return $this->return->success();
} }
public function add() /**
* 添加
* @return array
* @throws Exception
*/
public function add(): array
{ {
$name = $this->request->input('chinese_name'); $name = $this->request->input('chinese_name');
$account = $this->request->input('account'); $account = $this->request->input('account');
@@ -40,34 +59,60 @@ class EmployeeService extends BaseService
if (!empty($oldName) && !empty($oldAccount)) throw new AdminException('账号或者员工已存在'); if (!empty($oldName) && !empty($oldAccount)) throw new AdminException('账号或者员工已存在');
$salt = StringUtil::randStr(6); $salt = StringUtil::randStr(6);
$defaultPassword = config('system.admin_default_password');
$model = new AdminUser(); $model = new AdminUser();
$model->username = $account; $model->username = $account;
$model->chinese_name = $name; $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->status = $this->request->input('status', 1);
$model->avatar = $this->request->input('avatar',0); $model->avatar = $this->request->input('avatar',0);
$model->role_id = $this->request->input('role_id', 0); $model->role_id = $this->request->input('role_id', 0);
if (!$model->save()) throw new AdminException('账号添加失败');
$model->save();
return $this->return->success(); return $this->return->success();
} }
public function edit() public function edit()
{ {
return $this->return->success(); 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(); 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()]);
} }
} }

View File

@@ -19,4 +19,6 @@ return [
'jwt_expire' => env('JWT_EXPIRE',86400 * 30), 'jwt_expire' => env('JWT_EXPIRE',86400 * 30),
// admin jwt 过期时间 // admin jwt 过期时间
'admin_jwt_expire' => env('ADMIN_JWT_EXPIRE',86400 * 30), 'admin_jwt_expire' => env('ADMIN_JWT_EXPIRE',86400 * 30),
// admin 默认密码
'admin_default_password' => '123456'
]; ];