mirror of
https://codeup.aliyun.com/64f7d6b8ce01efaafef1e678/coal/coal.git
synced 2026-01-25 07:46:40 +08:00
feat: 商品搜索接口
This commit is contained in:
7
pom.xml
7
pom.xml
@@ -437,6 +437,13 @@
|
||||
</dependency>
|
||||
|
||||
|
||||
<dependency>
|
||||
<groupId>com.aliyun.api.gateway</groupId>
|
||||
<artifactId>sdk-core-java</artifactId>
|
||||
<version>1.1.7</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
<!-- <dependency>-->
|
||||
<!-- <groupId>com.hazelcast</groupId>-->
|
||||
<!-- <artifactId>hazelcast</artifactId>-->
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
package cn.lihongjie.coal.goodsSearch.controller;
|
||||
|
||||
import cn.lihongjie.coal.goodsSearch.dto.GoodsSearchRequest;
|
||||
import cn.lihongjie.coal.goodsSearch.dto.GoodsSearchResponse;
|
||||
import cn.lihongjie.coal.goodsSearch.service.GoodsSearchService;
|
||||
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
|
||||
@RequestMapping("/goodsSearch")
|
||||
@org.springframework.web.bind.annotation.RestController
|
||||
public class GoodsSearchController {
|
||||
|
||||
|
||||
private final GoodsSearchService goodsSearchService;
|
||||
|
||||
public GoodsSearchController(GoodsSearchService goodsSearchService) {
|
||||
this.goodsSearchService = goodsSearchService;
|
||||
}
|
||||
|
||||
@RequestMapping("/search")
|
||||
public GoodsSearchResponse search(GoodsSearchRequest request) {
|
||||
|
||||
|
||||
return
|
||||
goodsSearchService.search(request);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package cn.lihongjie.coal.goodsSearch.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
@Data
|
||||
public class GoodsSearchRequest {
|
||||
|
||||
|
||||
private String code;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package cn.lihongjie.coal.goodsSearch.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
@Data
|
||||
public class GoodsSearchResponse {
|
||||
|
||||
/**
|
||||
* {
|
||||
* "flag": "true",
|
||||
* "remark": "查询成功!",
|
||||
* "code": "6938166920785", //条形码
|
||||
* "goodsName": "苹果醋",//商品名称
|
||||
* "manuName": "新乡市和丝露饮品有限公司", //厂商
|
||||
* "spec": "268ml", //规格
|
||||
* "price": "3.00", //参考价格(单位:元)
|
||||
* "trademark": "醋美人生", //商标/品牌名称
|
||||
*
|
||||
* "img": "http://app2.showapi.com/img/barCode_img/2f7e639b-aa2f-4248-ae79-f0acc6ea56e6.jpg", //图片地址
|
||||
* "ret_code": "0",
|
||||
* "goodsType": "食品、饮料和烟草>>饮料", //商品分类
|
||||
* "sptmImg": "http://app2.showapi.com/img/barCode_img/20161116/14792662xxxxxxx.png", //条码图片
|
||||
* "ycg": "中国", //原产地(可能无此参数信息)
|
||||
* "engName": "cumeirensheng", //
|
||||
* "note": "" //备注信息
|
||||
* }
|
||||
*/
|
||||
|
||||
private String flag;
|
||||
|
||||
private String remark;
|
||||
|
||||
private String code;
|
||||
|
||||
private String goodsName;
|
||||
|
||||
private String manuName;
|
||||
|
||||
private String spec;
|
||||
|
||||
private String price;
|
||||
|
||||
private String trademark;
|
||||
|
||||
private String img;
|
||||
|
||||
private String goodsType;
|
||||
|
||||
private String sptmImg;
|
||||
|
||||
private String ycg;
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
package cn.lihongjie.coal.goodsSearch.service;
|
||||
|
||||
import cn.lihongjie.coal.goodsSearch.dto.GoodsSearchRequest;
|
||||
import cn.lihongjie.coal.goodsSearch.dto.GoodsSearchResponse;
|
||||
|
||||
import com.alibaba.cloudapi.sdk.client.ApacheHttpClient;
|
||||
import com.alibaba.cloudapi.sdk.enums.HttpMethod;
|
||||
import com.alibaba.cloudapi.sdk.enums.ParamPosition;
|
||||
import com.alibaba.cloudapi.sdk.enums.Scheme;
|
||||
import com.alibaba.cloudapi.sdk.model.ApiCallback;
|
||||
import com.alibaba.cloudapi.sdk.model.ApiRequest;
|
||||
import com.alibaba.cloudapi.sdk.model.ApiResponse;
|
||||
import com.alibaba.cloudapi.sdk.model.HttpClientBuilderParams;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
|
||||
import jakarta.annotation.PostConstruct;
|
||||
|
||||
import lombok.SneakyThrows;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
@Slf4j
|
||||
public class GoodsSearchService {
|
||||
|
||||
@Autowired ObjectMapper objectMapper;
|
||||
@Value("${goods.search.ak}")
|
||||
private String ak;
|
||||
@Value("${goods.search.sk}")
|
||||
private String sk;
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
|
||||
HttpClientBuilderParams httpParam = new HttpClientBuilderParams();
|
||||
httpParam.setAppKey(ak);
|
||||
httpParam.setAppSecret(sk);
|
||||
|
||||
Client.getInstance().init(httpParam);
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
public GoodsSearchResponse search(GoodsSearchRequest request) {
|
||||
|
||||
ApiResponse response = Client.getInstance().search(request.getCode());
|
||||
|
||||
String bodyStr = response.getBodyStr();
|
||||
|
||||
ObjectNode jsonNodes = objectMapper.readValue(bodyStr, ObjectNode.class);
|
||||
|
||||
return objectMapper.convertValue(
|
||||
jsonNodes.get("showapi_res_body"), GoodsSearchResponse.class);
|
||||
}
|
||||
|
||||
public static class Client extends ApacheHttpClient {
|
||||
public static final String HOST = "ali-barcode.showapi.com";
|
||||
static Client instance = new Client();
|
||||
|
||||
public static Client getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
public void init(HttpClientBuilderParams httpClientBuilderParams) {
|
||||
httpClientBuilderParams.setScheme(Scheme.HTTPS);
|
||||
httpClientBuilderParams.setHost(HOST);
|
||||
super.init(httpClientBuilderParams);
|
||||
}
|
||||
|
||||
public void search(String code, ApiCallback callback) {
|
||||
String path = "/barcode";
|
||||
ApiRequest request = new ApiRequest(HttpMethod.GET, path);
|
||||
request.addParam("code", code, ParamPosition.QUERY, true);
|
||||
|
||||
sendAsyncRequest(request, callback);
|
||||
}
|
||||
|
||||
public ApiResponse search(String code) {
|
||||
String path = "/barcode";
|
||||
ApiRequest request = new ApiRequest(HttpMethod.GET, path);
|
||||
request.addParam("code", code, ParamPosition.QUERY, true);
|
||||
|
||||
return sendSyncRequest(request);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -342,3 +342,8 @@ aliyun:
|
||||
dust:
|
||||
url: "http://dust.0531yun.cn/"
|
||||
|
||||
|
||||
goods:
|
||||
search:
|
||||
ak: 24805627
|
||||
sk: b787218aacfea062e2287c2cdfe41511
|
||||
Reference in New Issue
Block a user