当前位置:   article > 正文

Node框架thinkjs开发后台API_用thinkjs编写简单工具

用thinkjs编写简单工具

系列文章目录

Python搭建自己的基金查看管理Web项目(一)
Python搭建自己的基金查看管理Web项目(二)
Python搭建自己的基金查看管理Web项目(添加预览历史数据功能)
Python搭建自己的基金查看管理Web项目(实现前后端分离)
Python搭建自己的基金查看管理Web项目(实现高低点提醒)


前言

之前用python作为后台API的提供语言使用了一段时间之后开发起来不是很舒服,所以我觉得替换成node会比较好,然后使用thinkjs的框架来快速替换之前的python backend


一、初始化项目

Thinkjs 官网地址

1. 安装thinkjs模块

 npm install -g think-cli
  • 1

2. 初始化项目

thinkjs fund-admin-api-node;
cd fund-admin-api-node;
npm install; 
npm start; 
  • 1
  • 2
  • 3
  • 4

3. 建立必要的模块

我这边数据库使用的mysql,需要手动在mysql里面建立需要的table

thinkjs controller fund
thinkjs service fund
thinkjs model fund
thinkjs model fund_rate
thinkjs model history_rate
  • 1
  • 2
  • 3
  • 4
  • 5

4. 配置数据库

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
  }
};
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

5. 跨域处理

如果需要添加允许跨域的配置需要另外安装模块

npm install -s @koa/cors
  • 1

然后添加配置:
fund-admin-api-node/src/config/middleware.js

const cors = require('@koa/cors');
{
    handle: cors
},
  • 1
  • 2
  • 3
  • 4

二、编写业务代码

1. 接口类

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)
  }
  • 1
  • 2
  • 3
  • 4
  • 5

这个方法就可以通过: http://localhost:8088/fund/list的方式进行访问

2. 服务类

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
    }

};

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208

总结

因为切换完以后URL是不变的所以对前端来说是没有影响的,而且使用node来开发后端感觉的确是比python好舒服一些

需要源码的可以评论留言或者私信我都可以,大家一起交流一起进步。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/69062
推荐阅读
相关标签
  

闽ICP备14008679号