Python搭建自己的基金查看管理Web项目(一)
Python搭建自己的基金查看管理Web项目(二)
Python搭建自己的基金查看管理Web项目(添加预览历史数据功能)
Python搭建自己的基金查看管理Web项目(实现前后端分离)
Python搭建自己的基金查看管理Web项目(实现高低点提醒)
之前用python作为后台API的提供语言使用了一段时间之后开发起来不是很舒服,所以我觉得替换成node会比较好,然后使用thinkjs的框架来快速替换之前的python backend
npm install -g think-cli
thinkjs fund-admin-api-node;
cd fund-admin-api-node;
npm install;
npm start;
我这边数据库使用的mysql,需要手动在mysql里面建立需要的table
thinkjs controller fund
thinkjs service fund
thinkjs model fund
thinkjs model fund_rate
thinkjs model history_rate
dev: fund-admin-api-node/src/config/adapter.js
production: fund-admin-api-node/src/config/adapter.production.js
exports.model = {
type: 'mysql',
common: {
logConnect: isDev,
logSql: isDev,
logger: msg => think.logger.info(msg)
},
mysql: {
handle: mysql,
database: 'xxxx',
prefix: '',
encoding: 'utf8',
host: 'localhost',
port: '3306',
user: 'xxxx',
password: 'xxxxxx',
dateStrings: true
}
};
如果需要添加允许跨域的配置需要另外安装模块
npm install -s @koa/cors
然后添加配置:
fund-admin-api-node/src/config/middleware.js
const cors = require('@koa/cors');
{
handle: cors
},
– fund-admin-api-node/src/controller/fund.js
thinkjs 在controller里面的方法加上Action后缀就可以直接通过restfull接口的方式进行访问
例如:
async listAction() {
let fundService = this.service('fund')
let result = await fundService.listAllData()
this.success(result)
}
这个方法就可以通过: http://localhost:8088/fund/list的方式进行访问
– fund-admin-api-node/src/service/fund.js
服务类service用来编写一些复杂功能的具体实现,然后再controller里面可以直接通过this.service(‘fund’)进行实例化
const { max } = require('moment');
const moment = require('moment')
var util = require('util');
const httpUtil = require('../utils/httputil')
function judgeMaxOrMinVal(group, val, type) {
if (group.length == 0) {
return null
}
max_value = group.map(element => element.value).reduce((a, b) => { return Math.max(a, b) }, 0)
min_value = group.map(element => element.value).reduce((a, b) => { return Math.min(a, b) }, group[0].value)
if (val > max_value) {
return util.format('Hit <strong>Max</strong> value in last <strong>%s</strong> -> %s[%s]', type, max_value, val)
}
if (val < min_value) {
return util.format('Hit <strong>Min</strong> value in last <strong>%s</strong> -> %s[%s]', type, min_value, val)
}
return null
}
function getNotesFromHistory(historyData, currentVal) {
// 首先判断是不是一年最大最小值
year_group = historyData.filter(item => {
return item.date > moment().subtract(1, 'years').valueOf()
})
year_notes = judgeMaxOrMinVal(year_group, currentVal, '1 year')
if (year_notes != null && year_notes != undefined) {
return year_notes
}
// # 然后判断3个月内的数据
m3_group = historyData.filter(item => {
return item.date > moment().subtract(3, 'months').valueOf()
})
m3_notes = judgeMaxOrMinVal(m3_group, currentVal, '3 months')
if (m3_notes != null && year_notes != undefined) {
return m3_notes
}
// # 判断一个月内的数据
m1_group = historyData.filter(item => {
return item.date > moment().subtract(1, 'months').valueOf()
})
m1_notes = judgeMaxOrMinVal(m1_group, currentVal, '1 months')
if (m1_notes != null && year_notes != undefined) {
return m1_notes
}
// # 判断7天内的数据
d7_group = historyData.filter(item => {
return item.date > moment().subtract(7, 'days').valueOf()
})
d7_notes = judgeMaxOrMinVal(d7_group, currentVal, '7 days')
if (d7_notes != null && year_notes != undefined) {
return d7_notes
}
return ''
}
function getLastDateFromHistory(historyData) {
if (!historyData) {
return 'unknow'
}
historyData = historyData.sort((a, b) => {
return b.date - a.date
})
let lastDate = historyData[0].date
let time_str = moment(lastDate).format('MM/DD/YYYY')
return time_str
}
function convertAPIData(str) {
let strList = str.split(';')
let result = {}
for (let i = 0; i < strList.length; i++) {
let item = strList[i]
let keyValue = item.split('=')
if (keyValue.length < 2) {
continue
}
let value = keyValue[1].trim()
if (value.startsWith('{') || value.startsWith('[')) {
try {
value = JSON.parse(value)
} catch (error) {
think.logger.info('can not process node: ' + keyValue[0].trim());
continue
}
}
let key = keyValue[0].trim()
key = key.replace(/\/\*.*\*\//, '').replace('var', '').trim()
result[key] = value
}
return result
}
module.exports = class extends think.Service {
async listAllData() {
let fundModel = think.model('fund')
let fundRateModel = think.model('fund_rate')
let historyRateModel = think.model('history_rate')
let baseFundList = await fundModel.select()
let cur_date = moment(Date.now()).format('YYYY-MM-DD')
let rateList = await fundRateModel.where({ "date": cur_date }).select()
// # 如果当日数据还没刷新过,就先刷新一次
if (rateList.length == 0) {
await this.getRate()
}
let allHistoryRateData = await historyRateModel.select()
let allHistoryRateDataGroupObj = {}
allHistoryRateData.sort((a, b) => {
return b.date - a.date
}).forEach(item => {
let fundId = item.fund_id
if (!allHistoryRateDataGroupObj[fundId]) {
allHistoryRateDataGroupObj[fundId] = []
}
allHistoryRateDataGroupObj[fundId].push(item)
});
let allHistoryRateDataObj = {}
for (let key in allHistoryRateDataGroupObj) {
allHistoryRateDataObj[key] = allHistoryRateDataGroupObj[key]
}
let fund_obj = {}
let fund_tag = {}
let fund_amt = {}
baseFundList.forEach(item => {
fund_obj[item.fund_id] = item.name
fund_tag[item.fund_id] = item.tag
fund_amt[item.fund_id] = item.amount
});
let result = []
for (let i = 0; i < rateList.length; i++) {
let item = rateList[i]
let tag = fund_tag[item.fund_id]
let notes = ''
let lastDateInHistory = ''
if (allHistoryRateDataObj[item.fund_id].length > 0) {
notes = getNotesFromHistory(allHistoryRateDataObj[item.fund_id], item.gsz)
}
lastDateInHistory = getLastDateFromHistory(allHistoryRateDataObj[item.fund_id])
let obj = {
"fund_id": item.fund_id,
"dwjz": item.dwjz,
"gszzl": item.gszzl,
"gsz": item.gsz,
"name": fund_obj[item.fund_id],
"tag": tag,
"amount": fund_amt[item.fund_id],
"notes": notes,
"lastDateInHistory": lastDateInHistory
}
result.push(obj)
}
return result
}
async getRate() {
let fundModel = think.model('fund')
let fundRateModel = think.model('fund_rate')
let cur_date = moment(Date.now()).format('YYYY-MM-DD')
let list = await fundModel.select()
try {
await fundRateModel.where({ "date": cur_date }).delete()
} catch (error) {
console.log(error)
}
for (let i = 0; i < list.length; i++) {
let fund = list[i]
let fund_id = fund.fund_id
let detail_url = util.format("/js/%s.js", fund_id)
let r = await httpUtil.callAPIUsePort('fundgz.1234567.com.cn', detail_url, 'GET', 80)
console.log(util.format("get %s info success", fund_id))
let matched_obj = r.match(/^jsonpgz\((.*)\);/)
let response = JSON.parse(matched_obj[1])
let fundRate = {
fund_id: fund_id,
dwjz: response["dwjz"],
gszzl: response["gszzl"],
gsz: response["gsz"],
date: cur_date
}
await fundRateModel.add(fundRate)
}
}
async refreshHistoryById(id) {
let tail = '.js?v=' + moment(Date.now()).format('YYYYmmddHHMMSS')
let detail_url = '/pingzhongdata/' + id + tail
let content = await httpUtil.callAPIUsePort('fund.eastmoney.com', detail_url, 'GET', 80)
let jsContent = convertAPIData(content)
// # 单位净值走势
let netWorthTrend = jsContent['Data_netWorthTrend']
let historyList = []
netWorthTrend.forEach(item => {
if (item['y'] != null || item['y'] != undefined) {
historyList.push({ fund_id: id, date: item['x'], value: item['y'] })
}
})
return historyList
}
};
因为切换完以后URL是不变的所以对前端来说是没有影响的,而且使用node来开发后端感觉的确是比python好舒服一些
需要源码的可以评论留言或者私信我都可以,大家一起交流一起进步。