From 176610783bdd4700f887129af483807075fe9440 Mon Sep 17 00:00:00 2001 From: pjb7em4fo <318961206@qq.com> Date: Fri, 28 Jun 2024 16:19:39 +0800 Subject: [PATCH] =?UTF-8?q?Add=20=E7=94=A8=E6=88=B7=E7=99=BB=E5=BD=95?= =?UTF-8?q?=E3=80=81=E8=B4=AD=E7=89=A9=E8=AE=A2=E5=8D=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 用户登录、购物订单 | 208 ++++++++++++++++++++++++++++++++++++ 1 file changed, 208 insertions(+) create mode 100644 用户登录、购物订单 diff --git a/用户登录、购物订单 b/用户登录、购物订单 new file mode 100644 index 0000000..77f8246 --- /dev/null +++ b/用户登录、购物订单 @@ -0,0 +1,208 @@ +用户登录模块: +package priv.jesse.mall.web.user; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.ResponseBody; +import priv.jesse.mall.entity.User; +import priv.jesse.mall.entity.pojo.ResultBean; +import priv.jesse.mall.service.UserService; +import priv.jesse.mall.service.exception.LoginException; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.util.List; + +@Controller +@RequestMapping("/user") +public class UserController { + @Autowired + private UserService userService; + + /** + * 打开注册页面 + * + * @return + */ + @RequestMapping("/toRegister.html") + public String toRegister() { + return "mall/user/register"; + } + + /** + * 打开登录页面 + * + * @return + */ + @RequestMapping("/toLogin.html") + public String toLogin() { + return "mall/user/login"; + } + + /** + * 登录 + * + * @param username + * @param password + */ + @RequestMapping("/login.do") + public void login(String username, + String password, + HttpServletRequest request, + HttpServletResponse response) throws IOException { + User user = userService.checkLogin(username, password); + if (user != null) { + //登录成功 重定向到首页 + request.getSession().setAttribute("user", user); + response.sendRedirect("/mall/index.html"); + } else { + throw new LoginException("登录失败! 用户名或者密码错误"); + } + + } + + /** + * 注册 + */ + @RequestMapping("/register.do") + public void register(String username, + String password, + String name, + String phone, + String email, + String addr, + HttpServletResponse response) throws IOException { + User user = new User(); + user.setUsername(username); + user.setPhone(phone); + user.setPassword(password); + user.setName(name); + user.setEmail(email); + user.setAddr(addr); + userService.create(user); + // 注册完成后重定向到登录页面 + response.sendRedirect("/mall/user/toLogin.html"); + } + + /** + * 登出 + */ + @RequestMapping("/logout.do") + public void logout(HttpServletRequest request, HttpServletResponse response) throws IOException { + request.getSession().removeAttribute("user"); + response.sendRedirect("/mall/index.html"); + } + + /** + * 验证用户名是否唯一 + * @param username + * @return + */ + @ResponseBody + @RequestMapping("/checkUsername.do") + public ResultBean checkUsername(String username){ + List users = userService.findByUsername(username); + if (users==null||users.isEmpty()){ + return new ResultBean<>(true); + } + return new ResultBean<>(false); + } + + /** + * 如发生错误 转发到这页面 + * + * @param response + * @param request + * @return + */ + @RequestMapping("/error.html") + public String error(HttpServletResponse response, HttpServletRequest request) { + return "error"; + } +} + +购物订单模块: +package priv.jesse.mall.web.admin; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.ResponseBody; +import priv.jesse.mall.entity.Order; +import priv.jesse.mall.entity.OrderItem; +import priv.jesse.mall.entity.pojo.ResultBean; +import priv.jesse.mall.service.OrderService; + +import java.util.List; + +@Controller +@RequestMapping("/admin/order") +public class AdminOrderController { + @Autowired + private OrderService orderService; + + /** + * 打开订单列表页面 + * @return + */ + @RequestMapping("/toList.html") + public String toList() { + return "admin/order/list"; + } + + /** + * 获取所有订单的总数 + * @return + */ + @ResponseBody + @RequestMapping("/getTotal.do") + public ResultBean getTotal() { + Pageable pageable = new PageRequest(1, 15, null); + int total = (int) orderService.findAll(pageable).getTotalElements(); + return new ResultBean<>(total); + } + + /** + * 获取所有订单 + * @param pageindex + * @param pageSize + * @return + */ + @ResponseBody + @RequestMapping("/list.do") + public ResultBean> listData(int pageindex, + @RequestParam(value = "pageSize", defaultValue = "15") int pageSize) { + Pageable pageable = new PageRequest(pageindex, pageSize, null); + List list = orderService.findAll(pageable).getContent(); + return new ResultBean<>(list); + } + + /** + * 获取订单项 + * @param orderId + * @return + */ + @ResponseBody + @RequestMapping("/getDetail.do") + public ResultBean> getDetail(int orderId) { + List list = orderService.findItems(orderId); + return new ResultBean<>(list); + } + + /** + * 发货 + * @param id + * @return + */ + @ResponseBody + @RequestMapping("/send.do") + public ResultBean send(int id) { + orderService.updateStatus(id,3); + return new ResultBean<>(true); + } +}