1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
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<String, String> 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<SystemInfo> 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<SystemInfoAdminVO> 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<SystemInfo> 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;
        }
    }
 
}