cy
2022-06-23 b83c40548208609d0d6826be13d742c28a784806
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package cn.ksource.web.controller.uc;
 
import cn.ksource.core.util.*;
import cn.ksource.core.web.WebLoginUser;
import cn.ksource.core.web.WebUtil;
import cn.ksource.web.Constants;
import cn.ksource.web.entity.WebLoginEntity;
import cn.ksource.web.facade.uc.login.UserFacade;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
 
@Controller
@RequestMapping("/user")
public class UserController {
 
    @Resource(name="userFacade")
    private UserFacade userFacade;
    
    
    /**
     * 跳转到用户中心登录页面
     * @param req
     * @return
     */
    @RequestMapping(value="login.html", method=RequestMethod.GET)
    public ModelAndView toLogin(HttpServletRequest req) {
        WebLoginUser webLoginUser = WebUtil.getWebLoginUser(req);
        if (webLoginUser == null) {
            ModelAndView modelAndView = new ModelAndView("/uc/login/login");
            return modelAndView;
        }
        return new ModelAndView("redirect:/uc/home.html");
    }
    
    
    
    /**
     * 用户登录
     * @param request
     * @param response
     */
    @RequestMapping(value="login.html", method=RequestMethod.POST)
    public void login(HttpServletRequest request,HttpServletResponse response) {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String checkCode = request.getParameter("checkCode");
        WebLoginEntity webLoginEntity = new WebLoginEntity();
        //判断验证码
        if (StringUtil.notEmpty(checkCode) && StringUtils.equalsIgnoreCase(checkCode, (String) request.getSession().getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY))) {
            
            webLoginEntity = userFacade.doUserLogin(request,username, password);
        } else {
            webLoginEntity.setResult(WebLoginEntity.Login_Result_CheckCodeError);
        }
        int result = webLoginEntity.getResult();
        Map<String,String> map = new HashMap<String, String>();
        map.put("result", String.valueOf(result));
        map.put("msg", webLoginEntity.Login_Result_Info.get(result));
        String json = JsonUtil.map2Json(map);
        WebUtil.write(response, json);
    }
    
    /**
     * 跳转到找回密码页面
     * @param req
     * @return
     */
    @RequestMapping(value="findPwd.html", method=RequestMethod.GET)
    public ModelAndView forgotPwd(HttpServletRequest req) {
        return new ModelAndView("/uc/login/findPwd");
    }
    
    /**
     * 通过用户名查询用户名是否存在
     */
    
    @RequestMapping("queryUser.html")
    public void queryUser(HttpServletRequest request,HttpServletResponse response) {
        String username = request.getParameter("param");
        Map map = userFacade.queryUserByName(username);
        Map resultMap = new HashMap();
        if(null!=map&&map.size()>0) {
            resultMap.put("info", "用户名输入正确");
            resultMap.put("status", "y");
        } else {
            resultMap.put("info", "用户名不存在");
            resultMap.put("status", "n");
        }
        WebUtil.write(response, JsonUtil.map2Json(resultMap));
    }
    
    /**
     * 判断验证码是否正确
     */
    @RequestMapping("checkCode.html")
    public void checkCode(HttpServletResponse response,HttpServletRequest request) {
        //获取验证码
        String checkCode = request.getParameter("param");
        Map map = new HashMap();
        if (StringUtil.notEmpty(checkCode) && (StringUtils.equalsIgnoreCase(checkCode, (String) request.getSession().getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY)))) {
            map.put("info", "验证码输入正确");
            map.put("status", "y");
        } else {
            map.put("info", "验证码输入错误");
            map.put("status", "n");
        }
        WebUtil.write(response, JsonUtil.map2Json(map));
    }
    
    /**
     * 进入到身份验证页面
     */
    @RequestMapping("validPage.html")
    public ModelAndView validPage(HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView("/uc/login/vaildPage");
        String username = request.getParameter("username");
        String checkCode = request.getParameter("checkCode");
        Map map = userFacade.queryUserByName(username);
        if(null!=map&&map.size()>0&&StringUtil.notEmpty(checkCode) && StringUtils.equalsIgnoreCase(checkCode, (String) request.getSession().getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY))) {
            modelAndView.addObject("username", username);
            String mobile = null!=map.get("MOBILE")?String.valueOf(map.get("MOBILE")):"";
            if(StringUtil.notEmpty(mobile)) {
                modelAndView.addObject("mobile",mobile);
                
                //将用户名存放在session一份
                request.getSession().setAttribute("username", username);
                
                String random = StringUtil.randomNumber(6);
                request.getSession().setAttribute("random", random);
                String message = "【"+ Constants.company_name+"】亲爱的用户!您于"+DateUtil.getToday("yy年MM月dd日")+"申请了"+Constants.company_name+"用户中心手机号验证,验证码是"+random+"。";
                try {
                     SMSUtil.sendSMS(message,mobile );
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            
            //生成六位验证码,发到用户手机
            return modelAndView;
        }
        return new ModelAndView("/error");
    }
    
    /**
     * 跳转到重置密码页面
     */
    @RequestMapping("confirmCheck.html")
    public ModelAndView confirmCheck(HttpServletResponse response,HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView("/uc/login/resetPwd");
        String checkCode = request.getParameter("checkCode");
        if(checkCode.equals(request.getSession().getAttribute("random"))) {
            request.getSession().removeAttribute("random");
            //获取用户的邮箱信息
            Map map = userFacade.queryUserByName((String)request.getSession().getAttribute("username"));
            String mobile = ConvertUtil.obj2StrBlank(map.get("MOBILE"));
            
            //将邮箱地址放入到session中
            
            request.getSession().setAttribute("mobile", mobile);
            //生成隐式验证码
            String random = StringUtil.randomNumber(6);
            request.getSession().setAttribute("random", random);
            modelAndView.addObject("random", random);
            
            return modelAndView;
        }
        return new ModelAndView("/error");
    }
    
    /**
     * 判断验证码是否正确
     */
    @RequestMapping("checksendCode.html")
    public void checksendCode(HttpServletResponse response,HttpServletRequest request) {
        //获取验证码
        String checkCode = request.getParameter("param");
        Map map = new HashMap();
        if (StringUtil.notEmpty(checkCode) && (StringUtils.equalsIgnoreCase(checkCode, (String)request.getSession().getAttribute("random")))) {
            map.put("info", "验证码输入正确");
            map.put("status", "y");
        } else {
            map.put("info", "验证码输入错误");
            map.put("status", "n");
        }
        WebUtil.write(response, JsonUtil.map2Json(map));
    }
    
    /**
     * 重置密码
     */
    @RequestMapping("resetPwd.html")
    public void resetPwd(HttpServletResponse response,HttpServletRequest request) {
        String random = request.getParameter("random");
        String password = request.getParameter("password");
        String sessionRandom = (String)request.getSession().getAttribute("random");
        request.getSession().removeAttribute("random");
        Map resultMap = new HashMap();
        if(random.equals(sessionRandom)) {
            //通过用户名和邮箱重新查询用户信息,以免在此过程中恶意修改用户名或者邮箱
            String username = (String)request.getSession().getAttribute("username");
            String mobile = (String)request.getSession().getAttribute("mobile");
            boolean b = userFacade.doResetPwd(username,mobile,password);
            if(b) {
                resultMap.put("status", "0");
                resultMap.put("msg", "success");
            } else {
                resultMap.put("status", "1");
                resultMap.put("msg", "找回密码失败<br>请联系我们的客服人员");
            }
            WebUtil.write(response, JsonUtil.map2Json(resultMap));
        }
    }
}