小程序修改密码
This commit is contained in:
		@@ -99,7 +99,7 @@ public class UserController {
 | 
			
		||||
            user = new User();
 | 
			
		||||
            user.setId(null);
 | 
			
		||||
            user.setUsername(UUID.randomUUID().toString().replace("-", "").substring(0, 10));
 | 
			
		||||
            user.setPassword(UUID.randomUUID().toString());
 | 
			
		||||
            user.setPassword("");
 | 
			
		||||
            user.setRealname("微信用户" + user.getUsername().substring(0, 5));
 | 
			
		||||
            user.setRoleId(3);
 | 
			
		||||
            user.setPermission("1");
 | 
			
		||||
@@ -127,6 +127,34 @@ public class UserController {
 | 
			
		||||
        return userService.getUserById(id);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 修改密码的接口
 | 
			
		||||
     *
 | 
			
		||||
     * @param oldpwd
 | 
			
		||||
     * @param newpwd
 | 
			
		||||
     * @return
 | 
			
		||||
     */
 | 
			
		||||
    @PostMapping("/updatePwd")
 | 
			
		||||
    @ResponseBody
 | 
			
		||||
    public Res updatePwd(@RequestParam("userId") Integer userId,
 | 
			
		||||
                         @RequestParam("oldpwd") String oldpwd,
 | 
			
		||||
                         @RequestParam("newpwd") String newpwd) {
 | 
			
		||||
        if (newpwd == null || Objects.equals(newpwd, "")) {
 | 
			
		||||
            return Res.error("新密码不能为空");
 | 
			
		||||
        }
 | 
			
		||||
        if (oldpwd == null) {
 | 
			
		||||
            oldpwd = "";
 | 
			
		||||
        }
 | 
			
		||||
        // 调用Service层的方法,传入旧密码和新密码
 | 
			
		||||
        boolean result = userService.updatePwd(userId, oldpwd, newpwd);
 | 
			
		||||
        // 根据结果返回响应
 | 
			
		||||
        if (result) {
 | 
			
		||||
            return Res.success("修改成功");
 | 
			
		||||
        } else {
 | 
			
		||||
            return Res.error("旧密码不正确,修改失败");
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 获取用户列表
 | 
			
		||||
     *
 | 
			
		||||
 
 | 
			
		||||
@@ -3,6 +3,7 @@ package com.cxyxiaomo.epp.user.dao;
 | 
			
		||||
import com.cxyxiaomo.epp.common.pojo.User;
 | 
			
		||||
import com.cxyxiaomo.epp.common.vo.UserVO;
 | 
			
		||||
import org.apache.ibatis.annotations.Mapper;
 | 
			
		||||
import org.apache.ibatis.annotations.Param;
 | 
			
		||||
import org.springframework.stereotype.Repository;
 | 
			
		||||
 | 
			
		||||
import java.util.List;
 | 
			
		||||
@@ -15,6 +16,8 @@ public interface UserDao {
 | 
			
		||||
 | 
			
		||||
    public boolean updateUser(User user);
 | 
			
		||||
 | 
			
		||||
    public boolean updatePwd(@Param("id") Integer id, @Param("password") String password);
 | 
			
		||||
 | 
			
		||||
    public User getUserById(Integer id);
 | 
			
		||||
 | 
			
		||||
    User getUserByUsername(String username);
 | 
			
		||||
 
 | 
			
		||||
@@ -3,10 +3,12 @@ package com.cxyxiaomo.epp.user.service;
 | 
			
		||||
import com.cxyxiaomo.epp.common.pojo.User;
 | 
			
		||||
import com.cxyxiaomo.epp.common.vo.UserVO;
 | 
			
		||||
import com.cxyxiaomo.epp.user.dao.UserDao;
 | 
			
		||||
import org.apache.commons.codec.digest.DigestUtils;
 | 
			
		||||
import org.springframework.beans.factory.annotation.Autowired;
 | 
			
		||||
import org.springframework.stereotype.Service;
 | 
			
		||||
 | 
			
		||||
import java.util.List;
 | 
			
		||||
import java.util.Objects;
 | 
			
		||||
 | 
			
		||||
@Service
 | 
			
		||||
public class UserService {
 | 
			
		||||
@@ -47,4 +49,30 @@ public class UserService {
 | 
			
		||||
    public boolean deleteUser(Integer userId) {
 | 
			
		||||
        return userDao.deleteUserById(userId);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 修改密码的方法
 | 
			
		||||
     *
 | 
			
		||||
     * @param oldpwd
 | 
			
		||||
     * @param newpwd
 | 
			
		||||
     * @return
 | 
			
		||||
     */
 | 
			
		||||
    public boolean updatePwd(Integer userId, String oldpwd, String newpwd) {
 | 
			
		||||
        // 获取当前登录的用户信息
 | 
			
		||||
        User user = userDao.getUserById(userId);
 | 
			
		||||
        // 创建一个密码加密器
 | 
			
		||||
        String oldpwdHash = DigestUtils.sha512Hex(oldpwd);
 | 
			
		||||
        // 判断旧密码是否匹配 (或者是未设置密码的初始用户)
 | 
			
		||||
        if (Objects.equals(oldpwdHash, user.getPassword()) ||
 | 
			
		||||
                (Objects.equals(oldpwd, "") && Objects.equals(user.getPassword(), ""))) {
 | 
			
		||||
            // 如果匹配,对新密码进行加密
 | 
			
		||||
            String newpwdHash = DigestUtils.sha512Hex(newpwd);
 | 
			
		||||
            // 调用mapper层的方法,更新数据库中的密码
 | 
			
		||||
            userDao.updatePwd(user.getId(), newpwdHash);
 | 
			
		||||
            return true;
 | 
			
		||||
        } else {
 | 
			
		||||
            // 如果不匹配,返回false
 | 
			
		||||
            return false;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -48,6 +48,11 @@
 | 
			
		||||
        </set>
 | 
			
		||||
        WHERE id = #{id}
 | 
			
		||||
    </update>
 | 
			
		||||
    <update id="updatePwd" parameterType="com.cxyxiaomo.epp.common.pojo.User">
 | 
			
		||||
        UPDATE user
 | 
			
		||||
        SET `password` = #{password}
 | 
			
		||||
        WHERE id = #{id}
 | 
			
		||||
    </update>
 | 
			
		||||
    <select id="getUserById" parameterType="java.lang.Integer" resultType="com.cxyxiaomo.epp.common.pojo.User">
 | 
			
		||||
        SELECT *
 | 
			
		||||
        FROM user
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user