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


import com.ui.User.MD5Util;
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.AuthModule;
import com.ui.model.req.PageRequest;
import com.ui.model.req.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
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;

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

    @RequestMapping("/toModule")
    public ModelAndView toAddModule() {
        return new ModelAndView("manager/module");
    }

    @RequestMapping("/saveUser")
    @ResponseBody
    public BaseResponse saveUser(@RequestBody User user,HttpSession session) {
        BaseResponse baseResponse = null;
        if (user.getId() < 1) {//add
            if (userAuthLocal.getUserByname(user.getName()) != null) {
                return new BaseResponse(201, "用户已经存在");
            }
            user.setPwd(MD5Util.encryption("Yoho@9646"));
            baseResponse = httpRestClient.defaultPost(HttpUriContants.USER_INSERT, user, BaseResponse.class);
        } else {
            User u = (User) session.getAttribute("user");
            String username = u.getName();
            if ("admin".equals(user.getName()) && !"admin".equals(username)) {
                return new BaseResponse(201, "此用户无法修改");
            }
            baseResponse = httpRestClient.defaultPost(HttpUriContants.USER_UPDATE, user, BaseResponse.class);

        }
        userAuthLocal.flushUser(user.getName());
        return baseResponse;
    }

    @RequestMapping("/getUsers")
    @ResponseBody
    public BaseResponse getUsers(PageRequest req) {
        BaseResponse response = httpRestClient.defaultPost(HttpUriContants.GET_USERS, 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) {
        BaseResponse response = httpRestClient.defaultPost(HttpUriContants.GET_MODULES, 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;
    }

}