添加购物车
This commit is contained in:
parent
66692990c5
commit
329d74a21c
|
@ -1,4 +1,3 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<project version="4">
|
<project version="4">
|
||||||
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
||||||
<component name="MavenProjectsManager">
|
<component name="MavenProjectsManager">
|
||||||
|
|
|
@ -4,8 +4,24 @@
|
||||||
<option name="autoReloadType" value="SELECTIVE" />
|
<option name="autoReloadType" value="SELECTIVE" />
|
||||||
</component>
|
</component>
|
||||||
<component name="ChangeListManager">
|
<component name="ChangeListManager">
|
||||||
<list default="true" id="6ee93b58-a7e0-45a0-9e98-4c363b192afb" name="Changes" comment="获取用户信息并跳转">
|
<list default="true" id="6ee93b58-a7e0-45a0-9e98-4c363b192afb" name="Changes" comment="创建购物车">
|
||||||
|
<change afterPath="$PROJECT_DIR$/spzx-server-gateway/src/main/java/cn/bunny/getway/config/RedisConfig.java" afterDir="false" />
|
||||||
|
<change afterPath="$PROJECT_DIR$/spzx-service/service-cart/src/main/java/cn/bunny/web/controller/CartController.java" afterDir="false" />
|
||||||
|
<change afterPath="$PROJECT_DIR$/spzx-service/service-cart/src/main/java/cn/bunny/web/service/CartService.java" afterDir="false" />
|
||||||
|
<change afterPath="$PROJECT_DIR$/spzx-service/service-cart/src/main/java/cn/bunny/web/service/impl/CartServiceImpl.java" afterDir="false" />
|
||||||
|
<change beforePath="$PROJECT_DIR$/.idea/misc.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/misc.xml" afterDir="false" />
|
||||||
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
|
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
|
||||||
|
<change beforePath="$PROJECT_DIR$/spzx-common/common-service/src/main/java/cn/bunny/anno/EnableUserLoginAuthInterceptor.java" beforeDir="false" afterPath="$PROJECT_DIR$/spzx-common/common-service/src/main/java/cn/bunny/anno/EnableUserWebMvcConfiguration.java" afterDir="false" />
|
||||||
|
<change beforePath="$PROJECT_DIR$/spzx-common/common-service/src/main/java/cn/bunny/interceptor/UserLoginAuthInterceptor.java" beforeDir="false" afterPath="$PROJECT_DIR$/spzx-common/common-service/src/main/java/cn/bunny/interceptor/UserLoginAuthInterceptor.java" afterDir="false" />
|
||||||
|
<change beforePath="$PROJECT_DIR$/spzx-server-gateway/src/main/java/cn/bunny/getway/filter/AuthGlobalFilter.java" beforeDir="false" afterPath="$PROJECT_DIR$/spzx-server-gateway/src/main/java/cn/bunny/getway/filter/AuthGlobalFilter.java" afterDir="false" />
|
||||||
|
<change beforePath="$PROJECT_DIR$/spzx-server-gateway/src/main/resources/application-dev.yml" beforeDir="false" afterPath="$PROJECT_DIR$/spzx-server-gateway/src/main/resources/application-dev.yml" afterDir="false" />
|
||||||
|
<change beforePath="$PROJECT_DIR$/spzx-service/service-cart/pom.xml" beforeDir="false" afterPath="$PROJECT_DIR$/spzx-service/service-cart/pom.xml" afterDir="false" />
|
||||||
|
<change beforePath="$PROJECT_DIR$/spzx-service/service-cart/src/main/resources/application-dev.yml" beforeDir="false" afterPath="$PROJECT_DIR$/spzx-service/service-cart/src/main/resources/application-dev.yml" afterDir="false" />
|
||||||
|
<change beforePath="$PROJECT_DIR$/spzx-service/service-product/src/main/resources/application-dev.yml" beforeDir="false" afterPath="$PROJECT_DIR$/spzx-service/service-product/src/main/resources/application-dev.yml" afterDir="false" />
|
||||||
|
<change beforePath="$PROJECT_DIR$/spzx-service/service-user/src/main/java/cn/bunny/user/User.java" beforeDir="false" afterPath="$PROJECT_DIR$/spzx-service/service-user/src/main/java/cn/bunny/user/User.java" afterDir="false" />
|
||||||
|
<change beforePath="$PROJECT_DIR$/spzx-service/service-user/src/main/java/cn/bunny/user/controller/UserinfoController.java" beforeDir="false" afterPath="$PROJECT_DIR$/spzx-service/service-user/src/main/java/cn/bunny/user/controller/UserinfoController.java" afterDir="false" />
|
||||||
|
<change beforePath="$PROJECT_DIR$/spzx-service/service-user/src/main/java/cn/bunny/user/service/impl/UserinfoServiceImpl.java" beforeDir="false" afterPath="$PROJECT_DIR$/spzx-service/service-user/src/main/java/cn/bunny/user/service/impl/UserinfoServiceImpl.java" afterDir="false" />
|
||||||
|
<change beforePath="$PROJECT_DIR$/spzx-service/service-user/src/main/resources/application-dev.yml" beforeDir="false" afterPath="$PROJECT_DIR$/spzx-service/service-user/src/main/resources/application-dev.yml" afterDir="false" />
|
||||||
</list>
|
</list>
|
||||||
<option name="SHOW_DIALOG" value="false" />
|
<option name="SHOW_DIALOG" value="false" />
|
||||||
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
||||||
|
@ -15,8 +31,8 @@
|
||||||
<component name="FileTemplateManagerImpl">
|
<component name="FileTemplateManagerImpl">
|
||||||
<option name="RECENT_TEMPLATES">
|
<option name="RECENT_TEMPLATES">
|
||||||
<list>
|
<list>
|
||||||
<option value="Interface" />
|
|
||||||
<option value="mybatis-mapper" />
|
<option value="mybatis-mapper" />
|
||||||
|
<option value="Interface" />
|
||||||
<option value="Class" />
|
<option value="Class" />
|
||||||
</list>
|
</list>
|
||||||
</option>
|
</option>
|
||||||
|
@ -56,48 +72,48 @@
|
||||||
<option name="hideEmptyMiddlePackages" value="true" />
|
<option name="hideEmptyMiddlePackages" value="true" />
|
||||||
<option name="showLibraryContents" value="true" />
|
<option name="showLibraryContents" value="true" />
|
||||||
</component>
|
</component>
|
||||||
<component name="PropertiesComponent"><![CDATA[{
|
<component name="PropertiesComponent">{
|
||||||
"keyToString": {
|
"keyToString": {
|
||||||
"RequestMappingsPanelOrder0": "0",
|
"RequestMappingsPanelOrder0": "0",
|
||||||
"RequestMappingsPanelOrder1": "1",
|
"RequestMappingsPanelOrder1": "1",
|
||||||
"RequestMappingsPanelWidth0": "75",
|
"RequestMappingsPanelWidth0": "75",
|
||||||
"RequestMappingsPanelWidth1": "75",
|
"RequestMappingsPanelWidth1": "75",
|
||||||
"RunOnceActivity.OpenProjectViewOnStart": "true",
|
"RunOnceActivity.OpenProjectViewOnStart": "true",
|
||||||
"RunOnceActivity.ShowReadmeOnStart": "true",
|
"RunOnceActivity.ShowReadmeOnStart": "true",
|
||||||
"SHARE_PROJECT_CONFIGURATION_FILES": "true",
|
"SHARE_PROJECT_CONFIGURATION_FILES": "true",
|
||||||
"WebServerToolWindowFactoryState": "false",
|
"WebServerToolWindowFactoryState": "false",
|
||||||
"code.cleanup.on.save": "true",
|
"code.cleanup.on.save": "true",
|
||||||
"git-widget-placeholder": "master",
|
"git-widget-placeholder": "master",
|
||||||
"jdk.selected.JAVA_MODULE": "21",
|
"jdk.selected.JAVA_MODULE": "corretto-17",
|
||||||
"last_opened_file_path": "F:/File/Java/spzx-parent/spzx-service/service-user/src/main/resources",
|
"last_opened_file_path": "F:/File/Java/spzx-parent/spzx-service/service-cart/src/main/resources",
|
||||||
"node.js.detected.package.eslint": "true",
|
"node.js.detected.package.eslint": "true",
|
||||||
"node.js.detected.package.tslint": "true",
|
"node.js.detected.package.tslint": "true",
|
||||||
"node.js.selected.package.eslint": "(autodetect)",
|
"node.js.selected.package.eslint": "(autodetect)",
|
||||||
"node.js.selected.package.tslint": "(autodetect)",
|
"node.js.selected.package.tslint": "(autodetect)",
|
||||||
"nodejs_package_manager_path": "npm",
|
"nodejs_package_manager_path": "npm",
|
||||||
"project.structure.last.edited": "Project",
|
"project.structure.last.edited": "Project",
|
||||||
"project.structure.proportion": "0.15",
|
"project.structure.proportion": "0.15",
|
||||||
"project.structure.side.proportion": "0.2",
|
"project.structure.side.proportion": "0.2",
|
||||||
"rearrange.code.on.save": "true",
|
"rearrange.code.on.save": "true",
|
||||||
"settings.editor.selected.configurable": "preferences.pluginManager",
|
"settings.editor.selected.configurable": "MavenSettings",
|
||||||
"vue.rearranger.settings.migration": "true"
|
"vue.rearranger.settings.migration": "true"
|
||||||
},
|
},
|
||||||
"keyToStringList": {
|
"keyToStringList": {
|
||||||
"DatabaseDriversLRU": [
|
"DatabaseDriversLRU": [
|
||||||
"mysql"
|
"mysql"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
}]]></component>
|
}</component>
|
||||||
<component name="ReactorSettings">
|
<component name="ReactorSettings">
|
||||||
<option name="notificationShown" value="true" />
|
<option name="notificationShown" value="true" />
|
||||||
</component>
|
</component>
|
||||||
<component name="RecentsManager">
|
<component name="RecentsManager">
|
||||||
<key name="CopyFile.RECENT_KEYS">
|
<key name="CopyFile.RECENT_KEYS">
|
||||||
|
<recent name="F:\File\Java\spzx-parent\spzx-service\service-cart\src\main\resources" />
|
||||||
<recent name="F:\File\Java\spzx-parent\spzx-service\service-user\src\main\resources" />
|
<recent name="F:\File\Java\spzx-parent\spzx-service\service-user\src\main\resources" />
|
||||||
<recent name="F:\File\Java\spzx-parent\spzx-service\service-user\src\main\java\cn\bunny\user" />
|
<recent name="F:\File\Java\spzx-parent\spzx-service\service-user\src\main\java\cn\bunny\user" />
|
||||||
<recent name="F:\File\Java\spzx-parent\spzx-service\service-product\src\main\java\cn\bunny\web\product" />
|
<recent name="F:\File\Java\spzx-parent\spzx-service\service-product\src\main\java\cn\bunny\web\product" />
|
||||||
<recent name="F:\File\Java\spzx-parent\spzx-server-gateway\src\main\resources" />
|
<recent name="F:\File\Java\spzx-parent\spzx-server-gateway\src\main\resources" />
|
||||||
<recent name="F:\File\Java\spzx-parent\spzx-service\service-product\src\main\resources" />
|
|
||||||
</key>
|
</key>
|
||||||
<key name="MoveFile.RECENT_KEYS">
|
<key name="MoveFile.RECENT_KEYS">
|
||||||
<recent name="F:\File\Java\spzx-parent\spzx-service\service-product\src\main\resources" />
|
<recent name="F:\File\Java\spzx-parent\spzx-service\service-product\src\main\resources" />
|
||||||
|
@ -112,7 +128,7 @@
|
||||||
</set>
|
</set>
|
||||||
</option>
|
</option>
|
||||||
</component>
|
</component>
|
||||||
<component name="RunManager" selected="Spring Boot.User">
|
<component name="RunManager" selected="Spring Boot.ServerGetWayApplication">
|
||||||
<configuration name="ProductApplication" type="SpringBootApplicationConfigurationType" factoryName="Spring Boot" temporary="true" nameIsGenerated="true">
|
<configuration name="ProductApplication" type="SpringBootApplicationConfigurationType" factoryName="Spring Boot" temporary="true" nameIsGenerated="true">
|
||||||
<module name="service-product" />
|
<module name="service-product" />
|
||||||
<option name="SPRING_BOOT_MAIN_CLASS" value="cn.bunny.web.product.ProductApplication" />
|
<option name="SPRING_BOOT_MAIN_CLASS" value="cn.bunny.web.product.ProductApplication" />
|
||||||
|
@ -154,9 +170,9 @@
|
||||||
</configuration>
|
</configuration>
|
||||||
<recent_temporary>
|
<recent_temporary>
|
||||||
<list>
|
<list>
|
||||||
|
<item itemvalue="Spring Boot.ServerGetWayApplication" />
|
||||||
<item itemvalue="Spring Boot.User" />
|
<item itemvalue="Spring Boot.User" />
|
||||||
<item itemvalue="Spring Boot.ProductApplication" />
|
<item itemvalue="Spring Boot.ProductApplication" />
|
||||||
<item itemvalue="Spring Boot.ServerGetWayApplication" />
|
|
||||||
</list>
|
</list>
|
||||||
</recent_temporary>
|
</recent_temporary>
|
||||||
</component>
|
</component>
|
||||||
|
@ -171,7 +187,9 @@
|
||||||
<workItem from="1703487757122" duration="903000" />
|
<workItem from="1703487757122" duration="903000" />
|
||||||
<workItem from="1703488670206" duration="5932000" />
|
<workItem from="1703488670206" duration="5932000" />
|
||||||
<workItem from="1703549556395" duration="26425000" />
|
<workItem from="1703549556395" duration="26425000" />
|
||||||
<workItem from="1703654409796" duration="10240000" />
|
<workItem from="1703654409796" duration="10591000" />
|
||||||
|
<workItem from="1703684112039" duration="2403000" />
|
||||||
|
<workItem from="1703722925328" duration="9980000" />
|
||||||
</task>
|
</task>
|
||||||
<task id="LOCAL-00001" summary="前端接口初始化;显示商品页面">
|
<task id="LOCAL-00001" summary="前端接口初始化;显示商品页面">
|
||||||
<option name="closed" value="true" />
|
<option name="closed" value="true" />
|
||||||
|
@ -293,7 +311,15 @@
|
||||||
<option name="project" value="LOCAL" />
|
<option name="project" value="LOCAL" />
|
||||||
<updated>1703664991364</updated>
|
<updated>1703664991364</updated>
|
||||||
</task>
|
</task>
|
||||||
<option name="localTasksCounter" value="16" />
|
<task id="LOCAL-00016" summary="创建购物车">
|
||||||
|
<option name="closed" value="true" />
|
||||||
|
<created>1703667374773</created>
|
||||||
|
<option name="number" value="00016" />
|
||||||
|
<option name="presentableId" value="LOCAL-00016" />
|
||||||
|
<option name="project" value="LOCAL" />
|
||||||
|
<updated>1703667374773</updated>
|
||||||
|
</task>
|
||||||
|
<option name="localTasksCounter" value="17" />
|
||||||
<servers />
|
<servers />
|
||||||
</component>
|
</component>
|
||||||
<component name="TypeScriptGeneratedFilesManager">
|
<component name="TypeScriptGeneratedFilesManager">
|
||||||
|
@ -323,7 +349,8 @@
|
||||||
<MESSAGE value="会员登录" />
|
<MESSAGE value="会员登录" />
|
||||||
<MESSAGE value="获取用户信息-未测试" />
|
<MESSAGE value="获取用户信息-未测试" />
|
||||||
<MESSAGE value="获取用户信息并跳转" />
|
<MESSAGE value="获取用户信息并跳转" />
|
||||||
<option name="LAST_COMMIT_MESSAGE" value="获取用户信息并跳转" />
|
<MESSAGE value="创建购物车" />
|
||||||
|
<option name="LAST_COMMIT_MESSAGE" value="创建购物车" />
|
||||||
</component>
|
</component>
|
||||||
<component name="XDebuggerManager">
|
<component name="XDebuggerManager">
|
||||||
<pin-to-top-manager>
|
<pin-to-top-manager>
|
||||||
|
|
|
@ -12,5 +12,5 @@ import java.lang.annotation.Target;
|
||||||
@Retention(value = RetentionPolicy.RUNTIME)
|
@Retention(value = RetentionPolicy.RUNTIME)
|
||||||
@Target(value = ElementType.TYPE)
|
@Target(value = ElementType.TYPE)
|
||||||
@Import(value = {UserLoginAuthInterceptor.class, UserWebMvcConfiguration.class})
|
@Import(value = {UserLoginAuthInterceptor.class, UserWebMvcConfiguration.class})
|
||||||
public @interface EnableUserLoginAuthInterceptor {
|
public @interface EnableUserWebMvcConfiguration {
|
||||||
}
|
}
|
|
@ -11,13 +11,14 @@ import org.springframework.web.servlet.HandlerInterceptor;
|
||||||
|
|
||||||
|
|
||||||
public class UserLoginAuthInterceptor implements HandlerInterceptor {
|
public class UserLoginAuthInterceptor implements HandlerInterceptor {
|
||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
private RedisTemplate redisTemplate;
|
private RedisTemplate<String, String> redisTemplate;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
||||||
// 如果token不为空,那么此时验证token的合法性
|
// 如果token不为空,那么此时验证token的合法性
|
||||||
String userInfoJSON = (String) redisTemplate.opsForValue().get("user:spzx:" + request.getHeader("token"));
|
String userInfoJSON = redisTemplate.opsForValue().get("user:spzx:" + request.getHeader("token"));
|
||||||
AuthContextUtil.setUserInfo(JSON.parseObject(userInfoJSON, UserInfo.class));
|
AuthContextUtil.setUserInfo(JSON.parseObject(userInfoJSON, UserInfo.class));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,32 @@
|
||||||
|
package cn.bunny.getway.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;
|
||||||
|
|
||||||
|
@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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -26,21 +26,20 @@ import java.util.List;
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Component
|
@Component
|
||||||
public class AuthGlobalFilter implements GlobalFilter, Ordered {
|
public class AuthGlobalFilter implements GlobalFilter, Ordered {
|
||||||
|
|
||||||
|
private final AntPathMatcher antPathMatcher = new AntPathMatcher();
|
||||||
@Resource
|
@Resource
|
||||||
private RedisTemplate redisTemplate;
|
private RedisTemplate<String, String> redisTemplate;
|
||||||
private AntPathMatcher antPathMatcher;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
|
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
|
||||||
// 获取当前请求路径
|
|
||||||
ServerHttpRequest request = exchange.getRequest();
|
ServerHttpRequest request = exchange.getRequest();
|
||||||
String path = request.getURI().getPath();
|
String path = request.getURI().getPath();
|
||||||
|
UserInfo userInfo = this.getUserInfo(request);
|
||||||
|
|
||||||
// 判断路径是否满足 /api/**/auth/**
|
// api接口,异步请求,校验用户必须登录
|
||||||
if (antPathMatcher.match("/api/**/auth/**", path)) {
|
if (antPathMatcher.match("/api/**/auth/**", path)) {
|
||||||
// 登录校验
|
if (null == userInfo) {
|
||||||
UserInfo userInfo = this.getUserinfo(request);
|
|
||||||
if (userInfo == null) {
|
|
||||||
ServerHttpResponse response = exchange.getResponse();
|
ServerHttpResponse response = exchange.getResponse();
|
||||||
return out(response, ResultCodeEnum.LOGIN_AUTH);
|
return out(response, ResultCodeEnum.LOGIN_AUTH);
|
||||||
}
|
}
|
||||||
|
@ -54,26 +53,6 @@ public class AuthGlobalFilter implements GlobalFilter, Ordered {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private UserInfo getUserinfo(ServerHttpRequest request) {
|
|
||||||
String token = "";
|
|
||||||
List<String> tokenList = request.getHeaders().get("token");
|
|
||||||
if (tokenList != null) {
|
|
||||||
token = tokenList.get(0);
|
|
||||||
}
|
|
||||||
if (StringUtils.hasText(token)) {
|
|
||||||
String userJson = (String) redisTemplate.opsForValue().get("user:spzx" + token);
|
|
||||||
|
|
||||||
if (StringUtils.hasText(userJson)) {
|
|
||||||
return null;
|
|
||||||
} else {
|
|
||||||
return JSON.parseObject(userJson, UserInfo.class);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
private Mono<Void> out(ServerHttpResponse response, ResultCodeEnum resultCodeEnum) {
|
private Mono<Void> out(ServerHttpResponse response, ResultCodeEnum resultCodeEnum) {
|
||||||
Result result = Result.build(null, resultCodeEnum);
|
Result result = Result.build(null, resultCodeEnum);
|
||||||
byte[] bits = JSONObject.toJSONString(result).getBytes(StandardCharsets.UTF_8);
|
byte[] bits = JSONObject.toJSONString(result).getBytes(StandardCharsets.UTF_8);
|
||||||
|
@ -82,4 +61,21 @@ public class AuthGlobalFilter implements GlobalFilter, Ordered {
|
||||||
response.getHeaders().add("Content-Type", "application/json;charset=UTF-8");
|
response.getHeaders().add("Content-Type", "application/json;charset=UTF-8");
|
||||||
return response.writeWith(Mono.just(buffer));
|
return response.writeWith(Mono.just(buffer));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private UserInfo getUserInfo(ServerHttpRequest request) {
|
||||||
|
String token = "";
|
||||||
|
List<String> tokenList = request.getHeaders().get("token");
|
||||||
|
if (null != tokenList) {
|
||||||
|
token = tokenList.get(0);
|
||||||
|
}
|
||||||
|
if (!StringUtils.isEmpty(token)) {
|
||||||
|
String userInfoJSON = redisTemplate.opsForValue().get("user:spzx:" + token);
|
||||||
|
if (StringUtils.isEmpty(userInfoJSON)) {
|
||||||
|
return null;
|
||||||
|
} else {
|
||||||
|
return JSON.parseObject(userInfoJSON, UserInfo.class);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -7,6 +7,7 @@ spring:
|
||||||
nacos:
|
nacos:
|
||||||
discovery:
|
discovery:
|
||||||
server-addr: 192.168.2.82:8848
|
server-addr: 192.168.2.82:8848
|
||||||
|
# server-addr: 192.168.31.202:8848
|
||||||
gateway:
|
gateway:
|
||||||
discovery:
|
discovery:
|
||||||
locator:
|
locator:
|
||||||
|
@ -40,6 +41,7 @@ spring:
|
||||||
data:
|
data:
|
||||||
redis:
|
redis:
|
||||||
host: 192.168.2.82
|
host: 192.168.2.82
|
||||||
|
# host: 192.168.31.202
|
||||||
port: 6379
|
port: 6379
|
||||||
mybatis:
|
mybatis:
|
||||||
config-location: classpath:mybatis-config.xml
|
config-location: classpath:mybatis-config.xml
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<parent>
|
<parent>
|
||||||
|
@ -25,5 +25,9 @@
|
||||||
<version>3.8.1</version>
|
<version>3.8.1</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-data-redis</artifactId>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</project>
|
</project>
|
||||||
|
|
|
@ -0,0 +1,27 @@
|
||||||
|
package cn.bunny.web.controller;
|
||||||
|
|
||||||
|
import cn.bunny.common.spzx.model.vo.common.Result;
|
||||||
|
import cn.bunny.common.spzx.model.vo.common.ResultCodeEnum;
|
||||||
|
import cn.bunny.web.service.CartService;
|
||||||
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
|
import jakarta.annotation.Resource;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("api/order/cart")
|
||||||
|
public class CartController {
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private CartService cartService;
|
||||||
|
|
||||||
|
@Operation(summary = "添加购物车")
|
||||||
|
@GetMapping("auth/addToCart/{skuId}/{skuNum}")
|
||||||
|
public Result addToCart(@PathVariable Long skuId,
|
||||||
|
@PathVariable Integer skuNum) {
|
||||||
|
cartService.addToCart(skuId, skuNum);
|
||||||
|
return Result.build(null, ResultCodeEnum.SUCCESS);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,6 @@
|
||||||
|
package cn.bunny.web.service;
|
||||||
|
|
||||||
|
public interface CartService {
|
||||||
|
// 添加购物车
|
||||||
|
void addToCart(Long skuId, Integer skuNum);
|
||||||
|
}
|
|
@ -0,0 +1,63 @@
|
||||||
|
package cn.bunny.web.service.impl;
|
||||||
|
|
||||||
|
import cn.bunny.common.AuthContextUtil;
|
||||||
|
import cn.bunny.common.spzx.model.entity.h5.CartInfo;
|
||||||
|
import cn.bunny.common.spzx.model.entity.product.ProductSku;
|
||||||
|
import cn.bunny.web.service.CartService;
|
||||||
|
import com.alibaba.fastjson.JSON;
|
||||||
|
import jakarta.annotation.Resource;
|
||||||
|
import org.springframework.data.redis.core.RedisTemplate;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class CartServiceImpl implements CartService {
|
||||||
|
@Resource
|
||||||
|
private RedisTemplate redisTemplate;
|
||||||
|
|
||||||
|
// 定义key user:cart:userId
|
||||||
|
private String getCartKey(Long userId) {
|
||||||
|
return "user:cart:" + userId;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 添加购物车
|
||||||
|
@Override
|
||||||
|
public void addToCart(Long skuId, Integer skuNum) {
|
||||||
|
// 1.必须是登录状态,获取当前登录用户id,从ThreadLocal获取用户信息就可以了
|
||||||
|
Long userId = AuthContextUtil.getUserInfo().getId();
|
||||||
|
String cartKey = this.getCartKey(userId);
|
||||||
|
|
||||||
|
// 2.因为购物车放到Redis里面,从Redis里面获取购物车数据,根据key值 + skuId获取(hash类型key+field)
|
||||||
|
// hash类型 key:useId fileId:skuId value:sku信息CartInfo
|
||||||
|
Object cartInfoObj = redisTemplate.opsForHash().get(cartKey, String.valueOf(skuId));
|
||||||
|
|
||||||
|
// 3.如果购物车存在添加商品,把商品数量相加
|
||||||
|
CartInfo cartInfo = null;
|
||||||
|
if (cartInfoObj != null) {
|
||||||
|
cartInfo = JSON.parseObject(cartInfoObj.toString(), CartInfo.class);
|
||||||
|
// 数量相加
|
||||||
|
cartInfo.setSkuNum(cartInfo.getSkuNum() + skuNum);
|
||||||
|
// 购物车商品是选中状态
|
||||||
|
cartInfo.setIsChecked(1);
|
||||||
|
cartInfo.setUpdateTime(new Date());
|
||||||
|
} else {
|
||||||
|
// 4.如果购物车没有商品,直接商品添加购物车(添加到Redis里面)
|
||||||
|
// 远程调用实现:通过nacos + openFeign 实现,根据skuId获取商品sku信息
|
||||||
|
cartInfo = new CartInfo();
|
||||||
|
// 远程调用实现:根据skuId获取商品sku信息
|
||||||
|
ProductSku productSku = null;
|
||||||
|
cartInfo.setCartPrice(productSku.getSalePrice());
|
||||||
|
cartInfo.setSkuNum(skuNum);
|
||||||
|
cartInfo.setSkuId(skuId);
|
||||||
|
cartInfo.setUserId(userId);
|
||||||
|
cartInfo.setImgUrl(productSku.getThumbImg());
|
||||||
|
cartInfo.setSkuName(productSku.getSkuName());
|
||||||
|
cartInfo.setIsChecked(1);
|
||||||
|
cartInfo.setCreateTime(new Date());
|
||||||
|
cartInfo.setUpdateTime(new Date());
|
||||||
|
}
|
||||||
|
|
||||||
|
redisTemplate.opsForHash().put(cartKey, String.valueOf(skuId), JSON.toJSONString(cartInfo));
|
||||||
|
}
|
||||||
|
}
|
|
@ -11,4 +11,8 @@ spring:
|
||||||
data:
|
data:
|
||||||
redis:
|
redis:
|
||||||
host: 192.168.2.82
|
host: 192.168.2.82
|
||||||
|
# host: 192.168.31.202
|
||||||
port: 6379
|
port: 6379
|
||||||
|
mvc:
|
||||||
|
pathmatch:
|
||||||
|
matching-strategy: ant_path_matcher
|
|
@ -8,6 +8,7 @@ spring:
|
||||||
nacos:
|
nacos:
|
||||||
discovery:
|
discovery:
|
||||||
server-addr: 192.168.2.82:8848
|
server-addr: 192.168.2.82:8848
|
||||||
|
# server-addr: 192.168.31.202:8848
|
||||||
datasource:
|
datasource:
|
||||||
type: com.zaxxer.hikari.HikariDataSource
|
type: com.zaxxer.hikari.HikariDataSource
|
||||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||||
|
@ -18,6 +19,7 @@ spring:
|
||||||
redis:
|
redis:
|
||||||
port: 6379
|
port: 6379
|
||||||
host: 192.168.2.82
|
host: 192.168.2.82
|
||||||
|
# host: 192.168.31.202
|
||||||
|
|
||||||
mybatis:
|
mybatis:
|
||||||
config-location: classpath:mybatis-config.xml
|
config-location: classpath:mybatis-config.xml
|
||||||
|
|
|
@ -1,13 +1,13 @@
|
||||||
package cn.bunny.user;
|
package cn.bunny.user;
|
||||||
|
|
||||||
import cn.bunny.anno.EnableUserLoginAuthInterceptor;
|
import cn.bunny.anno.EnableUserWebMvcConfiguration;
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
import org.springframework.context.annotation.ComponentScan;
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
|
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
@ComponentScan(basePackages = {"cn.bunny"})
|
@ComponentScan(basePackages = {"cn.bunny"})
|
||||||
@EnableUserLoginAuthInterceptor
|
@EnableUserWebMvcConfiguration
|
||||||
public class User {
|
public class User {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
SpringApplication.run(User.class, args);
|
SpringApplication.run(User.class, args);
|
||||||
|
|
|
@ -14,7 +14,7 @@ import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
@Tag(name = "会员用户接口")
|
@Tag(name = "会员用户接口")
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("api/user/userInfo")
|
@RequestMapping("/api/user/userInfo")
|
||||||
public class UserinfoController {
|
public class UserinfoController {
|
||||||
@Resource
|
@Resource
|
||||||
private UserinfoService userinfoService;
|
private UserinfoService userinfoService;
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
package cn.bunny.user.service.impl;
|
package cn.bunny.user.service.impl;
|
||||||
|
|
||||||
|
import cn.bunny.common.AuthContextUtil;
|
||||||
import cn.bunny.common.exception.BunnyException;
|
import cn.bunny.common.exception.BunnyException;
|
||||||
import cn.bunny.common.spzx.model.dto.h5.UserLoginDto;
|
import cn.bunny.common.spzx.model.dto.h5.UserLoginDto;
|
||||||
import cn.bunny.common.spzx.model.dto.h5.UserRegisterDto;
|
import cn.bunny.common.spzx.model.dto.h5.UserRegisterDto;
|
||||||
|
@ -14,7 +15,6 @@ import org.springframework.beans.BeanUtils;
|
||||||
import org.springframework.data.redis.core.RedisTemplate;
|
import org.springframework.data.redis.core.RedisTemplate;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.util.DigestUtils;
|
import org.springframework.util.DigestUtils;
|
||||||
import org.springframework.util.StringUtils;
|
|
||||||
|
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
@ -29,7 +29,7 @@ public class UserinfoServiceImpl implements UserinfoService {
|
||||||
// 获取当前登录用户信息
|
// 获取当前登录用户信息
|
||||||
@Override
|
@Override
|
||||||
public UserInfoVo getCurrentUserInfo(String token) {
|
public UserInfoVo getCurrentUserInfo(String token) {
|
||||||
// 从Redis根据token获取用户信息
|
/* // 从Redis根据token获取用户信息
|
||||||
String userJson = (String) redisTemplate.opsForValue().get("user:spzx" + token);
|
String userJson = (String) redisTemplate.opsForValue().get("user:spzx" + token);
|
||||||
if (!StringUtils.hasText(userJson)) {
|
if (!StringUtils.hasText(userJson)) {
|
||||||
throw new BunnyException(ResultCodeEnum.LOGIN_ERROR);
|
throw new BunnyException(ResultCodeEnum.LOGIN_ERROR);
|
||||||
|
@ -40,6 +40,10 @@ public class UserinfoServiceImpl implements UserinfoService {
|
||||||
UserInfoVo userInfoVo = new UserInfoVo();
|
UserInfoVo userInfoVo = new UserInfoVo();
|
||||||
BeanUtils.copyProperties(userinfo, userInfoVo);
|
BeanUtils.copyProperties(userinfo, userInfoVo);
|
||||||
|
|
||||||
|
return userInfoVo; */
|
||||||
|
UserInfo userInfo = AuthContextUtil.getUserInfo();
|
||||||
|
UserInfoVo userInfoVo = new UserInfoVo();
|
||||||
|
BeanUtils.copyProperties(userInfo, userInfoVo);
|
||||||
return userInfoVo;
|
return userInfoVo;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -8,6 +8,7 @@ spring:
|
||||||
nacos:
|
nacos:
|
||||||
discovery:
|
discovery:
|
||||||
server-addr: 192.168.2.82:8848
|
server-addr: 192.168.2.82:8848
|
||||||
|
# server-addr: 192.168.31.202:8848
|
||||||
|
|
||||||
|
|
||||||
datasource:
|
datasource:
|
||||||
|
@ -20,6 +21,7 @@ spring:
|
||||||
redis:
|
redis:
|
||||||
port: 6379
|
port: 6379
|
||||||
host: 192.168.2.82
|
host: 192.168.2.82
|
||||||
|
# host: 192.168.31.202
|
||||||
|
|
||||||
mybatis:
|
mybatis:
|
||||||
config-location: classpath:mybatis-config.xml
|
config-location: classpath:mybatis-config.xml
|
||||||
|
|
Loading…
Reference in New Issue