|
| 1 | +package com.example.springbootshiro.controller; |
| 2 | + |
| 3 | +import com.example.springbootshiro.domain.MenuInfo; |
| 4 | +import com.example.springbootshiro.domain.ResponseResult; |
| 5 | +import com.example.springbootshiro.domain.Tree; |
| 6 | +import com.example.springbootshiro.service.MenuService; |
| 7 | +import org.apache.shiro.authz.annotation.RequiresPermissions; |
| 8 | +import org.springframework.beans.factory.annotation.Autowired; |
| 9 | +import org.springframework.stereotype.Controller; |
| 10 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 11 | +import org.springframework.web.bind.annotation.ResponseBody; |
| 12 | + |
| 13 | +import java.util.List; |
| 14 | + |
| 15 | +/** |
| 16 | + * Created by Douglee on 2018/4/9. |
| 17 | + */ |
| 18 | +@Controller |
| 19 | +public class MenuController extends BaseController { |
| 20 | + @Autowired |
| 21 | + private MenuService menuService; |
| 22 | + |
| 23 | + @RequestMapping("menu") |
| 24 | + public String index(){ |
| 25 | + return "menu/menu"; |
| 26 | + } |
| 27 | + |
| 28 | + @RequestMapping("menu/menu") |
| 29 | + @ResponseBody |
| 30 | + public ResponseResult getMenu(String userName){ |
| 31 | + try { |
| 32 | + List<MenuInfo> menus = menuService.findUserMenus(userName); |
| 33 | + return ResponseResult.ok(); |
| 34 | + }catch (Exception ex){ |
| 35 | + ex.printStackTrace(); |
| 36 | + return ResponseResult.error("获取菜单失败"); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + @RequestMapping("menu/tree") |
| 41 | + @ResponseBody |
| 42 | + public ResponseResult getMenuTree() { |
| 43 | + try { |
| 44 | + Tree<MenuInfo> tree = this.menuService.getMenuTree(); |
| 45 | + return ResponseResult.ok(tree); |
| 46 | + } catch (Exception e) { |
| 47 | + e.printStackTrace(); |
| 48 | + return ResponseResult.error("获取菜单列表失败!"); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + @RequestMapping("menu/menuButtonTree") |
| 53 | + @ResponseBody |
| 54 | + public ResponseResult getMenuButtonTree() { |
| 55 | + try { |
| 56 | + Tree<MenuInfo> tree = this.menuService.getMenuButtonTree(); |
| 57 | + return ResponseResult.ok(tree); |
| 58 | + } catch (Exception e) { |
| 59 | + e.printStackTrace(); |
| 60 | + return ResponseResult.error("获取菜单列表失败!"); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + @RequestMapping("menu/getMenu") |
| 65 | + @ResponseBody |
| 66 | + public ResponseResult getMenu(Long menuId){ |
| 67 | + try { |
| 68 | + MenuInfo menuInfo = menuService.findById(menuId); |
| 69 | + return ResponseResult.ok(menuInfo); |
| 70 | + }catch (Exception ex){ |
| 71 | + ex.printStackTrace(); |
| 72 | + return ResponseResult.error("获取信息失败"); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + |
| 77 | + @RequestMapping("menu/getUserMenu") |
| 78 | + @ResponseBody |
| 79 | + public ResponseResult getUserMenu(String userName){ |
| 80 | + try { |
| 81 | + Tree<MenuInfo> tree = menuService.getUserMenu(userName); |
| 82 | + return ResponseResult.ok(tree); |
| 83 | + }catch (Exception ex){ |
| 84 | + ex.printStackTrace(); |
| 85 | + return ResponseResult.error("获取用户菜单失败"); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + @RequestMapping("menu/list") |
| 90 | + @ResponseBody |
| 91 | + public List<MenuInfo> menuList(MenuInfo menuInfo){ |
| 92 | + try { |
| 93 | + return menuService.findAllMenus(menuInfo); |
| 94 | + }catch (Exception ex){ |
| 95 | + ex.printStackTrace(); |
| 96 | + return null; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + @RequiresPermissions("menu:add") |
| 101 | + @RequestMapping("menu/add") |
| 102 | + @ResponseBody |
| 103 | + public ResponseResult addMenu(MenuInfo menuInfo){ |
| 104 | + String name = ""; |
| 105 | + if (MenuInfo.TYPE_MENU.equals(menuInfo.getType())){ |
| 106 | + name = "菜单"; |
| 107 | + }else { |
| 108 | + name = "按钮"; |
| 109 | + } |
| 110 | + |
| 111 | + try { |
| 112 | + menuService.addMenu(menuInfo); |
| 113 | + return ResponseResult.ok("新增" + name + "成功"); |
| 114 | + }catch (Exception ex){ |
| 115 | + ex.printStackTrace(); |
| 116 | + return ResponseResult.ok("新增" + name + "失败"); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + @RequiresPermissions("menu:delete") |
| 121 | + @RequestMapping("menu/delete") |
| 122 | + @ResponseBody |
| 123 | + public ResponseResult deleteMenus(String ids) { |
| 124 | + try { |
| 125 | + this.menuService.deleteMeuns(ids); |
| 126 | + return ResponseResult.ok("删除成功!"); |
| 127 | + } catch (Exception e) { |
| 128 | + e.printStackTrace(); |
| 129 | + return ResponseResult.error("删除失败,请联系网站管理员!"); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + @RequiresPermissions("menu:update") |
| 134 | + @RequestMapping("menu/update") |
| 135 | + @ResponseBody |
| 136 | + public ResponseResult updateMenu(MenuInfo menu) { |
| 137 | + String name = ""; |
| 138 | + if (MenuInfo.TYPE_MENU.equals(menu.getType())) |
| 139 | + name = "菜单"; |
| 140 | + else |
| 141 | + name = "按钮"; |
| 142 | + try { |
| 143 | + this.menuService.updateMenu(menu); |
| 144 | + return ResponseResult.ok("修改" + name + "成功!"); |
| 145 | + } catch (Exception e) { |
| 146 | + e.printStackTrace(); |
| 147 | + return ResponseResult.error("修改" + name + "失败,请联系网站管理员!"); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + |
| 152 | +} |
0 commit comments