PermissionController.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. namespace App\Http\Controllers\Admin\System;
  3. use App\Http\Controllers\Controller;
  4. use Spatie\Permission\Models\Permission;
  5. use Illuminate\Http\Request;
  6. class PermissionController extends Controller
  7. {
  8. public function index()
  9. {
  10. return view('admin.system.permission.index');
  11. }
  12. public function getData()
  13. {
  14. $data = Permission::select([
  15. 'id', 'pid as parentId', 'name as title','route'
  16. ])->get();
  17. $data = getTree($data);
  18. return [
  19. "status" => [
  20. "code" => 200,
  21. "message" => 'success'
  22. ],
  23. "data" => $data
  24. ];
  25. }
  26. public function add(Request $request)
  27. {
  28. try {
  29. $res = Permission::create([
  30. 'name' => $request->context,
  31. 'pid' => $request->parentId??0,
  32. 'route' => $request->route??''
  33. ]);
  34. return $res ? $this->responseData(200, '添加成功',[ "id" => $res->id]):$this->responseData(400, '添加失败');
  35. } catch (\Exception $e) {
  36. llog($e);
  37. return $this->responseData(400, $e->getMessage());
  38. }
  39. }
  40. public function update(Request $request)
  41. {
  42. $res = Permission::where('id', $request->nodeId)->update([
  43. 'name' => $request->context,
  44. 'route' => $request->route,
  45. 'updated_at' => nowDate()
  46. ]);
  47. return $res?$this->responseData(200, '修改成功'):$this->responseData(400, '修改失败');
  48. }
  49. public function del(Request $request)
  50. {
  51. $res = Permission::where('id', $request->nodeId)->delete();
  52. return $res?$this->responseData(200, '删除成功'):$this->responseData(400, '删除失败');
  53. }
  54. public function getItem(Request $request)
  55. {
  56. $data = Permission::where('id', $request->nodeId)->first();
  57. return $this->responseData(200, 'success', ['route' => $data->route]);
  58. }
  59. }