Skip to content

Commit

Permalink
add route for v2
Browse files Browse the repository at this point in the history
  • Loading branch information
xxl4 committed Sep 6, 2024
1 parent c81806e commit ee083fe
Show file tree
Hide file tree
Showing 6 changed files with 905 additions and 1 deletion.
341 changes: 341 additions & 0 deletions src/Http/Controllers/Api/V2/Admin/User/AdminController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,341 @@
<?php

namespace NexaMerchant\Apis\Http\Controllers\Api\V2\Admin\User;

use App\Models\Admin;
use App\Models\Permission;
use App\Models\Role;
use Illuminate\Support\Facades\Validator;
use Lauthz\Facades\Enforcer;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use stdClass;

class AdminController extends Controller
{
public function menu(Request $request)
{
$admin = $request->user('admin');
$roles = Enforcer::getRolesForUser($admin->id);
$permissions = [];
foreach ($roles as $role)
{
$permissions[] = array_column(Enforcer::getPermissionsForUser($role), 2);
}
$permissions = array_unique(array_merge(...$permissions));
$menus = Permission::where('type', 0)
->where('status', 1)
->whereIn('permission', $permissions)
->get([
'id',
'path',
'name',
'redirect',
'component',
'parent_id',
'title',
'affix',
'icon',
'sort as orderNo',
'_lft',
'_rgt',
'permission',
])
->toTree();
$this->treeFormat($menus);
return $this->success('success', $menus);
}

/**
* @param Permission $menus
* @return void
*/
public function treeFormat($menus)
{
foreach ($menus as $menu)
{
$menu->meta = [
'title' => $menu->title,
'icon' => $menu->icon,
'affix' => $menu->affix == 1 ? true : false,
'orderNo' => $menu->orderNo,
];
unset($menu->parent_id);
unset($menu->_lft);
unset($menu->_rgt);
unset($menu->title);
unset($menu->icon);
unset($menu->affix);
if (count($menu->children))
{
$this->treeFormat($menu->children);
}
}
}

public function login(Request $request)
{
$validator = Validator::make($request->all(), [
'username' => 'required|string',
'password' => 'required|string',
]);
if ($validator->fails())
{
return $this->fails($validator->errors());
}
$credentials = request(['username', 'password']);
if (!$token = auth('admin')->setTTL(9999999999)->attempt($credentials))
{
return $this->fails('Username or password is wrong!');
}
$user = auth('admin')->user();
if ($user->status == 0)
{
return $this->fails('The account has been disabled!');
}
$roles = Enforcer::getRolesForUser($user->id);
$roles = Role::where('status', 1)
->whereIn('value', $roles)
->get(['name', 'value',]);
$result = new stdClass();
$result->id = $user->id;
$result->username = $user->username;
$result->name = $user->name;
$result->avatar = $user->avatar;
$result->email = $user->email;
$result->phone = $user->phone;
$result->token = $token;
$result->role = $roles->toArray();
return $this->success('success', $result);
}

public function refresh()
{
return $this->success('success', auth('admin')->refresh());
}

public function admin(Request $request)
{
$user = $request->user();
$roles = Enforcer::getRolesForUser($user->id);
$roles = Role::where('status', 1)
->whereIn('value', $roles)
->get(['name', 'value',]);
$user->role = $roles->toArray();
return $this->success('success', $user);
}

public function index(Request $request)
{
$validator = Validator::make($request->all(), [
'page' => 'required|integer',
'pageSize' => 'required|integer',
'username' => 'nullable|string',
'name' => 'nullable|string',
'phone' => 'nullable|string',
'gender' => 'nullable|integer',
'status' => 'nullable|integer',
]);
if ($validator->fails())
{
return $this->fails($validator->errors());
}
$query = Admin::query();
if ($request->filled('username'))
{
$query->where('username', 'like', '%' . $request->input('username') . '%');
}
if ($request->filled('name'))
{
$query->where('name', 'like', '%' . $request->input('name') . '%');
}
if ($request->filled('phone'))
{
$query->where('phone', 'like', '%' . $request->input('phone') . '%');
}
if ($request->filled('gender')){
$query->where('gender', $request->gender);
}
if ($request->filled('status'))
{
$query->where('status', $request->input('status'));
}
$result = new StdClass();
$result->total = $query->count();
$items = $query->offset(($request->input('page') - 1) * $request->input('pageSize'))
->limit($request->input('pageSize'))
->get();
foreach ($items as $v) {
$v->roles = Enforcer::getRolesForUser($v->id);
}
$result->items = $items;
return $this->success('success', $result);
}

public function create(Request $request)
{
$validator = Validator::make($request->all(), [
'username' => 'required|string|unique:admins',
'password' => 'required|string',
'gender' => 'nullable|integer',
'avatar' => 'nullable|string',
'name' => 'nullable|string',
'phone' => 'nullable|string',
'email' => 'nullable|email',
'email_status' => 'nullable|integer',
'status' => 'nullable|integer',
'birthday' => 'nullable|date',
'roles' => 'nullable|array',
]);
if ($validator->fails())
{
return $this->fails($validator->errors());
}
$admin = new Admin();
DB::beginTransaction();
try
{
$admin->id = app('snowflake')->id();
$admin->username = $request->input('username');
$admin->password = bcrypt($request->input('password'));
if($request->filled('gender')){
$admin->gender = $request->gender;
}
if($request->filled('birthday')){
$admin->birthday = $request->birthday;
}
if($request->filled('avatar')){
$admin->avatar = $request->avatar;
}
if($request->filled('name')){
$admin->name = $request->name;
}
if($request->filled('phone')){
$admin->phone = $request->phone;
}
if($request->filled('email')){
$admin->email = $request->email;
}
if($request->filled('email_status')){
$admin->email_status = $request->email_status;
}
if($request->filled('status')){
$admin->status = $request->status;
}
$admin->save();
foreach ($request->input('roles') as $role)
{
Enforcer::addRoleForUser($admin->id, $role);
}
DB::commit();
}
catch (\Exception $e)
{
DB::rollBack();
return $this->fails('添加失败');
}
return $this->success('添加成功');
}

public function update(Request $request)
{
$validator = Validator::make($request->all(), [
'id' => 'required',
'username' => 'required|string',
'password' => 'nullable|string',
'gender' => 'nullable|integer',
'avatar' => 'nullable|string',
'name' => 'nullable|string',
'phone' => 'nullable|string',
'email' => 'nullable|email',
'email_status' => 'nullable|integer',
'status' => 'nullable|integer',
'birthday' => 'nullable|date',
'roles' => 'nullable|array',
]);
if ($validator->fails())
{
return $this->fails($validator->errors());
}
$admin = Admin::where('username', $request->username)
->where('id', '!=', $request->id)
->first();
if ($admin) {
return $this->fails('用户名已存在');
}
$admin = Admin::find($request->id);
DB::beginTransaction();
try
{
$admin->username = $request->input('username');
if($request->filled('password')){
$admin->password = bcrypt($request->input('password'));
}
if ($request->filled('gender')) {
$admin->gender = $request->gender;
}
if ($request->filled('birthday')) {
$admin->birthday = $request->birthday;
}
if ($request->filled('avatar')) {
$admin->avatar = $request->input('avatar');
}
if ($request->filled('name')) {
$admin->name = $request->input('name');
}
if ($request->filled('phone')) {
$admin->phone = $request->input('phone');
}
if ($request->filled('email')) {
$admin->email = $request->input('email');
}
if($request->filled('email_status')){
$admin->email_status = $request->input('email_status');
}
if ($request->filled('status')) {
$admin->status = $request->input('status');
}
$admin->save();
foreach ($request->input('roles') as $role)
{
Enforcer::addRoleForUser($admin->id, $role);
}
DB::commit();
}
catch (\Exception $e)
{
DB::rollBack();
return $this->fails('修改失败');
}
return $this->success('修改成功');
}

public function delete(Request $request)
{
$validator = Validator::make($request->all(), [
'id' => 'required|integer',
]);
if ($validator->fails())
{
return $this->fails($validator->errors());
}
$admin = Admin::find($request->input('id'));
if (!$admin)
{
return $this->fails('用户不存在');
}
DB::beginTransaction();
try
{
$admin->delete();
Enforcer::deleteRolesForUser($admin->id);
DB::commit();
}
catch (\Exception $e)
{
DB::rollBack();
return $this->fails('删除失败');
}
return $this->success('删除成功');
}
}
34 changes: 34 additions & 0 deletions src/Http/Controllers/Api/V2/Admin/User/Controller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace NexaMerchant\Apis\Http\Controllers\Api\V2\Admin\User;

use Carbon\Traits\Mixin;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;

class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

public function fails(string $message, int $code = 400)
{
return response()->json([
'code' =>$code,
'type' => 'error',
'message' => $message,
]);
}

public function success(string $message, mixed $data = [], int $code = 200)
{
return response()->json([
'code' => $code,
'type' => 'success',
'message' => $message,
'result' => $data,
]);
}

}
Loading

0 comments on commit ee083fe

Please sign in to comment.