Compare commits

..

26 Commits

Author SHA1 Message Date
44b64aa9df 更新 cart_order.html 2024-01-18 22:07:15 +08:00
97ad9f1bbb 更新 cart_agreement.html 2024-01-18 22:07:12 +08:00
25d2148d8e 更新 cart.html 2024-01-18 22:07:10 +08:00
37e942c0c9 更新 UserService.java 2024-01-18 22:07:07 +08:00
429a2c6f9e 更新 UserController.java 2024-01-18 22:07:05 +08:00
7a7c2e85e3 更新 CartPageController.java 2024-01-18 22:07:02 +08:00
84206e653d 更新 UserInfoService.java 2024-01-18 21:27:47 +08:00
a788440b3b 更新 reg.html 2024-01-18 21:20:38 +08:00
ba0ad95e8d 更新 login.html 2024-01-18 21:20:36 +08:00
f1abd66657 更新 index.html 2024-01-18 21:20:34 +08:00
3dacb0022f 更新 UserService.java 2024-01-18 21:20:32 +08:00
4c470ac721 创建 UserInfoService.java 2024-01-18 21:20:30 +08:00
bcd373a21d 创建 OrderService.java 2024-01-18 21:20:28 +08:00
a7b92e1331 创建 CartService.java 2024-01-18 21:20:26 +08:00
05460ae359 创建 AddrService.java 2024-01-18 21:20:23 +08:00
98dfe79bcd 创建 PasswordData.java 2024-01-18 21:20:21 +08:00
29ec339e8f 更新 UserController.java 2024-01-18 21:20:19 +08:00
ed0204aae5 创建 OrderController.java 2024-01-18 21:20:17 +08:00
7134e6c5ff 创建 APIController.java 2024-01-18 21:20:15 +08:00
9f18c47514 更新 pom.xml 2024-01-18 21:20:11 +08:00
5399b6eb75 更新 login.html 2024-01-18 17:45:23 +08:00
a32cdb4c81 更新 UserService.java 2024-01-18 17:45:21 +08:00
2ac87ecbb0 更新 ServerResponseEntity.java 2024-01-18 17:45:18 +08:00
aa64cfd71c 更新 UserMapper.java 2024-01-18 17:45:16 +08:00
c4a6ebece0 更新 UserController.java 2024-01-18 17:45:14 +08:00
0bbfbcb49c 更新 pom.xml 2024-01-18 17:45:12 +08:00
19 changed files with 970 additions and 544 deletions

14
pom.xml
View File

@@ -43,24 +43,28 @@
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.4</version>
<version>3.5.5</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>3.0.3</version>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.0.33</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.0.33</version>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-core</artifactId>
<version>3.5.5</version>
</dependency>
</dependencies>
<build>

View File

@@ -0,0 +1,74 @@
package com.gao.finalhw.controller;
import com.gao.finalhw.model.Address;
import com.gao.finalhw.model.Orders;
import com.gao.finalhw.model.UserInfo;
import com.gao.finalhw.response.ServerResponseEntity;
import com.gao.finalhw.service.AddrService;
import com.gao.finalhw.service.CartService;
import com.gao.finalhw.service.OrderService;
import com.gao.finalhw.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api")
public class APIController {
@Autowired
private CartService cartService;
@Autowired
private UserInfoService userInfoService;
@Autowired
private OrderService orderService;
@Autowired
private AddrService addrService;
@GetMapping("/get_cart_count")
public ServerResponseEntity<Integer> getCartCount(@RequestParam("user_id") int userId) {
try {
int cartCount = cartService.getCartCount(userId);
return ServerResponseEntity.success(cartCount);
} catch (Exception e) {
// 处理异常,例如用户 ID 不存在的情况
return ServerResponseEntity.fail("server error");
}
}
@GetMapping("/get_user_info")
public ServerResponseEntity<UserInfo> get_userInfo(@RequestParam("user_id") int userId){
UserInfo userInfo = userInfoService.getUserInfo(userId);
if (userInfo != null){
return ServerResponseEntity.success(userInfo);
}
return ServerResponseEntity.fail("don't query user info by id.");
}
@GetMapping("/get_order_info")
public ServerResponseEntity<Orders> get_OrderInfo(@RequestParam("order_id") int orderId){
Orders order = orderService.getOrderInfo(orderId);
if (order != null){
return ServerResponseEntity.success(order);
}
return ServerResponseEntity.fail("don't query order info by id.");
}
@GetMapping("/get_order_info/user")
public ServerResponseEntity<Orders> get_OrderInfoByUserId(@RequestParam("user_id") int userId){
Orders order = orderService.getOrderInfoByUserId(userId);
if (order != null){
return ServerResponseEntity.success(order);
}
return ServerResponseEntity.fail("don't query order info by id.");
}
@GetMapping("/get_addr_info")
public ServerResponseEntity<Address> get_AddrInfo(@RequestParam("user_id") int userId){
Address addressInfo = addrService.getAddrById(userId);
if (addressInfo != null){
return ServerResponseEntity.success(addressInfo);
}
return ServerResponseEntity.fail("don't query addr info by id.");
}
}

