1
zj
2025-03-12 d12967b6b0d4a6da6946699aa14d7d008aa79db0
1
1 files modified
50 ■■■■■ changed files
src/main/java/com/nq/service/impl/PriceServicesImpl.java 50 ●●●●● patch | view | raw | blame | history
src/main/java/com/nq/service/impl/PriceServicesImpl.java
@@ -15,6 +15,7 @@
import com.nq.pojo.*;
import com.nq.service.IPriceServices;
import com.nq.service.IStockConfigServices;
import com.nq.utils.PropertiesUtil;
import com.nq.utils.http.HttpClientRequest;
import com.nq.utils.redis.RedisKeyUtil;
import com.nq.utils.timeutil.TimeUtil;
@@ -22,8 +23,7 @@
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.*;
import java.lang.reflect.Type;
import java.math.BigDecimal;
import  java.io.BufferedReader;
@@ -69,7 +69,7 @@
                    String s = doGet(stock.getStockCode());
                    if(null != s){
                        Map<String, Object> stringObjectMap = jsonToMap(s);
                        return   new BigDecimal(stringObjectMap.get("last").toString()).multiply(new BigDecimal(stockSetting.getPrice()));
                        return   new BigDecimal(stringObjectMap.get("Last").toString()).multiply(new BigDecimal(stockSetting.getPrice()));
                    }
                }
            }
@@ -78,7 +78,7 @@
        String s = doGet(stock.getStockCode());
        if(null != s) {
            Map<String, Object> stringObjectMap = jsonToMap(s);
            return  new BigDecimal(stringObjectMap.get("last").toString());
            return  new BigDecimal(stringObjectMap.get("Last").toString());
        }
        return BigDecimal.ZERO;
    }
@@ -110,26 +110,42 @@
    }
    public String doGet(String pid){
        EStockType eStockType = EStockType.getEStockTypeByCode("IN");
        String  apiUrl  =  eStockType.stockUrl+"?pid="+pid+"&key="+eStockType.stockKey;
        try  {
            URL  url  =  new  URL(apiUrl);
            HttpURLConnection  connection  =  (HttpURLConnection)  url.openConnection();
            connection.setRequestMethod("GET");
        // 从配置中获取 API URL,并拼接 key
        String apiUrl = PropertiesUtil.getProperty("JS_IN_HTTP_URL") + "stock?key=" + PropertiesUtil.getProperty("JS_IN_KEY");
        String result = null;
        try {
            URL url = new URL(apiUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            BufferedReader in  =  new  BufferedReader(new InputStreamReader(connection.getInputStream()));
            String  inputLine;
            StringBuffer  response  =  new  StringBuffer();
            // 设置请求方法为 POST
            connection.setRequestMethod("POST");
            // 设置请求头
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            connection.setDoOutput(true); // 允许向连接输出
            while  ((inputLine  =  in.readLine())  !=  null)  {
            // 构建 POST 数据
            String postData = "pid=" + pid;
            // 发送 POST 请求
            try (OutputStream os = connection.getOutputStream()) {
                byte[] input = postData.getBytes("utf-8");
                os.write(input, 0, input.length);
            }
            // 读取响应
            BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String inputLine;
            StringBuffer response = new StringBuffer();
            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();
            return response.toString();
        }  catch  (Exception  e)  {
            result = response.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
        return result;
    }
    @Override