Skip to content

Commit

Permalink
购物车模块
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoxianzi-99 committed Feb 17, 2025
1 parent 8a77ce3 commit 599a75c
Show file tree
Hide file tree
Showing 43 changed files with 1,108 additions and 18 deletions.
1 change: 1 addition & 0 deletions spzx/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<module>spzx-common/common-log</module>
<module>spzx-service</module>
<module>spzx-server-gateway</module>
<module>spzx-service-client</module>
</modules>

<!-- 指定父工程 -->
Expand Down
7 changes: 6 additions & 1 deletion spzx/spzx-common/common-service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@
<artifactId>spring-boot-starter-data-redis</artifactId>
<scope>provided</scope>
</dependency>

<!-- openfeign依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.service.anno;

import com.service.config.UserWebMvcConfiguration;
import com.service.interceptor.UserLoginAuthInterceptor;
import org.springframework.context.annotation.Import;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* @author 帕斯卡的芦苇
* @date 2025/2/16
**/
@Retention(value = RetentionPolicy.RUNTIME)
@Target(value = ElementType.TYPE)
@Import(value = {UserLoginAuthInterceptor.class,UserWebMvcConfiguration.class})
public @interface EnableUserWebMvcConfiguration {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.service.config;

import com.service.interceptor.UserLoginAuthInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
* @author 帕斯卡的芦苇
* @date 2025/2/16
**/

public class UserWebMvcConfiguration implements WebMvcConfigurer {
@Autowired
UserLoginAuthInterceptor userLoginAuthInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(userLoginAuthInterceptor)
.addPathPatterns("/api/**");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.service.interceptor;

import com.alibaba.fastjson.JSON;
import com.model.entity.user.UserInfo;
import com.service.utils.AuthContextUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.servlet.HandlerInterceptor;

/**
* @author 帕斯卡的芦苇
* @date 2025/2/16
**/

public class UserLoginAuthInterceptor implements HandlerInterceptor {
@Autowired
private RedisTemplate<String , String> redisTemplate ;
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

// 如果token不为空,那么此时验证token的合法性
String userInfoJSON = redisTemplate.opsForValue().get("user:login:" + request.getHeader("token"));
AuthContextUtil.setUserInfo(JSON.parseObject(userInfoJSON , UserInfo.class));
return true ;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.service.utils;

import com.model.entity.user.UserInfo;

/**
*
* @author 帕斯卡的芦苇
* @date 2025/2/16
**/public class AuthContextUtil {
private static final ThreadLocal<UserInfo> userInfoThreadLocal = new ThreadLocal<>() ;


// 定义存储数据的静态方法
public static void setUserInfo(UserInfo userInfo) {
userInfoThreadLocal.set(userInfo);
}

// 定义获取数据的方法
public static UserInfo getUserInfo() {
return userInfoThreadLocal.get() ;
}

// 删除数据的方法
public static void removeUserInfo() {
userInfoThreadLocal.remove();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ public static SysUser get() {
public static void remove(){
THREAD_LOCAL.remove();
}

}
82 changes: 82 additions & 0 deletions spzx/spzx-common/common-util/src/main/java/com/utils/IpUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package com.utils;

import jakarta.servlet.http.HttpServletRequest;
import org.springframework.http.HttpHeaders;
import org.springframework.http.server.reactive.ServerHttpRequest;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
* @author 帕斯卡的芦苇
* @date 2025/2/14
**/
public class IpUtil {
public static String getIpAddress(HttpServletRequest request) {
String ipAddress = null;
try {
ipAddress = request.getHeader("x-forwarded-for");
if (ipAddress == null || ipAddress.isEmpty() || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("Proxy-Client-IP");
}
if (ipAddress == null || ipAddress.isEmpty() || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("WL-Proxy-Client-IP");
}
if (ipAddress == null || ipAddress.isEmpty() || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getRemoteAddr();
if (ipAddress.equals("127.0.0.1")) {
// 根据网卡取本机配置的IP
InetAddress inet = null;
try {
inet = InetAddress.getLocalHost();
} catch (UnknownHostException e) {
e.printStackTrace();
}
ipAddress = inet.getHostAddress();
}
}
// 对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割
if (ipAddress != null && ipAddress.length() > 15) { // "***.***.***.***".length()
// = 15
if (ipAddress.indexOf(",") > 0) {
ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
}
}
} catch (Exception e) {
ipAddress="";
}
// ipAddress = this.getRequest().getRemoteAddr();

return ipAddress;
}
// 网关中获取Ip地址
public static String getGatwayIpAddress(ServerHttpRequest request) {
HttpHeaders headers = request.getHeaders();
String ip = headers.getFirst("x-forwarded-for");
if (ip != null && ip.length() != 0 && !"unknown".equalsIgnoreCase(ip)) {
// 多次反向代理后会有多个ip值,第一个ip才是真实ip
if (ip.indexOf(",") != -1) {
ip = ip.split(",")[0];
}
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = headers.getFirst("Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = headers.getFirst("WL-Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = headers.getFirst("HTTP_CLIENT_IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = headers.getFirst("HTTP_X_FORWARDED_FOR");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = headers.getFirst("X-Real-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddress().getAddress().getHostAddress();
}
return ip;
}
}
14 changes: 14 additions & 0 deletions spzx/spzx-model/src/main/java/com/model/dto/user/UserLoginDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.model.dto.user;

import lombok.Data;

/**
* @author 帕斯卡的芦苇
* @date 2025/2/14
**/
@Data
public class UserLoginDto {
private String username;

private String password;
}
22 changes: 22 additions & 0 deletions spzx/spzx-model/src/main/java/com/model/entity/h5/CartInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.model.entity.h5;

import com.model.entity.base.BaseEntity;
import lombok.Data;

import java.math.BigDecimal;

/**
* @author 帕斯卡的芦苇
* @date 2025/2/17
**/
@Data
public class CartInfo extends BaseEntity {
private static final long serialVersionUID = 1L;
private Long userId;
private Long skuId;
private BigDecimal cartPrice;
private Integer skuNum;
private String imgUrl;
private String skuName;
private Integer isChecked;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.model.entity.user;

import com.model.entity.base.BaseEntity;
import lombok.Data;

/**
* @author 帕斯卡的芦苇
* @date 2025/2/17
**/
@Data
public class UserAddress extends BaseEntity {
private static final long serialVersionUID = 1L;

private Long userId;

private String name;

private String phone;

private String tagName;

private String provinceCode;

private String cityCode;

private String districtCode;

private String address;

private String fullAddress;

private Integer isDefault;
}
13 changes: 13 additions & 0 deletions spzx/spzx-model/src/main/java/com/model/vo/user/UserInfoVo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.model.vo.user;

import lombok.Data;

/**
* @author 帕斯卡的芦苇
* @date 2025/2/14
**/
@Data
public class UserInfoVo {
private String nickName;
private String avatar;
}
4 changes: 4 additions & 0 deletions spzx/spzx-server-gateway/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

</dependencies>
<properties>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
* @author 帕斯卡的芦苇
* @date 2025/2/14
**/
@Configuration
public class RedisConfig {
@Bean
@Primary
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory);

//String的序列化方式
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
GenericJackson2JsonRedisSerializer genericJackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer();

//序列号key value
redisTemplate.setKeySerializer(stringRedisSerializer);
redisTemplate.setValueSerializer(genericJackson2JsonRedisSerializer);

redisTemplate.afterPropertiesSet();
return redisTemplate;
}
}
Loading

0 comments on commit 599a75c

Please sign in to comment.