Skip to content

Commit

Permalink
- 移除 encoding
Browse files Browse the repository at this point in the history
- 修复重构导致导入 bug
- 优化无基金时删除及我的自选基金列表展示
  • Loading branch information
adams549659584 committed Aug 18, 2020
1 parent c165eea commit 04aed21
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 81 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,20 @@
- 自选基金助手,数据来源于天天基金
- 支持新增删除自选基金,可填入持有份额,实时计算当天收益

#### 源码

> https://github.com/adams549659584/utools-fund
#### 使用说明

![Instructions.gif](https://img03.sogoucdn.com/app/a/100520146/4d63e4c89eb939f009344792b5f3afe8)

#### v1.3.4

- 移除 encoding
- 修复重构导致导入 bug
- 优化无基金时删除及我的自选基金列表展示

#### v1.3.3

- 重构代码
Expand Down
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "utools-fund",
"version": "v1.3.3",
"version": "v1.3.4",
"description": "自选基金助手",
"main": "main.ts",
"scripts": {
Expand All @@ -23,7 +23,6 @@
"typescript": "^3.9.7"
},
"dependencies": {
"axios": "^0.19.2",
"encoding": "^0.1.13"
"axios": "^0.19.2"
}
}
}
6 changes: 5 additions & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ const rollupOptions = {
replace({
ENV: JSON.stringify(NODE_ENV),
}),
NODE_ENV === 'production' && terser(),
// NODE_ENV === 'production' && terser({
// format: {
// comments: false
// }
// }),
],
};
export default rollupOptions;
50 changes: 2 additions & 48 deletions src/Helper/HttpHelper.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import axios, { AxiosRequestConfig } from 'axios';
import { IncomingMessage } from 'electron';
import { convert } from 'encoding';
// import { convert } from 'encoding';

// 超时时间
axios.defaults.timeout = 1000 * 10;
Expand Down Expand Up @@ -35,42 +35,6 @@ const get = async <T = string>(url: string, params?: { [key: string]: any }, con
return res.data as T;
};

/**
* get 请求中文站点等等
* @param url 请求链接
* @param coding 编码,默认gb2312
* @param params 参数
* @param config 配置
*/
const getByCoding = async (url: string, coding: string = 'gb2312', params?: { [key: string]: any }, config?: AxiosRequestConfig): Promise<string> => {
// const oldResponseType = (config && config.responseType) || 'text';
config = {
...config,
responseType: 'stream',
};
const resData = await get<IncomingMessage>(url, params, config);
// 编码处理
return await hanldeCodingData(resData, coding);
};

const hanldeCodingData = async (data: IncomingMessage, coding: string = 'gb2312'): Promise<string> => {
// 编码处理
return new Promise((resolve, reject) => {
const chunks = [];
data.on('data', chunk => {
chunks.push(chunk);
});
data.on('end', () => {
const buffer = Buffer.concat(chunks);
const convertResult = convert(buffer, 'UTF-8', coding);
resolve(convertResult.toString());
});
data.on('error', () => {
reject('请求失败');
});
});
};

