AdminmenuController.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. namespace app\admin\controller;
  3. use think\exception\ValidateException;
  4. use app\model\AdminMenu;
  5. class AdminmenuController extends Base
  6. {
  7. function index()
  8. {
  9. if ($this->userInfo['username'] == 'admin') {
  10. $keyword = input('post.keyword', '', 'serach_in');
  11. $query = AdminMenu::field('*');
  12. if (!empty($keyword)) {
  13. $query->where('title', 'like', '%' . $keyword . '%');
  14. }
  15. $list = $query->order('sort asc')->select()->toArray();
  16. //var_dump($query->getLastSql());
  17. if (!empty($keyword)) {
  18. $data['data'] = $list;
  19. } else {
  20. $data['data'] = _generateListTree($list, 0, ['id', 'pid']);
  21. }
  22. return $this->json($data);
  23. }
  24. }
  25. function listUpdate()
  26. {
  27. if ($this->userInfo['username'] == 'admin') {
  28. $data = only('id,is_console,is_admin,is_city,is_tuanzhang,is_store,is_v2,is_v3,is_v6,status,sort');
  29. if (!$data['id']) throw new ValidateException('参数错误');
  30. AdminMenu::update($data);
  31. return $this->json(['msg' => '操作成功']);
  32. }
  33. }
  34. public function update()
  35. {
  36. if ($this->userInfo['username'] == 'admin') {
  37. $id = $this->request->post('id');
  38. $data = input('post.');
  39. $data['pid'] = (int)$data['pid'];
  40. if (empty($id)) {
  41. try {
  42. $res = AdminMenu::create($data);
  43. if ($res->id && empty($data['sort'])) {
  44. AdminMenu::update(['sort' => $res->id, 'id' => $res->id]);
  45. }
  46. } catch (\Exception $e) {
  47. throw new ValidateException($e->getMessage());
  48. }
  49. return $this->json(['msg' => '添加成功', 'data' => $res->id]);
  50. } else {
  51. try {
  52. AdminMenu::update($data);
  53. } catch (\Exception $e) {
  54. throw new ValidateException($e->getMessage());
  55. }
  56. return $this->json(['msg' => '修改成功']);
  57. }
  58. }
  59. }
  60. function getInfo()
  61. {
  62. if ($this->userInfo['username'] == 'admin') {
  63. $id = $this->request->post('id', '', 'serach_in');
  64. if (!$id) throw new ValidateException('参数错误');
  65. $field = '*';
  66. $res = AdminMenu::field($field)->find($id);
  67. return $this->json(['data' => $res]);
  68. }
  69. }
  70. function delete()
  71. {
  72. if ($this->userInfo['username'] == 'admin') {
  73. return $this->del(new AdminMenu());
  74. }
  75. }
  76. function getField()
  77. {
  78. $data['pids'] = _generateSelectTree(AdminMenu::getpidarray());
  79. return $this->json(['data' => $data]);
  80. }
  81. }