From 1149fb6ce2c60e2b8286d16009cb0e0930d18692 Mon Sep 17 00:00:00 2001 From: zhangxiangliang Date: Thu, 30 Jul 2020 02:36:46 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BF=AE=E6=94=B9=20=E9=A1=B9=E7=9B=AE?= =?UTF-8?q?=E7=BB=93=E6=9E=84=E3=80=81=E4=BF=AE=E6=94=B9=20=E8=82=A1?= =?UTF-8?q?=E7=A5=A8=E4=BB=A3=E7=A0=81=E8=BD=AC=E6=8D=A2=E5=99=A8=E8=A7=84?= =?UTF-8?q?=E5=88=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/stocks/base/api.ts | 30 -------- src/stocks/base/index.ts | 30 +++++++- .../exchange.ts} | 0 .../{dataTransform.ts => transforms/stock.ts} | 4 +- src/stocks/netease/api.ts | 62 ---------------- src/stocks/netease/index.ts | 60 +++++++++++++++- .../exchange.ts} | 2 +- .../{dataTransform.ts => transforms/stock.ts} | 6 +- src/stocks/sina/api.ts | 72 ------------------- src/stocks/sina/index.ts | 70 +++++++++++++++++- .../exchange.ts} | 2 +- .../{dataTransform.ts => transforms/stock.ts} | 6 +- src/stocks/tencent/api.ts | 71 ------------------ src/stocks/tencent/index.ts | 69 +++++++++++++++++- .../exchange.ts} | 2 +- .../{dataTransform.ts => transforms/stock.ts} | 6 +- 16 files changed, 238 insertions(+), 254 deletions(-) delete mode 100644 src/stocks/base/api.ts rename src/stocks/base/{exchangeTransform.ts => transforms/exchange.ts} (100%) rename src/stocks/base/{dataTransform.ts => transforms/stock.ts} (94%) delete mode 100644 src/stocks/netease/api.ts rename src/stocks/netease/{exchangeTransform.ts => transforms/exchange.ts} (91%) rename src/stocks/netease/{dataTransform.ts => transforms/stock.ts} (88%) delete mode 100644 src/stocks/sina/api.ts rename src/stocks/sina/{exchangeTransform.ts => transforms/exchange.ts} (96%) rename src/stocks/sina/{dataTransform.ts => transforms/stock.ts} (94%) delete mode 100644 src/stocks/tencent/api.ts rename src/stocks/tencent/{exchangeTransform.ts => transforms/exchange.ts} (96%) rename src/stocks/tencent/{dataTransform.ts => transforms/stock.ts} (88%) diff --git a/src/stocks/base/api.ts b/src/stocks/base/api.ts deleted file mode 100644 index 9dcf58c..0000000 --- a/src/stocks/base/api.ts +++ /dev/null @@ -1,30 +0,0 @@ -// Types -import Stock from "types/stock"; - -/** - * 基础股票代码接口 - */ -class Base { - /** - * 构造函数 - */ - constructor() { } - - /** - * 获取股票数据 - * @param code 需要获取的股票代码 - */ - async getStock(code: string): Promise { - throw new Error("未实现获取股票数据"); - } - - /** - * 获取股票组数据 - * @param codes 需要获取的股票组代码 - */ - async getStocks(codes: string[]): Promise { - throw new Error("未实现获取股票组数据"); - } -} - -export default Base; diff --git a/src/stocks/base/index.ts b/src/stocks/base/index.ts index cee6026..9dcf58c 100644 --- a/src/stocks/base/index.ts +++ b/src/stocks/base/index.ts @@ -1,4 +1,30 @@ -// Stocks -import Base from "stocks/base/api"; +// Types +import Stock from "types/stock"; + +/** + * 基础股票代码接口 + */ +class Base { + /** + * 构造函数 + */ + constructor() { } + + /** + * 获取股票数据 + * @param code 需要获取的股票代码 + */ + async getStock(code: string): Promise { + throw new Error("未实现获取股票数据"); + } + + /** + * 获取股票组数据 + * @param codes 需要获取的股票组代码 + */ + async getStocks(codes: string[]): Promise { + throw new Error("未实现获取股票组数据"); + } +} export default Base; diff --git a/src/stocks/base/exchangeTransform.ts b/src/stocks/base/transforms/exchange.ts similarity index 100% rename from src/stocks/base/exchangeTransform.ts rename to src/stocks/base/transforms/exchange.ts diff --git a/src/stocks/base/dataTransform.ts b/src/stocks/base/transforms/stock.ts similarity index 94% rename from src/stocks/base/dataTransform.ts rename to src/stocks/base/transforms/stock.ts index 7838586..1b1e5a1 100644 --- a/src/stocks/base/dataTransform.ts +++ b/src/stocks/base/transforms/stock.ts @@ -4,7 +4,7 @@ import Stock from "types/stock"; /** * 基础股票数据解析 */ -class BaseDataTransform { +class BaseStockTransform { /** * 获取代码 */ @@ -71,4 +71,4 @@ class BaseDataTransform { } } -export default BaseDataTransform; +export default BaseStockTransform; diff --git a/src/stocks/netease/api.ts b/src/stocks/netease/api.ts deleted file mode 100644 index 7de35d0..0000000 --- a/src/stocks/netease/api.ts +++ /dev/null @@ -1,62 +0,0 @@ -// Stocks -import Base from "@stocks/base/api"; -import NeteaseDataTransform from "@stocks/netease/dataTransform"; -import NeteaseExchangeTransform from "@stocks/netease/exchangeTransform"; - -// Utils -import fetch from "@utils/fetch"; - -// Types -import Stock from "types/stock"; - -/** - * 网易股票代码接口 - */ -class Netease extends Base { - /** - * 构造函数 - */ - constructor() { - super(); - } - - /** - * 获取股票数据 - * @param code 需要获取的股票代码 - */ - async getStock(code: string): Promise { - const transform = (new NeteaseExchangeTransform).transform(code); - - const url = `https://api.money.126.net/data/feed/${transform},money.api?callback=topstock`; - const res = await fetch.get(url); - - const items = JSON.parse(res.body.toString().replace(/\(|\)|;|(topstock)/g, '')); - const params = items[transform]; - const data = (new NeteaseDataTransform(code, params)); - - return data.getStock(); - } - - /** - * 获取股票组数据 - * @param codes 需要获取的股票组代码 - */ - async getStocks(codes: string[]): Promise { - const transforms = (new NeteaseExchangeTransform).transforms(codes); - - const url = `https://api.money.126.net/data/feed/${transforms.join(',')},money.api?callback=topstock`; - const res = await fetch.get(url); - - const items = JSON.parse(res.body.toString().replace(/\(|\)|;|(topstock)/g, '')); - - return codes.map(code => { - const transform = (new NeteaseExchangeTransform).transform(code); - const params = items[transform]; - const data = (new NeteaseDataTransform(code, params)); - - return data.getStock(); - }); - } -} - -export default Netease; diff --git a/src/stocks/netease/index.ts b/src/stocks/netease/index.ts index f79e9f9..9fc0461 100644 --- a/src/stocks/netease/index.ts +++ b/src/stocks/netease/index.ts @@ -1,4 +1,62 @@ // Stocks -import Netease from "@stocks/netease/api"; +import Base from "@stocks/base"; +import NeteaseStockTransform from "@stocks/netease/transforms/stock"; +import NeteaseExchangeTransform from "@stocks/netease/transforms/exchange"; + +// Utils +import fetch from "@utils/fetch"; + +// Types +import Stock from "types/stock"; + +/** + * 网易股票代码接口 + */ +class Netease extends Base { + /** + * 构造函数 + */ + constructor() { + super(); + } + + /** + * 获取股票数据 + * @param code 需要获取的股票代码 + */ + async getStock(code: string): Promise { + const transform = (new NeteaseExchangeTransform).transform(code); + + const url = `https://api.money.126.net/data/feed/${transform},money.api?callback=topstock`; + const res = await fetch.get(url); + + const items = JSON.parse(res.body.toString().replace(/\(|\)|;|(topstock)/g, '')); + const params = items[transform]; + const data = (new NeteaseStockTransform(code, params)); + + return data.getStock(); + } + + /** + * 获取股票组数据 + * @param codes 需要获取的股票组代码 + */ + async getStocks(codes: string[]): Promise { + const transforms = (new NeteaseExchangeTransform).transforms(codes); + + const url = `https://api.money.126.net/data/feed/${transforms.join(',')},money.api?callback=topstock`; + const res = await fetch.get(url); + + const items = JSON.parse(res.body.toString().replace(/\(|\)|;|(topstock)/g, '')); + + return codes.map(code => { + const transform = (new NeteaseExchangeTransform).transform(code); + const params = items[transform]; + const data = (new NeteaseStockTransform(code, params)); + + return data.getStock(); + }); + } +} export default Netease; diff --git a/src/stocks/netease/exchangeTransform.ts b/src/stocks/netease/transforms/exchange.ts similarity index 91% rename from src/stocks/netease/exchangeTransform.ts rename to src/stocks/netease/transforms/exchange.ts index c6dd42e..b6ff2a2 100644 --- a/src/stocks/netease/exchangeTransform.ts +++ b/src/stocks/netease/transforms/exchange.ts @@ -1,5 +1,5 @@ // Stocks -import BaseExchangeTransform from "@stocks/base/exchangeTransform"; +import BaseExchangeTransform from "@stocks/base/transforms/exchange"; // Utils import { SZ, HK, US, SH } from "@utils/constant"; diff --git a/src/stocks/netease/dataTransform.ts b/src/stocks/netease/transforms/stock.ts similarity index 88% rename from src/stocks/netease/dataTransform.ts rename to src/stocks/netease/transforms/stock.ts index ea1fd25..228125d 100644 --- a/src/stocks/netease/dataTransform.ts +++ b/src/stocks/netease/transforms/stock.ts @@ -1,5 +1,5 @@ // Stocks -import BaseDataTransform from "@stocks/base/dataTransform"; +import BaseStockTransform from "@stocks/base/transforms/stock"; // Types import Stock from "types/stock"; @@ -8,7 +8,7 @@ import Dictionary from "types/dictionary"; /** * 网易股票数据解析 */ -class NeteaseDataTransform extends BaseDataTransform { +class NeteaseStockTransform extends BaseStockTransform { /** * 构造函数 */ @@ -82,4 +82,4 @@ class NeteaseDataTransform extends BaseDataTransform { } } -export default NeteaseDataTransform; +export default NeteaseStockTransform; diff --git a/src/stocks/sina/api.ts b/src/stocks/sina/api.ts deleted file mode 100644 index 93e3eb8..0000000 --- a/src/stocks/sina/api.ts +++ /dev/null @@ -1,72 +0,0 @@ -// Stocks -import Base from "@stocks/base/api"; -import SinaDataTransform from "@stocks/sina/dataTransform"; -import SinaExchangeTransform from "@stocks/sina/exchangeTransform"; - -// Utils -import fetch from "@utils/fetch"; -import iconv from "@utils/iconv"; - -// Types -import Stock from "types/stock"; - -/** - * 新浪股票代码接口 - */ -class Sina extends Base { - /** - * 构造函数 - */ - constructor() { - super(); - } - - /** - * 获取股票数据 - * @param code 需要获取的股票代码 - */ - async getStock(code: string): Promise { - const transform = (new SinaExchangeTransform).transform(code); - - // 数据获取 - const url = `https://hq.sinajs.cn/list=${transform}`; - const res = await fetch.get(url); - - const body = iconv.decode(res.body, "gb18030"); - const rows = body.split(";\n"); - const row = rows[0]; - - // 数据深解析 - const [_, paramsUnformat] = row.split('='); - const params = paramsUnformat.replace('"', '').split(","); - const data = (new SinaDataTransform(code, params)); - - return data.getStock(); - } - - /** - * 获取股票组数据 - * @param codes 需要获取的股票组代码 - */ - async getStocks(codes: string[]): Promise { - const transforms = (new SinaExchangeTransform).transforms(codes); - - // 数据获取 - const url = `https://hq.sinajs.cn/list=${transforms.join(',')}`; - const res = await fetch.get(url); - - const body = iconv.decode(res.body, "gb18030"); - const rows = body.split(";\n"); - - return codes.map((code, index) => { - // 数据深解析 - const [_, paramsUnformat] = rows[index].split('='); - const params = paramsUnformat.replace('"', '').split(","); - const data = (new SinaDataTransform(code, params)); - - return data.getStock(); - }) - } -} - -export default Sina; diff --git a/src/stocks/sina/index.ts b/src/stocks/sina/index.ts index b74d705..2795622 100644 --- a/src/stocks/sina/index.ts +++ b/src/stocks/sina/index.ts @@ -1,4 +1,72 @@ // Stocks -import Sina from "@stocks/sina/api"; +import Base from "@stocks/base"; +import SinaStockTransform from "@stocks/sina/transforms/stock"; +import SinaExchangeTransform from "@stocks/sina/transforms/exchange"; + +// Utils +import fetch from "@utils/fetch"; +import iconv from "@utils/iconv"; + +// Types +import Stock from "types/stock"; + +/** + * 新浪股票代码接口 + */ +class Sina extends Base { + /** + * 构造函数 + */ + constructor() { + super(); + } + + /** + * 获取股票数据 + * @param code 需要获取的股票代码 + */ + async getStock(code: string): Promise { + const transform = (new SinaExchangeTransform).transform(code); + + // 数据获取 + const url = `https://hq.sinajs.cn/list=${transform}`; + const res = await fetch.get(url); + + const body = iconv.decode(res.body, "gb18030"); + const rows = body.split(";\n"); + const row = rows[0]; + + // 数据深解析 + const [_, paramsUnformat] = row.split('='); + const params = paramsUnformat.replace('"', '').split(","); + const data = (new SinaStockTransform(code, params)); + + return data.getStock(); + } + + /** + * 获取股票组数据 + * @param codes 需要获取的股票组代码 + */ + async getStocks(codes: string[]): Promise { + const transforms = (new SinaExchangeTransform).transforms(codes); + + // 数据获取 + const url = `https://hq.sinajs.cn/list=${transforms.join(',')}`; + const res = await fetch.get(url); + + const body = iconv.decode(res.body, "gb18030"); + const rows = body.split(";\n"); + + return codes.map((code, index) => { + // 数据深解析 + const [_, paramsUnformat] = rows[index].split('='); + const params = paramsUnformat.replace('"', '').split(","); + const data = (new SinaStockTransform(code, params)); + + return data.getStock(); + }) + } +} export default Sina; diff --git a/src/stocks/sina/exchangeTransform.ts b/src/stocks/sina/transforms/exchange.ts similarity index 96% rename from src/stocks/sina/exchangeTransform.ts rename to src/stocks/sina/transforms/exchange.ts index 86c0cbd..b3ab7ef 100644 --- a/src/stocks/sina/exchangeTransform.ts +++ b/src/stocks/sina/transforms/exchange.ts @@ -1,5 +1,5 @@ // Stocks -import BaseTransform from "@stocks/base/exchangeTransform"; +import BaseTransform from "@stocks/base/transforms/exchange"; // Utils import { SZ, HK, US, SH } from "@utils/constant"; diff --git a/src/stocks/sina/dataTransform.ts b/src/stocks/sina/transforms/stock.ts similarity index 94% rename from src/stocks/sina/dataTransform.ts rename to src/stocks/sina/transforms/stock.ts index bf5f83b..43eb1b7 100644 --- a/src/stocks/sina/dataTransform.ts +++ b/src/stocks/sina/transforms/stock.ts @@ -1,5 +1,5 @@ // Stocks -import BaseDataTransform from "@stocks/base/dataTransform"; +import BaseStockTransform from "@stocks/base/transforms/stock"; // Utils import { SZ, HK, US, SH } from "@utils/constant"; @@ -10,7 +10,7 @@ import Stock from "types/stock"; /** * 新浪股票数据解析 */ -class SinaDataTransform extends BaseDataTransform { +class SinaStockTransform extends BaseStockTransform { /** * 构造函数 */ @@ -139,4 +139,4 @@ class SinaDataTransform extends BaseDataTransform { } } -export default SinaDataTransform; +export default SinaStockTransform; diff --git a/src/stocks/tencent/api.ts b/src/stocks/tencent/api.ts deleted file mode 100644 index f0d5eb3..0000000 --- a/src/stocks/tencent/api.ts +++ /dev/null @@ -1,71 +0,0 @@ -// Stocks -import Base from "@stocks/base/api"; -import TencentDataTransform from "@stocks/tencent/dataTransform"; -import TencentExchangeTransform from "@stocks/tencent/exchangeTransform"; - -// Utils -import fetch from "@utils/fetch"; - -// Types -import Stock from "types/stock"; - -/** - * 腾讯股票代码接口 - */ -class Tencent extends Base { - /** - * 构造函数 - */ - constructor() { - super(); - } - - /** - * 获取股票数据 - * @param code 需要获取的股票代码 - */ - async getStock(code: string): Promise { - const transform = (new TencentExchangeTransform).transform(code); - - // 数据获取 - const url = `https://qt.gtimg.cn/q=${transform}`; - const res = await fetch.get(url); - - const body = res.text; - const rows = body.split(";\n"); - const row = rows[0]; - - // 数据深解析 - const [_, paramsUnformat] = row.split('='); - const params = paramsUnformat.replace('"', '').split("~"); - const data = (new TencentDataTransform(code, params)); - - return data.getStock(); - } - - /** - * 获取股票组数据 - * @param codes 需要获取的股票组代码 - */ - async getStocks(codes: string[]): Promise { - const transforms = (new TencentExchangeTransform).transforms(codes); - - // 数据获取 - const url = `https://qt.gtimg.cn/q=${transforms.join(',')}`; - const res = await fetch.get(url); - - const body = res.text; - const rows = body.split(";\n"); - - return codes.map((code, index) => { - // 数据深解析 - const [_, paramsUnformat] = rows[index].split('='); - const params = paramsUnformat.replace('"', '').split("~"); - const data = (new TencentDataTransform(code, params)); - - return data.getStock(); - }) - } -} - -export default Tencent; diff --git a/src/stocks/tencent/index.ts b/src/stocks/tencent/index.ts index 2467688..2bb1d0f 100644 --- a/src/stocks/tencent/index.ts +++ b/src/stocks/tencent/index.ts @@ -1,4 +1,71 @@ // Stocks -import Tencent from "@stocks/tencent/api"; +import Base from "@stocks/base"; +import TencentStockTransform from "@stocks/tencent/transforms/stock"; +import TencentExchangeTransform from "@stocks/tencent/transforms/exchange"; + +// Utils +import fetch from "@utils/fetch"; + +// Types +import Stock from "types/stock"; + +/** + * 腾讯股票代码接口 + */ +class Tencent extends Base { + /** + * 构造函数 + */ + constructor() { + super(); + } + + /** + * 获取股票数据 + * @param code 需要获取的股票代码 + */ + async getStock(code: string): Promise { + const transform = (new TencentExchangeTransform).transform(code); + + // 数据获取 + const url = `https://qt.gtimg.cn/q=${transform}`; + const res = await fetch.get(url); + + const body = res.text; + const rows = body.split(";\n"); + const row = rows[0]; + + // 数据深解析 + const [_, paramsUnformat] = row.split('='); + const params = paramsUnformat.replace('"', '').split("~"); + const data = (new TencentStockTransform(code, params)); + + return data.getStock(); + } + + /** + * 获取股票组数据 + * @param codes 需要获取的股票组代码 + */ + async getStocks(codes: string[]): Promise { + const transforms = (new TencentExchangeTransform).transforms(codes); + + // 数据获取 + const url = `https://qt.gtimg.cn/q=${transforms.join(',')}`; + const res = await fetch.get(url); + + const body = res.text; + const rows = body.split(";\n"); + + return codes.map((code, index) => { + // 数据深解析 + const [_, paramsUnformat] = rows[index].split('='); + const params = paramsUnformat.replace('"', '').split("~"); + const data = (new TencentStockTransform(code, params)); + + return data.getStock(); + }) + } +} export default Tencent; diff --git a/src/stocks/tencent/exchangeTransform.ts b/src/stocks/tencent/transforms/exchange.ts similarity index 96% rename from src/stocks/tencent/exchangeTransform.ts rename to src/stocks/tencent/transforms/exchange.ts index fdc10d5..ec65e0f 100644 --- a/src/stocks/tencent/exchangeTransform.ts +++ b/src/stocks/tencent/transforms/exchange.ts @@ -1,5 +1,5 @@ // Stocks -import BaseTransform from "@stocks/base/exchangeTransform"; +import BaseTransform from "@stocks/base/transforms/exchange"; // Utils import { SZ, HK, US, SH } from "@utils/constant"; diff --git a/src/stocks/tencent/dataTransform.ts b/src/stocks/tencent/transforms/stock.ts similarity index 88% rename from src/stocks/tencent/dataTransform.ts rename to src/stocks/tencent/transforms/stock.ts index 243cfad..b4f8e5b 100644 --- a/src/stocks/tencent/dataTransform.ts +++ b/src/stocks/tencent/transforms/stock.ts @@ -1,5 +1,5 @@ // Stocks -import BaseDataTransform from "@stocks/base/dataTransform"; +import BaseStockTransform from "@stocks/base/transforms/stock"; // Types import { Stock } from "types/stock"; @@ -7,7 +7,7 @@ import { Stock } from "types/stock"; /** * 腾讯股票数据解析 */ -class TencentDataTransform extends BaseDataTransform { +class TencentStockTransform extends BaseStockTransform { /** * 构造函数 */ @@ -81,4 +81,4 @@ class TencentDataTransform extends BaseDataTransform { } } -export default TencentDataTransform; +export default TencentStockTransform;