/**
* post 请求
* @param url 请求链接
Expand All @@ -82,17 +46,7 @@ const post = async <T = string>(url: string, data?: { [key: string]: any }, conf
return res.data as T;
};

const postByCoding = async (url: string, coding: string = 'gb2312', data?: { [key: string]: any }, config?: AxiosRequestConfig): Promise<string> => {
config = {
...config,
responseType: 'stream',
};
const resData = await post<IncomingMessage>(url, data, config);
// 编码处理
return await hanldeCodingData(resData, coding);
};

export { get, post, getByCoding, postByCoding };
export { get, post };

export default class HttpHelper {
/**
Expand Down
3 changes: 3 additions & 0 deletions src/features/fundDel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ const fundDel: TplFeature = {
placeholder: '选择需删除的基金,回车键确认',
enter: (action, callbackSetList) => {
const dbList = FundDBHelper.getAll();
if (dbList.length === 0) {
utools.redirect('我的自选基金', '');
}
const cbList = dbList.map(db => {
const cb: CallbackListItem = {
title: db.data.id,
Expand Down
27 changes: 19 additions & 8 deletions src/features/fundMy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ const getMyFundDetails = async () => {
// console.log(JSON.stringify(db.data));
} catch (error) {
console.error(error);
utools.showNotification(`网络请求失败,请稍后再试`);
}
return db;
})
Expand All @@ -78,14 +79,24 @@ const fundDetailsToCbList = (dbList: DBItem<IFundEnt>[]) => {
};
return cb;
});
cbList = [
{
title: `今日总收益 ${dbList.every(x => !x.data.isValuation) ? '✅' : ''}`,
description: `¥${sumIncome.toFixed(2)}`,
icon: sumIncome >= 0 ? 'assets/img/up.png' : 'assets/img/down.png',
},
...cbList,
];
if (cbList.length === 0) {
cbList = [
{
title: ``,
description: ``,
icon: 'assets/img/add.png',
},
];
} else {
cbList = [
{
title: `今日总收益 ${dbList.every(x => !x.data.isValuation) ? '✅' : ''}`,
description: `¥${sumIncome.toFixed(2)}`,
icon: sumIncome >= 0 ? 'assets/img/up.png' : 'assets/img/down.png',
},
...cbList,
];
}
return cbList;
};

Expand Down
3 changes: 2 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import fundDel from './features/fundDel';
import fundMarket from './features/fundMarket';
import fundExport from './features/fundExport';
import fundMy from './features/fundMy';
import fundImport from './features/fundImport';

const preload: TemplatePlugin = {
utools_fund_add: fundAdd,
utools_fund_del: fundDel,
utools_fund_my: fundMy,
utools_fund_market: fundMarket,
utools_fund_config_export: fundExport,
utools_fund_config_import: fundExport,
utools_fund_config_import: fundImport,
};

window.exports = preload;
19 changes: 0 additions & 19 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -156,13 +156,6 @@ dir-glob@^3.0.1:
dependencies:
path-type "^4.0.0"

encoding@^0.1.13:
version "0.1.13"
resolved "https://registry.yarnpkg.com/encoding/-/encoding-0.1.13.tgz#56574afdd791f54a8e9b2785c0582a2d26210fa9"
integrity sha512-ETBauow1T35Y/WZMkio9jiM0Z5xjHHmJ4XmjZOq1l/dXz3lr2sRn87nJy20RupqSh1F2m3HHPSp8ShIPQJrJ3A==
dependencies:
iconv-lite "^0.6.2"

escape-string-regexp@^1.0.5:
version "1.0.5"
resolved "https://registry.npm.taobao.org/escape-string-regexp/download/escape-string-regexp-1.0.5.tgz#1b61c0562190a8dff6ae3bb2cf0200ca130b86d4"
Expand Down Expand Up @@ -260,13 +253,6 @@ has-flag@^4.0.0:
resolved "https://registry.yarnpkg.com/has-flag/-/has-flag-4.0.0.tgz#944771fd9c81c81265c4d6941860da06bb59479b"
integrity sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==

iconv-lite@^0.6.2:
version "0.6.2"
resolved "https://registry.yarnpkg.com/iconv-lite/-/iconv-lite-0.6.2.tgz#ce13d1875b0c3a674bd6a04b7f76b01b1b6ded01"
integrity sha512-2y91h5OpQlolefMPmUlivelittSWy0rP+oYVpn6A7GwVHNE8AWzoYOBNmlwks3LobaJxgHCYZAnyNo2GgpNRNQ==
dependencies:
safer-buffer ">= 2.1.2 < 3.0.0"

ignore@^5.1.1:
version "5.1.8"
resolved "https://registry.npm.taobao.org/ignore/download/ignore-5.1.8.tgz#f150a8b50a34289b33e22f5889abd4d8016f0e57"
Expand Down Expand Up @@ -494,11 +480,6 @@ safe-buffer@^5.1.0:
resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.2.1.tgz#1eaf9fa9bdb1fdd4ec75f58f9cdb4e6b7827eec6"
integrity sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==

"safer-buffer@>= 2.1.2 < 3.0.0":
version "2.1.2"
resolved "https://registry.yarnpkg.com/safer-buffer/-/safer-buffer-2.1.2.tgz#44fa161b0187b9549dd84bb91802f9bd8385cd6a"
integrity sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==

serialize-javascript@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/serialize-javascript/-/serialize-javascript-4.0.0.tgz#b525e1238489a5ecfc42afacc3fe99e666f4b1aa"
Expand Down

0 comments on commit 04aed21

Please sign in to comment.