SpringBoot + Vue micro personnel authority group management module (fourteen)

Authorization group front-end page production

Permission groups manage the relationship between roles and menus, and operators manage the relationship between users and roles.
The English input box must have a prefix, and the English name of the role in SpringSecurity needs to be prefixed with ROLE_
insert image description here

insert image description here
upper code

   <div>
       <div class="permissManaTool">
        <el-input placeholder="请输入角色英文名" v-model="role.name">
            <template slot="prepend">ROLE_</template>
        </el-input>
         <el-input placeholder="请输入角色中文名" v-model="role.nameZh"></el-input>
      </div>
   </div>
 data(){
    
    
            return{
    
    
                role:{
    
    
                    name:"",
                    nameZh:"",
                }
            }
        }

add button
insert image description here

           <el-button type="primary" size="small" icon="el-icon-plus">添加角色</el-button>

Add input box style
insert image description here

    .permissManaTool{
    
    
        display: flex;
        justify-content: flex-start;
    }
    .permissManaTool .el-input{
    
    
        width: 300px;
        margin-right: 8px;
    }

Add a accordion
insert image description here
![Insert picture description here](https://img-blog.csdnimg.cn/02579f78440e450db2497be5c21db539.png

![Insert picture description here](https://img-blog.csdnimg.cn/d5d150b440f7435ebae4f6a4d3287138.png
It can be seen that the second one is expanded, and
insert image description here
only one is enough
insert image description here
. Get all the roles from the database and display them on the page. Note that the backend not only returns all the roles but also returns the role operation menu, and returns which resources the roles can operate.

Front-end and back-end docking of authority user roles

Let's first query all the roles and display them on the front end

PermissController

@RestController
@RequestMapping("/system/basic/permiss")
public class PermissController {
    
    

    @Autowired
    RoleService roleService;

    @GetMapping("/")
    public List<Role> getAllRoles(){
    
    

        return roleService.getAllRoles();
    }
}

RoleService

@Service
public class RoleService {
    
    


    @Autowired
    RoleMapper roleMapper;
    public List<Role> getAllRoles(){
    
    

        return roleMapper.getAllRoles();
    }
}

RoleMapper

insert image description here

    List<Role> getAllRoles();

RoleMapper.xml

  <select id="getAllRoles" resultMap="BaseResultMap">
    select *
    from role;
  </select>

docking front

insert image description here

 <div style="margin-top: 15px;width: 720px">
           <el-collapse accordion>
               <el-collapse-item  :title="item.namezh" :name="index"  v-for="(item,index) in roles" :key="index">
                   <div>与现实生活一致:与现实生活的流程、逻辑保持一致,遵循用户习惯的语言和概念;</div>
                   <div>在界面中一致:所有的元素和结构需保持一致,比如:设计样式、图标和文本、元素的位置等。</div>
               </el-collapse-item>
           </el-collapse>
       </div>

insert image description here
insert image description here
Display effect
insert image description here
The content displayed inside should be a card
insert image description here
component
insert image description here
insert image description here

  <div>
                      <el-card class="box-card">
                          <div slot="header" class="clearfix">
                              <span>卡片名称</span>
                              <el-button style="float: right; padding: 3px 0" type="text">操作按钮</el-button>
                          </div>
                          <div v-for="o in 4" :key="o" class="text item">
                              {
    
    {
    
    '列表内容 ' + o }}
                          </div>
                      </el-card>
                  </div>

insert image description here
Change button style to trash can icon
insert image description here
insert image description here

 <el-button style="float: right; padding: 3px 0;color: #ff2a0c" type="text" icon="el-icon-delete"></el-button>

Permission group menu tree display

Query all menus to display the tree structure, which needs to be processed and returned on the server side.
Query all parent-child menus, and associate yourself three times to form a table.
insert image description here

select m1.id as id1,m1.name as name1,m2.id as id2,m2.name as name2,m3.id as id3,m3.name as name3 
 from menu m1,menu m2,menu m3 
where m1.id = m2.parentId and m2.id =m3.parentId and m3.enabled = true  
ORDER BY  m1.id,m2.id,m3.id```

### PermissController
![在这里插入图片描述](https://img-blog.csdnimg.cn/8bf2e5f06fc647038f6066e6603dfa49.png)

```java
@RestController
@RequestMapping("/system/basic/permiss")
public class PermissController {

    @Autowired
    RoleService roleService;

    @Autowired
    MenuService menuService;

    @GetMapping("/")
    public List<Role> getAllRoles(){

        return roleService.getAllRoles();
    }

    @PostMapping("/menus")
    public List<Menu> getAllMenus(){
        return menuService.getAllMenus();
    }
}

MenuService

insert image description here

@Service
public class MenuService {
    
    

    @Autowired
    MenuMapper menuMapper;
    public RespBean getMenusByHrId() {
    
    
      return RespBean.ok("操作成功!",menuMapper.getMenusByHrId( ((Hr) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getId()));//Id从哪里来呢,前端传过来的信息是不可信,我们登录的用户信息保存在security,可以从Security里获取登录用户信息
    }

    /**
     * 获取所有的菜单角色   一对多 一个菜单项有多个角色
     * @return
     */
//    @Cacheable
    public List<Menu> getAllMenusWithRole(){
    
    
        return menuMapper.getAllMenusWithRole();
    }

    public List<Menu> getAllMenus() {
    
    
        return menuMapper.getAllMenus();
    }
}

MenuMapper

insert image description here

   List<Menu> getAllMenus();

MenuMapper.xml

A parent menu has multiple submenus, and there are menus in the submenus, three-level menus, direct query
If you don’t know how many levels of menus you can use recursion
insert image description here

  <resultMap id="MenuWithChildren" type="com.xyg.pojo.Menu" extends="BaseResultMap">
    <id column="id1" property="id"></id>
    <result column="name1" property="name"></result>
    <collection property="children" ofType="com.xyg.pojo.Menu">
      <id column="id2" property="id"></id>
      <result column="name2" property="name"></result>
      <collection property="children" ofType="com.xyg.pojo.Menu">
        <id column="id3" property="id"></id>
        <result column="name3" property="name"></result>
      </collection>
    </collection>
  </resultMap>

  <select id="getAllMenus" resultMap="MenuWithChildren">
    select m1.id as id1,m1.name as name1,m2.id as id2,m2.name as name2,m3.id as id3,m3.name as name3
    from menu m1,menu m2,menu m3
    where m1.id = m2.parentId and m2.id =m3.parentId and m3.enabled = true
    ORDER BY  m1.id,m2.id,m3.id  
    </select>

test
insert image description here

front end

insert image description here
Copy it over
insert image description here
, define the corresponding variables
insert image description here
, add a click event for the folding panel,
insert image description here
insert image description here
insert image description here
and make a request to load data from the backend
insert image description here

   change(name){
    
    
                if (name){
    
    
                    this.getAllMenus();//点击有id就执行
                }
            },
   getAllMenus(){
    
    
                this.postRequest("/system/basic/permiss/menus").then(resp=>{
    
    
                    if(resp){
    
    
                        console.log(resp)
                        this.allmenus=resp
                    }
                })
            },

Add multi-select box
insert image description here
display effect
insert image description here

Modification of menu role relationship

All the roles are displayed, and there is still a problem of pre-selection, which is to write the menu that the selected role can operate. See the
database has a menu_role menu role association table, query which menu can be operated according to the role id, and query the id of the menu that the role can operate come out.
Use components
insert image description here
insert image description here
to hardcode the data to see the effect of the default selection
insert image description here
Show the effect
insert image description here
Dynamically modify the data returned by the hardcoded data server, you can query the menu mid according to the role rid

PermissController

    @GetMapping("/mids/{rid}")
    public List<Integer>  getMidsByTid(@PathVariable  Integer rid){
    
    
        return menuService.getMidsByTid(rid);
    }

menuMapper

    public  List<Integer> getMidsByTid(Integer rid) {
    
    
        return menuMapper.getMidsByTid(rid);
    }

MenuMapper

List<Integer> getMidsByTid(Integer rid);

MenuMapper.xml


  <select id="getMidsByTid" resultType="integer">
    select mid from menu_role where rid=#{rid}
  </select>

docking front

The name parameter is required to bind the role id
insert image description here

insert image description here
Set the default expanded and default checked nodes through default-expanded-keys and default-checked-keys respectively. It should be noted that node-key must be set at this time, and its value is a field name in the node data, which is unique in the entire tree.

insert image description here
Dock the backend
insert image description here
Click the event chance, pass the role id, call getSelectedMenus to pass the role id, query the id of the menu and assign the selectedMenus array, and tree will pre-select the multi-selection box by default

 change(rid){
    
    
                if (rid){
    
    
                    this.getAllMenus();//点击有id就执行
                    this.getSelectedMenus(rid)
                }
            },
            getSelectedMenus(rid){
    
    
                this.getRequest("/system/basic/permiss/mids/"+rid).then(resp=>{
    
    
                    if (resp){
    
    
                        this.selectedMenus=resp
                    }
                })
            },

Modification of menu role relationship

insert image description here
The component element can be obtained by ref="tree"insert image description here
insert image description here

insert image description here
If the getCheckedKeys method can be selected (that is, the show-checkbox is true), it will return an array of the keys of the currently selected nodes, (leafOnly) accepts a parameter of boolean type, and if it is true, only the selected leaves will be returned The keys of the node, the default value is false,
insert image description here
print it out and have a look
insert image description here
insert image description here

        methods:{
    
    
            doUpdate(rid,index){
    
    
              console.log(rid)
               let tree = this.$refs.tree[index];//返回当前选中的元素
              let selectedKeys = tree.getCheckedKeys(true)//getCheckedKeys方法获取选中菜单节点的key值就是菜单id,若为true就返回叶子节点也就是子节点
                console.log(selectedKeys)
            },

insert image description here

Write modification operation backend interface

The update operation here is a bit cumbersome, such as user ## authority group front-end page production
authority group management role and the relationship between the menu, the operator manages the relationship between the user and the role. The English input box must have a prefix, and the English name of the role in SpringSecurity needs to be prefixed with a ROLE_. The insert image description hereabove insert image description herecode

<div>      
   <div class="permissManaTool">        
   <el-input placeholder="请输入角色英文名" v-model="role.name">                            <template slot="prepend">ROLE_</template>       
     </el-input>             
  <el-input placeholder="请输入角色中文名" v-model="role.nameZh">         </el-input>      
     </div>   
</div>
data(){
    
                
    return{
    
                    
         role:{
    
                        
             name:"",                    
             nameZh:"",               
              }           
          }        
       }

Add buttonAdd insert image description herebash <el-button type="primary" size="small" icon="el-icon-plus">添加角色</el-button>input box styleinsert image description here

permissManaTool{
    
            
    display: flex;       
    justify-content: flex-start;   
  }    
  .permissManaTool .el-input{
    
           
       width: 300px;       
       margin-right: 8px;  
    }

Add a folding panel  insert image description here![Insert picture description here](https://img-blog.csdnimg.cn/02579f78440e450db2497be5c21db539.png![Insert picture description here](https://img-blog.csdnimg.cn/d5d150b440f7435ebae4f6a4d3287138.pngIt can be seen that the second one is expanded one insert image description hereis enough to insert image description hereget all the roles from the database and display them on the page. Note that the backend not only returns all roles but also returns the role operation menu, role Which resources can be operated should also be returned ## Permission user role front-end docking Let's first query all roles and display them on the front end ### PermissController java @RestController@RequestMapping("/system/basic/permiss")public class PermissController { @Autowired RoleService roleService; @GetMapping("/") public List<Role> getAllRoles(){ return roleService.getAllRoles(); }}### RoleService java@Servicepublic class RoleService { @Autowired RoleMapper roleMapper; public List<Role> getAllRoles(){ return roleMapper.getAllRoles(); }}### RoleMapper insert image description herejava List<Role> getAllRoles();### RoleMapper.xml xml <select id="getAllRoles" resultMap="BaseResultMap"> select * from role; </select>### Docking front-endDisplay effectThe content displayed inside should be cardcomponentThe button style is changed to the trash can icon ## Permission group menu tree display Query all menu display tree structures insert image description herebash <div style="margin-top: 15px;width: 720px"> <el-collapse accordion> <el-collapse-item :title="item.namezh" :name="index" v-for="(item,index) in roles" :key="index"> <div>与现实生活一致:与现实生活的流程、逻辑保持一致,遵循用户习惯的语言和概念;</div> <div>在界面中一致:所有的元素和结构需保持一致,比如:设计样式、图标和文本、元素的位置等。</div> </el-collapse-item> </el-collapse> </div>, which need to be processed and returned on the server side Query all parent-child menus, associate yourself three times to form a table,  ### PermissController ### MenuService ### MenuMapper ### MenuMapper.xml A parent menu has multiple submenus, and there are also submenus in the submenu Menu, three-level menu, direct query If you don’t confirm how many levels of menu you can use recursive test ## Front end Copy over Define the corresponding variable Add a click event for the folding panel insert image description hereinsert image description hereinsert image description hereinsert image description hereinsert image description hereinsert image description herebash <div> <el-card class="box-card"> <div slot="header" class="clearfix"> <span>卡片名称</span> <el-button style="float: right; padding: 3px 0" type="text">操作按钮</el-button> </div> <div v-for="o in 4" :key="o" class="text item"> { {'列表内容 ' + o }} </div> </el-card> </div>insert image description hereinsert image description hereinsert image description herebash <el-button style="float: right; padding: 3px 0;color: #ff2a0c" type="text" icon="el-icon-delete"></el-button>insert image description heresqlselect m1.id as id1,m1.name as name1,m2.id as id2,m2.name as name2,m3.id as id3,m3.name as name3 from menu m1,menu m2,menu m3 where m1.id = m2.parentId and m2.id =m3.parentId and m3.enabled = true ORDER BY m1.id,m2.id,m3.idinsert image description herejava@RestController@RequestMapping("/system/basic/permiss")public class PermissController { @Autowired RoleService roleService; @Autowired MenuService menuService; @GetMapping("/") public List<Role> getAllRoles(){ return roleService.getAllRoles(); } @PostMapping("/menus") public List<Menu> getAllMenus(){ return menuService.getAllMenus(); }}insert image description herejava@Servicepublic class MenuService { @Autowired MenuMapper menuMapper; public RespBean getMenusByHrId() { return RespBean.ok("操作成功!",menuMapper.getMenusByHrId( ((Hr) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getId()));//Id从哪里来呢,前端传过来的信息是不可信,我们登录的用户信息保存在security,可以从Security里获取登录用户信息 } /** * 获取所有的菜单角色 一对多 一个菜单项有多个角色 * @return */// @Cacheable public List<Menu> getAllMenusWithRole(){ return menuMapper.getAllMenusWithRole(); } public List<Menu> getAllMenus() { return menuMapper.getAllMenus(); }}insert image description herejava List<Menu> getAllMenus();insert image description herexml <resultMap id="MenuWithChildren" type="com.xyg.pojo.Menu" extends="BaseResultMap"> <id column="id1" property="id"></id> <result column="name1" property="name"></result> <collection property="children" ofType="com.xyg.pojo.Menu"> <id column="id2" property="id"></id> <result column="name2" property="name"></result> <collection property="children" ofType="com.xyg.pojo.Menu"> <id column="id3" property="id"></id> <result column="name3" property="name"></result> </collection> </collection> </resultMap> <select id="getAllMenus" resultMap="MenuWithChildren"> select m1.id as id1,m1.name as name1,m2.id as id2,m2.name as name2,m3.id as id3,m3.name as name3 from menu m1,menu m2,menu m3 where m1.id = m2.parentId and m2.id =m3.parentId and m3.enabled = true ORDER BY m1.id,m2.id,m3.id </select>insert image description hereinsert image description hereinsert image description hereinsert image description hereinsert image description hereinsert image description hereinsert image description hereMake a request to load data at the back end Add insert image description herejava change(name){ if (name){ this.getAllMenus();//点击有id就执行 } }, getAllMenus(){ this.postRequest("/system/basic/permiss/menus").then(resp=>{ if(resp){ console.log(resp) this.allmenus=resp } }) },multi-selection boxes insert image description hereDisplay effect insert image description here## Menu role relationship modification All roles are displayed, and there is still a problem of pre-selection, which is the selected role can be operated. Write the menu to see that the database has a menu_role menu role In the association table, query which menus can be operated according to the role id, and query the id of the menu that the role can operate. Use componentsinsert image description hereWrite insert image description heredead data to see the default selection effect  insert image description hereDisplay effect  insert image description hereDynamically modify the data returned by the dead written data server, you can query the menu mid according to the role rid ### PermissController java @GetMapping("/mids/{rid}") public List<Integer> getMidsByTid(@PathVariable Integer rid){ return menuService.getMidsByTid(rid); }### menuMapper java public List<Integer> getMidsByTid(Integer rid) { return menuMapper.getMidsByTid(rid); }### MenuMapper javaList<Integer> getMidsByTid(Integer rid);MenuMapper.xml xml <select id="getMidsByTid" resultType="integer"> select mid from menu_role where rid=#{rid} </select>### The name parameter is required to bind the role id to the front end. insert image description hereSet insert image description herethe default expanded and default selected nodes through default-expanded-keys and default-checked-keys respectively. It should be noted that node-key must be set at this time, and its value is a field name in the node data, which is unique in the entire tree. Docking insert image description herethe backend Click insert image description herethe event chance, pass the role id, call getSelectedMenus to pass the role id, query the id of the menu and assign the selectedMenus array, and the tree will pre-select the multi-selection box by default ### Menu role modification can be referenced java change(rid){ if (rid){ this.getAllMenus();//点击有id就执行 this.getSelectedMenus(rid) } }, getSelectedMenus(rid){ this.getRequest("/system/basic/permiss/mids/"+rid).then(resp=>{ if (resp){ this.selectedMenus=resp } }) },by insert image description hereref ="tree" Get the component element  insert image description hereinsert image description hereinsert image description hereIf the getCheckedKeys method can be selected (that is, the show-checkbox is true), it will return an array of the keys of the currently selected nodes, (leafOnly) accepts a parameter of boolean type, and if it is true, only the selected leaves will be returned Node’s keys, the default value is falsePrint insert image description hereit out and have a look ### It’s a bit troublesome to write and modify the back-end interface insert image description here. The update operation here is a bit troublesome. For example, the user has checked a few and canceled a few. Judgment one by one, here directly delete the original, direct update operationinsert image description herejava methods:{ doUpdate(rid,index){ console.log(rid) let tree = this.$refs.tree[index];//返回当前选中的元素 let selectedKeys = tree.getCheckedKeys(true)//getCheckedKeys方法获取选中菜单节点的key值就是菜单id,若为true就返回叶子节点也就是子节点 console.log(selectedKeys) },insert image description here

PermissController

    @PutMapping("/")
    public RespBean updateMenuRole(Integer rid,Integer[] mids){
    
    
        if(menuService.updateMenuRole(rid,mids)){
    
    
             return RespBean.ok("更新成功");
        }
        return RespBean.err("更新失败");
    }

MenuService

    @Transactional
    public boolean updateMenuRole(Integer rid, Integer[] mids) {
    
    
        menuRoleMapper.deleteById(rid);
        Integer result=menuRoleMapper.insertRecord(rid,mids);
        return result==mids.length;
    }

MenuRoleMapper

    void deleteById(Integer rid);

    Integer insertRecord(@Param("rid") Integer rid,@Param("mids") Integer[] mids);

MenuRoleMapper.xml

  <delete id="deleteById">
    delete from menu_role where rid=#{rid}
  </delete>

  <insert id="insertRecord">
    insert into  menu_role (mid,rid) values
    <foreach collection="mids" separator="," item="mid">
      (#{mid},#{rid})
    </foreach>
  </insert>

docking front

doUpdate(rid,index){
    
    
               let tree = this.$refs.tree[index];//返回当前选中的元素
               let selectedKeys = tree.getCheckedKeys(true)//getCheckedKeys方法获取选中菜单节点的key值就是菜单id,为true就获取子节点
                let url='/system/basic/permiss/?rid='+rid
                selectedKeys.forEach(key=>{
    
    
                    url +='&mids='+key;
                    console.log(url)
                })

                this.putRequest(url).then(resp=>{
    
    
                    if(resp){
    
    
                        this.getRolesAll()
                    }
                })
            },

insert image description here
The control panel is bound to an attribute with a
insert image description here
value of -1, that is, no one will display
insert image description here
insert image description here
activeName. If its value is equal to the name value in the figure, it will be displayed. If it is equal to -1, there is no corresponding value and it will not be displayed.
insert image description here

 doUpdate(rid,index){
    
    
               let tree = this.$refs.tree[index];//返回当前选中的元素
               let selectedKeys = tree.getCheckedKeys(true)//getCheckedKeys方法获取选中菜单节点的key值就是菜单id,为true就获取子节点
                let url='/system/basic/permiss/?rid='+rid
                selectedKeys.forEach(key=>{
    
    
                    url +='&mids='+key;
                    console.log(url)
                })

                this.putRequest(url).then(resp=>{
    
    
                    if(resp){
    
    
                        this.getRolesAll()
                        this.activeName=-1
                    }
                })
            },

It will not be displayed if the modification is cancelled.
insert image description here
insert image description here

 cancelUpdate(){
    
    
                this.activeName=-1
            },

Add permission group role

PermissController

    @PostMapping("/role")
    public RespBean addRole(@RequestBody Role role){
    
    
        if(roleService.addRole(role)==1){
    
    
            return RespBean.ok("添加成功");
        }

        return RespBean.err("添加失败");
    }

RoleService

Because the role of SpringSecurity needs to start with ROLE data

    public Integer addRole(Role role) {
    
    

        if(!role.getName().startsWith("ROLE_")){
    
    
            role.setName("ROLE"+role.getName());
        }
        return roleMapper.insert(role);
    }

mapper generated using reverse engineering tools

insert image description here

Add role interface docking front end

insert image description here
insert image description here
Determine whether the data in the input box is empty, and execute the backend interface if it is not empty
insert image description here

permission group role deletion

PermissController

    @DeleteMapping("/")
    public RespBean deletePositionByIds(Integer[] ids){
    
    
        if(positionsService.deletePositionsByIds(ids)==ids.length){
    
    
            return RespBean.ok("删除成功");
        }
        return RespBean.err("删除失败");
    }

PositionService

    public int deletePositionsByIds(Integer[] ids) {
    
    
        return positionMapper.deletePositionsByIds(ids);
    }

RoleMapper

insert image description here

delete docking frontend

insert image description here
insert image description here

Guess you like

Origin blog.csdn.net/qq_45709416/article/details/132374721