Skip to content

Commit

Permalink
fix: 修复股票搜索,支持中英文
Browse files Browse the repository at this point in the history
  • Loading branch information
giscafer committed Aug 20, 2020
1 parent 8606206 commit f03cdfb
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# ChangeLog

## [1.2.12]

- fix: 修复股票搜索,支持中英文

## [1.2.11]

- chore: 优化界面样式
Expand Down
1 change: 1 addition & 0 deletions demo/pzgznew.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "leek-fund",
"displayName": "韭菜盒子",
"description": "韭菜盒子,VSCode 里也可以看股票 & 基金实时数据,做最好用的养基插件",
"version": "1.2.11",
"version": "1.2.12",
"author": "giscafer <[email protected]>",
"repository": {
"type": "git",
Expand Down
22 changes: 17 additions & 5 deletions src/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export class FundService {
private _fundList: Array<LeekTreeItem> = [];
private context: ExtensionContext;
szItem: any;
searchStockKeyMap: any = {}; // 标记搜索不到记录,避免死循环
constructor(context: ExtensionContext) {
this.context = context;
}
Expand Down Expand Up @@ -92,11 +93,13 @@ export class FundService {
});
}

async getStockSuggestList(searchText = ''): Promise<QuickPickItem[]> {
async getStockSuggestList(searchText = '', type = '2'): Promise<QuickPickItem[]> {
if (!searchText) {
return [{ label: '请输入关键词查询,如:0000001' }];
return [{ label: '请输入关键词查询,如:0000001 或 上证指数' }];
}
const url = `http://suggest3.sinajs.cn/suggest/type=&key=${encodeURIComponent(searchText)}`;
const url = `http://suggest3.sinajs.cn/suggest/type=${type}&key=${encodeURIComponent(
searchText
)}`;
try {
console.log('getStockSuggestList: getting...');
const response = await axios.get(url, {
Expand All @@ -110,6 +113,12 @@ export class FundService {
headers: randHeader(),
});
const text = response.data.slice(18, -1);
if (text.length <= 1 && !this.searchStockKeyMap[searchText]) {
this.searchStockKeyMap[searchText] = true;
// 兼容一些查询不到的股票,如sz123044
return this.getStockSuggestList(searchText, '');
}
this.searchStockKeyMap = {};
const tempArr = text.split(';');
const result: QuickPickItem[] = [];
tempArr.forEach((item: string) => {
Expand Down Expand Up @@ -169,7 +178,9 @@ export class FundService {
var stockList: Array<LeekTreeItem> = [];
if (/FAILED/.test(resp.data)) {
if (codes.length === 1) {
window.showErrorMessage(`fail: error Stock code in ${codes}, please delete error Stock code`);
window.showErrorMessage(
`fail: error Stock code in ${codes}, please delete error Stock code`
);
return [
{
id: codes[0],
Expand Down Expand Up @@ -258,7 +269,8 @@ export class FundService {
stockItem.symbol = symbol;
stockItem.updown = formatNumber(+price - +yestclose, 2, false);
stockItem.percent =
(stockItem.updown >= 0 ? '+' : '-') + formatNumber((Math.abs(stockItem.updown) / +yestclose) * 100, 2, false);
(stockItem.updown >= 0 ? '+' : '-') +
formatNumber((Math.abs(stockItem.updown) / +yestclose) * 100, 2, false);
if (code === 'sh000001') {
sz = new LeekTreeItem(stockItem, this.context);
}
Expand Down
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export const sortData = (data: LeekTreeItem[] = [], order = SortType.NORMAL) =>
export const formatTreeText = (text = '', num = 10) => {
const str = text + '';
const lenx = num - str.length;
console.log(str + '&nbsp;'.repeat(lenx), lenx);
// console.log(str + '&nbsp;'.repeat(lenx), lenx);
return str + ' '.repeat(lenx);
};

Expand Down

0 comments on commit f03cdfb

Please sign in to comment.