andorid后台根据用户权限配置前台菜单功能实现

Posted 静思浅行

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了andorid后台根据用户权限配置前台菜单功能实现相关的知识,希望对你有一定的参考价值。

项目需求,android显示的菜单要通过后台用户权限控制显示哪些菜单,前端通过接口请求菜单获取菜单Id集合然后显示相应的菜单。(获取菜单后可以在本地缓存下来,在没网的情况下依然可以显示。)

首先看看效果图

第一步:因为每个菜单的Id是固定不变的,在前端可以定义一个菜单实体类,如下:

//中队-受理业务分配
MenuBean menuBean2 = new MenuBean();
menuBean2.setMenuId(87);//菜单Id
menuBean2.setTabId(2);//该菜单所属页面
menuBean2.setTitle(BaseApplication.context.getString(R.string.tal1_act1));//菜单标题
menuBean2.setDrawbleId(R.drawable.icon_divider);//菜单图片
menuBean2.setIntent(new Intent(BaseApplication.context, TaskToMemberActivity.class));//点击菜单跳转到的页面
menus.add(menuBean2);
//中队 --任务处置审批(中队反馈确认)
MenuBean menuBean9 = new MenuBean();
menuBean9.setMenuId(86);
menuBean9.setTabId(2);
menuBean9.setTitle(BaseApplication.context.getString(R.string.tal2_act6));
menuBean9.setDrawbleId(R.drawable.icon_rwcz_confirm);
Intent submitIntent = new Intent(BaseApplication.context, TaskHandlerActivity.class);
submitIntent.putExtra("url", URLTool.QueryWorkTaskManageZDCZList);
submitIntent.putExtra("title", BaseApplication.context.getString(R.string.tal2_act6));
menuBean9.setIntent(submitIntent);
menus.add(menuBean9);

//中队 --巡检问题确认
MenuBean menuBean126 = new MenuBean();
menuBean126.setMenuId(126);
menuBean126.setTabId(2);
menuBean126.setTitle(BaseApplication.context.getString(R.string.tal2_act7));
menuBean126.setDrawbleId(R.drawable.icon_zdsp);
menuBean126.setIntent(new Intent(BaseApplication.context, ZDConfirmActivity.class));
menus.add(menuBean126);

第二步:登录完成之后请求角色菜单集合,然后根据角色菜单Id集合显示菜单
/**
 * 获取每个tab下的菜单
 *
 * @param tabID 页面ID
 * @param roleMenuResult 后台返回的角色菜单ID集合
 * @return
 */
public static List<MenuBean> getMenu(RoleMenuResult roleMenuResult,int tabID) 
    List<MenuBean> menuResult = new ArrayList<>();
    menuResult.clear();
    if(roleMenuResult==null)
        return menuResult;
    
    List<RoleMenu> menuList = roleMenuResult.getDt();
    if(menuList==null)
        return null;
    

    for (int i = 0; i < menus.size(); i++) 
        MenuBean localMenu = menus.get(i);
        if(localMenu.getTabId()!=tabID)
            continue;
        
        for (int j = 0; j < menuList.size(); j++) 
            RoleMenu roleMenu = menuList.get(j);
           if(localMenu.getMenuId()==roleMenu.getMENUID())
               if(!menuResult.contains(localMenu))
                   menuResult.add(menus.get(i));
               
           
        
    
    return menuResult;

第三步:使用gradView显示菜单:
menus = MenuUtils.getMenu(MainActivity.roleMenuResult, 3);
本地默认的菜单可以在本地定义好,然后添加到集合中。
menus.addAll(defaultMenus);
adapter = new RoleMenuAdaper(menus);
gridView.setAdapter(adapter);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() 
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
        Intent intent = (Intent) menus.get(position).getIntent();
        if (intent == null) 
            //现场取证
            if (menus.get(position).getTitle().equals(mActivity.getString(R.string.quzheng))) 
                popupWindow.showAtLocation(ll_main, Gravity.BOTTOM, 0, 0);
                CommonUtils.makeWindowDark(mActivity);
             else 
                ToastUtils.showShort(mActivity, "该功能暂未开放");
            
            return;
        
这些操作可以放到菜单工具类中,方便操作,可参考下面的代码片段
  
   
package com.yingde.onlinelaw2.utils;

import android.app.Activity;
import android.content.Intent;

