Compare commits

..

No commits in common. "a788440b3b89b8e73da24778433265233888f81d" and "5399b6eb75eb81c46849c2aa22fade86b97cd75c" have entirely different histories.

13 changed files with 553 additions and 955 deletions

View File

@ -60,11 +60,6 @@
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-core</artifactId>
<version>3.5.5</version>
</dependency>
</dependencies>
<build>

View File

@ -1,74 +0,0 @@
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

@ -1,70 +0,0 @@
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,17 +1,12 @@
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;
@ -21,55 +16,19 @@ public class UserController {
private final Logger logger = LoggerFactory.getLogger(UserController.class);
@Autowired
private UserService userService;
@PostMapping("/login")
public ServerResponseEntity<User> login(@RequestBody LoginRequest loginRequest, HttpSession session, HttpServletResponse response) {
public ServerResponseEntity<User> login(@RequestBody LoginRequest loginRequest) {
logger.info("username: {},password: {}",loginRequest.getUsername(),loginRequest.getPassword());
User user = userService.getUserByUsernameAndPassword(loginRequest.getUsername(), loginRequest.getPassword());
logger.info("user: {}",user.toString());
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(0, user);
return ServerResponseEntity.success("1", 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) {
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

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

View File

@ -1,17 +0,0 @@
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

@ -1,17 +0,0 @@
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

@ -1,53 +0,0 @@
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

@ -1,23 +0,0 @@
package com.gao.finalhw.service;
import com.gao.finalhw.controller.UserController;
import com.gao.finalhw.mapper.UserInfoMapper;
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){
UserInfo result = userInfoMapper.selectById(id);
logger.info("select user info result: {}",result);
return result;
}
}

View File

@ -1,9 +1,7 @@
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;
@ -24,34 +22,7 @@ public class UserService {
.eq("password", password); // 假设字段名为 'password'
User result = userMapper.selectOne(wrapper);
logger.info("query result: {}", result);
logger.info("query result: {}",result.toString());
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());
if (currentPassword == passwordData.getOldPassword()) {
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

@ -30,8 +30,7 @@
<div id="ucheader1_pllogin1">
<li><a rel="nofollow" href="/login">登录</a><em>|</em></li>
<li><a rel="nofollow" href="/reg">注册</a></li>
<li class="headed"><em class="icon shooping"></em><a target="black" rel="nofollow"
href="/cart">购物车</a><i>(0)</i>
<li class="headed"><em class="icon shooping"></em><a target="black" rel="nofollow" href="/cart">购物车</a><i>(0)</i>
</li>
</div>
</ul>
@ -644,12 +643,6 @@
}
</script>
<script th:inline="javascript">
/*<![CDATA[*/
var isUserLoggedIn = [[${session.user != null}]];
console.log("isUserLoggedIn: " + isUserLoggedIn);
/*]]>*/
</script>
<script type="text/javascript">
function showMask() {
$("#masks").css("height", $(document).height());
@ -672,51 +665,6 @@
$("#masks").fadeOut("slow");
$("#mask").fadeOut("slow");
}
function getCartCountFromSession() {
var cartCount = 0;
var userId = getCookie('user_id');
console.log("user id: " + userId)
if (userId == null){
return
}
$.ajax({
url: '/api/get_cart_count',
type: 'GET',
data: { userId: userId },
success: function(response) {
var cartCount = response.cartCount;
console.log("购物车数量: " + cartCount);
},
error: function(xhr, status, error) {
console.error("Error fetching cart count:", error);
}
});
return cartCount;
}
$(document).ready(function () {
if (isUserLoggedIn) {
var userName = getUserFromSession();
var cartCount = getCartCountFromSession();
$('#ucheader1_pllogin1').hide();
$('#ctl00_ucheader_lit').text('你好!' + userName);
$('.shopping-cart-count').text(cartCount);
}
});
function getCookie(name) {
var cookies = document.cookie.split(';');
for(var i = 0; i < cookies.length; i++) {
var cookie = cookies[i].trim();
var cookieParts = cookie.split('=');
if (cookieParts[0] === name) {
return cookieParts[1];
}
}
return null;
}
</script>
</div>
</body>

View File

@ -137,18 +137,12 @@
contentType: 'application/json',
data: JSON.stringify({username: email, password: password}),
success: function(response) {
if (response.code === "0" && response.msg === 'ok') {
window.location.href = '/index'; // 确保这是正确的重定向 URL
} else {
alert("账号或密码错误");
setTimeout(function() {
window.location.href = '/login';
}, 2000);
}
// 处理响应
console.log(response);
},
error: function(xhr, status, error) {
window.location.href = '/error';
// 处理错误
console.error(error);
}
});
});
@ -158,11 +152,6 @@
$('#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" title="Darry Ring">
<a href="index.html" title="Darry Ring">
<img width="186" height="42" src="../images/logo_01.png ">
</a>
<span>求婚钻戒领导品牌</span>