| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- package com.uas.sso.controller;
- import com.uas.message.sms.service.SmsService;
- import com.uas.sso.core.Const;
- import com.uas.sso.core.Type;
- import com.uas.sso.core.PasswordStrength;
- import com.uas.sso.entity.Setting;
- import com.uas.sso.entity.Token;
- import com.uas.sso.entity.User;
- import com.uas.sso.logging.RegisterBufferedLogger;
- import com.uas.sso.service.SettingService;
- import com.uas.sso.service.TokenService;
- import com.uas.sso.service.UserService;
- import com.uas.sso.util.StringUtil;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Controller;
- import org.springframework.ui.ModelMap;
- import org.springframework.util.StringUtils;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestMethod;
- import org.springframework.web.bind.annotation.ResponseBody;
- /**
- * 个人注册controller
- *
- * @author uas
- * @date 2018/1/2
- */
- @Controller
- @RequestMapping("/sso/personal")
- public class PersonalRegisterController extends BaseController {
- @Autowired
- private UserService userService;
- @Autowired
- private TokenService tokenService;
- @Autowired
- private SettingService settingService;
- @Autowired
- private SmsService smsService;
- private RegisterBufferedLogger registerLogger = new RegisterBufferedLogger();
- /**
- * 注册个人信息
- *
- * @param user 用户信息(需要会员名,手机号,手机号所属区域,密码)
- * @param appId 注册应用标志
- * @param code 验证码
- * @param token 验证码tokenId
- * @return 成功:success(),失败:error("错误码", "错误信息")
- */
- @RequestMapping(value = "/register", method = RequestMethod.POST)
- @ResponseBody
- public ModelMap register(User user, String appId, String code, String token) {
- // 获取参数
- String vipName = user.getVipName();
- String mobile = user.getMobile();
- String mobileArea = user.getMobileArea();
- String password = user.getPassword();
- // 参数空校验
- if (StringUtils.isEmpty(vipName)) {
- return error("400", "会员名不能为空");
- }
- if (StringUtils.isEmpty(password)) {
- return error("400", "密码不能为空");
- }
- if (StringUtils.isEmpty(mobile)) {
- return error("400", "手机号不能为空");
- }
- // 校验验证码
- ModelMap checkResult = checkCode(mobile, code, token);
- if (checkResult.get(Const.SUCCESS) == null) {
- // 校验不成功,返回校验的错误信息
- return checkResult;
- }
- // 校验手机号
- if (Const.CONTINENT.equals(mobileArea)) {
- if (!mobile.matches(Const.REGEXP_MOBILE_CONTINENT)) {
- return error("400", "请输入正确的手机号格式");
- }
- } else if (Const.HONGKONG.equals(mobileArea)) {
- if (!mobile.matches(Const.REGEXP_MOBILE_HONGKONG)) {
- return error("400", "请输入正确的手机号格式");
- }
- } else {
- return error("400", "未找到所选地区");
- }
- // 校验密码
- if (PasswordStrength.WEAK.equals(checkPasswordLevel(password))) {
- return error("400", "密码强度过低,请重新输入密码");
- }
- // 注册
- user.setFromApp(StringUtils.isEmpty(appId) ? "sso" : appId);
- userService.register(user);
- tokenService.delete(token);
- registerLogger.info(Type.REGISTER_PERSONAL.getValue(), 1, "个人注册成功", user, user.getFromApp());
- return success();
- }
- /**
- * 获取验证码
- *
- * @param mobile 手机号
- * @return success(tokenId)
- */
- @RequestMapping(value = "/checkCode", method = RequestMethod.GET)
- @ResponseBody
- public ModelMap getCode(String mobile) {
- String code = StringUtil.getRandomNumber(6);
- Token token = new Token(code, 10*60);
- token.setMobile(mobile);
- tokenService.save(token);
- ModelMap data = new ModelMap();
- data.put("checkcode", code);
- request.getSession().setAttribute("token", token.getId());
- // 手机短信
- try {
- if (!StringUtils.isEmpty(mobile)) {
- Setting smsTplId = settingService.findOne("templateForSendSmsWhenRegister");
- if (!StringUtils.isEmpty(smsTplId)) {
- smsService.send(smsTplId.getValue(), mobile, new Object[]{code});
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- return new ModelMap("token", token.getId());
- }
- /**
- * 校验验证码
- *
- * @param code 验证码
- * @param token 验证码tokenId
- * @return 验证成功:success(),验证失败:error("错误信息")
- */
- @RequestMapping(value = "/checkCode", method = RequestMethod.POST)
- @ResponseBody
- public ModelMap checkCode(String mobile, String code, String token) {
- // 校验参数
- if (StringUtils.isEmpty(token) || StringUtils.isEmpty(token)) {
- return error("参数错误");
- }
- Token existToken = tokenService.findOne(token);
- if (existToken == null || existToken.isExpired()) {
- return error("验证码已经失效,请重新获取");
- }
- if (StringUtils.isEmpty(mobile) || !mobile.equals(existToken.getMobile())) {
- return error("手机号被修改,请重新获取验证码");
- }
- // 校验验证码
- String existCode = existToken.getBind().toString();
- if (!code.equals(existCode)) {
- return error("验证码错误");
- }
- return success();
- }
- }
|