View File

@@ -22,7 +22,7 @@ public class CartPageController {
}
@GetMapping("/cart_oder_success")
@GetMapping("/cart_order_success")
public String cartOrderSuccessPage() {
return "cart_order_success";
}

View File

@@ -0,0 +1,70 @@
package com.gao.finalhw.controller;
import com.gao.finalhw.model.Orders;
import com.gao.finalhw.response.ServerResponseEntity;
import com.gao.finalhw.service.OrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RequestMapping("/order")
@RestController
public class OrderController {
private final Logger logger = LoggerFactory.getLogger(OrderController.class);
@Autowired
OrderService orderService;
@PostMapping("/add")
public ServerResponseEntity<Boolean> addOrder(@RequestBody Orders orders) {
logger.info("start add order.");
if (orders != null) {
if (orderService.addOrder(orders)) {
return ServerResponseEntity.success(true);
}
return ServerResponseEntity.fail("insert fail.");
}
return ServerResponseEntity.fail("order data error.");
}
@PostMapping("/update")
public ServerResponseEntity<Boolean> updateOrder(@RequestBody Orders orders) {
logger.info("start update order.");
if (orders != null) {
if (orderService.updateOrder(orders)) {
return ServerResponseEntity.success(true);
}
return ServerResponseEntity.fail("update fail.");
}
return ServerResponseEntity.fail("order data error.");
}
@PostMapping("/delete")
public ServerResponseEntity<Boolean> deleteOrder(@RequestBody Orders orders) {
logger.info("start delete order by id.");
if (orders != null) {
if (orderService.deleteById(orders)) {
return ServerResponseEntity.success(true);
}
return ServerResponseEntity.fail("delete fail.");
}
return ServerResponseEntity.fail("order data error.");
}
@PostMapping("/delete1")
public ServerResponseEntity<Boolean> deleteOrderByUserId(@RequestBody Orders orders) {
logger.info("start delete order by user id.");
if (orders != null) {
if (orderService.deleteByUserId(orders)) {
return ServerResponseEntity.success(true);
}
return ServerResponseEntity.fail("delete fail.");
}
return ServerResponseEntity.fail("order data error.");
}
}

View File

@@ -1,29 +1,76 @@
package com.gao.finalhw.controller;
import com.gao.finalhw.model.PasswordData;
import com.gao.finalhw.model.User;
import com.gao.finalhw.pojo.LoginRequest;
import com.gao.finalhw.response.ServerResponseEntity;
import com.gao.finalhw.service.UserService;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
private final Logger logger = LoggerFactory.getLogger(UserController.class);
@Autowired
private UserService userService;
@PostMapping("/login")
public ServerResponseEntity<User> login(@RequestBody LoginRequest loginRequest) {
public ServerResponseEntity<User> login(@RequestBody LoginRequest loginRequest, HttpSession session, HttpServletResponse response) {
logger.info("username: {},password: {}", loginRequest.getUsername(), loginRequest.getPassword());
User user = userService.getUserByUsernameAndPassword(loginRequest.getUsername(), loginRequest.getPassword());
if (user != null) {
logger.info("user: {}", user);
// 隐藏密码
user.setPassword("null");
Cookie cookie = new Cookie("user_id", String.valueOf(user.getUserId()));
response.addCookie(cookie);
session.setAttribute("user", user);
// 如果验证成功
return ServerResponseEntity.success("login success", user);
return ServerResponseEntity.success(0, user);
} else {
// 如果验证失败
return ServerResponseEntity.fail("username or password error.");
}
}
@PostMapping("/register")
public ServerResponseEntity<Boolean> register(@RequestBody User user) {
if (userService.register(user)) {
return ServerResponseEntity.success(true);
}
return ServerResponseEntity.fail("register fail");
}
@PostMapping("/user/update")
public ServerResponseEntity<Boolean> updatePassword(@RequestBody PasswordData passwordData) {
logger.info("request passwordData: {}",passwordData);
if(passwordData != null){
boolean isSuccess = userService.updateUserPasswordByUserId(passwordData);
if (isSuccess) {
return ServerResponseEntity.success(true);
}
return ServerResponseEntity.fail("update password fail");
}
return ServerResponseEntity.fail("data error");
}
@GetMapping("/logout")
public ServerResponseEntity<Boolean> logout(HttpSession session) {
try {
session.removeAttribute("user");
return ServerResponseEntity.success(true);
} catch (Exception e) {
logger.error("server error", e);
return ServerResponseEntity.fail("server error");
}
}
}

