Browse Source

修改密码

sangwenwei 1 year ago
parent
commit
b71a31e2c6

+ 117 - 1
jeeplus-modules/jeeplus-system/src/main/java/com/jeeplus/sys/controller/UserController.java

@@ -18,6 +18,7 @@ import com.jeeplus.common.SecurityUtils;
 import com.jeeplus.common.constant.CommonConstants;
 import com.jeeplus.common.excel.ExcelOptions;
 import com.jeeplus.common.excel.annotation.ExportMode;
+import com.jeeplus.common.redis.RedisUtils;
 import com.jeeplus.common.utils.ResponseUtil;
 import com.jeeplus.core.query.QueryWrapperGenerator;
 import com.jeeplus.logging.annotation.ApiLog;
@@ -39,6 +40,7 @@ import io.swagger.annotations.ApiOperation;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.http.ResponseEntity;
 import org.springframework.security.access.prepost.PreAuthorize;
+import org.springframework.transaction.annotation.Transactional;
 import org.springframework.web.bind.annotation.*;
 import org.springframework.web.multipart.MultipartFile;
 
@@ -46,6 +48,7 @@ import javax.servlet.http.HttpServletResponse;
 import javax.validation.Valid;
 import java.io.IOException;
 import java.util.*;
+import java.util.concurrent.TimeUnit;
 import java.util.stream.Collectors;
 
 /**
@@ -66,6 +69,9 @@ public class UserController {
     @Autowired
     private OfficeService officeService;
 
+    @Autowired
+    private RedisUtils redisUtils;
+
     /**
      * 根据ids查询用户基本信息(姓名、手机、角色、部门)
      *
@@ -535,7 +541,7 @@ public class UserController {
      */
     @DemoMode
     @ApiLog("修改密码")
