11.17
完成顾客注册页面优化后,下一步可聚焦于完善用户登录功能及实现角色权限控制,确保不同用户(顾客、经纪人、管理员)登录后进入对应功能页面。以下是具体步骤:
第一步:完善登录功能(后端+前端)
1. 统一登录入口页面(templates/login.html)
创建一个公共登录页,支持选择角色(顾客/经纪人/管理员),替换原分散的登录页:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>系统登录</title>
</head>
<body>
<form th:action="@{/login}" method="post">
<h2>房产信息管理系统</h2>
<div th:if="${error}" style="color: red;">[[${error}]]</div>
<!-- 角色选择 -->
<label>角色:</label>
<select name="role" required>
<option value="customer">顾客</option>
<option value="agent">经纪人</option>
<option value="admin">管理员</option>
</select><br>
<!-- 账号密码 -->
<label for="username">账号:</label>
<input type="text" id="username" name="username" required><br>
<label for="password">密码:</label>
<input type="password" id="password" name="password" required><br>
<button type="submit">登录</button>
<a href="/customer/register">顾客注册</a>
</form>
</body>
</html>
2. 登录验证后端逻辑(LoginController.java)
创建统一登录控制器,根据角色验证账号密码:
package com.school.house.controller;
import com.school.house.entity.Admin;
import com.school.house.entity.Agent;
import com.school.house.entity.Customer;
import com.school.house.repository.AdminRepository;
import com.school.house.repository.AgentRepository;
import com.school.house.repository.CustomerRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class LoginController {
@Autowired
private CustomerRepository customerRepository;
@Autowired
private AgentRepository agentRepository;
@Autowired
private AdminRepository adminRepository;
// 跳转登录页
@GetMapping("/login")
public String toLoginPage() {
return "login"; // 对应公共登录页
}
// 处理登录请求
@PostMapping("/login")
public String login(
@RequestParam String role,
@RequestParam String username,
@RequestParam String password,
Model model
) {
// 根据角色验证
switch (role) {
case "customer":
// 顾客账号为手机号,需审核通过
Customer customer = customerRepository.findByPhone(username);
if (customer != null && customer.getPassword().equals(password) && customer.getIsApproved() == 1) {
return "redirect:/customer/index"; // 顾客首页
}
break;
case "agent":
// 经纪人账号为工号
Agent agent = agentRepository.findById(username).orElse(null);
if (agent != null && agent.getPassword().equals(password)) {
return "redirect:/agent/index"; // 经纪人首页
}
break;
case "admin":
// 管理员账号为username
Admin admin = adminRepository.findByUsername(username);
if (admin != null && admin.getPassword().equals(password)) {
return "redirect:/admin/index"; // 管理员首页
}
break;
default:
model.addAttribute("error", "角色选择错误");
return "login";
}
// 验证失败
model.addAttribute("error", "账号/密码错误或未通过审核");
return "login";
}
}
第二步:实现各角色首页与核心功能
1. 顾客首页(templates/customer/index.html)
展示在售房产列表,提供查询入口:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>顾客中心</title>
</head>
<body>
<h2>在售房产列表</h2>
<!-- 搜索表单 -->
<form th:action="@{/customer/search}" method="get">
<label for="roomType">户型:</label>
<select id="roomType" name="roomType">
<option value="">全部</option>
<option value="四室两厅">四室两厅</option>
<option value="三室两厅">三室两厅</option>
<!-- 其他户型选项 -->
</select>
<label for="address">地址:</label>
<input type="text" id="address" name="address" placeholder="输入地址关键词">
<button type="submit">查询</button>
</form>
<!-- 房产列表 -->
<table border="1">
<tr>
<th>地址</th>
<th>户型</th>
<th>面积</th>
<th>售价</th>
<th>操作</th>
</tr>
<tr th:each="house : ${houses}">
<td th:text="${house.address}"></td>
<td th:text="${house.roomType}"></td>
<td th:text="${house.area}"></td>
<td th:text="${house.price}"></td>
<td>
<a th:href="@{/customer/house/detail(houseId=${house.houseId})}">查看详情</a>
<a th:href="@{/customer/house/buy(houseId=${house.houseId})}">购买</a>
</td>
</tr>
</table>
</body>
</html>
2. 顾客查询与购买功能(CustomerController补充)
@Controller
@RequestMapping("/customer")
public class CustomerController {
@Autowired
private HouseService houseService;
// 顾客首页(加载在售房产)
@GetMapping("/index")
public String index(Model model) {
model.addAttribute("houses", houseService.findAllOnSale());
return "customer/index";
}
// 房产搜索
@GetMapping("/search")
public String search(
@RequestParam(required = false) String roomType,
@RequestParam(required = false) String address,
Model model
) {
List<House> houses = houseService.searchHouses(roomType, address);
model.addAttribute("houses", houses);
return "customer/index"; // 复用首页展示结果
}
// 顾客下单(状态改为“意向”)
@GetMapping("/house/buy")
public String buyHouse(@RequestParam String houseId) {
houseService.updateStatus(houseId, "意向");
return "redirect:/customer/index";
}
}
第三步:测试登录与权限流转
- 启动项目:运行
HouseApplication.java,访问http://localhost:8080/login。 - 测试场景:
- 顾客:使用已注册且审核通过的手机号+密码登录,查看在售房产并下单(状态变为“意向”)。
- 经纪人:使用正确工号+密码(默认123456)登录,后续可开发“确认交易”功能(将“意向”改为“售出”)。
- 管理员:使用默认账号
admin+密码123456登录,后续可开发“新增房产”“审核顾客”功能。
后续开发方向
- 完善经纪人模块(查看负责房产、确认交易)。
- 开发管理员核心功能(新增房产、授权经纪人、停售房产)。
- 添加密码加密(使用Spring Security的BCrypt加密),提升安全性。
- 优化前端样式(添加CSS),使页面更美观。
按此步骤推进,可逐步实现系统的核心业务流程,确保各角色功能闭环。

浙公网安备 33010602011771号