初始化
parent
2c52d0762a
commit
e4c0ed2b3d
|
@ -1,6 +1,11 @@
|
|||
package com.ysy.auth.controller;
|
||||
|
||||
import com.ysy.auth.service.AuthService;
|
||||
import com.ysy.common.domain.result.Result;
|
||||
import com.ysy.common.domain.vo.UserVO;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
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;
|
||||
|
||||
|
@ -12,4 +17,10 @@ public class AuthController {
|
|||
public AuthController(AuthService authService) {
|
||||
this.authService = authService;
|
||||
}
|
||||
|
||||
@PostMapping("/login")
|
||||
public Result<String> login(@RequestBody @Validated UserVO userVO) {
|
||||
String token = authService.loginByUserVO(userVO);
|
||||
return Result.success("登录成功", token);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,4 +1,7 @@
|
|||
package com.ysy.auth.service;
|
||||
|
||||
import com.ysy.common.domain.vo.UserVO;
|
||||
|
||||
public interface AuthService {
|
||||
String loginByUserVO(UserVO userVO);
|
||||
}
|
||||
|
|
|
@ -1,8 +1,37 @@
|
|||
package com.ysy.auth.service.impl;
|
||||
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import com.ysy.auth.service.AuthService;
|
||||
import com.ysy.common.domain.po.User;
|
||||
import com.ysy.common.domain.vo.UserVO;
|
||||
import com.ysy.common.remote.SystemServiceClient;
|
||||
import com.ysy.common.utils.jwt.JwtUtil;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@Service
|
||||
public class AuthServiceImpl implements AuthService {
|
||||
@Autowired
|
||||
private SystemServiceClient systemServiceClient;
|
||||
|
||||
@Autowired
|
||||
private StringRedisTemplate stringRedisTemplate;
|
||||
|
||||
@Override
|
||||
public String loginByUserVO(UserVO userVO) {
|
||||
User user = systemServiceClient.getByPhone(userVO.getPhone()).getData();
|
||||
Assert.notNull(user, "该账户不存在");
|
||||
boolean equals = user.getPassword().equals(userVO.getPassword());
|
||||
Assert.isTrue(equals, "密码错误");
|
||||
|
||||
String token = JwtUtil.createToken(BeanUtil.beanToMap(user, new HashMap<>(), false, false));
|
||||
stringRedisTemplate.opsForValue().set(user.getUserId().toString(), JSON.toJSONString(user), 20, TimeUnit.MINUTES);
|
||||
return token;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,19 @@
|
|||
package com.ysy.common.domain.dto;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Builder
|
||||
public class GoodDTO {
|
||||
private String goodNum;
|
||||
private String goodName;
|
||||
private String brand;
|
||||
private String info;
|
||||
private Integer price;
|
||||
private Boolean isSecured;
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
package com.ysy.common.domain.po;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Builder
|
||||
@TableName("tb_cart")
|
||||
public class Cart {
|
||||
@TableId(value = "cart_id", type = IdType.AUTO)
|
||||
private Long cartId;
|
||||
@TableField("good_id")
|
||||
private Long goodId;
|
||||
@TableField("number")
|
||||
private Integer number;
|
||||
@TableField("is_deleted")
|
||||
private Boolean isDeleted;
|
||||
@TableField("user_id")
|
||||
private Long userId;
|
||||
|
||||
@TableField(exist = false)
|
||||
private Good good;
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
package com.ysy.common.domain.po;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Builder
|
||||
@TableName("tb_good")
|
||||
public class Good {
|
||||
@TableId(value = "good_id", type = IdType.AUTO)
|
||||
private Long goodId;
|
||||
@TableField("good_num")
|
||||
private String goodNum;
|
||||
@TableField("good_name")
|
||||
private String goodName;
|
||||
private String brand;
|
||||
private String info;
|
||||
private Integer price;
|
||||
@TableField("is_secured")
|
||||
private Boolean isSecured;
|
||||
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
package com.ysy.common.domain.po;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Builder
|
||||
@TableName("tb_order")
|
||||
public class Order {
|
||||
@TableId(value = "order_id", type = IdType.AUTO)
|
||||
private Long orderId;
|
||||
@TableField("order_num")
|
||||
private String orderNum;
|
||||
@TableField("cart_id")
|
||||
private Long cartId;
|
||||
@TableField("buy_time")
|
||||
private Date buyTime;
|
||||
@TableField("refund_time")
|
||||
private Date refundTime;
|
||||
@TableField("isRefund")
|
||||
private Boolean is_refund;
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
package com.ysy.common.domain.po;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Builder
|
||||
@TableName("tb_user")
|
||||
public class User {
|
||||
@TableId(value = "user_id", type = IdType.AUTO)
|
||||
private Long userId;
|
||||
private String username;
|
||||
private String phone;
|
||||
private String password;
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
package com.ysy.common.domain.vo;
|
||||
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Builder
|
||||
public class GoodVO {
|
||||
private String goodName;
|
||||
private String brand;
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
package com.ysy.common.domain.vo;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Builder
|
||||
public class UserVO {
|
||||
@NotBlank(message = "手机号不能为空")
|
||||
private String phone;
|
||||
@NotBlank(message = "密码不能为空")
|
||||
private String password;
|
||||
}
|
|
@ -16,6 +16,7 @@ public class GlobalExceptionHandler {
|
|||
public Result illegalArgumentExceptionHandler(IllegalArgumentException e) {
|
||||
log.error("服务端报错:[{}]", e.getMessage(), e.getLocalizedMessage(), e);
|
||||
return Result.fail(
|
||||
e.getMessage()
|
||||
// JSON.toJSONString()
|
||||
);
|
||||
}
|
||||
|
@ -23,6 +24,6 @@ public class GlobalExceptionHandler {
|
|||
@ExceptionHandler(MethodArgumentNotValidException.class)
|
||||
public Result methodArgumentNotValidExceptionHandler(MethodArgumentNotValidException e) {
|
||||
log.error("服务端报错:[{}]", e.getMessage(), e.getLocalizedMessage(), e);
|
||||
return Result.fail(JSON.toJSONString(e.getBindingResult()));
|
||||
return Result.fail(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,14 @@
|
|||
package com.ysy.common.remote;
|
||||
|
||||
import com.ysy.common.domain.po.Good;
|
||||
import com.ysy.common.domain.po.User;
|
||||
import com.ysy.common.domain.result.Result;
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
|
||||
@FeignClient(value = "goodService", path = "good")
|
||||
public interface GoodServiceClient {
|
||||
@GetMapping("/get/{id}")
|
||||
public Result<Good> getById(@PathVariable String id);
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
package com.ysy.common.remote;
|
||||
|
||||
import com.ysy.common.domain.po.User;
|
||||
import com.ysy.common.domain.result.Result;
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
|
||||
@FeignClient(value = "systemService", path = "user")
|
||||
public interface SystemServiceClient {
|
||||
@GetMapping("/get/{phone}")
|
||||
public Result<User> getByPhone(@PathVariable String phone);
|
||||
}
|
|
@ -0,0 +1,73 @@
|
|||
package com.ysy.gateway.filter;
|
||||
|
||||
import com.auth0.jwt.interfaces.Claim;
|
||||
import com.ysy.common.utils.gateway.GatewayUtils;
|
||||
import com.ysy.common.utils.jwt.JwtUtil;
|
||||
import com.ysy.common.utils.tools.StringUtils;
|
||||
import com.ysy.gateway.white.AuthProperties;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
|
||||
import org.springframework.cloud.gateway.filter.GlobalFilter;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.server.reactive.ServerHttpRequest;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@Component
|
||||
@Order(1)
|
||||
@Slf4j
|
||||
public class GlobalAuthFilter implements GlobalFilter {
|
||||
@Autowired
|
||||
private AuthProperties authProperties;
|
||||
|
||||
@Autowired
|
||||
private StringRedisTemplate stringRedisTemplate;
|
||||
|
||||
@Override
|
||||
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
|
||||
ServerHttpRequest request = exchange.getRequest();
|
||||
String path = request.getPath().toString();
|
||||
HttpMethod method = request.getMethod();
|
||||
|
||||
log.info("请求方式:{},请求路径:{}", method, path);
|
||||
|
||||
boolean matches = StringUtils.matches(path, authProperties.getExcludePaths());
|
||||
|
||||
if (matches) {
|
||||
log.info("请求白名单:{}", request.getPath());
|
||||
return chain.filter(exchange);
|
||||
}
|
||||
|
||||
String token = request.getHeaders().get("token").get(0);
|
||||
Claim claim = null;
|
||||
try {
|
||||
claim = JwtUtil.getClaim(token);
|
||||
} catch (Exception e) {
|
||||
return GatewayUtils.errorResponse(exchange, "令牌异常,请重新登录");
|
||||
}
|
||||
|
||||
Map<String, Object> map = claim.asMap();
|
||||
Integer userId = (Integer) map.get("userId");
|
||||
Object username = map.get("username");
|
||||
Object phone = map.get("phone");
|
||||
|
||||
if (Boolean.FALSE.equals(stringRedisTemplate.hasKey(String.valueOf(userId)))) {
|
||||
return GatewayUtils.errorResponse(exchange, "令牌已过期,请重新登录");
|
||||
}
|
||||
|
||||
ServerHttpRequest.Builder mutate = request.mutate();
|
||||
GatewayUtils.removeHeader(mutate, "token");
|
||||
|
||||
GatewayUtils.addHeader(mutate, "userId", userId);
|
||||
GatewayUtils.addHeader(mutate, "username", username);
|
||||
GatewayUtils.addHeader(mutate, "phone", phone);
|
||||
|
||||
return chain.filter(exchange);
|
||||
}
|
||||
}
|
|
@ -10,6 +10,11 @@
|
|||
</parent>
|
||||
|
||||
<artifactId>ysy-modules</artifactId>
|
||||
<packaging>pom</packaging>
|
||||
<modules>
|
||||
<module>ysy-good</module>
|
||||
<module>ysy-order</module>
|
||||
</modules>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
|
|
|
@ -0,0 +1,41 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
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>
|
||||
<parent>
|
||||
<groupId>com.ysy</groupId>
|
||||
<artifactId>ysy-modules</artifactId>
|
||||
<version>3.0.2</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>ysy-good</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<!-- 自定义公共模块 -->
|
||||
<dependency>
|
||||
<groupId>com.ysy</groupId>
|
||||
<artifactId>ysy-common</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- lombok -->
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<!-- test -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
|
@ -0,0 +1,44 @@
|
|||
package com.ysy.good;
|
||||
|
||||
import co.elastic.clients.elasticsearch.ElasticsearchClient;
|
||||
import co.elastic.clients.elasticsearch.core.BulkRequest;
|
||||
import co.elastic.clients.elasticsearch.core.bulk.BulkOperation;
|
||||
import com.ysy.common.domain.po.Good;
|
||||
import com.ysy.good.mapper.GoodMapper;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.ApplicationArguments;
|
||||
import org.springframework.boot.ApplicationRunner;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@SpringBootApplication
|
||||
public class GoodApplication implements ApplicationRunner {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(GoodApplication.class);
|
||||
}
|
||||
|
||||
@Autowired
|
||||
private ElasticsearchClient elasticsearchClient;
|
||||
|
||||
@Autowired
|
||||
private GoodMapper goodMapper;
|
||||
|
||||
@Override
|
||||
public void run(ApplicationArguments args) throws Exception {
|
||||
List<Good> goodList = goodMapper.selectList(null);
|
||||
List<BulkOperation> bulkOperations = new ArrayList<>();
|
||||
|
||||
goodList.forEach(good -> bulkOperations.add(BulkOperation.of(builder -> builder
|
||||
.create(c -> c
|
||||
.id(String.valueOf(good.getGoodId()))
|
||||
.document(good)
|
||||
)))
|
||||
);
|
||||
|
||||
BulkRequest bulkRequest = new BulkRequest.Builder().operations(bulkOperations).index("good_index").build();
|
||||
elasticsearchClient.bulk(bulkRequest);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,56 @@
|
|||
package com.ysy.good.controller;
|
||||
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import co.elastic.clients.elasticsearch.ElasticsearchClient;
|
||||
import co.elastic.clients.elasticsearch.core.CreateResponse;
|
||||
import com.ysy.common.domain.dto.GoodDTO;
|
||||
import com.ysy.common.domain.po.Good;
|
||||
import com.ysy.common.domain.result.Result;
|
||||
import com.ysy.common.domain.vo.GoodVO;
|
||||
import com.ysy.good.service.GoodService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/good")
|
||||
@Slf4j
|
||||
public class GoodController {
|
||||
@Autowired
|
||||
private GoodService goodService;
|
||||
|
||||
@Autowired
|
||||
private ElasticsearchClient elasticsearchClient;
|
||||
|
||||
@PostMapping("/list")
|
||||
public Result<List<Good>> list(@RequestBody GoodVO goodVO) {
|
||||
List<Good> goodList = goodService.getListByGoodVO(goodVO);
|
||||
return Result.success(goodList);
|
||||
}
|
||||
|
||||
@PostMapping("/add")
|
||||
public Result<String> add(@RequestBody GoodDTO goodDTO) {
|
||||
Good good = new Good();
|
||||
goodDTO.setGoodNum(new UUID(32, 16).toString());
|
||||
BeanUtil.copyProperties(goodDTO, good);
|
||||
goodService.save(good);
|
||||
CreateResponse createResponse = null;
|
||||
try {
|
||||
createResponse = elasticsearchClient.create(c -> c.id(String.valueOf(good.getGoodId())).document(good).index("good_index"));
|
||||
} catch (IOException e) {
|
||||
log.info("添加es出现错误:{}", createResponse.result());
|
||||
}
|
||||
|
||||
return Result.success("添加商品成功");
|
||||
}
|
||||
|
||||
@GetMapping("/get/{id}")
|
||||
public Result<Good> getById(@PathVariable String id) {
|
||||
Good good = goodService.getById(id);
|
||||
return Result.success(good);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
package com.ysy.good.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.ysy.common.domain.po.Good;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
@Mapper
|
||||
public interface GoodMapper extends BaseMapper<Good> {
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
package com.ysy.good.service;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.ysy.common.domain.po.Good;
|
||||
import com.ysy.common.domain.vo.GoodVO;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface GoodService extends IService<Good> {
|
||||
List<Good> getListByGoodVO(GoodVO goodVO);
|
||||
}
|
|
@ -0,0 +1,71 @@
|
|||
package com.ysy.good.service.impl;
|
||||
|
||||
import co.elastic.clients.elasticsearch.ElasticsearchClient;
|
||||
import co.elastic.clients.elasticsearch._types.query_dsl.Query;
|
||||
import co.elastic.clients.elasticsearch.core.SearchRequest;
|
||||
import co.elastic.clients.elasticsearch.core.SearchResponse;
|
||||
import co.elastic.clients.elasticsearch.core.search.HighlightField;
|
||||
import co.elastic.clients.elasticsearch.core.search.Hit;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.ysy.common.domain.po.Good;
|
||||
import com.ysy.common.domain.vo.GoodVO;
|
||||
import com.ysy.good.mapper.GoodMapper;
|
||||
import com.ysy.good.service.GoodService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
@Slf4j
|
||||
public class GoodServiceImpl extends ServiceImpl<GoodMapper, Good>
|
||||
implements GoodService {
|
||||
@Autowired
|
||||
private ElasticsearchClient elasticsearchClient;
|
||||
|
||||
@Override
|
||||
public List<Good> getListByGoodVO(GoodVO goodVO) {
|
||||
SearchRequest.Builder searchRequestBuilder = new SearchRequest.Builder().index("good_index");
|
||||
Query.Builder queryBuilder = new Query.Builder();
|
||||
List<Query> queryList = new ArrayList<>();
|
||||
|
||||
if (StringUtils.isNotBlank(goodVO.getGoodName())) {
|
||||
Query query = new Query.Builder().match(m -> m.field("goodName").query(goodVO.getGoodName())).build();
|
||||
queryList.add(query);
|
||||
}
|
||||
|
||||
if (StringUtils.isNotBlank(goodVO.getBrand())) {
|
||||
Query query = new Query.Builder().term(t -> t.field("brand").value(goodVO.getBrand())).build();
|
||||
queryList.add(query);
|
||||
}
|
||||
|
||||
Query query = queryBuilder.bool(builder -> builder.must(queryList)).build();
|
||||
SearchRequest searchRequest = searchRequestBuilder.query(query)
|
||||
.highlight(h -> h.fields("goodName", HighlightField.of(f -> f.matchedFields("goodName"))).preTags("<font color='red'>").postTags("</font>"))
|
||||
.build();
|
||||
|
||||
SearchResponse<Good> search = null;
|
||||
try {
|
||||
search = elasticsearchClient.search(searchRequest, Good.class);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
List<Good> goodList = new ArrayList<>();
|
||||
for (Hit<Good> hit : search.hits().hits()) {
|
||||
Good source = hit.source();
|
||||
if (!hit.highlight().isEmpty()) {
|
||||
String highlight = hit.highlight().get("goodName").get(0);
|
||||
source.setGoodName(highlight);
|
||||
}
|
||||
goodList.add(source);
|
||||
}
|
||||
|
||||
log.info("商品列表:" + goodList);
|
||||
return goodList;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
██ ██ ████████ ██ ██
|
||||
░░██ ██ ██░░░░░░ ░░██ ██
|
||||
░░████ ░██ ░░████
|
||||
░░██ ░█████████ ░░██
|
||||
░██ ░░░░░░░░██ ░██
|
||||
░██ ░██ ░██
|
||||
░██ ████████ ░██
|
||||
░░ ░░░░░░░░ ░░
|
||||
:: ysy boot :: version 1.0
|
|
@ -0,0 +1,39 @@
|
|||
server:
|
||||
#服务端口
|
||||
port: 10003
|
||||
|
||||
spring:
|
||||
application:
|
||||
#服务名称
|
||||
name: goodService
|
||||
profiles:
|
||||
#配置文件生效环境
|
||||
active: dev
|
||||
|
||||
cloud:
|
||||
nacos:
|
||||
#nacos服务器地址
|
||||
server-addr: http://124.70.132.13:8848
|
||||
#nacos服务发现配置
|
||||
discovery:
|
||||
#nacos服务注册命名空间ID,默认为public
|
||||
namespace: 2e6a4975-46b3-4035-b77d-92b05ae48a66
|
||||
#cluster-name: SH #服务集群设置
|
||||
config:
|
||||
#指定读取命名空间配置,默认为public
|
||||
namespace: ${spring.cloud.nacos.discovery.namespace}
|
||||
#nacos配置文件类型
|
||||
file-extension: yaml
|
||||
#共享配置
|
||||
shared-configs:
|
||||
- data-id: common.yaml
|
||||
group: SPRING_CLOUD_COMMONS
|
||||
refresh: true
|
||||
|
||||
- data-id: common-mysql.yaml
|
||||
group: SPRING_CLOUD_COMMONS
|
||||
refresh: true
|
||||
|
||||
- data-id: common-redis.yaml
|
||||
group: SPRING_CLOUD_COMMONS
|
||||
refresh: true
|
|
@ -0,0 +1,20 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
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>
|
||||
<parent>
|
||||
<groupId>com.ysy</groupId>
|
||||
<artifactId>ysy-modules</artifactId>
|
||||
<version>3.0.2</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>ysy-order</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,7 @@
|
|||
package com.ysy;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
System.out.println("Hello world!");
|
||||
}
|
||||
}
|
|
@ -1,9 +1,75 @@
|
|||
package com.ysy.system.controller;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.ysy.common.domain.po.Cart;
|
||||
import com.ysy.common.domain.po.Good;
|
||||
import com.ysy.common.domain.po.User;
|
||||
import com.ysy.common.domain.result.Result;
|
||||
import com.ysy.common.remote.GoodServiceClient;
|
||||
import com.ysy.system.mapper.CartMapper;
|
||||
import com.ysy.system.service.UserService;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
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;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/user")
|
||||
public class UserController {
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
|
||||
@Autowired
|
||||
private CartMapper cartMapper;
|
||||
|
||||
@Autowired
|
||||
private HttpServletRequest request;
|
||||
|
||||
@Autowired
|
||||
private GoodServiceClient goodServiceClient;
|
||||
|
||||
@GetMapping("/get/{phone}")
|
||||
public Result<User> getByPhone(@PathVariable String phone) {
|
||||
User user = userService.getOne(
|
||||
new LambdaQueryWrapper<User>()
|
||||
.eq(User::getPhone, phone)
|
||||
);
|
||||
return Result.success(user);
|
||||
}
|
||||
|
||||
@GetMapping("/cart")
|
||||
public Result<List<Cart>> cart() {
|
||||
String userId = request.getHeader("userId");
|
||||
List<Cart> cartList = cartMapper.selectList(new LambdaQueryWrapper<Cart>().eq(Cart::getUserId, userId).eq(Cart::getIsDeleted, false));
|
||||
cartList.forEach(cart -> {
|
||||
Good good = goodServiceClient.getById(String.valueOf(cart.getGoodId())).getData();
|
||||
cart.setGood(good);
|
||||
});
|
||||
return Result.success(cartList);
|
||||
}
|
||||
|
||||
@GetMapping("/cart/{id}")
|
||||
public Result<String> cartById(@PathVariable String id) {
|
||||
Cart cart = cartMapper.selectById(id);
|
||||
cart.setIsDeleted(true);
|
||||
cartMapper.updateById(cart);
|
||||
return Result.success("移除成功");
|
||||
}
|
||||
|
||||
@GetMapping("/cart/add/{id}")
|
||||
public Result<String> cartAddById(@PathVariable String id) {
|
||||
String userId = request.getHeader("userId");
|
||||
Cart build = Cart.builder()
|
||||
.userId(Long.valueOf(userId))
|
||||
.goodId(Long.valueOf(id))
|
||||
.number(1)
|
||||
.isDeleted(false)
|
||||
.build();
|
||||
cartMapper.insert(build);
|
||||
return Result.success("添加购物车成功");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,9 @@
|
|||
package com.ysy.system.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.ysy.common.domain.po.Cart;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
@Mapper
|
||||
public interface CartMapper extends BaseMapper<Cart> {
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
package com.ysy.system.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.ysy.common.domain.po.User;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
@Mapper
|
||||
public interface UserMapper extends BaseMapper<User> {
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
package com.ysy.system.service;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.ysy.common.domain.po.User;
|
||||
|
||||
public interface UserService extends IService<User> {
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
package com.ysy.system.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.ysy.common.domain.po.User;
|
||||
import com.ysy.system.mapper.UserMapper;
|
||||
import com.ysy.system.service.UserService;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
|
||||
implements UserService {
|
||||
}
|
Loading…
Reference in New Issue