-    @RequestMapping("savePwd")
+    @PostMapping("/savePwd")
     public ResponseEntity savePwd(String oldPassword, String newPassword) {
         UserDTO userDTO = UserUtils.getCurrentUserDTO ( );
         if ( StrUtil.isNotBlank ( oldPassword ) && StrUtil.isNotBlank ( newPassword ) ) {
@@ -612,7 +618,117 @@ public class UserController {
     @GetMapping("isAdmin")
     @ApiOperation(value = "判断当前用户是否是管理员")
     public Boolean isAdmin() {
+
         return UserUtils.getCurrentUserDTO().isAdmin();
     }
 
+
+
+
+    /**
+     * 通过手机号获取验证码(忘记密码)
+     * @param mobile 手机号码
+     * @return
+     */
+    @ApiOperation(value = "通过手机号获取验证码(忘记密码)")
+    @GetMapping(value = "/getPhoneCode")
+    public ResponseEntity<HashMap<String,Object>> getPhoneCode(@RequestParam("mobile") String mobile) {
+        HashMap<String,Object> j = new HashMap<String,Object>();
+        //验证该手机号是否已经进行注册
+        User user = userService.getOne(new LambdaQueryWrapper<User>().eq(User::getMobile, mobile));
+        if (ObjectUtil.isNotEmpty( user )) {
+            //生成四位随机验证码
+            String randomCode = String.valueOf((int) (Math.random() * 9000 + 1000));
+
+            HashMap<String,Object> result = null;
+            try{
+                //调用工具类返回结果
+                result = UserUtils.sendRandomCodes(mobile, randomCode);
+                String statusCode = (String) result.get("statusCode");
+                if (("000000").equals(statusCode)) {
+                    j.put("success",true);
+                    j.put("message","短信发送成功!");
+                    //存放验证码
+                    //以手机号+为key 五分钟为时效 将验证码进行短期存储
+                    redisUtils.setEx(mobile+ "resetPassword", randomCode, 300, TimeUnit.SECONDS);
+                }else if(statusCode.equals("160040")){
+                    j.put("success",false);
+                    j.put("message","手机号获取验证码次数已达每日上限!");
+                }else{
+                    j.put("success",false);
+                    j.put("message","短信发送失败,错误代码:101,请联系管理员!");
+                    j.put("ErrorXml",result);
+                }
+            }catch (Exception e){
+                e.printStackTrace();
+                j.put("success",false);
+                j.put("message","短信发送失败!");
+            }
+        } else {
+            j.put("success",false);
+            j.put("message","当前手机号未注册!请联系管理员");
+        }
+        return ResponseEntity.ok(j);
+    }
+
+    /**
+     * 修改密码
+     * @param mobile
+     * @param code
+     * @param newPassword
+     * @return
+     */
+    @GetMapping(value = "/saveNewPassword")
+    @ApiOperation(value = "保存新密码")
+    @Transactional(rollbackFor = Exception.class)
+    public ResponseEntity<HashMap<String,Object>> saveNewPassword(@RequestParam("mobile") String mobile,
+                                                                  @RequestParam("code") String code,
+                                                                  @RequestParam("newPassword") String newPassword) {
+        HashMap<String,Object> j = new HashMap<String,Object>();
+        // 判断当前页面输入的验证码是否与redis中存储的验证码匹配
+        String redisCode = (String) redisUtils.get(mobile + "resetPassword");
+        if (org.apache.commons.lang3.StringUtils.isNotBlank(redisCode)) {
+            if (redisCode.equals( code )) {
+                // 进行密码修改操作
+                User user = userService.getOne(new LambdaQueryWrapper<User>().eq(User::getMobile, mobile));
+                if (ObjectUtil.isNotEmpty( user )) {
+                    if (org.apache.commons.lang3.StringUtils.isNotBlank( newPassword )) {
+                        user.setPassword(SecurityUtils.encryptPassword ( newPassword ));
+                        user.setUpPassword( "1" );
+                        userService.updateById( user ); // 修改密码
+                        redisUtils.delete(mobile + "resetPassword"); // 删除redis中的key
+                        j.put("success",true);
+                        j.put("message","密码修改成功!");
+                    } else {
+                        j.put("success",false);
+                        j.put("message","请输入有效密码!");
+                    }
+                } else {
+                    j.put("success",false);
+                    j.put("message","当前手机号未注册!");
+                }
+            } else {
+                j.put("success",false);
+                j.put("message","验证码输入不正确!");
+            }
+        } else {
+            j.put("success",false);
+            j.put("message","当前手机验证码已失效,请重新发送验证码!");
+        }
+        return ResponseEntity.ok(j);
+    }
+    /**
+     * 获取当前用户是否已经修改过密码
+     *
+     * @return
+     */
+    @ApiLog("获取当前用户是否已经修改过密码")
+    @GetMapping("isUpdatePassword")
+    @ApiOperation(value = "获取当前用户是否已经修改过密码")
+    public ResponseEntity<Boolean> isUpdatePassword() {
+        // 返回结果  true为以修改  false为未修改
+        UserDTO userDTO = UserUtils.getCurrentUserDTO();
+        Boolean updatePassword = userService.isUpdatePassword(userDTO.getId());
+        return ResponseEntity.ok(updatePassword);
+    }
 }

+ 7 - 0
jeeplus-modules/jeeplus-system/src/main/java/com/jeeplus/sys/mapper/UserMapper.java

@@ -208,4 +208,11 @@ public interface UserMapper extends BaseMapper <User> {
      */
     @InterceptorIgnore(tenantLine = "true")
     List<User> selectListByName(@Param("name")String name);
+
+    /**
+     * 判断当前用户是否修改过密码
+     * @param userId
+     * @return
+     */
+    String isUpdatePassword(String userId);
 }

+ 11 - 0
jeeplus-modules/jeeplus-system/src/main/java/com/jeeplus/sys/service/UserService.java

@@ -442,4 +442,15 @@ public class UserService extends ServiceImpl <UserMapper, User> {
         page.setRecords(list1);
         return  page;
     }
+
+    public Boolean isUpdatePassword(String userId) {
+        String updatePassword = userMapper.isUpdatePassword(userId);
+        if (StringUtils.isNotBlank(updatePassword)) {
+            if ("1".equals(updatePassword)) {
+                // 密码已经修改过
+                return true;
+            }
+        }
+        return false;
+    }
 }

+ 9 - 43
jeeplus-modules/jeeplus-system/src/main/java/com/jeeplus/sys/utils/Global.java

@@ -32,6 +32,11 @@ public class Global {
 	 */
 	private static Map<String, String> map = Maps.newHashMap();
 
+	/**
+	 * 属性文件加载对象
+	 */
+	private static PropertiesLoader loader = new PropertiesLoader("bootstrap.yml");
+
 
 	/**
 	 * 显示/隐藏
@@ -68,49 +73,10 @@ public class Global {
 	 * @see {fns:getConfig('adminPath')}
 	 */
 	public static String getConfig(String key) {
-		String value = null;
-		Properties prop = new Properties();
-		Properties applicationProp = new Properties();
-		try {
-			ClassLoader classLoader = DaoAuthenticationProvider.class.getClassLoader();// 读取属性文件xxxxx.properties
-			InputStream applicationIn = classLoader.getResourceAsStream("application.yml");
-			InputStream productionIn = classLoader.getResourceAsStream("application-production.yml");
-			InputStream developmentIn = classLoader.getResourceAsStream("application-development.yml");
-			applicationProp.load(applicationIn);
-			Iterator applicationIt = applicationProp.stringPropertyNames().iterator();
-
-			while (applicationIt.hasNext()) {
-				if (applicationIt.next().equals("active")) {
-					String applicationValue = applicationProp.getProperty("active");
-					switch (applicationValue){
-						case "development":
-							prop.load(developmentIn); /// 加载属性列表
-							Iterator it = prop.stringPropertyNames().iterator();
-							while (it.hasNext()) {
-								if (it.next().equals(key)) {
-									value = prop.getProperty(key);
-									break;
-								}
-							}
-							developmentIn.close();
-							break;
-						case "production":
-							prop.load(productionIn); /// 加载属性列表
-							it = prop.stringPropertyNames().iterator();
-							while (it.hasNext()) {
-								if (it.next().equals(key)) {
-									value = prop.getProperty(key);
-									break;
-								}
-							}
-							productionIn.close();
-							break;
-					}
-				}
-			}
-
-		} catch (Exception e) {
-
+		String value = map.get(key);
+		if (value == null){
+			value = loader.getProperty(key);
+			map.put(key, value != null ? value : StringUtils.EMPTY);
 		}
 		return value;
 	}