Skip to content

Commit

Permalink
OPTIMIZE: stock getStockDetail
Browse files Browse the repository at this point in the history
  • Loading branch information
wuyuedefeng committed Aug 13, 2022
1 parent c4e8759 commit 7e7f657
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 12 deletions.
60 changes: 49 additions & 11 deletions src/services/StockService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,26 @@ export class StockService {
db: SQLite;

static async getDetail(code: string) {
return await Promise.race([
this.getTTDetail([code]).then(items => items.length ? items[0] : null)
])
let failCount = 0;
let platformPromises = [
this.getTTDetail([`sz${code}`]).then(items => {
if (items.length) { return items[0] } else { throw new Error('Not Found') }
}).catch(e => { failCount += 1; if (failCount === platformPromises.length) { throw(e) } }),
this.getTTDetail([`sh${code}`]).then(items => {
if (items.length) { return items[0] } else { throw new Error('Not Found') }
}).catch(e => { failCount += 1; if (failCount === platformPromises.length) { throw(e) } }),
this.getDFCFDetail([`0.${code}`]).then(items => {
if (items.length) { return items[0] } else { throw new Error('Not Found') }
}).catch(e => { failCount += 1; if (failCount === platformPromises.length) { throw(e) } }),
this.getDFCFDetail([`1.${code}`]).then(items => {
if (items.length) { return items[0] } else { throw new Error('Not Found') }
}).catch(e => {failCount += 1; if (failCount === platformPromises.length) { throw(e) } })
]
return await Promise.race(platformPromises)
}
// 腾讯股票
// 腾讯股票 http://qt.gtimg .cn/q=sz000625,sh600519
static async getTTDetail(codes: string[]) {
const apiUrl = `http://qt.gtimg.cn/q=sz${ codes.join(',') }`
const apiUrl = 'http://qt.gtimg' + `.cn/q=${ codes.join(',') }`
const response: any = await http.fetch(apiUrl, {
method: 'GET',
responseType: http.ResponseType.Binary,
Expand All @@ -21,10 +34,37 @@ export class StockService {
const resData = utf8decoder.decode(new Uint8Array(response.data))
let list = [], arr = resData.split(';');
for (let i = 0; i < arr.length; i++) {
arr[i].split('~').length > 33 && list.push({
name: arr[i].split('~')[1],
price: arr[i].split('~')[3],
// range: arr[i].split('~')[32]
const data = arr[i].split('~')
if (data.length > 33) {
list.push({
name: data[1],
code: data[2],
price: Number(data[3]),
_from: `腾讯股票 ${ codes.join(',') }`
})
}
}
return list
}
// 东方财富网
static async getDFCFDetail(codes: string[]) {
const apiUrl = 'https://push2.eastmoney' + `.com/api/qt/ulist/get?invt=3&pi=0&pz=1&mpi=2000&secids=${ codes.join(',') }&ut=6d2ffaa6a585d612eda28417681d58fb&fields=f12,f13,f19,f14,f139,f148,f2,f4,f1,f125,f18,f3,f152,f5,f30,f31,f32,f8,f6,f9&po=1&_=1660391397865`
const response: any = await http.fetch(apiUrl, {
method: 'GET',
headers: {
Host: 'https://push2.eastmoney' + '.com',
},
responseType: http.ResponseType.JSON,
})
let list = [];
let keys = Object.keys(response.data.data.diff);
for (let i = 0; i < keys.length; i++) {
const item = response.data.data.diff[keys[i]]
list.push({
name: item['f14'],
code: item['f12'],
price: Number(Number(item['f2'] * Math.pow(0.1, item['f1'])).toFixed(2)),
_from: `东方财富网 ${ codes.join(',') }`
})
}
return list
Expand All @@ -44,9 +84,7 @@ export class StockService {
stock.price = Number(onlineDetail.price)
const sql = `UPDATE stocks set price = $1, price_at = datetime('now', 'localtime') where id = ${stock.id}`
await this.db.execute(sql, [stock.price])
console.log(110, stock.price, stock.notice_lower_price)
if (stock.price <= stock.notice_lower_price) {
console.log(111111)
notification.sendNotification({title: stock.name, body: `当前价格${stock.price}, 低于通知价格${stock.notice_lower_price}`})
}
if (stock.price > stock.notice_higher_price) {
Expand Down
1 change: 0 additions & 1 deletion src/views/others/stocks/Form.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<template>
<fragment-form v-bind="formConfig">
<template #default="{setFormRef, formRef, model, submitLoading, onSubmit, onCancel}">
{{model}}
<el-form :ref="setFormRef" :model="model" label-width="150px">
<el-form-item label="代码" prop="code" :rules="[{required: true, message: '必填', trigger: ['change', 'blur']}]">
<el-input v-model="model.code" placeholder="代码" :disabled="!!record" @blur="queryCode(model.code)"></el-input>
Expand Down

0 comments on commit 7e7f657

Please sign in to comment.