Skip to content

Commit

Permalink
完成基本框架
Browse files Browse the repository at this point in the history
  • Loading branch information
adams549659584 committed Aug 16, 2020
1 parent a5e1a6d commit 9a4462f
Show file tree
Hide file tree
Showing 17 changed files with 310 additions and 91 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
### utools-tmpl
### utools-fund

- 使用 uTools 模板插件的模板
- 自选基金助手

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "utools-tmpl",
"name": "utools-fund",
"version": "1.0.0",
"description": "使用 uTools 模板插件的模板",
"description": "自选基金助手",
"main": "main.ts",
"scripts": {
"dev": "set NODE_ENV=development && rollup -c -w",
Expand All @@ -26,4 +26,4 @@
"axios": "^0.19.2",
"encoding": "^0.1.13"
}
}
}
29 changes: 16 additions & 13 deletions plugin.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
* @type { import ('./src/types/utools').PluginConfig }
*/
const pluginConfig = {
pluginName: 'utools-tmpl',
pluginName: '自选基金助手',
version: 'v1.0.0',
description: '使用模板插件模式',
description: '自选基金助手',
author: '罗君',
homepage: 'https://github.com/adams549659584/utools-tmpl',
homepage: 'https://github.com/adams549659584/utools-fund',
// main: 'index.html',
preload: 'preload.js',
logo: 'assets/img/logo.png',
Expand All @@ -23,20 +23,23 @@ const pluginConfig = {
// },
features: [
{
code: 'utools_tmpl_hello_none',
explain: '无 UI 模式',
cmds: ['hello', 'none'],
code: 'utools_fund_add',
explain: '添加自选基金',
icon: 'assets/img/add.png',
cmds: ['添加自选基金', '基金', 'fund']
},
{
code: 'utools_tmpl_hello_list',
explain: '列表模式',
cmds: ['hello', 'list'],
code: 'utools_fund_del',
explain: '删除自选基金',
icon: 'assets/img/del.png',
cmds: ['删除自选基金', '基金', 'fund']
},
{
code: 'utools_tmpl_hello_doc',
explain: '文档模式',
cmds: ['hello', 'doc'],
},
code: 'utools_fund_my',
explain: '我的自选基金',
icon: 'assets/img/logo.png',
cmds: ['我的自选基金', '基金', 'fund'],
}
],
};
export default pluginConfig;
2 changes: 1 addition & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const rollupOptions = {
output: {
file: './dist/preload.js',
format: 'cjs',
sourcemap: NODE_ENV === 'production' ? 'hidden' : 'inline',
sourcemap: NODE_ENV === 'production' ? false : 'inline',
},
plugins: [
cleaner({
Expand Down
13 changes: 0 additions & 13 deletions src/Hello.ts

This file was deleted.

29 changes: 29 additions & 0 deletions src/Helper/FundDBHelper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { IFundEnt } from '@/model/IFundEnt';
import { DBItem } from '@/types/utools';

const FUND_DB_PRE_FIX = 'fund_';

export default class FundDBHelper {
static set(fundId: string, data: IFundEnt) {
return utools.db.put({
_id: `${FUND_DB_PRE_FIX}${fundId}`,
data,
});
}

static update<IFundEnt>(data: DBItem<IFundEnt>) {
return utools.db.put(data);
}

static get(fundId: string) {
return utools.db.get<IFundEnt>(`${FUND_DB_PRE_FIX}${fundId}`);
}

static getAll() {
return utools.db.allDocs<IFundEnt>(FUND_DB_PRE_FIX);
}

static del(fundId: string) {
return utools.db.remove(`${FUND_DB_PRE_FIX}${fundId}`);
}
}
11 changes: 0 additions & 11 deletions src/assets/doc/index.html

This file was deleted.

Binary file added src/assets/img/add.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/assets/img/del.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/assets/img/down.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified src/assets/img/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/assets/img/up.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
220 changes: 173 additions & 47 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,69 +1,195 @@
import { TemplatePlugin } from '@/types/utools';
import Hello from './Hello';
import { TemplatePlugin, CallbackListItem, DBItem } from '@/types/utools';
import { get } from './Helper/HttpHelper';
import FundDBHelper from './Helper/FundDBHelper';
import { ISearchFundResult } from './model/ISearchFundResult';
import { IFundValuationDetailResult } from './model/IFundValuationDetailResult';
import { IFundEnt } from './model/IFundEnt';

// utools.onPluginEnter(params => {
// Hello.sayHello();
// });
// 缓存基金详情
let CACHE_FUND_DB_LIST: DBItem<IFundEnt>[];
// 当前基金持有数量
let CURRENT_FUND_HOLD_COUNT = 0;

const getMyFundDetails = async () => {
const dbList = FundDBHelper.getAll();
for (const db of dbList) {
const oldData = db.data;
const fundValuationDetail = await get<IFundValuationDetailResult>(
`https://fundmobapi.eastmoney.com/FundMApi/FundVarietieValuationDetail.ashx?FCODE=${oldData.id}&deviceid=D03E8A22-9E0A-473F-B045-3745FC7931C4&plat=Iphone&product=EFund&version=6.2.9&GTOKEN=793EAE9248BC4181A9380C49938D1E31`
);
if (fundValuationDetail.ErrCode !== 0) {
utools.showMessageBox({
message: fundValuationDetail.ErrMsg,
});
continue;
}
let lastTime = fundValuationDetail.Expansion.GZTIME;
let nowJJJZ = Number(fundValuationDetail.Expansion.GZ);
const searchFundResult = await get<ISearchFundResult>(`http://fundsuggest.eastmoney.com/FundSearch/api/FundSearchAPI.ashx?m=1&key=${oldData.id}`);
if (searchFundResult.ErrCode !== 0) {
utools.showMessageBox({
message: searchFundResult.ErrMsg,
});
continue;
} else {
const searchFundDetail = searchFundResult.Datas[0];
// 最后单位净值
if (lastTime.includes(searchFundDetail.FundBaseInfo.FSRQ)) {
lastTime = searchFundDetail.FundBaseInfo.FSRQ;
nowJJJZ = Number(searchFundDetail.FundBaseInfo.DWJZ);
}
}
db.data = {
...oldData,
yesJJJZ: Number(fundValuationDetail.Expansion.DWJZ),
nowJJJZ: Number(nowJJJZ),
lastTime,
};
FundDBHelper.update(db);
console.log(db.data);
}
return FundDBHelper.getAll();
};

const fundDetailsToCbList = (dbList: DBItem<IFundEnt>[]) => {
let sumIncome = 0;
let cbList = dbList.map(db => {
const fund = db.data;
const rate = Math.round((fund.nowJJJZ / fund.yesJJJZ - 1) * 10000) / 10000;
const income = fund.holdCount > 0 ? rate * fund.holdCount * fund.yesJJJZ : 0;
sumIncome += income;
const cb: CallbackListItem = {
fundCode: fund.id,
title: `${fund.id} ${fund.name}`,
description: `${(rate * 100).toFixed(2)}% ¥${income.toFixed(2)}`,
icon: rate >= 0 ? 'assets/img/up.png' : 'assets/img/down.png',
};
return cb;
});
cbList = [
{
title: `今日总收益`,
description: `¥${sumIncome.toFixed(2)}`,
icon: sumIncome >= 0 ? 'assets/img/up.png' : 'assets/img/down.png',
},
...cbList,
];
return cbList;
};

const preload: TemplatePlugin = {
utools_tmpl_hello_none: {
mode: 'none',
utools_fund_add: {
mode: 'list',
args: {
placeholder: '无 UI 模式',
enter: (action, cb) => {
Hello.sayHello('Hello None');
utools.outPlugin();
placeholder: '输入基金简称/代码/拼音,回车键确认',
search: async (action, searchWord, callbackSetList) => {
// 获取一些数据
let cbList: CallbackListItem[] = [];
if (searchWord) {
const searchResult = await get<ISearchFundResult>(`http://fundsuggest.eastmoney.com/FundSearch/api/FundSearchAPI.ashx?m=1&key=${searchWord}`);
if (searchResult) {
if (searchResult.ErrCode === 0) {
cbList = searchResult.Datas.map(fund => {
const cb: CallbackListItem = {
title: fund.CODE,
description: fund.NAME,
DWJZ: fund.FundBaseInfo.DWJZ,
FSRQ: fund.FundBaseInfo.FSRQ,
};
return cb;
});
} else {
utools.showMessageBox({
message: searchResult.ErrMsg,
});
}
}
}
callbackSetList(cbList);
}, // 用户选择列表中某个条目时被调用
select: (action, itemData, callbackSetList) => {
const existFund = FundDBHelper.get(itemData.title);
if (!existFund) {
FundDBHelper.set(itemData.title, {
id: itemData.title,
name: itemData.description,
holdCount: 0,
yesJJJZ: 0,
nowJJJZ: itemData.DWJZ,
lastTime: itemData.FSRQ,
});
}
utools.redirect('我的自选基金', '');
},
},
},
utools_tmpl_hello_list: {
utools_fund_del: {
mode: 'list',
args: {
placeholder: '列表模式',
placeholder: '选择需删除的基金,回车键确认',
enter: (action, callbackSetList) => {
console.log(action);
const dbList = FundDBHelper.getAll();
const cbList = dbList.map(db => {
const cb: CallbackListItem = {
title: db.data.id,
description: db.data.name,
};
return cb;
});
// 如果进入插件就要显示列表数据
callbackSetList([
{
title: '这是标题',
description: '这是描述(跳去我的github)',
icon: '', // 图标(可选)
url: 'https://github.com/adams549659584/utools-tmpl',
},
]);
callbackSetList(cbList);
},
search: (action, searchWord, callbackSetList) => {
// 获取一些数据
// 执行 callbackSetList 显示出来
callbackSetList([
{
title: searchWord,
description: '虽然文字变了,但是还是跳去我的github',
icon: '', // 图标,
url: 'https://github.com/adams549659584/utools-tmpl',
other: 'xxx',
},
]);
let dbList = FundDBHelper.getAll();
if (searchWord) {
dbList = dbList.filter(x => x.data.id.includes(searchWord) || x.data.name.includes(searchWord));
}
const cbList = dbList.map(db => {
const cb: CallbackListItem = {
title: db.data.id,
description: db.data.name,
};
return cb;
});
callbackSetList(cbList);
}, // 用户选择列表中某个条目时被调用
select: (action, itemData, callbackSetList) => {
utools.hideMainWindow();
const url = itemData.url;
require('electron').shell.openExternal(url);
utools.outPlugin();
FundDBHelper.del(itemData.title);
utools.redirect('我的自选基金', '');
},
},
},
utools_tmpl_hello_doc: {
mode: 'doc',
utools_fund_my: {
mode: 'list',
args: {
indexes: [
{
t: '这是标题',
d: '这是描述',
p: 'assets/doc/index.html', //页面, 只能是相对路径
},
],
placeholder: '文档模式',
placeholder: '输入持有份额,选择对应基金,回车键保存',
enter: async (action, callbackSetList) => {
const dbList = await getMyFundDetails();
// 缓存
CACHE_FUND_DB_LIST = dbList;
CURRENT_FUND_HOLD_COUNT = 0;
const cbList = fundDetailsToCbList(dbList);
// 如果进入插件就要显示列表数据
callbackSetList(cbList);
},
search: async (action, searchWord, callbackSetList) => {
let dbList = CACHE_FUND_DB_LIST && CACHE_FUND_DB_LIST.length > 0 ? CACHE_FUND_DB_LIST : await getMyFundDetails();
CURRENT_FUND_HOLD_COUNT = Number(searchWord);
const cbList = fundDetailsToCbList(dbList);
callbackSetList(cbList);
}, // 用户选择列表中某个条目时被调用
select: (action, itemData, callbackSetList) => {
if (!CACHE_FUND_DB_LIST || CACHE_FUND_DB_LIST.length === 0) {
utools.redirect('添加自选基金', '');
return;
}
if (action.type === 'text' && itemData.fundCode) {
const fundDb = FundDBHelper.get(itemData.fundCode);
fundDb.data.holdCount = CURRENT_FUND_HOLD_COUNT;
FundDBHelper.update(fundDb);
}
utools.redirect('我的自选基金', '');
},
},
},
};
Expand Down
Loading

0 comments on commit 9a4462f

Please sign in to comment.