UserRegisterController.java 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. }
  66. // 验证码15分钟有效
  67. if (TimeUtils.checkOverTime("register")) {
  68. res.getError().add(buildError(ErrorConstants.MCODE_OVERTIME_ERROR, "手机验证码超时失效"));
  69. return res;
  70. }
  71. if (!TokenManager.getSession().getAttribute(VerifyCodeUtils.M_CODE).equals(mobileCode.toLowerCase())) {
  72. res.getError().add(buildError(ErrorConstants.MCODE_ERROR, "手机验证码错误"));
  73. return res;
  74. }
  75. if (res.getError().isEmpty()) {
  76. User us = new User();
  77. us.setId(UUID.randomUUID().toString());
  78. us.setMobile(user.getMobile().trim());
  79. us.setPassword(user.getPassword());
  80. Calendar now = Calendar.getInstance();
  81. now.set(Calendar.MILLISECOND, 0);
  82. us.setCreateTime(now.getTime());
  83. us.setUpdateTime(now.getTime());
  84. us.setPassword(passwordUtil.getEncryptPwd(us));
  85. us.setNickname(user.getUnitName());
  86. us.setSource(AFTConstants.USER_SOURCE_REGISTER);
  87. us.setType(user.getType());
  88. us.setStatus(AFTConstants.USER_STATUS_NORMAL);
  89. us.setShareType(AFTConstants.USER_SHARE_PUBLIC);
  90. us.setIdentifyName(StringUtils.isBlank(user.getUnitName())?"":user.getUnitName());
  91. us.setLvl(UserLevel.GENERAL.getCode());
  92. us.setAuthentication(UserAuthentication.UN_AUTHENTICATION.getCode());
  93. us.setTransferTime(now.getTime());
  94. userService.insertRegister(us,user.getUnitName(),user.getUsername());
  95. }
  96. return res;
  97. }
  98. /**
  99. * APP用户注册
  100. *
  101. * @param user
  102. * @return
  103. */
  104. @RequestMapping(value = "/mregister", method = RequestMethod.POST)
  105. @ResponseBody
  106. public Result mRegister(@Valid InputUser user, String mobileCode,String uuid,BindingResult bindingResult) {
  107. Result res = new Result();
  108. if (bindingResult.hasErrors()) {
  109. res.getError().add(buildErrorByMsg(bindingResult.getFieldError().getDefaultMessage(),
  110. UserFields.getFieldDesc(bindingResult.getFieldError().getField())));
  111. return res;
  112. }
  113. User u = userService.selectByMobieAndType(user.getMobile().trim(),user.getType());
  114. if (null != u) {
  115. res.getError().add(buildError(ErrorConstants.USER_ALREADY_EXIST, "用户已存在"));
  116. }
  117. // 验证码15分钟有效
  118. if (TimeUtils.checkOverTime("register")) {
  119. res.getError().add(buildError(ErrorConstants.MCODE_OVERTIME_ERROR, "手机验证码超时失效"));
  120. return res;
  121. }
  122. if (!TokenManager.getSession().getAttribute(VerifyCodeUtils.M_CODE).equals(mobileCode.toLowerCase())) {
  123. res.getError().add(buildError(ErrorConstants.MCODE_ERROR, "手机验证码错误"));
  124. return res;
  125. }
  126. if (res.getError().isEmpty()) {
  127. User us = new User();
  128. us.setId(UUID.randomUUID().toString());
  129. us.setMobile(user.getMobile().trim());
  130. us.setPassword(user.getPassword());
  131. Calendar now = Calendar.getInstance();
  132. now.set(Calendar.MILLISECOND, 0);
  133. us.setCreateTime(now.getTime());
  134. us.setUpdateTime(now.getTime());
  135. us.setPassword(passwordUtil.getEncryptPwd(us));
  136. us.setNickname(user.getUnitName());
  137. us.setType(user.getType());
  138. us.setSource(AFTConstants.USER_SOURCE_REGISTER);
  139. us.setStatus(AFTConstants.USER_STATUS_NORMAL);
  140. us.setShareType(AFTConstants.USER_SHARE_PUBLIC);
  141. us.setIdentifyName(StringUtils.isBlank(user.getUnitName())?"":user.getUnitName());
  142. us.setLvl(UserLevel.GENERAL.getCode());
  143. us.setAuthentication(UserAuthentication.UN_AUTHENTICATION.getCode());
  144. us.setTransferTime(now.getTime());
  145. userService.insertRegister(us,user.getUnitName(),user.getUsername());
  146. }
  147. return res;
  148. }
  149. /*private void registerEasemobAfterRegister(String uuid,User u){
  150. JSONObject resultObj = new JSONObject();
  151. String easemobName = String.valueOf(u.getNumber());
  152. String easemobPass = new SimpleHash("md5", u.getId(), null, 1).toHex();
  153. resultObj.put("password", easemobPass);
  154. resultObj.put("username", easemobName);
  155. resultObj.put("nickname", StringUtils.isBlank(u.getNickname()) ? "技淘用户" + u.getNumber() : u.getNickname());
  156. easemobUtils.send(
  157. new EasemobInfo().uri("/users/").data(resultObj.toJSONString()).method(HttpMethod.POST));
  158. //绑定账号
  159. messageService.addJpushEasemobAccount(uuid,u.getId(),easemobName,easemobPass);
  160. }*/
  161. }