View File

@@ -14,8 +14,6 @@ import org.apache.ibatis.annotations.Select;
@Mapper
public interface UserMapper extends BaseMapper<User> {
@Select("SELECT * FROM user WHERE user_name = #{username} AND password = #{password}")
User findByUsernameAndPassword(String username, String password);
}

View File

@@ -0,0 +1,10 @@
package com.gao.finalhw.model;
import lombok.Data;
@Data
public class PasswordData {
private String userId;
private String newPassword;
private String oldPassword;
}

View File

@@ -76,11 +76,12 @@ public class ServerResponseEntity <T> implements Serializable {
ServerResponseEntity<T> serverResponseEntity = new ServerResponseEntity<>();
serverResponseEntity.setCode(code);
serverResponseEntity.setData(data);
serverResponseEntity.getMsg();
serverResponseEntity.setMsg(ResponseEnum.OK.getMsg());
serverResponseEntity.setTimestamp(Instant.now().getEpochSecond());
return serverResponseEntity;
}
public static <T> ServerResponseEntity<T> showFailMsg(String msg) {
logger.error(msg);
ServerResponseEntity<T> serverResponseEntity = new ServerResponseEntity<>();

View File

@@ -0,0 +1,17 @@
package com.gao.finalhw.service;
import com.gao.finalhw.mapper.AddressMapper;
import com.gao.finalhw.model.Address;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class AddrService {
@Autowired
AddressMapper addressMapper;
public Address getAddrById(int id) {
Address result = addressMapper.selectById(id);
return result;
}
}

View File

@@ -0,0 +1,17 @@
package com.gao.finalhw.service;
import com.gao.finalhw.mapper.ShoppingCartMapper;
import com.gao.finalhw.model.ShoppingCart;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class CartService {
@Autowired
ShoppingCartMapper shoppingCartMapper;
public int getCartCount(int id){
ShoppingCart result = shoppingCartMapper.selectById(id);
return result.getCartId();
}
}

View File

@@ -0,0 +1,53 @@
package com.gao.finalhw.service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.gao.finalhw.mapper.OrdersMapper;
import com.gao.finalhw.model.Orders;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class OrderService {
@Autowired
OrdersMapper ordersMapper;
public Orders getOrderInfo(int orderId) {
Orders orderInfo = ordersMapper.selectById(orderId);
return orderInfo;
}
public Orders getOrderInfoByUserId(int userId) {
QueryWrapper<Orders> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("user_id", userId);
return ordersMapper.selectOne(queryWrapper);
}
public boolean addOrder(Orders orders) {
int result = ordersMapper.insert(orders);
return result > 0;
}
public boolean updateOrder(Orders orders) {
UpdateWrapper<Orders> updateWrapper = new UpdateWrapper<>();
updateWrapper.eq("user_id", orders.getUserId());
return ordersMapper.update(orders, updateWrapper) > 0;
}
public boolean deleteById(Orders orders) {
QueryWrapper<Orders> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("id", orders.getOrderId());
return ordersMapper.delete(queryWrapper) > 0;
}
public boolean deleteByUserId(Orders orders) {
QueryWrapper<Orders> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("user_id", orders.getUserId());
return ordersMapper.delete(queryWrapper) > 0;
}
}

View File

@@ -0,0 +1,28 @@
package com.gao.finalhw.service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gao.finalhw.controller.UserController;
import com.gao.finalhw.mapper.UserInfoMapper;
import com.gao.finalhw.model.Orders;
import com.gao.finalhw.model.UserInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserInfoService {
private final Logger logger = LoggerFactory.getLogger(UserInfoService.class);
@Autowired
UserInfoMapper userInfoMapper;
public UserInfo getUserInfo(int id){
logger.info("id: {}",id);
QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("user_id", id);
UserInfo result = userInfoMapper.selectOne(queryWrapper);
logger.info("select user info result: {}",result);
return result;
}
}

View File

@@ -1,16 +1,59 @@
package com.gao.finalhw.service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.gao.finalhw.mapper.UserMapper;
import com.gao.finalhw.model.PasswordData;
import com.gao.finalhw.model.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserService {
private final Logger logger = LoggerFactory.getLogger(UserService.class);
@Autowired
private UserMapper userMapper;
public User getUserByUsernameAndPassword(String username, String password) {
return userMapper.findByUsernameAndPassword(username, password);
logger.info("start getUserByUsernameAndPassword");
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.eq("user_name", username) // 假设字段名为 'username'
.eq("password", password); // 假设字段名为 'password'
User result = userMapper.selectOne(wrapper);
logger.info("query result: {}", result);
return result;
}
public boolean register(User user) {
int result = userMapper.insert(user);
return result > 0;
}
public boolean updateUserPasswordByUserId(PasswordData passwordData) {
String currentPassword = queryPasswordByUserId(passwordData.getUserId());
logger.info("currentPassword: {}, oldPassword: {}.",currentPassword,passwordData.getOldPassword());
if (currentPassword.equals(passwordData.getOldPassword())) {
logger.info("start update password.");
UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
updateWrapper.eq("user_id", passwordData.getUserId())
.set("password", passwordData.getNewPassword());
return userMapper.update(null, updateWrapper) > 0;
} else {
return false;
}
}
public String queryPasswordByUserId(String userId) {
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.select("password").eq("user_id", userId);
User user = userMapper.selectOne(queryWrapper);
return user != null ? user.getPassword() : null;
}
}

View File

@@ -52,9 +52,7 @@
function toPay() {
var nu = '1';
window.location = "cart_agreement.html";
window.location = "/cart_agreement";
}
</script>
<!--内容-->

View File

@@ -58,12 +58,14 @@
var nu = '1';
if (check()) {
$("#aspnetForm").submit();
// $("#aspnetForm").submit();
window.location.href = "/cart_order";
}
}
$(function() {
$("#aspnetForm").attr("action", "/cart_order");
// window.location.href = "/cart_order";
});
</script>
<!--真爱协议中的内容-->

