UserRegisterController.java 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. package com.goafanti.user.controller;
  2. import java.util.Calendar;
  3. import java.util.Date;
  4. import java.util.List;
  5. import java.util.UUID;
  6. import javax.annotation.Resource;
  7. import javax.validation.Valid;
  8. import org.apache.commons.lang3.StringUtils;
  9. import org.apache.log4j.Logger;
  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.goafanti.admin.service.AdminService;
  16. import com.goafanti.common.bo.Result;
  17. import com.goafanti.common.constant.AFTConstants;
  18. import com.goafanti.common.constant.ErrorConstants;
  19. import com.goafanti.common.controller.BaseController;
  20. import com.goafanti.common.dao.UserMapper;
  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.enums.VoucherSourceType;
  25. import com.goafanti.common.model.JtVoucherDetail;
  26. import com.goafanti.common.model.User;
  27. import com.goafanti.common.utils.PasswordUtil;
  28. import com.goafanti.common.utils.TimeUtils;
  29. import com.goafanti.common.utils.VerifyCodeUtils;
  30. import com.goafanti.core.mybatis.JDBCIdGenerator;
  31. import com.goafanti.core.shiro.token.TokenManager;
  32. import com.goafanti.easemob.EasemobUtils;
  33. import com.goafanti.message.service.MessageService;
  34. import com.goafanti.user.bo.InputUser;
  35. import com.goafanti.user.service.UserService;
  36. import com.goafanti.voucher.service.VoucherDetailService;
  37. import com.goafanti.voucher.service.VoucherService;
  38. @Controller
  39. public class UserRegisterController extends BaseController {
  40. @Resource
  41. private UserService userService;
  42. @Resource(name = "passwordUtil")
  43. private PasswordUtil passwordUtil;
  44. @Resource
  45. private AdminService adminService;
  46. @Resource
  47. private MessageService messageService;
  48. @Resource
  49. private EasemobUtils easemobUtils;
  50. @Resource
  51. private JDBCIdGenerator jDBCIdGenerator;
  52. @Resource
  53. private VoucherDetailService voucherDetailService;
  54. @Resource
  55. private VoucherService voucherService;
  56. Logger logger = Logger.getLogger(UserRegisterController.class);
  57. /**
  58. * 网站用户注册
  59. *
  60. * @param user
  61. * @return
  62. */
  63. @RequestMapping(value = "/register", method = RequestMethod.POST)
  64. @ResponseBody
  65. public Result register(@Valid InputUser user, String mobileCode,BindingResult bindingResult) {
  66. Result res = new Result();
  67. if (bindingResult.hasErrors()) {
  68. res.getError().add(buildErrorByMsg(bindingResult.getFieldError().getDefaultMessage(),
  69. UserFields.getFieldDesc(bindingResult.getFieldError().getField())));
  70. return res;
  71. }
  72. if (StringUtils.isBlank(user.getUnitName())) {
  73. res.getError().add(buildError(ErrorConstants.USER_ALREADY_EXIST, "用户名不能为空"));
  74. return res;
  75. }
  76. if (StringUtils.isBlank(user.getMobile())) {
  77. res.getError().add(buildError(ErrorConstants.USER_ALREADY_EXIST, "手机号码不能为空"));
  78. return res;
  79. }
  80. User u = userService.selectByMobieAndType(user.getMobile().trim(),user.getType());
  81. if (null != u) {
  82. res.getError().add(buildError(ErrorConstants.USER_ALREADY_EXIST, "用户已存在"));
  83. return res;
  84. }
  85. if (TokenManager.getSession().getAttribute(VerifyCodeUtils.M_CODE_TIME) ==null||!TokenManager.getSession().getAttribute(VerifyCodeUtils.M_CODE).equals(mobileCode.toLowerCase())) {
  86. res.getError().add(buildError(ErrorConstants.MCODE_ERROR, "手机验证码错误"));
  87. return res;
  88. }
  89. // 验证码15分钟有效
  90. if (TimeUtils.checkOverTime("register")) {
  91. res.getError().add(buildError(ErrorConstants.MCODE_OVERTIME_ERROR, "手机验证码超时失效"));
  92. return res;
  93. }
  94. if (res.getError().isEmpty()) {
  95. User us = new User();
  96. us.setId(UUID.randomUUID().toString());
  97. us.setMobile(user.getMobile().trim());
  98. us.setPassword(user.getPassword());
  99. Calendar now = Calendar.getInstance();
  100. now.set(Calendar.MILLISECOND, 0);
  101. us.setCreateTime(now.getTime());
  102. us.setUpdateTime(now.getTime());
  103. us.setPassword(passwordUtil.getEncryptPwd(us));
  104. us.setNickname(user.getUnitName());
  105. us.setSource(AFTConstants.USER_SOURCE_REGISTER);
  106. us.setType(user.getType());
  107. us.setBeInviteCode(user.getBeInviteCode());
  108. us.setStatus(AFTConstants.USER_STATUS_NORMAL);
  109. us.setShareType(AFTConstants.USER_SHARE_PUBLIC);
  110. us.setIdentifyName(StringUtils.isBlank(user.getUnitName())?"":user.getUnitName());
  111. us.setLvl(UserLevel.GENERAL.getCode());
  112. us.setAuthentication(UserAuthentication.UN_AUTHENTICATION.getCode());
  113. us.setTransferTime(now.getTime());
  114. userService.insertRegister(us,user.getUnitName(),user.getUsername());
  115. //获得抵用券
  116. userService.saveVoucher(us);
  117. }
  118. return res;
  119. }
  120. /**
  121. * APP用户注册
  122. *
  123. * @param user
  124. * @return
  125. */
  126. @RequestMapping(value = "/mregister", method = RequestMethod.POST)
  127. @ResponseBody
  128. public Result mRegister(@Valid InputUser user, String mobileCode,String uuid,BindingResult bindingResult) {
  129. Result res = new Result();
  130. if (bindingResult.hasErrors()) {
  131. res.getError().add(buildErrorByMsg(bindingResult.getFieldError().getDefaultMessage(),
  132. UserFields.getFieldDesc(bindingResult.getFieldError().getField())));
  133. return res;
  134. }
  135. if (StringUtils.isBlank(user.getUnitName())) {
  136. res.getError().add(buildError(ErrorConstants.USER_ALREADY_EXIST, "用户名不能为空"));
  137. return res;
  138. }
  139. if (StringUtils.isBlank(user.getMobile())) {
  140. res.getError().add(buildError(ErrorConstants.USER_ALREADY_EXIST, "手机号码不能为空"));
  141. return res;
  142. }
  143. User u = userService.selectByMobieAndType(user.getMobile().trim(),user.getType());
  144. if (null != u) {
  145. res.getError().add(buildError(ErrorConstants.USER_ALREADY_EXIST, "用户已存在"));
  146. }
  147. if (TokenManager.getSession().getAttribute(VerifyCodeUtils.M_CODE_TIME) ==null||!TokenManager.getSession().getAttribute(VerifyCodeUtils.M_CODE).equals(mobileCode.toLowerCase())) {
  148. res.getError().add(buildError(ErrorConstants.MCODE_ERROR, "手机验证码错误"));
  149. return res;
  150. }
  151. // 验证码15分钟有效
  152. if (TimeUtils.checkOverTime("register")) {
  153. res.getError().add(buildError(ErrorConstants.MCODE_OVERTIME_ERROR, "手机验证码超时失效"));
  154. return res;
  155. }
  156. if (userService.checkeToInviteCode(user.getBeInviteCode())) {
  157. res.getError().add(buildError("邀请码不存在", "邀请码不存在"));
  158. return res;
  159. }
  160. if (res.getError().isEmpty()) {
  161. User us = new User();
  162. us.setId(UUID.randomUUID().toString());
  163. us.setMobile(user.getMobile().trim());
  164. us.setPassword(user.getPassword());
  165. Calendar now = Calendar.getInstance();
  166. now.set(Calendar.MILLISECOND, 0);
  167. us.setCreateTime(now.getTime());
  168. us.setUpdateTime(now.getTime());
  169. us.setPassword(passwordUtil.getEncryptPwd(us));
  170. us.setNickname(user.getUnitName());
  171. us.setType(user.getType());
  172. us.setBeInviteCode(user.getBeInviteCode());
  173. us.setSource(AFTConstants.USER_SOURCE_REGISTER);
  174. us.setStatus(AFTConstants.USER_STATUS_NORMAL);
  175. us.setShareType(AFTConstants.USER_SHARE_PUBLIC);
  176. us.setIdentifyName(StringUtils.isBlank(user.getUnitName())?"":user.getUnitName());
  177. us.setLvl(UserLevel.GENERAL.getCode());
  178. us.setAuthentication(UserAuthentication.UN_AUTHENTICATION.getCode());
  179. us.setTransferTime(now.getTime());
  180. userService.insertRegister(us,user.getUnitName(),user.getUsername());
  181. //获得抵用券
  182. userService.saveVoucher(us);
  183. }
  184. return res;
  185. }
  186. /*public void getVoucher(User u){
  187. if (StringUtils.isNotBlank(u.getBackgroundUrl())) {
  188. List<String> ids=voucherService.getSourceVoucher(VoucherSourceType.BYQFWS.getCode());
  189. for (String s : ids) {
  190. JtVoucherDetail j=new JtVoucherDetail();
  191. j.setId(jDBCIdGenerator.generateId().toString());
  192. j.setReceiveTime(new Date());
  193. j.setVoucherId(s);
  194. j.setUseUid(u.getId());
  195. j.setStatus(0);
  196. voucherDetailService.saveVoucherDetail(j);
  197. }
  198. }
  199. }
  200. private void registerEasemobAfterRegister(String uuid,User u){
  201. JSONObject resultObj = new JSONObject();
  202. String easemobName = String.valueOf(u.getNumber());
  203. String easemobPass = new SimpleHash("md5", u.getId(), null, 1).toHex();
  204. resultObj.put("password", easemobPass);
  205. resultObj.put("username", easemobName);
  206. resultObj.put("nickname", StringUtils.isBlank(u.getNickname()) ? "技淘用户" + u.getNumber() : u.getNickname());
  207. easemobUtils.send(
  208. new EasemobInfo().uri("/users/").data(resultObj.toJSONString()).method(HttpMethod.POST));
  209. //绑定账号
  210. messageService.addJpushEasemobAccount(uuid,u.getId(),easemobName,easemobPass);
  211. }*/
  212. }