|
|
@@ -6,14 +6,15 @@ use App\Constants\HttpStatus;
|
|
|
use App\Http\Controllers\Controller;
|
|
|
use App\Services\JwtService;
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
+use Illuminate\Support\Facades\Cache;
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
use Illuminate\Support\Facades\Validator;
|
|
|
use App\Models\Admin as AdminModel;
|
|
|
use Exception;
|
|
|
use Illuminate\Validation\ValidationException;
|
|
|
use function Symfony\Component\HttpFoundation\Session\Storage\Handler\commit;
|
|
|
-use App\Services\AdminService;
|
|
|
-use Illuminate\Validation\Rule;
|
|
|
+use App\Services\AdminService;
|
|
|
+use Illuminate\Validation\Rule;
|
|
|
|
|
|
/**
|
|
|
* @apiDefine result
|
|
|
@@ -149,6 +150,9 @@ class Admin extends Controller
|
|
|
$password = request()->input('password');
|
|
|
$user = AdminModel::login($username, $password);
|
|
|
$token = $this->jwtService->generateToken($user);
|
|
|
+ Cache::put("user_{$user->id}_jwt", $token);
|
|
|
+
|
|
|
+
|
|
|
} catch (Exception $e) {
|
|
|
return $this->error(intval($e->getCode()));
|
|
|
}
|
|
|
@@ -191,12 +195,12 @@ class Admin extends Controller
|
|
|
public function index()
|
|
|
{
|
|
|
// try {
|
|
|
- request()->validate([
|
|
|
- 'username' => ['nullable', 'string'],
|
|
|
- 'nickname' => ['nullable', 'string'],
|
|
|
- ]);
|
|
|
- $search = request()->all();
|
|
|
- $result = AdminService::paginate($search);
|
|
|
+ request()->validate([
|
|
|
+ 'username' => ['nullable', 'string'],
|
|
|
+ 'nickname' => ['nullable', 'string'],
|
|
|
+ ]);
|
|
|
+ $search = request()->all();
|
|
|
+ $result = AdminService::paginate($search);
|
|
|
// } catch (ValidationException $e) {
|
|
|
// return $this->error(HttpStatus::VALIDATION_FAILED, '', $e->errors());
|
|
|
// } catch (Exception $e) {
|
|
|
@@ -223,36 +227,36 @@ class Admin extends Controller
|
|
|
public function store()
|
|
|
{
|
|
|
// try {
|
|
|
- $params = request()->all();
|
|
|
- if(isset($params['id']) && $params['id'] == 1){
|
|
|
- return $this->error(0, '超级管理员禁止操作');
|
|
|
- }
|
|
|
- $validator = [
|
|
|
- 'username' => 'required|string|max:50|alpha_dash|unique:admin,username',
|
|
|
- 'nickname' => 'required|string|max:100',
|
|
|
- 'password' => ['nullable', 'string', 'min:6', 'max:20'],
|
|
|
- // 'display_name' => 'nullable|string|max:100',
|
|
|
- // 'description' => 'nullable|string',
|
|
|
+ $params = request()->all();
|
|
|
+ if (isset($params['id']) && $params['id'] == 1) {
|
|
|
+ return $this->error(0, '超级管理员禁止操作');
|
|
|
+ }
|
|
|
+ $validator = [
|
|
|
+ 'username' => 'required|string|max:50|alpha_dash|unique:admin,username',
|
|
|
+ 'nickname' => 'required|string|max:100',
|
|
|
+ 'password' => ['nullable', 'string', 'min:6', 'max:20'],
|
|
|
+ // 'display_name' => 'nullable|string|max:100',
|
|
|
+ // 'description' => 'nullable|string',
|
|
|
+ ];
|
|
|
+ if (isset($params['id']) && !empty($params['id'])) {
|
|
|
+ $validator['username'] = [
|
|
|
+ 'required',
|
|
|
+ 'string',
|
|
|
+ 'max:50',
|
|
|
+ 'alpha_dash',
|
|
|
+ Rule::unique('admin', 'username')->ignore($params['id']), // 忽略当前 ID
|
|
|
];
|
|
|
- if(isset($params['id']) && !empty($params['id'])){
|
|
|
- $validator['username'] = [
|
|
|
- 'required',
|
|
|
- 'string',
|
|
|
- 'max:50',
|
|
|
- 'alpha_dash',
|
|
|
- Rule::unique('admin', 'username')->ignore($params['id']), // 忽略当前 ID
|
|
|
- ];
|
|
|
- }else{
|
|
|
+ } else {
|
|
|
|
|
|
- }
|
|
|
-
|
|
|
+ }
|
|
|
|
|
|
- request()->validate($validator);
|
|
|
|
|
|
- $ret = AdminService::submit($params);
|
|
|
- if ($ret['code'] == AdminService::NOT) {
|
|
|
- return $this->error($ret['code'], $ret['msg']);
|
|
|
- }
|
|
|
+ request()->validate($validator);
|
|
|
+
|
|
|
+ $ret = AdminService::submit($params);
|
|
|
+ if ($ret['code'] == AdminService::NOT) {
|
|
|
+ return $this->error($ret['code'], $ret['msg']);
|
|
|
+ }
|
|
|
// } catch (ValidationException $e) {
|
|
|
// return $this->error(HttpStatus::VALIDATION_FAILED, '', $e->errors());
|
|
|
// } catch (Exception $e) {
|
|
|
@@ -275,7 +279,7 @@ class Admin extends Controller
|
|
|
public function destroy()
|
|
|
{
|
|
|
$id = request()->post('id');
|
|
|
- if($id == 1){
|
|
|
+ if ($id == 1) {
|
|
|
return $this->error(0, '超级管理员禁止操作');
|
|
|
}
|
|
|
// 示例:通过 ID 删除菜单
|