View File

@@ -179,15 +179,16 @@
//check
if ($(".shop_adress-top:not(:last) :radio:checked").length == 0) {
alert("请先选择一个地址。");
return;
}
var nu = '1';
$("#aspnetForm").submit();
// $("#aspnetForm").submit();
window.location.href = "/cart_order_success";
}
$(function() {
$("#aspnetForm").attr("action", "/cart_order_success");
// window.location.href = "/cart_order_success";
// $("#aspnetForm").attr("action", "/cart_order_success");
});
</script>
<!--内容-->

File diff suppressed because it is too large Load Diff

View File

@@ -135,14 +135,20 @@
url: '/login',
type: 'POST',
contentType: 'application/json',
data: JSON.stringify({email: email, password: password}),
data: JSON.stringify({username: email, password: password}),
success: function(response) {
// 处理响应
console.log(response);
if (response.code === "0" && response.msg === 'ok') {
window.location.href = '/index'; // 确保这是正确的重定向 URL
} else {
alert("账号或密码错误");
setTimeout(function() {
window.location.href = '/login';
}, 2000);
}
},
error: function(xhr, status, error) {
// 处理错误
console.error(error);
window.location.href = '/error';
}
});
});
@@ -152,6 +158,11 @@
$('#login span').eq(1).click();
}
});
$(document).ready(function() {
$('.left').click(function() {
window.location.href = '/reg'; // 将 '/register' 替换为您的注册页面 URL
});
});
});
</script>

View File

@@ -13,7 +13,7 @@
<div class="head clearfix">
<!--头部左边-->
<div class="topLeft left">
<a href="index.html" title="Darry Ring">
<a href="/index" title="Darry Ring">
<img width="186" height="42" src="../images/logo_01.png ">
</a>
<span>求婚钻戒领导品牌</span>