package com.yeshi.buwan.controller.admin.login; import java.io.PrintWriter; import java.util.ArrayList; import java.util.List; import java.util.Map; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import com.yeshi.buwan.domain.system.SystemInfo; import com.yeshi.buwan.service.imp.SystemService; import com.yeshi.buwan.util.SystemUtil; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.yeshi.buwan.domain.AdminInfo; import com.yeshi.buwan.service.imp.AdminUserService; import com.yeshi.buwan.service.imp.DetailSystemConfigService; import com.yeshi.buwan.util.Constant; import com.yeshi.buwan.util.StringUtil; import net.sf.json.JSONObject; @Controller @RequestMapping("admin/new/api/login") public class LoginController { @Resource private DetailSystemConfigService configService; @Resource private AdminUserService adminUserService; @Resource private SystemService systemService; @RequestMapping("adminLogin") public void adminLogin(String username, String pwd, String code, HttpServletRequest request, PrintWriter out) { System.out.println("username" + username); System.out.println("pwd" + pwd); Map map = configService.getConfigAsMap(SystemUtil.getDetailSystem(), SystemUtil.getDefaultVersion()); JSONObject json = new JSONObject(); if (!StringUtil.isNullOrEmpty(map.get("login_permission")) && !map.get("login_permission").equalsIgnoreCase(username)) { json.put("code", "1"); json.put("error", "没有登陆权限"); out.print(json); return; } String ocode = request.getSession().getAttribute(Constant.RANDKEY) + ""; request.getSession().removeAttribute(Constant.RANDKEY); if (StringUtil.isNullOrEmpty(code) || !code.equalsIgnoreCase(ocode)) { json.put("code", "1"); json.put("error", "验证码错误"); out.print(json); return; } else { AdminInfo info = adminUserService.login(username.trim(), pwd.trim()); if (info == null) { // MailSenderUtil.sendEmail("1101184511@qq.com", "影音后台登录", // username.trim() + "--" + "登录失败 IP:" // + IPUtil.getRemotIP(request) + "--" + // IPUtil.getIPInfo(IPUtil.getRemotIP(request))); json.put("code", "1"); json.put("error", "用户名或密码错误"); out.print(json); return; } else { request.getSession().setAttribute(Constant.ADMIN, info); SystemUtil.saveAdminSelectedSystem(request.getSession(), getSelectedSystem(request.getSession())); // MailSenderUtil.sendEmail("1101184511@qq.com", "影音后台登录", // username.trim() + "--" + "登录成功 IP:" // + IPUtil.getRemotIP(request) + "--" + // IPUtil.getIPInfo(IPUtil.getRemotIP(request))); json.put("code", "0"); json.put("success", "登陆成功"); out.print(json); return; } } } @RequestMapping("loginExit") public String loginExit(HttpServletRequest request, PrintWriter out) { request.getSession().removeAttribute("ADMIN_INFO"); request.getSession().invalidate(); return "redirect:/admin/new/login.html"; } @RequestMapping("getLoginName") public void getLoginName(HttpServletRequest request, PrintWriter out) { AdminInfo info = (AdminInfo) request.getSession().getAttribute(Constant.ADMIN); JSONObject json = new JSONObject(); if (info != null) { List systemInfoList = systemService.getSystemList(); SystemInfo system = getSelectedSystem(request.getSession()); Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create(); json.put("code", "0"); json.put("info", gson.toJson(info)); List systemInfoAdminVOS = new ArrayList<>(); for (SystemInfo systemInfo : systemInfoList) { systemInfoAdminVOS.add(new SystemInfoAdminVO(systemInfo.getId(), systemInfo.getName(), systemInfo.getId().equalsIgnoreCase(system.getId()))); } json.put("systems", new Gson().toJson(systemInfoAdminVOS)); } else { json.put("code", "1"); } out.print(json); } @RequestMapping("selectSystem") public void selectSystem(HttpServletRequest request, String system, PrintWriter out) { SystemInfo systemInfo = systemService.getSystem(system); if (systemInfo == null) { JSONObject json = new JSONObject(); json.put("code", "1"); json.put("error", "系统不存在"); out.print(json); } else { saveSelectedSystem(request.getSession(), systemInfo); JSONObject json = new JSONObject(); json.put("code", "0"); out.print(json); } } private SystemInfo getSelectedSystem(HttpSession session) { SystemInfo systemInfo = SystemUtil.getAdminSelectedSystem(session); if (systemInfo == null) { List systemInfoList = systemService.getSystemList(); systemInfo = systemInfoList.get(0); } return systemInfo; } private void saveSelectedSystem(HttpSession session, SystemInfo systemInfo) { SystemUtil.saveAdminSelectedSystem(session, systemInfo); } static class SystemInfoAdminVO { private String id; private String name; private boolean selected; public SystemInfoAdminVO(String id, String name, boolean selected) { this.id = id; this.name = name; this.selected = selected; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public boolean isSelected() { return selected; } public void setSelected(boolean selected) { this.selected = selected; } } }