ManagerCtrl.java 5.65 KB
package com.ui.ctrl;


import com.ui.User.UserAuthLocal;
import com.ui.contants.HttpUriContants;
import com.ui.http.HttpRestClient;
import com.ui.model.BaseResponse;
import com.ui.model.req.AuthGroup;
import com.ui.model.req.AuthModule;
import com.ui.model.req.PageRequest;
import com.ui.model.req.User;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("manage")
public class ManagerCtrl {

    Logger log = LoggerFactory.getLogger(ManagerCtrl.class);

    @Autowired
    HttpRestClient httpRestClient;

    @Autowired
    UserAuthLocal userAuthLocal;

    @Autowired
    UserSaveComponent userSaveComponent;

    @RequestMapping("/toUser")
    public ModelAndView toAddUser() {
        return new ModelAndView("manager/user");
    }

    @RequestMapping("/toModule")
    public ModelAndView toAddModule(Model model) {
        BaseResponse baseResponse = httpRestClient.defaultPost(HttpUriContants.GET_All_MODULEGROUP, null, BaseResponse.class);
        if (baseResponse != null) {
            model.addAttribute("modulegroups", baseResponse.getData());
        }
        return new ModelAndView("manager/module");
    }

    @RequestMapping("/toAuthority")
    public ModelAndView toAuthority(Model model) {
        return new ModelAndView("manager/authgroup");
    }



    @RequestMapping("/getAuthorityGroup")
    @ResponseBody
    public BaseResponse getAuthorityGroup() {
        BaseResponse response = httpRestClient.defaultPost(HttpUriContants.GET_AUTHORITY_GROUP, null
                , BaseResponse.class);
        return response;
    }

    @RequestMapping("/getUsersByAuthGroup")
    @ResponseBody
    public BaseResponse getUsersByAuthGroup(String id) {
        if(id==null){
            id="";
        }
        BaseResponse response = httpRestClient.defaultPost(HttpUriContants.USER_BY_AUTH_GROUP+"?id="+id, null
                , BaseResponse.class);
        return response;
    }

    @RequestMapping("/saveAuthGroup")
    @ResponseBody
    public BaseResponse saveAuthGroup(@RequestBody AuthGroup authGroup) {
        return  httpRestClient.defaultPost(HttpUriContants.GET_AUTHORITY_GROUP_SAVE, authGroup, BaseResponse.class);
    }

    @RequestMapping("/deleteAuthGroup")
    @ResponseBody
    public BaseResponse deleteAuthGroup(Integer id) {
        Map map = new HashMap<>();
        map.put("id", id);
        return  httpRestClient.defaultGet(HttpUriContants.GET_AUTHORITY_GROUP_DELETE, BaseResponse.class,map);
    }

    @RequestMapping("/saveUser")
    @ResponseBody
    public BaseResponse saveUser(@RequestBody User user, HttpSession session) {
        return userSaveComponent.saveUser(user,session);
    }

    @RequestMapping("/getUsers")
    @ResponseBody
    public BaseResponse getUsers(PageRequest req, String keys) {
        BaseResponse response = httpRestClient.defaultPost(HttpUriContants.GET_USERS + "?keys="+keys, req, BaseResponse.class);
        return response;
    }
    
    @RequestMapping("/getAllUser")
    @ResponseBody
    public BaseResponse getAllUser(PageRequest req) {
        BaseResponse response = httpRestClient.defaultPost(HttpUriContants.GET_USERS_ALL, req, BaseResponse.class);
        return response;
    }

    @RequestMapping("/deleteUser")
    @ResponseBody
    public BaseResponse deleteUser(String name) {
        if ("admin".equals(name)) {
            return new BaseResponse(201, "此用户无法删除");
        }
        Map map = new HashMap<>();
        map.put("name", name);
        BaseResponse response = httpRestClient.defaultGet(HttpUriContants.USER_DELETE_NAME, BaseResponse.class, map);
        userAuthLocal.flushUser(name);
        return response;
    }

    @RequestMapping("/saveAuthModule")
    @ResponseBody
    public BaseResponse saveAuthModule(@RequestBody AuthModule authModule) {
        BaseResponse baseResponse = null;
        if (authModule.getId() < 1) {//add
            if (userAuthLocal.getAuthModuleByname(authModule.getModuleName()) != null) {
                return new BaseResponse(201, "模块已经存在");
            }
            baseResponse = httpRestClient.defaultPost(HttpUriContants.MODULE_INSERT, authModule, BaseResponse.class);
        } else {
            baseResponse = httpRestClient.defaultPost(HttpUriContants.MODULE_UPDATE, authModule, BaseResponse.class);

        }
        userAuthLocal.flushModule(authModule.getModuleName());
        return baseResponse;
    }

    @RequestMapping("/getAuthModules")
    @ResponseBody
    public BaseResponse getAuthModules(PageRequest req,String keys) {
        BaseResponse response = httpRestClient.defaultPost(HttpUriContants.GET_MODULES+"?keys="+keys, req, BaseResponse.class);
        return response;
    }

    @RequestMapping("/deleteAuthModule")
    @ResponseBody
    public BaseResponse deleteAuthModule(String name) {
        if ("manage".equals(name)) {
            return new BaseResponse(201, "此模块无法delete");
        }
        Map map = new HashMap<>();
        map.put("name", name);
        BaseResponse response = httpRestClient.defaultGet(HttpUriContants.MODULE_DELETE_NAME, BaseResponse.class, map);
        userAuthLocal.flushModule(name);
        return response;
    }

}