import com.yingde.onlinelaw2.R;
import com.yingde.onlinelaw2.activitys.inspect.InspectActivity;
import com.yingde.onlinelaw2.activitys.leaderConfirm.LeaderConfirmActivity;
import com.yingde.onlinelaw2.activitys.taskToZD.TaskToZDListActivity;
import com.yingde.onlinelaw2.activitys.problem.ProblemAddActivity;
import com.yingde.onlinelaw2.activitys.search.SearchInspectActivity;
import com.yingde.onlinelaw2.activitys.search.SearchProblemActivity;
import com.yingde.onlinelaw2.activitys.search.SearchTaskActivity;
import com.yingde.onlinelaw2.activitys.serviceConfirm.ServiceConfirmActivity;
import com.yingde.onlinelaw2.activitys.simplecasess.SimplenessActivityOne;
import com.yingde.onlinelaw2.activitys.teamLeader.TaskToMemberActivity;
import com.yingde.onlinelaw2.activitys.teamLeader.ZDConfirmActivity;
import com.yingde.onlinelaw2.activitys.teamMember.TaskHandlerActivity;
import com.yingde.onlinelaw2.base.BaseApplication;
import com.yingde.onlinelaw2.bean.MenuBean;
import com.yingde.onlinelaw2.bean.RoleMenu;
import com.yingde.onlinelaw2.bean.RoleMenuResult;
import com.yingde.onlinelaw2.utils.http.URLTool;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/11/14.
 * 菜单
 */
