Browse Source

修改手机号除去修改为该账号的校验

wangmh 7 years ago
parent
commit
b8591f401f

+ 5 - 8
sso-server/src/main/java/com/uas/sso/controller/UpdateUserController.java

@@ -5,6 +5,7 @@ import com.uas.sso.core.Status;
 import com.uas.sso.entity.*;
 import com.uas.sso.service.UserService;
 import com.uas.sso.support.SystemSession;
+import org.springframework.util.Assert;
 import org.springframework.util.CollectionUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.ui.ModelMap;
@@ -258,12 +259,8 @@ public class UpdateUserController extends BaseController {
     @RequestMapping(value = "/setMobile", method = RequestMethod.POST)
     public ModelMap updateMobile(String mobile, String code, @RequestParam String token) {
         // 校验空参数
-        if (StringUtils.isEmpty(mobile)) {
-            return error("手机号不能为空");
-        }
-        if (StringUtils.isEmpty(code)) {
-            return error("验证码不能为空");
-        }
+        Assert.hasText(mobile, "手机号不能为空");
+        Assert.hasText(code, "验证码不能为空");
 
         // 从session中获取用户信息
         User user = (User) request.getSession().getAttribute("user");
@@ -272,7 +269,7 @@ public class UpdateUserController extends BaseController {
         }
 
         // 校验手机号是否被使用
-        if (userService.mobileHasRegistered(mobile)){
+        if (!mobile.equals(user.getMobile()) && userService.mobileHasRegistered(mobile)){
             return error("手机号已注册");
         }
 
@@ -284,10 +281,10 @@ public class UpdateUserController extends BaseController {
 
         // 校验验证码
         checkMobileCode(token, mobile, code);
-        tokenService.delete(token);
 
         // 修改手机号
         userService.updateMobile(user.getUserUU(), mobile);
+        tokenService.delete(token);
         return success();
     }
 

+ 13 - 4
sso-server/src/main/java/com/uas/sso/service/impl/UserServiceImpl.java

@@ -27,6 +27,7 @@ import org.springframework.data.domain.Pageable;
 import org.springframework.data.jpa.domain.Specification;
 import org.springframework.stereotype.Service;
 import org.springframework.ui.ModelMap;
+import org.springframework.util.Assert;
 import org.springframework.util.CollectionUtils;
 import org.springframework.util.StringUtils;
 
@@ -300,17 +301,25 @@ public class UserServiceImpl implements UserService {
 
     @Override
     public void updateMobile(Long userUU, String newMobile) {
+        // 参数空校验
+        Assert.notNull(userUU, "userUU must not be null!");
+        Assert.hasText(newMobile, "新手机号不能为空");
+
         // 获取用户信息
         User user = userDao.findOne(userUU);
         if (user == null) {
             throw new VisibleError("用户不存在");
         }
 
-        // 修改手机号
-        User oldUser = userDao.findByMobile(newMobile);
-        if (oldUser != null) {
-            throw new VisibleError("该手机号已被注册");
+        // 判断手机号是否被注册
+        if (!newMobile.equals(user.getMobile())) {
+            User oldUser = userDao.findByMobile(newMobile);
+            if (oldUser != null) {
+                throw new VisibleError("该手机号已被注册");
+            }
         }
+
+        // 修改手机号
         user.setMobile(newMobile);
         user.setMobileValidCode((short) Status.AUTHENTICATED.getCode());