feat : user

This commit is contained in:
2024-10-29 17:25:19 +08:00
parent d6465b21d8
commit ed9f874293
6 changed files with 303 additions and 0 deletions

View File

@@ -0,0 +1,26 @@
<?php
namespace App\Constants\Common;
class VerifyTypeCode
{
public const int HexString = 1; //0-9 a-f A-F
public const int LowHexString = 2;
public const int UpperHexString = 3;
public const int DigestString = 4;
public const int AlphaString = 5; // a-z A-Z
public const int AEnumString = 6; //a-z A-Z 0-9
public const int UinString = 7; // a-z A-Z 0-9 _ 用户ID
public const int EmailString = 8; ///^(\w)+(\.\w+)*@(\w)+((\.\w+)+)$/ email
public const int MobileString = 9; ///^0?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/ mobile
public const int TelString = 10; // /^([0-9]{3,4}-)?[0-9]{7,8}$/ telephone
}

View File

@@ -0,0 +1,56 @@
<?php
declare(strict_types=1);
namespace App\Controller\Admin;
use App\Middleware\Admin\JwtAuthMiddleware;
use App\Request\Admin\EmployeeRequest;
use App\Service\Admin\EmployeeService;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\Middlewares;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Hyperf\Validation\Annotation\Scene;
use Symfony\Component\HttpFoundation\JsonResponse;
#[Controller(prefix: "admin/employee")]
#[Middlewares([
JwtAuthMiddleware::class,
])]
class EmployeeController
{
#[RequestMapping(path: "add", methods: "POST")]
#[Scene(scene: "add")]
public function add(EmployeeRequest $request)
{
return (new EmployeeService)->add();
}
#[RequestMapping(path: "edit", methods: "POST")]
#[Scene(scene: "edit")]
public function edit(EmployeeRequest $request)
{
return (new EmployeeService)->edit();
}
#[RequestMapping(path: "del", methods: "POST")]
#[Scene(scene: "del")]
public function delete(EmployeeRequest $request)
{
return (new EmployeeService)->delete();
}
#[RequestMapping(path: "list", methods: "POST")]
#[Scene(scene: "list")]
public function list(EmployeeRequest $request)
{
return (new EmployeeService)->handle();
}
#[RequestMapping(path: "info", methods: "POST")]
#[Scene(scene: "info")]
public function info(EmployeeRequest $request)
{
return (new EmployeeService)->get();
}
}

83
app/Extend/StringUtil.php Normal file
View File

@@ -0,0 +1,83 @@
<?php
namespace App\Extend;
use App\Constants\Common\VerifyTypeCode;
class StringUtil
{
/**
* 字符串是否有效
* @param $str
* @param $type
* @return bool
*/
public static function isStrValid($str, $type)
{
$ret = false;
switch ($type) {
case VerifyTypeCode::HexString:
$ret = preg_match('/^[0-9a-fA-F]*$/', $str);
break;
case VerifyTypeCode::LowHexString:
$ret = preg_match('/^[0-9a-f]*$/', $str);
break;
case VerifyTypeCode::UpperHexString:
$ret = preg_match('/^[0-9A-F]*$/', $str);
break;
case VerifyTypeCode::DigestString:
$ret = preg_match('/^[0-9]*$/', $str);
break;
case VerifyTypeCode::AlphaString:
$ret = preg_match('/^[a-zA-Z]*$/', $str);
break;
case VerifyTypeCode::AEnumString:
$ret = preg_match('/^[a-zA-Z0-9]*$/', $str);
break;
case VerifyTypeCode::UinString:
$ret = preg_match('/^[a-zA-Z0-9][a-zA-Z0-9_]*$/', $str);
break;
case VerifyTypeCode::EmailString:
$ret = preg_match(
'/^(\w)+(\.\w+)*(\w+)*@(\w)+((\.\w+)+)$/',
$str
);
break;
case VerifyTypeCode::MobileString:
$ret = preg_match(
'/^0?(10[0-9]|13[0-9]|15[0-9]|17[0-9]|18[0-9]|14[0-9]|16[0-9]|19[0-9])[0-9]{8}$/',
$str
);
break;
case VerifyTypeCode::TelString:
$ret = preg_match('/^[0-9]{3,4}-[0-9]{7,8}$/', $str);
break;
default:
return false;
}
return $ret;
}
/**
* 生成随机字符串
* @param int $length
* @return string
*/
public static function randStr(int $length): string
{
$str = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D',
'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
$keys = array_rand($str, $length);
$password = '';
for ($i = 0; $i < $length; $i++) {
$password .= $str[$keys[$i]];
}
return str_shuffle($password);
}
}

View File

@@ -54,4 +54,13 @@ class AdminUser extends Model
{
return $this->where('username', $account)->where('is_del',1)->first();
}
/**
* @param $name
* @return \Hyperf\Database\Model\Model|Builder|null
*/
public function getAdminInfoByName($name): \Hyperf\Database\Model\Model|Builder|null
{
return $this->where('chinese_name', $name)->where('is_del',1)->first();
}
}

View File

@@ -0,0 +1,56 @@
<?php
declare(strict_types=1);
namespace App\Request\Admin;
use Hyperf\Validation\Request\FormRequest;
class EmployeeRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}
/**
* Get the validation rules that apply to the request.
*/
public function rules(): array
{
return [
'account' => 'required|digits:11',
'chinese_name' =>'required|string',
'status' => 'required|in:1,2',
'role_id' => 'exists:admin_role,id',
'avatar' => '',
'limit' => 'required|integer',
];
}
public function messages(): array
{
return [
'account.required' => '账号必填',
'account.digits' => '账号格式错误',
'chinese_name.required' => '名称必填',
'status.required' => '状态必填',
'status.in' => '状态值错误',
'role_id.exists' => '角色不存在',
// 'avatar.required' => '头像必填',
'limit.required' => 'limit必填'
];
}
protected array $scenes = [
'add' => ['account','chinese_name','status','role_id','avatar'],
'edit' => ['id','account','chinese_name','status','role_id','avatar'],
'del' => ['id'],
'reset_password' => ['id'],
'info' => ['id'],
'list' => ['limit']
];
}

View File

@@ -0,0 +1,73 @@
<?php
/**
* This service file is part of item.
*
* @author ctexthuang
* @contact ctexthuang@qq.com
*/
declare(strict_types=1);
namespace App\Service\Admin;
use App\Exception\AdminException;
use App\Extend\StringUtil;
use App\Model\AdminUser;
use Hyperf\Di\Annotation\Inject;
class EmployeeService extends BaseService
{
/**
* 注入用户模型
* @var AdminUser $adminUserModel
*/
#[Inject]
protected AdminUser $adminUserModel;
public function handle()
{
return $this->return->success();
}
public function add()
{
$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);
$model = new AdminUser();
$model->username = $account;
$model->chinese_name = $name;
$model->salt =
$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();
return $this->return->success();
}
public function edit()
{
return $this->return->success();
}
public function delete()
{
return $this->return->success();
}
public function get()
{
return $this->return->success();
}
}