itheima苍穹外卖项目学习笔记--补充-获取店铺电话/修改员工密码

Day13: 获取店铺电话/修改员工密码

a. 获取店铺电话

在user/ShopController中,声明一个常量用于存放电话号码

public static final String PHONE = "18900000000";

在user/ShopController中,编写获取店铺电话的方法

/**
 * 获取店铺电话
 * @return
 */
@GetMapping("/phone")
@ApiOperation("获取店铺电话")
public Result<String> getShopPhone(){
    log.info("获取店铺电话");
    return Result.success(PHONE);
}

b. 修改员工密码

在admin/EmployeeController中,编写相关接口信息

/**
 * 修改密码
 * @param passwordEditDTO
 * @return
 */
@PutMapping("/editPassword")
@ApiOperation("修改密码")
public Result editPassword(@RequestBody PasswordEditDTO passwordEditDTO, HttpServletRequest request){
    log.info("修改密码,{}", passwordEditDTO);
    employeeService.editPassword(passwordEditDTO, request);
    return Result.success();
}

在EmployeeServiceImpl中,实现修改密码的方法,及其父类接口

本次由于前端代码无法修改,所以通过cookie获取username

/**
 * 修改密码
 * @param passwordEditDTO
 */
@Override
public void editPassword(PasswordEditDTO passwordEditDTO, HttpServletRequest request) {

    Cookie[] cookies = request.getCookies();
    if (cookies == null){
        throw new PasswordErrorException(MessageConstant.PASSWORD_EDIT_FAILED);
    }

    // 通过cookie获取用户的name
    String username = null;
    for (Cookie cookie : cookies) {
        if (cookie.getName().equals("username")) {
            username = cookie.getValue();
            break;
        }
    }

    // 获取旧密码
    Employee employee = employeeMapper.getByName(username);

    // 匹配旧密码
    if (!DigestUtils.md5DigestAsHex(passwordEditDTO.getOldPassword().getBytes()).equals(employee.getPassword())) {
        // 匹配失败,抛出业务异常
        throw new PasswordErrorException(MessageConstant.PASSWORD_EDIT_FAILED);
    }

    employee.setPassword(DigestUtils.md5DigestAsHex(passwordEditDTO.getNewPassword().getBytes()));

    employeeMapper.update(employee);
}

在EmployeeMapper中,编写通过username查询员工信息的SQL语句

/**
 * 根据username查询员工
 * @param username
 * @return
 */
@Select("select * from employee where username = #{username}")
Employee getByName(String username);
  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值