public class MenuUtils 
    private static List
     
       menus = new ArrayList<>();

    static 

        //中队-受理业务分配
        MenuBean menuBean2 = new MenuBean();
        menuBean2.setMenuId(87);//菜单Id
        menuBean2.setTabId(2);//该菜单所属页面
        menuBean2.setTitle(BaseApplication.context.getString(R.string.tal1_act1));//菜单标题
        menuBean2.setDrawbleId(R.drawable.icon_divider);//菜单图片
        menuBean2.setIntent(new Intent(BaseApplication.context, TaskToMemberActivity.class));//点击菜单跳转到的页面
        menus.add(menuBean2);

        //中队 --任务处置审批(中队反馈确认)
        MenuBean menuBean9 = new MenuBean();
        menuBean9.setMenuId(86);
        menuBean9.setTabId(2);
        menuBean9.setTitle(BaseApplication.context.getString(R.string.tal2_act6));
        menuBean9.setDrawbleId(R.drawable.icon_rwcz_confirm);
        Intent submitIntent = new Intent(BaseApplication.context, TaskHandlerActivity.class);
        submitIntent.putExtra("url", URLTool.QueryWorkTaskManageZDCZList);
        submitIntent.putExtra("title", BaseApplication.context.getString(R.string.tal2_act6));
        menuBean9.setIntent(submitIntent);
        menus.add(menuBean9);

        //中队 --巡检问题确认
        MenuBean menuBean126 = new MenuBean();
        menuBean126.setMenuId(126);
        menuBean126.setTabId(2);
        menuBean126.setTitle(BaseApplication.context.getString(R.string.tal2_act7));
        menuBean126.setDrawbleId(R.drawable.icon_zdsp);
        menuBean126.setIntent(new Intent(BaseApplication.context, ZDConfirmActivity.class));
        menus.add(menuBean126);

        //领导审批 ---处置结果审批
        MenuBean menuBean3 = new MenuBean();
        menuBean3.setMenuId(88);
        menuBean3.setTabId(2);
        menuBean3.setTitle("处置结果审批");
        menuBean3.setDrawbleId(R.drawable.icon_leader_confirm);
        Intent intent3 = new Intent(BaseApplication.context, LeaderConfirmActivity.class);
        intent3.putExtra("position",0);
        menuBean3.setIntent(intent3);
        menus.add(menuBean3);
        //领导审批 --- 处置意见审批
        MenuBean menuBean4 = new MenuBean();
        menuBean4.setMenuId(74);
        menuBean4.setTabId(2);
        menuBean4.setTitle("处置意见审批");
        menuBean4.setDrawbleId(R.drawable.icon_leadercomfirm);
        Intent intent4 = new Intent(new Intent(BaseApplication.context, LeaderConfirmActivity.class));
        intent4.putExtra("position",1);
        menuBean4.setIntent(intent4);
        menus.add(menuBean4);

        //勤务审批  --- 预处理
        MenuBean menuBean5 = new MenuBean();
        menuBean5.setMenuId(80);
        menuBean5.setTabId(2);
        menuBean5.setTitle("预处理");
        menuBean5.setDrawbleId(R.drawable.icon_ycl);
        Intent intent5 = new Intent(BaseApplication.context, ServiceConfirmActivity.class);
        intent5.putExtra("position",0);
        menuBean5.setIntent(intent5);
        menus.add(menuBean5);

        //勤务审批  --- 处置结果审批
        MenuBean menuBean6 = new MenuBean();
        menuBean6.setMenuId(84);
        menuBean6.setTabId(2);
        menuBean6.setTitle("处置结果审批");
        menuBean6.setDrawbleId(R.drawable.icon_check);
        Intent intent6 = new Intent(BaseApplication.context, ServiceConfirmActivity.class);
        intent6.putExtra("position",1);
        menuBean6.setIntent(intent6);
        menus.add(menuBean6);

        //勤务审批  --- 任务处置反馈非当事人 id=89
        MenuBean menuBean7 = new MenuBean();
        menuBean7.setMenuId(89);
        menuBean7.setTabId(2);
        menuBean7.setTitle("任务处置反馈");
        menuBean7.setDrawbleId(R.drawable.icon_check_result);
        Intent intnet7  = new Intent(BaseApplication.context, ServiceConfirmActivity.class);
        intnet7.putExtra("position",2);
        menuBean7.setIntent(intnet7);
        menus.add(menuBean7);

        //任务下达中队
        MenuBean menuBean8 = new MenuBean();
        menuBean8.setMenuId(82);
        menuBean8.setTabId(2);
        menuBean8.setTitle("任务下达中队");
        menuBean8.setDrawbleId(R.drawable.icon_task_zd);
        Intent intent8 = new Intent(BaseApplication.context, ServiceConfirmActivity.class);
        intent8.putExtra("position",3);
        menuBean8.setIntent(intent8);
        menus.add(menuBean8);

        //简易案件
        MenuBean menuBean10 = new MenuBean();
        menuBean10.setMenuId(26);
        menuBean10.setTabId(3);
        menuBean10.setTitle(BaseApplication.context.getString(R.string.tal2_act1));
        menuBean10.setDrawbleId(R.drawable.icon_simple_case);
        menuBean10.setIntent(new Intent(BaseApplication.context, SimplenessActivityOne.class));
        menus.add(menuBean10);

        //执法检查记录
        MenuBean menuBean11 = new MenuBean();
        menuBean11.setMenuId(72);
        menuBean11.setTabId(3);
        menuBean11.setTitle(BaseApplication.context.getString(R.string.tal2_act2));
        menuBean11.setDrawbleId(R.drawable.icon_polices);
        menuBean11.setIntent(new Intent(BaseApplication.context, InspectActivity.class));
        menus.add(menuBean11);

        //问题登记
        MenuBean menuBean12 = new MenuBean();
        menuBean12.setMenuId(71);
        menuBean12.setTabId(3);
        menuBean12.setTitle(BaseApplication.context.getString(R.string.tal2_act3));
        menuBean12.setDrawbleId(R.drawable.icon_register);
        menuBean12.setIntent(new Intent(BaseApplication.context, ProblemAddActivity.class));
        menus.add(menuBean12);

        // 队员--处置结果填写
        MenuBean menuBean13 = new MenuBean();
        menuBean13.setMenuId(85);
        menuBean13.setTabId(3);
        menuBean13.setTitle(BaseApplication.context.getString(R.string.tal2_act5));
        menuBean13.setDrawbleId(R.drawable.icon_czjgtx);
        Intent handlerIntent = new Intent(BaseApplication.context, TaskHandlerActivity.class);
        handlerIntent.putExtra("url", URLTool.workTaskManageDYCZList);
        handlerIntent.putExtra("title", BaseApplication.context.getString(R.string.tal2_act5));
        menuBean13.setIntent(handlerIntent);
        menus.add(menuBean13);

        //投诉举报查询
        MenuBean menuBean14 = new MenuBean();
        menuBean14.setMenuId(75);
        menuBean14.setTabId(4);
        menuBean14.setTitle("问题登记查询");
        menuBean14.setDrawbleId(R.drawable.icon_pro_search);
        Intent intent14 = new Intent(BaseApplication.context, SearchProblemActivity.class);
        intent14.putExtra("action","search");
        intent14.putExtra("title","问题登记查询");
        menuBean14.setIntent(intent14);
        menus.add(menuBean14);

        //检查记录查询
        MenuBean menuBean15 = new MenuBean();
        menuBean15.setMenuId(76);
        menuBean15.setTabId(4);
        menuBean15.setTitle("检查记录查询");
        menuBean15.setDrawbleId(R.drawable.icon_inpect_search);
        Intent intent7 = new Intent(BaseApplication.context, SearchInspectActivity.class);
        intent7.putExtra("action","search");
        intent7.putExtra("title","检查记录查询");
        menuBean15.setIntent(intent7);
        menus.add(menuBean15);

        //问题登记
        MenuBean menuBean16 = new MenuBean();
        menuBean16.setMenuId(83);
        menuBean16.setTabId(4);
        menuBean16.setTitle("任务分派查询");
        menuBean16.setDrawbleId(R.drawable.icon_task_search);
        menuBean16.setIntent(new Intent(BaseApplication.context, SearchTaskActivity.class));
        menus.add(menuBean16);
    

    /**
     * 获取每个tab下的菜单
     *
     * @param tabID 页面ID
     * @param roleMenuResult 后台返回的角色菜单ID集合
     * @return
     */
    public static List
      
        getMenu(RoleMenuResult roleMenuResult,int tabID) 
        List
       
         menuResult = new ArrayList<>();
        menuResult.clear();
        if(roleMenuResult==null)
            return menuResult;
        
        List
        
          menuList = roleMenuResult.getDt();
        if(menuList==null)
            return null;
        

        for (int i = 0; i < menus.size(); i++) 
            MenuBean localMenu = menus.get(i);
            if(localMenu.getTabId()!=tabID)
                continue;
            
            for (int j = 0; j < menuList.size(); j++) 
                RoleMenu roleMenu = menuList.get(j);
               if(localMenu.getMenuId()==roleMenu.getMENUID())
                   if(!menuResult.contains(localMenu))
                       menuResult.add(menus.get(i));
                   
               
            
        
        return menuResult;
    

    /**
     * 根据菜单id获取intent
     * @param menuID
     * @return
     */
    public static Intent getIntent(int menuID) 
        for (int i = 0; i < menus.size(); i++) 
            if (menus.get(i).getMenuId() == menuID) 
                return menus.get(i).getIntent();
            
        
        return null;
    

    /**
     * 根据id获取menu
     */
    public static MenuBean getMenu(int menuId)

        for (int i = 0; i < menus.size(); i++) 
            if (menus.get(i).getMenuId() == menuId) 
                return menus.get(i);
            
        
        return null;
    

    public static String replaceMenuStr(String menuStr) 
        String menuString = menuStr;
        switch (menuStr) 
            case "任务处置结果反馈非当事人":
                menuString = "处置结果反馈";
                break;
            case "任务下达队员":
                menuString = "受理业务分派";
                break;
            case "任务处置结果反馈":
                menuString = "处置结果填写";
                break;
        
        return menuString;
    

    public static Intent getMenuIntent(Activity mActivity, String menuStr) 
        Intent intent = null;
        switch (menuStr) 
            case "预处理":
            case "处置结果审批":
            case "任务处置结果反馈非当事人":
                intent = new Intent(mActivity, ServiceConfirmActivity.class);
                break;
            case "任务下达中队":
                new Intent(mActivity, TaskToZDListActivity.class);
                break;
            //领导审批
            case "处置意见审批":
                new Intent(mActivity, LeaderConfirmActivity.class);
                break;
        
        return intent;
    




        
       
      
     

以上是关于andorid后台根据用户权限配置前台菜单功能实现的主要内容,如果未能解决你的问题,请参考以下文章

JavaWeb SSM 《精品毕设》个人博客系统 前台 + 后台(源码 + 论文)主要实现的登录注册主页博客随笔文章管理消息管理评论管理用户管理角色管理主题管理栏目管理等功能

JavaWeb SSM 《精品毕设》个人博客系统 前台 + 后台(完整源码+论文)主要实现的登录注册主页博客随笔文章管理消息管理评论管理用户管理角色管理主题管理栏目管理等功能

菜单权限,按钮权限的设计及实现

网站后台系统权限部分实现

权限管理:菜单管理

vue实现页面权限中的菜单配置