UserRegisterController.java 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. package com.goafanti.user.controller;
  2. import java.util.Calendar;
  3. import java.util.UUID;
  4. import javax.annotation.Resource;
  5. import javax.validation.Valid;
  6. import org.apache.commons.lang3.StringUtils;
  7. import org.apache.log4j.Logger;
  8. import org.apache.shiro.crypto.hash.SimpleHash;
  9. import org.springframework.http.HttpMethod;
  10. import org.springframework.stereotype.Controller;
  11. import org.springframework.validation.BindingResult;
  12. import org.springframework.web.bind.annotation.RequestMapping;
  13. import org.springframework.web.bind.annotation.RequestMethod;
  14. import org.springframework.web.bind.annotation.ResponseBody;
  15. import com.alibaba.fastjson.JSONObject;
  16. import com.goafanti.admin.service.AdminService;
  17. import com.goafanti.common.bo.Result;
  18. import com.goafanti.common.constant.AFTConstants;
  19. import com.goafanti.common.constant.ErrorConstants;
  20. import com.goafanti.common.controller.BaseController;
  21. import com.goafanti.common.enums.UserAuthentication;
  22. import com.goafanti.common.enums.UserFields;
  23. import com.goafanti.common.enums.UserLevel;
  24. import com.goafanti.common.model.User;
  25. import com.goafanti.common.utils.PasswordUtil;
  26. import com.goafanti.common.utils.TimeUtils;
  27. import com.goafanti.common.utils.VerifyCodeUtils;
  28. import com.goafanti.core.shiro.token.TokenManager;
  29. import com.goafanti.easemob.EasemobUtils;
  30. import com.goafanti.easemob.bo.EasemobInfo;
  31. import com.goafanti.message.service.MessageService;
  32. import com.goafanti.user.bo.InputUser;
  33. import com.goafanti.user.service.UserService;
  34. @Controller
  35. public class UserRegisterController extends BaseController {
  36. @Resource
  37. private UserService userService;
  38. @Resource(name = "passwordUtil")
  39. private PasswordUtil passwordUtil;
  40. @Resource
  41. private AdminService adminService;
  42. @Resource
  43. private MessageService messageService;
  44. @Resource
  45. private EasemobUtils easemobUtils;
  46. Logger logger = Logger.getLogger(UserRegisterController.class);
  47. /**
  48. * 网站用户注册
  49. *
  50. * @param user
  51. * @return
  52. */
  53. @RequestMapping(value = "/register", method = RequestMethod.POST)
  54. @ResponseBody
  55. public Result register(@Valid InputUser user, String mobileCode,BindingResult bindingResult) {
  56. Result res = new Result();
  57. if (bindingResult.hasErrors()) {
  58. res.getError().add(buildErrorByMsg(bindingResult.getFieldError().getDefaultMessage(),
  59. UserFields.getFieldDesc(bindingResult.getFieldError().getField())));
  60. return res;
  61. }
  62. User u = userService.selectByMobieAndType(user.getMobile().trim(), user.getType());
  63. if (null != u) {
  64. res.getError().add(buildError(ErrorConstants.USER_ALREADY_EXIST, "用户已存在"));
  65. return res;
  66. }
  67. // 验证码15分钟有效
  68. if (TimeUtils.checkOverTime("register")) {
  69. res.getError().add(buildError(ErrorConstants.MCODE_OVERTIME_ERROR, "手机验证码超时失效"));
  70. return res;
  71. }
  72. if (!TokenManager.getSession().getAttribute(VerifyCodeUtils.M_CODE).equals(mobileCode.toLowerCase())) {
  73. res.getError().add(buildError(ErrorConstants.MCODE_ERROR, "手机验证码错误"));
  74. return res;
  75. }
  76. if (res.getError().isEmpty()) {
  77. User us = new User();
  78. us.setId(UUID.randomUUID().toString());
  79. us.setMobile(user.getMobile().trim());
  80. us.setPassword(user.getPassword());
  81. Calendar now = Calendar.getInstance();
  82. now.set(Calendar.MILLISECOND, 0);
  83. us.setCreateTime(now.getTime());
  84. us.setUpdateTime(now.getTime());
  85. us.setPassword(passwordUtil.getEncryptPwd(us));
  86. us.setNickname(user.getUnitName());
  87. us.setType(user.getType());
  88. us.setSource(AFTConstants.USER_SOURCE_REGISTER);
  89. us.setStatus(AFTConstants.USER_STATUS_NORMAL);
  90. us.setShareType(AFTConstants.USER_SHARE_PUBLIC);
  91. us.setIdentifyName(StringUtils.isBlank(user.getUnitName())?"":user.getUnitName());
  92. us.setLvl(UserLevel.GENERAL.getCode());
  93. us.setAuthentication(UserAuthentication.UN_AUTHENTICATION.getCode());
  94. us.setTransferTime(now.getTime());
  95. userService.insertRegister(us,user.getUnitName(),user.getUsername());
  96. }
  97. return res;
  98. }
  99. /**
  100. * APP用户注册
  101. *
  102. * @param user
  103. * @return
  104. */
  105. @RequestMapping(value = "/mregister", method = RequestMethod.POST)
  106. @ResponseBody
  107. public Result mRegister(@Valid InputUser user, String mobileCode,String uuid,BindingResult bindingResult) {
  108. Result res = new Result();
  109. if (bindingResult.hasErrors()) {
  110. res.getError().add(buildErrorByMsg(bindingResult.getFieldError().getDefaultMessage(),
  111. UserFields.getFieldDesc(bindingResult.getFieldError().getField())));
  112. return res;
  113. }
  114. User u = userService.selectByMobieAndType(user.getMobile().trim(), user.getType());
  115. if (null != u) {
  116. res.getError().add(buildError(ErrorConstants.USER_ALREADY_EXIST, "用户已存在"));
  117. }
  118. // 验证码15分钟有效
  119. if (TimeUtils.checkOverTime("register")) {
  120. res.getError().add(buildError(ErrorConstants.MCODE_OVERTIME_ERROR, "手机验证码超时失效"));
  121. return res;
  122. }
  123. if (!TokenManager.getSession().getAttribute(VerifyCodeUtils.M_CODE).equals(mobileCode.toLowerCase())) {
  124. res.getError().add(buildError(ErrorConstants.MCODE_ERROR, "手机验证码错误"));
  125. return res;
  126. }
  127. if (res.getError().isEmpty()) {
  128. User us = new User();
  129. us.setId(UUID.randomUUID().toString());
  130. us.setMobile(user.getMobile().trim());
  131. us.setPassword(user.getPassword());
  132. Calendar now = Calendar.getInstance();
  133. now.set(Calendar.MILLISECOND, 0);
  134. us.setCreateTime(now.getTime());
  135. us.setUpdateTime(now.getTime());
  136. us.setPassword(passwordUtil.getEncryptPwd(us));
  137. us.setType(user.getType());
  138. us.setNickname(user.getUnitName());
  139. us.setSource(AFTConstants.USER_SOURCE_REGISTER);
  140. us.setStatus(AFTConstants.USER_STATUS_NORMAL);
  141. us.setShareType(AFTConstants.USER_SHARE_PUBLIC);
  142. us.setIdentifyName(StringUtils.isBlank(user.getUnitName())?"":user.getUnitName());
  143. us.setLvl(UserLevel.GENERAL.getCode());
  144. us.setAuthentication(UserAuthentication.UN_AUTHENTICATION.getCode());
  145. us.setTransferTime(now.getTime());
  146. userService.insertRegister(us,user.getUnitName(),user.getUsername());
  147. }
  148. return res;
  149. }
  150. /*private void registerEasemobAfterRegister(String uuid,User u){
  151. JSONObject resultObj = new JSONObject();
  152. String easemobName = String.valueOf(u.getNumber());
  153. String easemobPass = new SimpleHash("md5", u.getId(), null, 1).toHex();
  154. resultObj.put("password", easemobPass);
  155. resultObj.put("username", easemobName);
  156. resultObj.put("nickname", StringUtils.isBlank(u.getNickname()) ? "技淘用户" + u.getNumber() : u.getNickname());
  157. easemobUtils.send(
  158. new EasemobInfo().uri("/users/").data(resultObj.toJSONString()).method(HttpMethod.POST));
  159. //绑定账号
  160. messageService.addJpushEasemobAccount(uuid,u.getId(),easemobName,easemobPass);
  161. }*/
  162. }