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

补充内容

  • Day13: 获取店铺电话/修改员工密码
    • a. 获取店铺电话
    • b. 修改员工密码

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);

你可能感兴趣的:(苍穹外卖开发笔记,java,spring,mybatis,学习,intellij-idea,tomcat)