Skip to content

Commit

Permalink
chore: 优化stock逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
giscafer committed Oct 21, 2023
1 parent 8233ed8 commit d968566
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 20 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
{
}
"cSpell.words": ["xueqiu"]
}
7 changes: 1 addition & 6 deletions src/config.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
//指定某些群或者对象可以起作用,其他人不行
const activeRooms = [
'没回信息可能在这里',
'熊市历史见证者',
'WechatyTest',
'LeekHub',
];
const activeRooms = ['熊市历史见证者'];

module.exports = { activeRooms };
5 changes: 3 additions & 2 deletions src/contants.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const RegType = {
stock: /^(:|:|#)/,
stockPrefix: /^#/,
stock: /^(:|:|#|\/)/,
stockPrefix: /^#/, // 精简模式
stockPrefix2: /^\//, // 极简模式
};

module.exports = { RegType };
12 changes: 7 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ let bot, loginUserName;
let ocrOn = false;
let contactUsers = [];

const adminName = 'Nickbing Lao';
const adminName = 'Nicky Lao';

// 尝试获取本地登录数据,免扫码
// bot = new Wechaty({ name: 'leekhub' });
Expand Down Expand Up @@ -179,6 +179,7 @@ function textMsgHandler(msg) {
text = text.substr(index + 1, text.length);
}
text = text.trim();
console.log(`${talker.name()}${text}`);
// 图片搜索
if (text.indexOf('图 ') === 0) {
const keyword = text.replace('图', '').replace('图片', '').trim();
Expand Down Expand Up @@ -292,8 +293,9 @@ function textMsgHandler(msg) {
sendText(result, msg);
}
}

// 本人同意开启
else if (text.startsWith('open bot')) {
else if (text.startsWith('#上班')) {
const roomName = room.payload.topic;
if (
talker.payload.name === adminName ||
Expand All @@ -308,20 +310,20 @@ function textMsgHandler(msg) {
}
}
// 关闭 bot
else if (text.startsWith('close bot')) {
else if (text.startsWith('#下班')) {
if (
talker.payload.name === adminName ||
talker.payload.name === loginUserName
) {
const roomName = room.payload.topic;
const index = activeRooms.indexOf(roomName);
activeRooms.splice(index, 1);
// console.log('activeRooms splice=', activeRooms);

sendText('LeekHub Robot 已关闭', msg);
} else {
sendText('抱歉,您没有权限!', msg);
}
} else if (RegType.stock.test(text)) {
stockMsgHandler(msg, RegType.stockPrefix.test(text));
stockMsgHandler(msg, text);
}
}
11 changes: 7 additions & 4 deletions src/modules/stock.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ const debugFlag = true;

// 大盘
const overviewCodes = [
'SH600031',
'SH000001',
'SH000300',
'SZ399001',
Expand All @@ -18,14 +17,17 @@ const overviewCodes = [
];

// 我的持仓
const myCodes = ['SH600036', 'SH002142', 'SH601012', 'SH000858'];
const myCodes = ['SH600036', 'SZ002142', 'SH601012', 'SZ000858'];

/**
* 股票消息处理
* @param {*} message
*/

async function message(message, isSimple = false) {
async function message(message, text) {
const simpleMode = RegType.stockPrefix.test(text);
const numberMode = RegType.stockPrefix2.test(text);
const type = simpleMode ? 1 : numberMode ? 0 : 2;
try {
const room = message.room();
const from = message.talker();
Expand All @@ -42,6 +44,7 @@ async function message(message, isSimple = false) {

const [names, codes] = parseMsg(text, true);
let symbol = '';
console.log(text, codes);
if (codes.length > 0) {
symbol = codes.join(',');
} else if (text.indexOf('大盘') >= 0 || text.indexOf('指数') >= 0) {
Expand All @@ -52,7 +55,7 @@ async function message(message, isSimple = false) {
if (symbol) {
xueqiu.quote(symbol).then((res) => {
const { items } = res?.data || {};
const msg = xueqiu.batchQuoteResp(items, isSimple);
const msg = xueqiu.batchQuoteResp(items, type);
if (!msg) return;
sayer.say(msg);
});
Expand Down
9 changes: 7 additions & 2 deletions src/sites/xueqiu.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class Xueqiu {
const url = `https://stock.xueqiu.com/v5/stock/batch/quote.json?symbol=${symbol}&_=${timestamp()}`;
return this.request(url);
}
batchQuoteResp(items = [], isSimple = true) {
batchQuoteResp(items = [], type = 0) {
return items
.map(({ market, quote }) => {
const { status } = market;
Expand All @@ -90,7 +90,12 @@ class Xueqiu {
volume,
symbol,
} = quote;
if (isSimple) {
if (type === 0) {
return [
`${symbol.substr(2)}${percent >= 0 ? '+' : ''}${percent}%`,
].join(',');
}
if (type === 1) {
return [
`${percent >= 0 ? '🍖' : '🌱'} ${name}:现价 ${current}`,
`涨幅 ${percent}%`,
Expand Down

0 comments on commit d968566

Please sign in to comment.