| | |
| | | |
| | | import cn.hutool.core.date.DateTime; |
| | | import cn.hutool.core.date.DateUtil; |
| | | import cn.hutool.http.HttpUtil; |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.fasterxml.jackson.core.JsonProcessingException; |
| | | import com.google.gson.Gson; |
| | |
| | | import com.nq.dao.StockMapper; |
| | | import com.nq.dao.StockSettingMapper; |
| | | import com.nq.enums.EConfigKey; |
| | | import com.nq.enums.EStockType; |
| | | 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; |
| | |
| | | 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; |
| | |
| | | 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())); |
| | | } |
| | | } |
| | | } |
| | |
| | | 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; |
| | | } |
| | |
| | | } |
| | | |
| | | public String doGet(String pid){ |
| | | String apiUrl = "http://api-in-2.js-stock.top/stock?pid="+pid+"&key=eVKtHt7aG4m6ozwWL9qG"; |
| | | try { |
| | | URL url = new URL(apiUrl); |
| | | HttpURLConnection connection = (HttpURLConnection) url.openConnection(); |
| | | connection.setRequestMethod("GET"); |
| | | String apiUrl = PropertiesUtil.getProperty("JS_IN_HTTP_URL") + "stock?key=" + PropertiesUtil.getProperty("JS_IN_KEY"); |
| | | String result = null; |
| | | |
| | | BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); |
| | | String inputLine; |
| | | StringBuffer response = new StringBuffer(); |
| | | try { |
| | | // 添加请求间隔,避免频率过高 |
| | | Thread.sleep(1000); // 等待1秒再请求 |
| | | } catch (InterruptedException e) { |
| | | Thread.currentThread().interrupt(); |
| | | } |
| | | |
| | | while ((inputLine = in.readLine()) != null) { |
| | | response.append(inputLine); |
| | | try { |
| | | URL url = new URL(apiUrl); |
| | | HttpURLConnection connection = (HttpURLConnection) url.openConnection(); |
| | | |
| | | // 设置超时时间 |
| | | connection.setConnectTimeout(10000); |
| | | connection.setReadTimeout(10000); |
| | | connection.setRequestMethod("POST"); |
| | | connection.setRequestProperty("Content-Type", "application/json; charset=utf-8"); |
| | | connection.setDoOutput(true); |
| | | |
| | | // JSON格式请求体 |
| | | String jsonInputString = "{\"pid\":\"" + pid + "\"}"; |
| | | |
| | | try (OutputStream os = connection.getOutputStream()) { |
| | | byte[] input = jsonInputString.getBytes("utf-8"); |
| | | os.write(input, 0, input.length); |
| | | } |
| | | in.close(); |
| | | return response.toString(); |
| | | } catch (Exception e) { |
| | | |
| | | // 检查HTTP响应码 |
| | | int responseCode = connection.getResponseCode(); |
| | | if (responseCode == 429) { |
| | | // 如果还是429,等待更长时间后重试 |
| | | Thread.sleep(3000); |
| | | return doGet(pid); // 递归重试(注意设置最大重试次数) |
| | | } |
| | | |
| | | // 读取响应 |
| | | try (BufferedReader in = new BufferedReader( |
| | | new InputStreamReader(connection.getInputStream()))) { |
| | | String inputLine; |
| | | StringBuilder response = new StringBuilder(); |
| | | while ((inputLine = in.readLine()) != null) { |
| | | response.append(inputLine); |
| | | } |
| | | result = response.toString(); |
| | | } |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return null; |
| | | return result; |
| | | } |
| | | |
| | | @Override |