liuxiaolong
2019-05-09 0d1d88cdb668e75ea8609417ac18ae19947e9525
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package com.jeeplus.modules.sys.web;
 
 
import java.io.IOException;
import java.util.List;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
 
import com.google.common.collect.Lists;
import com.jeeplus.common.config.Global;
import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.common.sms.SMSUtils;
import com.jeeplus.common.utils.FileUtils;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.common.web.BaseController;
import com.jeeplus.modules.sys.dao.UserDao;
import com.jeeplus.modules.sys.entity.Office;
import com.jeeplus.modules.sys.entity.Role;
import com.jeeplus.modules.sys.entity.SystemConfig;
import com.jeeplus.modules.sys.entity.User;
import com.jeeplus.modules.sys.service.OfficeService;
import com.jeeplus.modules.sys.service.SystemConfigService;
import com.jeeplus.modules.sys.service.SystemService;
import com.jeeplus.modules.sys.utils.UserUtils;
import com.jeeplus.modules.tools.utils.TwoDimensionCode;
 
/**
 * 用户Controller
 * @author jeeplus
 * @version 2013-8-29
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/register")
public class RegisterController extends BaseController {
 
 
    @Autowired
    private SystemConfigService systemConfigService;
    
    @Autowired
    private SystemService systemService;
    
    @Autowired
    private OfficeService officeService;
    
    @Autowired
    private UserDao userDao;
    
    @ModelAttribute
    public User get(@RequestParam(required=false) String id) {
        if (StringUtils.isNotBlank(id)){
            return systemService.getUser(id);
        }else{
            return new User();
        }
    }
    
    
    
    @RequestMapping(value = {"index",""})
    public String register(User user, Model model) {
        return "modules/sys/register";
    }
 
 
    @RequestMapping(value = "registerUser")
    public String registerUser(  HttpServletRequest request,HttpServletResponse response, boolean mobileLogin, String randomCode, String roleName, User user, Model model, RedirectAttributes redirectAttributes) {
        
    
        //验证手机号是否已经注册
        
        if(userDao.findUniqueByProperty("mobile", user.getMobile()) != null){
            // 如果是手机登录,则返回JSON字符串
            if (mobileLogin){
                AjaxJson j = new AjaxJson();
                j.setSuccess(false);
                j.setErrorCode("1");
                j.setMsg("手机号已经被使用!");
                return renderString(response, j.getJsonStr());
            }else{
                addMessage(model, "手机号已经被使用!");
                return register(user, model);
            }
        }
        
        //验证用户是否已经注册
        
        if(userDao.findUniqueByProperty("login_name", user.getLoginName()) != null){
            // 如果是手机登录,则返回JSON字符串
            if (mobileLogin){
                AjaxJson j = new AjaxJson();
                j.setSuccess(false);
                j.setErrorCode("2");
                j.setMsg("用户名已经被注册!");
                return renderString(response, j.getJsonStr());
            }else{
                addMessage(model, "用户名已经被注册!");
                return register(user, model);
            }
        }
        
        //验证短信内容
        if(!randomCode.equals(request.getSession().getServletContext().getAttribute(user.getMobile()))){
            // 如果是手机登录,则返回JSON字符串
            if (mobileLogin){
                AjaxJson j = new AjaxJson();
                j.setSuccess(false);
                j.setErrorCode("3");
                j.setMsg("手机验证码不正确!");
                return renderString(response, j.getJsonStr());
            }else{
                addMessage(model, "手机验证码不正确!");
                return register(user, model);
            }
        }
        
        
        // 修正引用赋值问题,不知道为何,Company和Office引用的一个实例地址,修改了一个,另外一个跟着修改。
        Role role = systemService.getRoleByEnname(roleName);
        String officeCode = "1000";
        if(roleName.equals("patient")){
            officeCode = "1001";
        }
        Office office = officeService.getByCode(officeCode);
        // 密码MD5加密
        user.setPassword(SystemService.entryptPassword(user.getPassword()));
        if (systemService.getUserByLoginName(user.getLoginName()) != null){
            addMessage(model, "注册用户'" + user.getLoginName() + "'失败,用户名已存在");
            return register(user, model);
        }
        // 角色数据有效性验证,过滤不在授权内的角色
        List<Role> roleList = Lists.newArrayList();
        roleList.add(role);
        user.setRoleList(roleList);
        //保存机构
        user.setCompany(office);
        user.setOffice(office);
        //生成用户二维码,使用登录名
        String realPath = Global.getUserfilesBaseDir() + Global.USERFILES_BASE_URL
        + user.getId() + "/qrcode/";
        FileUtils.createDirectory(realPath);
        String name= user.getId()+".png"; //encoderImgId此处二维码的图片名
        String filePath = realPath + name;  //存放路径
        TwoDimensionCode.encoderQRCode(user.getLoginName(), filePath, "png");//执行生成二维码
        user.setQrCode(request.getContextPath()+Global.USERFILES_BASE_URL
            +  user.getId()  + "/qrcode/"+name);
        // 保存用户信息
        systemService.saveUser(user);
        // 清除当前用户缓存
        if (user.getLoginName().equals(UserUtils.getUser().getLoginName())){
            UserUtils.clearCache();
            //UserUtils.getCacheMap().clear();
        }
        request.getSession().getServletContext().removeAttribute(user.getMobile());//清除验证码
        
        // 如果是手机登录,则返回JSON字符串
        if (mobileLogin){
            AjaxJson j = new AjaxJson();
            j.setSuccess(true);
            j.setMsg("注册用户'" + user.getLoginName() + "'成功");
            return renderString(response, j);
        }
        
        
        addMessage(redirectAttributes, "注册用户'" + user.getLoginName() + "'成功");
        return "redirect:" + adminPath + "/login";
    }
    
    
    /**
     * 获取验证码
     * @param request
     * @param response
     * @param mobile
     * @param model
     * @param redirectAttributes
     * @return
     */
    @RequestMapping(value = "getRegisterCode")
    @ResponseBody
    public AjaxJson getRegisterCode(HttpServletRequest request,HttpServletResponse response, String mobile,
            Model model, RedirectAttributes redirectAttributes) {
        
        SystemConfig config = systemConfigService.get("1");
        
        AjaxJson j = new AjaxJson();
        
        //验证手机号是否已经注册
        if(userDao.findUniqueByProperty("mobile", mobile) != null){
            
                j.setSuccess(false);
                j.setErrorCode("1");
                j.setMsg("手机号已经被使用!");
                return j;
        }
        
        String randomCode = String.valueOf((int) (Math.random() * 9000 + 1000));
        try {
            String result = UserUtils.sendRandomCode(config.getSmsName(),config.getSmsPassword(), mobile, randomCode);
            if (!result.equals("100")) {
                j.setSuccess(false);
                j.setErrorCode("2");
                j.setMsg("短信发送失败,错误代码:"+result+",请联系管理员。");
            }else{
                j.setSuccess(true);
                j.setErrorCode("-1");
                j.setMsg("短信发送成功!");
                request.getSession().getServletContext().setAttribute(mobile, randomCode);
            }
        } catch (IOException e) {
            j.setSuccess(false);
            j.setErrorCode("3");
            j.setMsg("因未知原因导致短信发送失败,请联系管理员。");
        }
        return j;
    }
    
    
    /**
     * web端ajax验证手机验证码是否正确
     */
    @ResponseBody
    @RequestMapping(value = "validateMobileCode")
    public boolean validateMobileCode(HttpServletRequest request,
            String mobile, String randomCode) {
        if (randomCode.equals(request.getSession().getServletContext().getAttribute(mobile))) {
            return true;
        } else {
            return false;
        }
    }
 
 
}