苍穹外卖的修改密码接口找不到empid的解决方法

这是修改密码接口,但是直接请求的话会找不到empid。解决方法是:使用ThreadLocal拿到当前id

修改密码接口代码如下:

EmployeeController:

···

@PutMapping("/editPassword")
public Result editPassword(@RequestBody PasswordEditDTO passwordEditDTO) {
    //如果新旧密码一样,直接抛出异常
    if(passwordEditDTO.getOldPassword() .equals(passwordEditDTO.getNewPassword())){
        return Result.error("新旧密码相同");
    }
    employeeService.updatePassword(passwordEditDTO);
    return Result.success();

}

···

EmployeeServiceImpl:

···

@Override
public void updatePassword(PasswordEditDTO passwordEditDTO) {
    //先验证密码是否正确
    String password = passwordEditDTO.getOldPassword();
    //把旧密码进行md5加密,再进行对比
    password = DigestUtils.md5DigestAsHex(password.getBytes());
    Employee employee = employeeMapper.getById(BaseContext.getCurrentId());
    if(!password.equals(employee.getPassword())){
        throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
    }else{
        employee.setPassword(DigestUtils.md5DigestAsHex(passwordEditDTO.getNewPassword().getBytes()));
        employeeMapper.updateEmployee(employee);
    }
}

···

EmployeeMapper:

···

@Select("select id ,name, username, password, phone, sex, id_number, status, create_time, update_time, create_user, update_user from employee where id = #{id}")
Employee getById(long id);
void updateEmployee(Employee employee);

···

EmployeeMapper.xml:

···

<update id="updateEmployee">
    update employee <set>
    <if test="idNumber != null">   id_number = #{idNumber},</if>
    <if test="name!= null">   name = #{name},</if>
    <if test="phone!= null">   phone = #{phone},</if>
    <if test="sex!= null">   sex = #{sex},</if>
    <if test="username!= null">   username = #{username},</if>
    <if test="updateTime!= null">   update_time = #{updateTime},</if>
    <if test="status != null">   status = #{status},</if>
    <if test="password != null"> password = #{password},</if>
</set>
    where id = #{id}
</update>

···

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值