node

node

Node.js 是一个基于 Chrome V8 引擎的 JavaScript 运行环境。
javascript/jQuery

javascript/jQuery

一种直译式脚本语言,是一种动态类型、弱类型、基于原型的语言,内置支持类型。
MongoDB

MongoDB

MongoDB 是一个基于分布式文件存储的数据库
openstack

openstack

OpenStack是一个由NASA(美国国家航空航天局)和Rackspace合作研发并发起的,以Apache许可证授权的自由软件和开放源代码项目。
VUE

VUE

一套构建用户界面的渐进式框架。与其他重量级框架不同的是,Vue 采用自底向上增量开发的设计。
bootstrap

bootstrap

Bootstrap is the most popular HTML, CSS, and JS framework for developing responsive, mobile first projects on the web.
HTML

HTML

超文本标记语言,标准通用标记语言下的一个应用。
CSS/SASS/SCSS/Less

CSS/SASS/SCSS/Less

层叠样式表(英文全称:Cascading Style Sheets)是一种用来表现HTML(标准通用标记语言的一个应用)或XML(标准通用标记语言的一个子集)等文件样式的计算机语言。
PHP

PHP

PHP(外文名:PHP: Hypertext Preprocessor,中文名:“超文本预处理器”)是一种通用开源脚本语言。语法吸收了C语言、Java和Perl的特点,利于学习,使用广泛,主要适用于Web开发领域。PHP 独特的语法混合了C、Java、Perl以及PHP自创的语法。它可以比CGI或者Perl更快速地执行动态网页。用PHP做出的动态页面与其他的编程语言相比,PHP是将程序嵌入到HTML(标准通用标记语言下的一个应用)文档中去执行,执行效率比完全生成HTML标记的CGI要高许多;PHP还可以执
每天进步一点点

每天进步一点点

乌法把门的各累笑寂静
求职招聘

求职招聘

用nodejs编写股票数据爬虫

Nodejslopo1983 发表了文章 • 0 个评论 • 401 次浏览 • 2019-01-16 15:52 • 来自相关话题

以下代码仅仅用作学习交流,请勿用作其他用途
?基础类 getBoard
/*************************************************************
*
*- Copyright (c), 股票数据API接口基础Class, 2018, lopo
*- FileName : default.js
*- Author : 罗波 Version : 1.0 Date:2018-8-4
*- Descripttion : 股票 Node Express
*- Other : 数据来源-东方财富
*- JSVersion : ES6
*- Modules :
* 1.request
* 2.cheerio
*
*----------------------------------------------------------
*
*- Function List :
* - tools
* 1. getCodeExchange
*
*- Class List :
* 1. boards
*
*- History :
* <Author> <Date> <Desc> <Modi>
*
**************************************************************/
'use strict'
const request = require('request-promise-native');
const { getCodeExchange } = require('../utils/tools');

/**
* 基础配置
*
* @class getBoards
*/
class getBoards {
// 基础参数
constructor() {
// - stockSty : ['列表概览','个股详细']
this.reqOpt = {
uri: 'http://nufm.dfcfw.com/EM_Finance2014NumericApplication/JS.aspx',
qs: {
type: 'CT',
token: '64a483cbad8b666efa51677820e6b21c',
js: '({data:[(x)],totle:(tot)})',
'_': (new Date()).valueOf(),
}
};
// _rp_options.qs
this.request = request.defaults(this.reqOpt);
// - pageFn : p/当前页 ps/页数据量 st/排序字段 sr/排序方式
//* @param {Number} p 当前页
//* @param {Number} ps 每页数量 20
//* @param {String} st 排序字段
//* @param {Number} sr 排序方式 -1,1
this.pageOpt = (params) => {
return {
qs: {
p: params[0] || '1',
ps: params[1] || '20',
st: params[2] || '(ChangePercent)',
sr: params[3] || '-1'
}
}
};
/**
*
* @param {Objec} cmdMap 参数比对数组
* @param {Object} params page参数
* @param {String} type 参数
* @param {String} sty 展示方式
*
* @returns promise
*
* @memberof boards
*/
this.boradFn = (cmdMap, ...params) => (type, sty = this.stockSty[0]) => {
const boardMap = cmdMap;
if (!boardMap[type]) return { 'err': 'TypeError' };
const opt = this.pageOpt([...params]);
Object.assign(opt.qs, {
'cmd': boardMap[type],
'sty': sty
});
return this.getDatas({...opt })
};
};
/**
*格式化cmd配置亲求
*
* @param {String,Arrary} codes 股票代码格式化
* @returns String
* @memberof boards
*/
setCodeCmd(codes) {
if (codes.includes(',')) codes = codes.split(',');
return Array.isArray(codes) ? codes.reduce((a, b, i) => a += `${getCodeExchange(b)}${i<codes.length-1?',':''}`, '') : `${getCodeExchange(codes)}`
};
/**
*request 请求函数
*
* @param {Object} opt 请求配置
* @returns promise
* @memberof boards
*/
async getDatas(opt) {
try {
const datas = await this.request({...opt });
return await eval(datas);
} catch (error) {
return { 'err': 'NETError' }
}
};
}
module.exports = getBoards辅助类 tool.js
const fixMoney = (money, fix = 2) => (money / (money.length < 8 ? 10000 : 100000000)).toFixed(fix) + (money.length < 8 ? '万' : '亿');
//
const timeout = async(ms) => {
await new Promise((resolve) => {
setTimeout(resolve, ms);
});
};
const getCodeExchange = (code, type = "stock") => {
let codes = code.substr(0, 3);
if (type === 'stock') return (codes === '000' || codes === '002') && `${code}2` || (codes === '600' || codes === '601' || codes === '603') && `${code}1` || `${code}2`
else return codes === '399' ? `${code}2` : `${code}1`
};
module.exports = {
fixMoney,
timeout,
getCodeExchange
}基金 fund.js
/*************************************************************
*
*- Copyright (c), 股票数据API接口 基金, 2018, lopo
*- FileName : default.js
*- Author : 罗波 Version : 1.0 Date:2018-8-4
*- Descripttion : 股票 Node Express
*- Other : 数据来源-东方财富
*- JSVersion : ES6
*
*----------------------------------------------------------
*- Class List :
* 1.getBoard
*
*- History :
* <Author> <Date> <Desc> <Modi>
*
**************************************************************/
'use strict'
const getBoard = require('../../utils/getBoard');
//
class funds extends getBoard {
/**
* 封闭基金
*
* @param {string} [type='ALL']
* @param {*} params
* @returns
* @memberof futures
*/
// 封闭基金 ETF基金 LOF基金
async FUND_Board(type = 'ALL', ...params) {
const ENERGYMap = {
'CLOSE_END': 'C.__285002',
'ETF': 'C.__2850013',
'LOF': 'C.__2850014'
};
const datas = this.boradFn(ENERGYMap, ...params);
return datas(type, 'FCOIATC')
};

}
const client = new funds();?
// client.FE_DCE_Board('ALL', 1, 50).then(res => console.log(res));
client.FUND_Board('CLOSE_END', 1, 50).then(res => console.log(res))期货类 futures.js
/*************************************************************
*
*- Copyright (c), 股票数据API接口 期货, 2018, lopo
*- FileName : default.js
*- Author : 罗波 Version : 1.0 Date:2018-8-4
*- Descripttion : 股票 Node Express
*- Other : 数据来源-东方财富
*- JSVersion : ES6
*
*----------------------------------------------------------
*- Class List :
* 1.getBoard
*
*- Class List :
* 1. futures
*
*- History :
* <Author> <Date> <Desc> <Modi>
*
**************************************************************/
'use strict'
const getBoard = require('../../utils/getBoard');
//
class futures extends getBoard {
constructor() {
super();
this.fecSty = ['FCHKEGL', 'FCFL4O']
};
/**
* 国内期货数据
*
* @param {*} type
* @param {*} params
* @returns
* @memberof boards
*/
//[上期所所有,["沪锌", "沪铅", "石油沥青", "沪金", "螺纹钢", "沪银", "热轧卷板", "沪铝", "沪锡", "橡胶", "沪铜", "沪镍", "线材", "燃油","原油"]]
async FE_SH_Board(type, ...params) {
const FESHMap = {
ALL: 'C.SHFE',
ZN: 'C.F_SHFE_ZN',
PB: 'C.F_SHFE_PB',
BU: 'C.F_SHFE_BU',
AU: 'C.F_SHFE_AU',
RB: 'C.F_SHFE_RB',
AG: 'C.F_SHFE_AG',
HC: 'C.F_SHFE_HC',
AL: 'C.F_SHFE_AL',
SN: 'C.F_SHFE_SN',
RU: 'C.F_SHFE_RU',
CU: 'C.F_SHFE_CU',
NI: 'C.F_SHFE_NI',
WR: 'C.F_SHFE_WR',
FU: 'C.F_SHFE_FU',
INE: 'C.INE',
};
const datas = this.boradFn(FESHMap, ...params);
return datas(type, this.fecSty[1])
};
// 大商所 ["焦炭","聚丙烯","铁矿石","鸡蛋","豆油","豆一","棕榈油","纤维板","聚氯乙烯","聚乙烯","豆粕","豆二","焦煤","玉米","胶合板","玉米淀粉"]
async FE_DCE_Board(type, ...params) {
const FEDCEMap = {
"ALL": 'C.DCE',
"J": "C.F_DCE_J",
"PP": "C.F_DCE_PP",
"I": "C.F_DCE_I",
"JD": "C.F_DCE_JD",
"Y": "C.F_DCE_Y",
"A": "C.F_DCE_A",
"P": "C.F_DCE_P",
"FB": "C.F_DCE_FB",
"V": "C.F_DCE_V",
"L": "C.F_DCE_L",
"M": "C.F_DCE_M",
"B": "C.F_DCE_B",
"JM": "C.F_DCE_JM",
"C": "C.F_DCE_C",
"BB": "C.F_DCE_BB",
"CS": "C.F_DCE_CS"
};
const datas = this.boradFn(FEDCEMap, ...params);
return datas(type, this.fecSty[1])
};
// ["一号棉花","强麦","棉纱","苹果","早籼稻","硅铁","普麦","晚籼稻","甲醇","菜粕","玻璃","锰硅","PTA","动力煤","粳稻","白糖","菜籽","菜油"]
async FE_CZCE_Board(type, ...params) {
const CZCEMap = {
"ALL": "C.CZCE",
"CF": "C.F_CZCE_CF",
"WH": "C.F_CZCE_WH",
"CY": "C.F_CZCE_CY",
"AP": "C.F_CZCE_AP",
"ER": "C.F_CZCE_ER",
"SF": "C.F_CZCE_SF",
"WT": "C.F_CZCE_WT",
"LR": "C.F_CZCE_LR",
"MA": "C.F_CZCE_MA",
"RM": "C.F_CZCE_RM",
"FG": "C.F_CZCE_FG",
"SM": "C.F_CZCE_SM",
"TA": "C.F_CZCE_TA",
"ZC": "C.F_CZCE_ZC",
"JR": "C.F_CZCE_JR",
"SR": "C.F_CZCE_SR",
"RS": "C.F_CZCE_RS",
"OI": "C.F_CZCE_OI"
};
const datas = this.boradFn(CZCEMap, ...params);
return datas(type, this.fecSty[1])
};
// 中金所
async FE_CFFEX_Board(type, ...params) {
const FEMap = {
CFFEX_ALL: 'R._168|_169',
CFFEX_5: 'C._TF_FO',
CFFEX_10: 'C._T_FO',
CFFEX_IC_FO: 'C._IC_FO',
CFFEX_IF_FO: 'C._IF_FO',
CFFEX_IH_FO: 'C._IH_FO'
}
const datas = this.boradFn(FEMap, ...params);
return datas(type, this.fecSty[1])
};
/**
* 香港期货
*
* @param {*} type
* @param {*} params
* @returns
* @memberof boards
*/
// async FECHKBoard(type, ...params) {
// // [港交所,指数期货]
// const FEMap = {
// HKSTOCKF: 'C.HEX.HKSTOCKF',
// HKINDEXF: 'C.HEX.HKINDEXF',
// }
// const datas = this.boradFn(FEMap, ...params);
// return datas(type, this.fecSty[1])
// };
// ["玉米","豆油","超国债","迷你玉米","年美国债","小型道指","燕麦","小麦","十年美国债","大豆","豆粕","五年美国债","乙醇","稻谷","迷你大豆","迷你小麦","COMEX铜","COMEX白银","COMEX黄金","微型黄金","迷你黄金","迷你白银","重柴油","布伦特原油","天然气","场内锌","综合铜","伦铜现","LmeS_铜","场内铜","LmeS_锌","LmeS_铅","综合铅","综合镍","伦镍现","LmeS_镍","伦铅现","场内铝","伦铝现","场内镍","LmeS_铝","场内铅","综合铝","场内锡","综合锌","伦锌现","LmeS_锡","综合锡","伦锡现","LmeS合金","棕榈油","糖号","棉花","NYMEX汽油","NYMEX燃油","迷你原油","NYMEX钯金","NYMEX铂金","热轧钢卷","NYMEX原油","现货黄金","现货白银","现货铂金","A期指","号合成胶","cst燃油","号烟片胶","日铂金","日煤油","日黄金","日原油","日钯金","日白银","日汽油","日橡胶"]
async FE_GLO_Board(type, ...params) {
const FEGLOMap = {
"ZC": "C.UF_COBOT_ZC",
"ZL": "C.UF_COBOT_ZL",
"UL": "C.UF_COBOT_UL",
"XC": "C.UF_COBOT_XC",
"US": "C.UF_COBOT_US",
"YM": "C.UF_COBOT_YM",
"ZO": "C.UF_COBOT_ZO",
"ZW": "C.UF_COBOT_ZW",
"TY": "C.UF_COBOT_TY",
"ZS": "C.UF_COBOT_ZS",
"ZM": "C.UF_COBOT_ZM",
"FV": "C.UF_COBOT_FV",
"EH": "C.UF_COBOT_EH",
"ZR": "C.UF_COBOT_ZR",
"XK": "C.UF_COBOT_XK",
"XW": "C.UF_COBOT_XW",
"HG": "C.UF_COMEX_HG",
"SI": "C.UF_COMEX_SI",
"GC": "C.UF_COMEX_GC",
"MGC": "C.UF_COMEX_MGC",
"QO": "C.UF_COMEX_QO",
"QI": "C.UF_COMEX_QI",
"G": "C.UF_IPE_G",
"B": "C.UF_IPE_B",
"M": "C.UF_IPE_M",
"LZN": "C.UF_LME_LZN",
"LCP": "C.UF_LME_LCP",
"CPR": "C.UF_LME_CPR",
"LLD": "C.UF_LME_LLD",
"LNK": "C.UF_LME_LNK",
"NKR": "C.UF_LME_NKR",
"LDR": "C.UF_LME_LDR",
"LAL": "C.UF_LME_LAL",
"ALR": "C.UF_LME_ALR",
"LDD": "C.UF_LME_LDD",
"LTN": "C.UF_LME_LTN",
"ZHR": "C.UF_LME_ZHR",
"TNR": "C.UF_LME_TNR",
"LAA": "C.UF_LME_LAA",
"MPM": "C.UF_MDEX_MPM",
"SB": "C.UF_NYBOT_SB",
"CT": "C.UF_NYBOT_CT",
"RB": "C.UF_NYMEX_RB",
"HO": "C.UF_NYMEX_HO",
"QM": "C.UF_NYMEX_QM",
"PA": "C.UF_NYMEX_PA",
"PL": "C.UF_NYMEX_PL",
"HR": "C.UF_NYMEX_HR",
"CL": "C.UF_NYMEX_CL",
"AU": "C.UF_SGE_AU",
"AG": "C.UF_SGE_AG",
"PT": "C.UF_SGE_PT",
"CN": "C.UF_SGX_CN",
"TF": "C.UF_SGX_TF",
"FB": "C.UF_SGX_FB",
"RT": "C.UF_SGX_RT",
"JPL": "C.UF_TOCOM_JPL",
"JKE": "C.UF_TOCOM_JKE",
"JAU": "C.UF_TOCOM_JAU",
"JCO": "C.UF_TOCOM_JCO",
"JPA": "C.UF_TOCOM_JPA",
"JAG": "C.UF_TOCOM_JAG",
"JGL": "C.UF_TOCOM_JGL",
"JRU": "C.UF_TOCOM_JRU"
}
const datas = this.boradFn(FEGLOMap, ...params);
return datas(type, this.fecSty[1])
};
/**
* 金融期货GOL
* @param {*} type
* @param {*} params
*/
async FE_FINANCE_Board(type = 'ALL', ...params) {
const FINAMap = {
'ALL': 'R._ZJMF_Main_MonetaryFutures|_UMF_Main_MonetaryFutures'
};
const datas = this.boradFn(FINAMap, ...params);
return datas(type, this.fecSty[1])
};
/**
* 能源化工
*
* @param {string} [type='ALL']
* @param {*} params
* @returns promise
* @memberof futures
*/
async FE_ENERGY_Board(type = 'ALL', ...params) {
const ENERGYMap = {
'ALL': 'R._F_MAIN_ENERGY|_UF_MAIN_ENERGY'
};
const datas = this.boradFn(ENERGYMap, ...params);
return datas(type, this.fecSty[1])
};
/**
* 金属钢材
*
* @param {string} [type='ALL']
* @param {*} params
* @memberof futures
*/
async FE_METAL_Board(type = 'ALL', ...params) {
const METALMap = {
'ALL': 'R._F_MAIN_METAL|_UF_MAIN_METAL'
};
const datas = this.boradFn(METALMap, ...params);
return datas(type, this.fecSty[1])
};
//
/**
* 农产品食品原料
*
* @param {string} [type='ALL']
* @param {*} params
* @returns
* @memberof futures
*/
async FE_FARM_Board(type = 'ALL', ...params) {
const FARMMap = {
'ALL': 'R._F_MAIN_FARMPRODUCE|_UF_MAIN_FARMPRODUCE'
};
const datas = this.boradFn(FARMMap, ...params);
return datas(type, this.fecSty[1])
}
}
const client = new futures();?
// client.FE_DCE_Board('ALL', 1, 50).then(res => console.log(res));
client.FE_FARM_Board().then(res => console.log(res))股票类 stocks.js
/*************************************************************
*
*- Copyright (c), 股票数据API接口, 2018, lopo
*- FileName : stocks.js 股票
*- Author : 罗波 Version : 1.0 Date:2018-8-4
*- Descripttion : 股票 Node Express
*- Other : 数据来源-东方财富
*- JSVersion : ES6
*- Modules :
* 1.request
*
*----------------------------------------------------------
*- Class List :
* 1.getBoard
*- Function List :
* - tools
* 1. getCodeExchange
*
*- Class List :
* 1. stocks
*
*- History :
* <Author> <Date> <Desc> <Modi>
*
**************************************************************/
'use strict'
const getBoard = require('../../utils/getBoard');
//
class stocks extends getBoard {
constructor() {
super();
this.stockSty = ['FCOIATC', 'CTBF', 'FPGBKI', 'CTF', 'FCABHL', 'FCRH']
};
/**
* 沪深个股集合
*
* - params
* @param {String|Array} code 股票代码 可以为 600000 | 600000,600001 | [600000,600001]
*
* - Desc 多用于个股自选接口用
**/
StockList(code) {
const opt = {
qs: {
cmd: this.setCodeCmd(code),
sty: this.stockSty[1]
}
}
return this.getDatas({...opt })
};
/**
* 沪深股市
*
* @param {String} type 指定字符串 1
*
* @returns Promise
*
* @memberof boards
*/
async HSBoard(type, ...params) {
// [沪深A股,上证A股,深证A股,新股,中小板,创业板,沪AB股比价,深AB股比价,B股,AB股比价,风险警示,两网及退市]
const boardMap = {
CHSA: 'C._A',
CHA: 'C.2',
CSA: 'C._SZAME',
CHSN: 'C.BL05011',
CHSZX: 'C.13',
CHSCY: 'C.80',
CHAB: 'C._ABPCSHZ',
CHSAB: 'C._ABPCSZZ',
CHSB: 'C._B',
CHSAB_AH: 'C._ABPCSHZ',
CHSW: 'C._AB_FXJS',
CHSE: 'R.__40|__42'
};
const datas = this.boradFn(boardMap, ...params);
return datas(type, type !== 'CHSAB_AH' ? this.stockSty[0] : this.stockSty[4])
};
/**
* 行业板块
* <type> <name> <desc> <default> <Must>
* @param {String} type 指定字符串 1
* @param {Number} p 当前页
* @param {Number} ps 每页数量 20
* @param {String} st 排序字段
* @param {Number} sr 排序方式 -1,1
*
* @returns promise
*
* @memberof boards
*/
async BKBoard(type, ...params) {
// [行业板块,地域板块,概念板块]
const BKMap = {
BKHY: 'C._BKHY',
BKDY: 'C._BKDY',
BKGN: 'C._BKGN'
};
const datas = this.boradFn(BKMap, ...params);
return datas(type, this.stockSty[2])
};
/**
* 国内指数
*
* @param {*} type
* @param {*} params
* @returns promise
* @memberof boards
*/
async ZSBoard(type, ...params) {
// [上证指数,深证指数,指数成分]
const ZSMap = {
ZSSH: 'C.1',
ZSSZ: 'C.5',
ZSALL: 'C.IE.ALL'
};
const datas = this.boradFn(ZSMap, ...params);
return datas(type)
};
/**
* 港股通
*
* @param {*} type
* @param {*} params
* @returns promise
* @memberof boards
*/
async HSGTBoard(type, ...params) {
// [沪股通,深股通,港股通(沪),港股通(深)]
const HSGTMap = {
SH_HK: 'C.BK07071',
SZ_HK: 'C.BK08041',
HK_SH: 'C.MK0144',
HK_SZ: 'C.MK0146',
};
const datas = this.boradFn(HSGTMap, ...params);
return datas(type)
};
/**
* 港股列表
*
* @param {*} type
* @param {*} params
* @returns promise
* @memberof boards
*/
async HKBoard(type, ...params) {
// [所有港股,主力港股,创新港股,知名港股,港股蓝筹,港股红筹,港股红筹指数成分股,国企股,国企指数成分股,港股通成分股,HS综合大型,HS综合中型,AH比价,ADR,恒生指数]
// 比价参数 client.HKBoard('AH_COMP', 1, 5, '(AB/AH/HKD)').then(res => console.log(res))
const HKMap = {
HKALL: 'C._HKS',
HKMAIN: 'C.MK0107',
HKGEM: 'C.__28GEM',
HKWELL: 'C.MK0009',
HKBLUE: 'C.MK0104',
HKRED: 'C.MK0102',
HKRED_COMP: 'C.__28HSCIINDEX',
HKSTATE: 'C.__28HSCEI',
HKSTATE_COMP: 'C.__28HSCEIINDEX',
HK_COMP: 'C.MK0144',
HSI_LG_COMP: 'C.MK0141',
HSI_MD_COMP: 'C.MK0142',
AH_COMP: 'C._AHH',
HK_ADR: 'C._ADRA',
HS_ZS: 'R.HKI|HKIN|HS',
HK_WARRANTS: 'C._HKW'
};
const datas = this.boradFn(HKMap, ...params);
return datas(type, type !== 'AH_COMP' || type !== 'HS_ZS' || type !== 'HK_WARRANTS' ? this.stockSty[3] : this.stockSty[4])
};
/**
* 美股
*
* @param {*} type
* @param {*} params
* @returns promise
* @memberof boards
*/
async USBoard(type, ...params) {
// [全部美股,[知名美股,[美科技股,金融,医药食品,媒体,汽车能源,制造零售]],[中国概念,中国互联网],美股指数]
const USMap = {
USALL: 'C._UNS',
USWELL: 'R.MK0216|MK0217|MK0218|MK0219|MK0220|MK0221',
USTECH: 'C.MK0216',
USFINA: 'C.MK0217',
USMEDI_FOOD: 'C.MK0218',
USMEDIA: 'C.MK0219',
USRAUTO_ENGNIN: 'C.MK0220',
USMADE_RETA: 'C.MK0221',
US_CN: 'R.MK0214|MK0212|MK0213|MK0202',
US_CNET: 'C.MK0202',
USZS: 'C._UI_MAP_USOA'
}
const datas = this.boradFn(USMap, ...params);
return datas(type)
};
/**
* 全球指数
*
* @param {*} type
* @param {*} params
* @returns promise
* @memberof boards
*/
async GLOBoard(type, ...params) {
// [亚洲,美洲,欧洲,澳洲]
const GLOMap = {
ASIA: 'R.0000011,3990012,0003001,3990062,3990052,HSI5,HSCEI5,HSCCI5|_UI_MAP_ASIA',
AMERICA: 'C._UI_MAP_AME',
EURO: 'C._UI_MAP_EUR',
AUSTRALIA: 'C._UI_MAP_AUS'
}
const datas = this.boradFn(GLOMap, ...params);
return datas(type, this.stockSty[5])
};
};
module.exports = stocks;
const client = new stocks();
// client.getStockFullInfo('600803,600601').then(res => console.log(res));
// client.HSBoard('CHSA', 1, 2).then(res => console.log(res));
// client.HSBoard('CHSAB_AH', 1, 2, '(AB/AH/USD)').then(res => console.log(res));
// client.BKBoard('BKDY', 1, 5).then(res => console.log(res));
client.ZSBoard('ZSSH', 1, 15).then(res => console.log(res));
// client.HSGTBoard('HK_SH', 1, 5).then(res => console.log(res));
// client.HKBoard('AH_COMP', 1, 5, '(AB/AH/HKD)').then(res => console.log(res));
// client.HKBoard('HK_WARRANTS', 1, 5).then(res => console.log(res));
// client.ZSHSBoard('ALL', 1, 5).then(res => console.log(res));
// client.USBoard('USZS', 1, 5).then(res => console.log(res));
// client.HSBoard('CHSA', 1, 10000).then(res => console.log(res))
// client.FECHKBoard('HKSTOCKF').then(res => console.log(res)); 查看全部

以下代码仅仅用作学习交流,请勿用作其他用途


?基础类 getBoard
 /*************************************************************
*
*- Copyright (c), 股票数据API接口基础Class, 2018, lopo
*- FileName : default.js
*- Author : 罗波 Version : 1.0 Date:2018-8-4
*- Descripttion : 股票 Node Express
*- Other : 数据来源-东方财富
*- JSVersion : ES6
*- Modules :
* 1.request
* 2.cheerio
*
*----------------------------------------------------------
*
*- Function List :
* - tools
* 1. getCodeExchange
*
*- Class List :
* 1. boards
*
*- History :
* <Author> <Date> <Desc> <Modi>
*
**************************************************************/
'use strict'
const request = require('request-promise-native');
const { getCodeExchange } = require('../utils/tools');

/**
* 基础配置
*
* @class getBoards
*/
class getBoards {
// 基础参数
constructor() {
// - stockSty : ['列表概览','个股详细']
this.reqOpt = {
uri: 'http://nufm.dfcfw.com/EM_Finance2014NumericApplication/JS.aspx',
qs: {
type: 'CT',
token: '64a483cbad8b666efa51677820e6b21c',
js: '({data:[(x)],totle:(tot)})',
'_': (new Date()).valueOf(),
}
};
// _rp_options.qs
this.request = request.defaults(this.reqOpt);
// - pageFn : p/当前页 ps/页数据量 st/排序字段 sr/排序方式
//* @param {Number} p 当前页
//* @param {Number} ps 每页数量 20
//* @param {String} st 排序字段
//* @param {Number} sr 排序方式 -1,1
this.pageOpt = (params) => {
return {
qs: {
p: params[0] || '1',
ps: params[1] || '20',
st: params[2] || '(ChangePercent)',
sr: params[3] || '-1'
}
}
};
/**
*
* @param {Objec} cmdMap 参数比对数组
* @param {Object} params page参数
* @param {String} type 参数
* @param {String} sty 展示方式
*
* @returns promise
*
* @memberof boards
*/
this.boradFn = (cmdMap, ...params) => (type, sty = this.stockSty[0]) => {
const boardMap = cmdMap;
if (!boardMap[type]) return { 'err': 'TypeError' };
const opt = this.pageOpt([...params]);
Object.assign(opt.qs, {
'cmd': boardMap[type],
'sty': sty
});
return this.getDatas({...opt })
};
};
/**
*格式化cmd配置亲求
*
* @param {String,Arrary} codes 股票代码格式化
* @returns String
* @memberof boards
*/
setCodeCmd(codes) {
if (codes.includes(',')) codes = codes.split(',');
return Array.isArray(codes) ? codes.reduce((a, b, i) => a += `${getCodeExchange(b)}${i<codes.length-1?',':''}`, '') : `${getCodeExchange(codes)}`
};
/**
*request 请求函数
*
* @param {Object} opt 请求配置
* @returns promise
* @memberof boards
*/
async getDatas(opt) {
try {
const datas = await this.request({...opt });
return await eval(datas);
} catch (error) {
return { 'err': 'NETError' }
}
};
}
module.exports = getBoards
辅助类 tool.js
const fixMoney = (money, fix = 2) => (money / (money.length < 8 ? 10000 : 100000000)).toFixed(fix) + (money.length < 8 ? '万' : '亿');
//
const timeout = async(ms) => {
await new Promise((resolve) => {
setTimeout(resolve, ms);
});
};
const getCodeExchange = (code, type = "stock") => {
let codes = code.substr(0, 3);
if (type === 'stock') return (codes === '000' || codes === '002') && `${code}2` || (codes === '600' || codes === '601' || codes === '603') && `${code}1` || `${code}2`
else return codes === '399' ? `${code}2` : `${code}1`
};
module.exports = {
fixMoney,
timeout,
getCodeExchange
}
基金 fund.js
 /*************************************************************
*
*- Copyright (c), 股票数据API接口 基金, 2018, lopo
*- FileName : default.js
*- Author : 罗波 Version : 1.0 Date:2018-8-4
*- Descripttion : 股票 Node Express
*- Other : 数据来源-东方财富
*- JSVersion : ES6
*
*----------------------------------------------------------
*- Class List :
* 1.getBoard
*
*- History :
* <Author> <Date> <Desc> <Modi>
*
**************************************************************/
'use strict'
const getBoard = require('../../utils/getBoard');
//
class funds extends getBoard {
/**
* 封闭基金
*
* @param {string} [type='ALL']
* @param {*} params
* @returns
* @memberof futures
*/
// 封闭基金 ETF基金 LOF基金
async FUND_Board(type = 'ALL', ...params) {
const ENERGYMap = {
'CLOSE_END': 'C.__285002',
'ETF': 'C.__2850013',
'LOF': 'C.__2850014'
};
const datas = this.boradFn(ENERGYMap, ...params);
return datas(type, 'FCOIATC')
};

}
const client = new funds();?
// client.FE_DCE_Board('ALL', 1, 50).then(res => console.log(res));
client.FUND_Board('CLOSE_END', 1, 50).then(res => console.log(res))
期货类 futures.js
 /*************************************************************
*
*- Copyright (c), 股票数据API接口 期货, 2018, lopo
*- FileName : default.js
*- Author : 罗波 Version : 1.0 Date:2018-8-4
*- Descripttion : 股票 Node Express
*- Other : 数据来源-东方财富
*- JSVersion : ES6
*
*----------------------------------------------------------
*- Class List :
* 1.getBoard
*
*- Class List :
* 1. futures
*
*- History :
* <Author> <Date> <Desc> <Modi>
*
**************************************************************/
'use strict'
const getBoard = require('../../utils/getBoard');
//
class futures extends getBoard {
constructor() {
super();
this.fecSty = ['FCHKEGL', 'FCFL4O']
};
/**
* 国内期货数据
*
* @param {*} type
* @param {*} params
* @returns
* @memberof boards
*/
//[上期所所有,["沪锌", "沪铅", "石油沥青", "沪金", "螺纹钢", "沪银", "热轧卷板", "沪铝", "沪锡", "橡胶", "沪铜", "沪镍", "线材", "燃油","原油"]]
async FE_SH_Board(type, ...params) {
const FESHMap = {
ALL: 'C.SHFE',
ZN: 'C.F_SHFE_ZN',
PB: 'C.F_SHFE_PB',
BU: 'C.F_SHFE_BU',
AU: 'C.F_SHFE_AU',
RB: 'C.F_SHFE_RB',
AG: 'C.F_SHFE_AG',
HC: 'C.F_SHFE_HC',
AL: 'C.F_SHFE_AL',
SN: 'C.F_SHFE_SN',
RU: 'C.F_SHFE_RU',
CU: 'C.F_SHFE_CU',
NI: 'C.F_SHFE_NI',
WR: 'C.F_SHFE_WR',
FU: 'C.F_SHFE_FU',
INE: 'C.INE',
};
const datas = this.boradFn(FESHMap, ...params);
return datas(type, this.fecSty[1])
};
// 大商所 ["焦炭","聚丙烯","铁矿石","鸡蛋","豆油","豆一","棕榈油","纤维板","聚氯乙烯","聚乙烯","豆粕","豆二","焦煤","玉米","胶合板","玉米淀粉"]
async FE_DCE_Board(type, ...params) {
const FEDCEMap = {
"ALL": 'C.DCE',
"J": "C.F_DCE_J",
"PP": "C.F_DCE_PP",
"I": "C.F_DCE_I",
"JD": "C.F_DCE_JD",
"Y": "C.F_DCE_Y",
"A": "C.F_DCE_A",
"P": "C.F_DCE_P",
"FB": "C.F_DCE_FB",
"V": "C.F_DCE_V",
"L": "C.F_DCE_L",
"M": "C.F_DCE_M",
"B": "C.F_DCE_B",
"JM": "C.F_DCE_JM",
"C": "C.F_DCE_C",
"BB": "C.F_DCE_BB",
"CS": "C.F_DCE_CS"
};
const datas = this.boradFn(FEDCEMap, ...params);
return datas(type, this.fecSty[1])
};
// ["一号棉花","强麦","棉纱","苹果","早籼稻","硅铁","普麦","晚籼稻","甲醇","菜粕","玻璃","锰硅","PTA","动力煤","粳稻","白糖","菜籽","菜油"]
async FE_CZCE_Board(type, ...params) {
const CZCEMap = {
"ALL": "C.CZCE",
"CF": "C.F_CZCE_CF",
"WH": "C.F_CZCE_WH",
"CY": "C.F_CZCE_CY",
"AP": "C.F_CZCE_AP",
"ER": "C.F_CZCE_ER",
"SF": "C.F_CZCE_SF",
"WT": "C.F_CZCE_WT",
"LR": "C.F_CZCE_LR",
"MA": "C.F_CZCE_MA",
"RM": "C.F_CZCE_RM",
"FG": "C.F_CZCE_FG",
"SM": "C.F_CZCE_SM",
"TA": "C.F_CZCE_TA",
"ZC": "C.F_CZCE_ZC",
"JR": "C.F_CZCE_JR",
"SR": "C.F_CZCE_SR",
"RS": "C.F_CZCE_RS",
"OI": "C.F_CZCE_OI"
};
const datas = this.boradFn(CZCEMap, ...params);
return datas(type, this.fecSty[1])
};
// 中金所
async FE_CFFEX_Board(type, ...params) {
const FEMap = {
CFFEX_ALL: 'R._168|_169',
CFFEX_5: 'C._TF_FO',
CFFEX_10: 'C._T_FO',
CFFEX_IC_FO: 'C._IC_FO',
CFFEX_IF_FO: 'C._IF_FO',
CFFEX_IH_FO: 'C._IH_FO'
}
const datas = this.boradFn(FEMap, ...params);
return datas(type, this.fecSty[1])
};
/**
* 香港期货
*
* @param {*} type
* @param {*} params
* @returns
* @memberof boards
*/
// async FECHKBoard(type, ...params) {
// // [港交所,指数期货]
// const FEMap = {
// HKSTOCKF: 'C.HEX.HKSTOCKF',
// HKINDEXF: 'C.HEX.HKINDEXF',
// }
// const datas = this.boradFn(FEMap, ...params);
// return datas(type, this.fecSty[1])
// };
// ["玉米","豆油","超国债","迷你玉米","年美国债","小型道指","燕麦","小麦","十年美国债","大豆","豆粕","五年美国债","乙醇","稻谷","迷你大豆","迷你小麦","COMEX铜","COMEX白银","COMEX黄金","微型黄金","迷你黄金","迷你白银","重柴油","布伦特原油","天然气","场内锌","综合铜","伦铜现","LmeS_铜","场内铜","LmeS_锌","LmeS_铅","综合铅","综合镍","伦镍现","LmeS_镍","伦铅现","场内铝","伦铝现","场内镍","LmeS_铝","场内铅","综合铝","场内锡","综合锌","伦锌现","LmeS_锡","综合锡","伦锡现","LmeS合金","棕榈油","糖号","棉花","NYMEX汽油","NYMEX燃油","迷你原油","NYMEX钯金","NYMEX铂金","热轧钢卷","NYMEX原油","现货黄金","现货白银","现货铂金","A期指","号合成胶","cst燃油","号烟片胶","日铂金","日煤油","日黄金","日原油","日钯金","日白银","日汽油","日橡胶"]
async FE_GLO_Board(type, ...params) {
const FEGLOMap = {
"ZC": "C.UF_COBOT_ZC",
"ZL": "C.UF_COBOT_ZL",
"UL": "C.UF_COBOT_UL",
"XC": "C.UF_COBOT_XC",
"US": "C.UF_COBOT_US",
"YM": "C.UF_COBOT_YM",
"ZO": "C.UF_COBOT_ZO",
"ZW": "C.UF_COBOT_ZW",
"TY": "C.UF_COBOT_TY",
"ZS": "C.UF_COBOT_ZS",
"ZM": "C.UF_COBOT_ZM",
"FV": "C.UF_COBOT_FV",
"EH": "C.UF_COBOT_EH",
"ZR": "C.UF_COBOT_ZR",
"XK": "C.UF_COBOT_XK",
"XW": "C.UF_COBOT_XW",
"HG": "C.UF_COMEX_HG",
"SI": "C.UF_COMEX_SI",
"GC": "C.UF_COMEX_GC",
"MGC": "C.UF_COMEX_MGC",
"QO": "C.UF_COMEX_QO",
"QI": "C.UF_COMEX_QI",
"G": "C.UF_IPE_G",
"B": "C.UF_IPE_B",
"M": "C.UF_IPE_M",
"LZN": "C.UF_LME_LZN",
"LCP": "C.UF_LME_LCP",
"CPR": "C.UF_LME_CPR",
"LLD": "C.UF_LME_LLD",
"LNK": "C.UF_LME_LNK",
"NKR": "C.UF_LME_NKR",
"LDR": "C.UF_LME_LDR",
"LAL": "C.UF_LME_LAL",
"ALR": "C.UF_LME_ALR",
"LDD": "C.UF_LME_LDD",
"LTN": "C.UF_LME_LTN",
"ZHR": "C.UF_LME_ZHR",
"TNR": "C.UF_LME_TNR",
"LAA": "C.UF_LME_LAA",
"MPM": "C.UF_MDEX_MPM",
"SB": "C.UF_NYBOT_SB",
"CT": "C.UF_NYBOT_CT",
"RB": "C.UF_NYMEX_RB",
"HO": "C.UF_NYMEX_HO",
"QM": "C.UF_NYMEX_QM",
"PA": "C.UF_NYMEX_PA",
"PL": "C.UF_NYMEX_PL",
"HR": "C.UF_NYMEX_HR",
"CL": "C.UF_NYMEX_CL",
"AU": "C.UF_SGE_AU",
"AG": "C.UF_SGE_AG",
"PT": "C.UF_SGE_PT",
"CN": "C.UF_SGX_CN",
"TF": "C.UF_SGX_TF",
"FB": "C.UF_SGX_FB",
"RT": "C.UF_SGX_RT",
"JPL": "C.UF_TOCOM_JPL",
"JKE": "C.UF_TOCOM_JKE",
"JAU": "C.UF_TOCOM_JAU",
"JCO": "C.UF_TOCOM_JCO",
"JPA": "C.UF_TOCOM_JPA",
"JAG": "C.UF_TOCOM_JAG",
"JGL": "C.UF_TOCOM_JGL",
"JRU": "C.UF_TOCOM_JRU"
}
const datas = this.boradFn(FEGLOMap, ...params);
return datas(type, this.fecSty[1])
};
/**
* 金融期货GOL
* @param {*} type
* @param {*} params
*/
async FE_FINANCE_Board(type = 'ALL', ...params) {
const FINAMap = {
'ALL': 'R._ZJMF_Main_MonetaryFutures|_UMF_Main_MonetaryFutures'
};
const datas = this.boradFn(FINAMap, ...params);
return datas(type, this.fecSty[1])
};
/**
* 能源化工
*
* @param {string} [type='ALL']
* @param {*} params
* @returns promise
* @memberof futures
*/
async FE_ENERGY_Board(type = 'ALL', ...params) {
const ENERGYMap = {
'ALL': 'R._F_MAIN_ENERGY|_UF_MAIN_ENERGY'
};
const datas = this.boradFn(ENERGYMap, ...params);
return datas(type, this.fecSty[1])
};
/**
* 金属钢材
*
* @param {string} [type='ALL']
* @param {*} params
* @memberof futures
*/
async FE_METAL_Board(type = 'ALL', ...params) {
const METALMap = {
'ALL': 'R._F_MAIN_METAL|_UF_MAIN_METAL'
};
const datas = this.boradFn(METALMap, ...params);
return datas(type, this.fecSty[1])
};
//
/**
* 农产品食品原料
*
* @param {string} [type='ALL']
* @param {*} params
* @returns
* @memberof futures
*/
async FE_FARM_Board(type = 'ALL', ...params) {
const FARMMap = {
'ALL': 'R._F_MAIN_FARMPRODUCE|_UF_MAIN_FARMPRODUCE'
};
const datas = this.boradFn(FARMMap, ...params);
return datas(type, this.fecSty[1])
}
}
const client = new futures();?
// client.FE_DCE_Board('ALL', 1, 50).then(res => console.log(res));
client.FE_FARM_Board().then(res => console.log(res))
股票类 stocks.js
 /*************************************************************
*
*- Copyright (c), 股票数据API接口, 2018, lopo
*- FileName : stocks.js 股票
*- Author : 罗波 Version : 1.0 Date:2018-8-4
*- Descripttion : 股票 Node Express
*- Other : 数据来源-东方财富
*- JSVersion : ES6
*- Modules :
* 1.request
*
*----------------------------------------------------------
*- Class List :
* 1.getBoard
*- Function List :
* - tools
* 1. getCodeExchange
*
*- Class List :
* 1. stocks
*
*- History :
* <Author> <Date> <Desc> <Modi>
*
**************************************************************/
'use strict'
const getBoard = require('../../utils/getBoard');
//
class stocks extends getBoard {
constructor() {
super();
this.stockSty = ['FCOIATC', 'CTBF', 'FPGBKI', 'CTF', 'FCABHL', 'FCRH']
};
/**
* 沪深个股集合
*
* - params
* @param {String|Array} code 股票代码 可以为 600000 | 600000,600001 | [600000,600001]
*
* - Desc 多用于个股自选接口用
**/
StockList(code) {
const opt = {
qs: {
cmd: this.setCodeCmd(code),
sty: this.stockSty[1]
}
}
return this.getDatas({...opt })
};
/**
* 沪深股市
*
* @param {String} type 指定字符串 1
*
* @returns Promise
*
* @memberof boards
*/
async HSBoard(type, ...params) {
// [沪深A股,上证A股,深证A股,新股,中小板,创业板,沪AB股比价,深AB股比价,B股,AB股比价,风险警示,两网及退市]
const boardMap = {
CHSA: 'C._A',
CHA: 'C.2',
CSA: 'C._SZAME',
CHSN: 'C.BL05011',
CHSZX: 'C.13',
CHSCY: 'C.80',
CHAB: 'C._ABPCSHZ',
CHSAB: 'C._ABPCSZZ',
CHSB: 'C._B',
CHSAB_AH: 'C._ABPCSHZ',
CHSW: 'C._AB_FXJS',
CHSE: 'R.__40|__42'
};
const datas = this.boradFn(boardMap, ...params);
return datas(type, type !== 'CHSAB_AH' ? this.stockSty[0] : this.stockSty[4])
};
/**
* 行业板块
* <type> <name> <desc> <default> <Must>
* @param {String} type 指定字符串 1
* @param {Number} p 当前页
* @param {Number} ps 每页数量 20
* @param {String} st 排序字段
* @param {Number} sr 排序方式 -1,1
*
* @returns promise
*
* @memberof boards
*/
async BKBoard(type, ...params) {
// [行业板块,地域板块,概念板块]
const BKMap = {
BKHY: 'C._BKHY',
BKDY: 'C._BKDY',
BKGN: 'C._BKGN'
};
const datas = this.boradFn(BKMap, ...params);
return datas(type, this.stockSty[2])
};
/**
* 国内指数
*
* @param {*} type
* @param {*} params
* @returns promise
* @memberof boards
*/
async ZSBoard(type, ...params) {
// [上证指数,深证指数,指数成分]
const ZSMap = {
ZSSH: 'C.1',
ZSSZ: 'C.5',
ZSALL: 'C.IE.ALL'
};
const datas = this.boradFn(ZSMap, ...params);
return datas(type)
};
/**
* 港股通
*
* @param {*} type
* @param {*} params
* @returns promise
* @memberof boards
*/
async HSGTBoard(type, ...params) {
// [沪股通,深股通,港股通(沪),港股通(深)]
const HSGTMap = {
SH_HK: 'C.BK07071',
SZ_HK: 'C.BK08041',
HK_SH: 'C.MK0144',
HK_SZ: 'C.MK0146',
};
const datas = this.boradFn(HSGTMap, ...params);
return datas(type)
};
/**
* 港股列表
*
* @param {*} type
* @param {*} params
* @returns promise
* @memberof boards
*/
async HKBoard(type, ...params) {
// [所有港股,主力港股,创新港股,知名港股,港股蓝筹,港股红筹,港股红筹指数成分股,国企股,国企指数成分股,港股通成分股,HS综合大型,HS综合中型,AH比价,ADR,恒生指数]
// 比价参数 client.HKBoard('AH_COMP', 1, 5, '(AB/AH/HKD)').then(res => console.log(res))
const HKMap = {
HKALL: 'C._HKS',
HKMAIN: 'C.MK0107',
HKGEM: 'C.__28GEM',
HKWELL: 'C.MK0009',
HKBLUE: 'C.MK0104',
HKRED: 'C.MK0102',
HKRED_COMP: 'C.__28HSCIINDEX',
HKSTATE: 'C.__28HSCEI',
HKSTATE_COMP: 'C.__28HSCEIINDEX',
HK_COMP: 'C.MK0144',
HSI_LG_COMP: 'C.MK0141',
HSI_MD_COMP: 'C.MK0142',
AH_COMP: 'C._AHH',
HK_ADR: 'C._ADRA',
HS_ZS: 'R.HKI|HKIN|HS',
HK_WARRANTS: 'C._HKW'
};
const datas = this.boradFn(HKMap, ...params);
return datas(type, type !== 'AH_COMP' || type !== 'HS_ZS' || type !== 'HK_WARRANTS' ? this.stockSty[3] : this.stockSty[4])
};
/**
* 美股
*
* @param {*} type
* @param {*} params
* @returns promise
* @memberof boards
*/
async USBoard(type, ...params) {
// [全部美股,[知名美股,[美科技股,金融,医药食品,媒体,汽车能源,制造零售]],[中国概念,中国互联网],美股指数]
const USMap = {
USALL: 'C._UNS',
USWELL: 'R.MK0216|MK0217|MK0218|MK0219|MK0220|MK0221',
USTECH: 'C.MK0216',
USFINA: 'C.MK0217',
USMEDI_FOOD: 'C.MK0218',
USMEDIA: 'C.MK0219',
USRAUTO_ENGNIN: 'C.MK0220',
USMADE_RETA: 'C.MK0221',
US_CN: 'R.MK0214|MK0212|MK0213|MK0202',
US_CNET: 'C.MK0202',
USZS: 'C._UI_MAP_USOA'
}
const datas = this.boradFn(USMap, ...params);
return datas(type)
};
/**
* 全球指数
*
* @param {*} type
* @param {*} params
* @returns promise
* @memberof boards
*/
async GLOBoard(type, ...params) {
// [亚洲,美洲,欧洲,澳洲]
const GLOMap = {
ASIA: 'R.0000011,3990012,0003001,3990062,3990052,HSI5,HSCEI5,HSCCI5|_UI_MAP_ASIA',
AMERICA: 'C._UI_MAP_AME',
EURO: 'C._UI_MAP_EUR',
AUSTRALIA: 'C._UI_MAP_AUS'
}
const datas = this.boradFn(GLOMap, ...params);
return datas(type, this.stockSty[5])
};
};
module.exports = stocks;
const client = new stocks();
// client.getStockFullInfo('600803,600601').then(res => console.log(res));
// client.HSBoard('CHSA', 1, 2).then(res => console.log(res));
// client.HSBoard('CHSAB_AH', 1, 2, '(AB/AH/USD)').then(res => console.log(res));
// client.BKBoard('BKDY', 1, 5).then(res => console.log(res));
client.ZSBoard('ZSSH', 1, 15).then(res => console.log(res));
// client.HSGTBoard('HK_SH', 1, 5).then(res => console.log(res));
// client.HKBoard('AH_COMP', 1, 5, '(AB/AH/HKD)').then(res => console.log(res));
// client.HKBoard('HK_WARRANTS', 1, 5).then(res => console.log(res));
// client.ZSHSBoard('ALL', 1, 5).then(res => console.log(res));
// client.USBoard('USZS', 1, 5).then(res => console.log(res));
// client.HSBoard('CHSA', 1, 10000).then(res => console.log(res))
// client.FECHKBoard('HKSTOCKF').then(res => console.log(res));

Nodejs 常用helper函数(持续更新)

Nodejslopo1983 发表了文章 • 0 个评论 • 300 次浏览 • 2018-12-29 13:38 • 来自相关话题

const crypto = require('crypto');
const toArray = require('stream-to-array');
const sendToWormhole = require('stream-wormhole');
const moment = require('moment');
const Decimal = require('decimal');
const _Array = require('lodash/array')
const _Collection = require('lodash/collection')
Date.prototype.format = function () {
let s = '';
s += this.getFullYear() + '-';
s += (this.getMonth() + 1) + "-";
s += this.getDate();
return (s);
};
module.exports = {
/**
* GET querys 分离函数
* @param {String} pages 分页 [上一页最后一个数据的_id,分页数量] 分页数量默认为10
* @param {String} fields 需要展示的字段 a,b,c,d
* @param {String} unFields 不需要展示的字段 a,b,c,d
* @param {String} querys 查询条件 如 a=1&b=2&c=3
* @param {String} orderBy 排序字段
* @param {String} sort 排序方式【1:正序,-1:倒序】(必须与orderBy同时出现)
* @param {String} dates 时间范围/时间查询 [开始时间,结束时间]/查询时间
*
* @version 1.0.1
* 2018/10/17 修改返回值类型可直接供mongoose使用
*
* @example
* GET /user/?orderBy=compName&sort=-1&pages=20,1&fields=compName,compAuth,compEmail&unFields=status&dates=2018-8-5&name=lopo
* index(){
* const {ctx} = this;
* const {queryParamFn} = ctx.helper;
* ctx.body = queryParamFn(ctx.query);
* }
*
* @return {Object}
* {
* querys: { name: 'lopo' },
* select: { compName: 1, compAuth: 1, compEmail: 1, status: 0 },
* pages: { marker: '1', limit: '20' },
* sort: { compName: '-1' },
* dates: '2018-8-5'
* }
*/
queryParamFn(querys, db = false) {
const mapFN = (str) => (field, val) => str[field].split(',').reduce((a, b) => {
a[b] = val;
return a
}, {});
let strToArr = mapFN(querys);
const sort = !!querys.sort ? JSON.parse(querys.sort) : {};
// const dates = querys.dates || [];
const fields = !!querys.fields ? strToArr('fields', 1) : {};
const unFields = !!querys.unFields ? strToArr('unFields', 0) : {};
const limit = !!querys.limit ? querys.limit * 1 : 10;
const marker = querys.marker || ''
const dateFn = () => {
if (!!dates) {
const ARR = dates.split(',');
return ARR.length === 2 ? {
'$gte': ARR[1],
'$lte': ARR[0]
} : `${ARR}`
}
}
delete querys.pages;
delete querys.fields;
delete querys.unFields;
delete querys.sort;
delete querys.dates;
delete querys.marker;
delete querys.limit;
delete querys._;
delete querys._type;
return {
'querys': {
...querys,
} || {},
'select': {
...fields,
...unFields
},
limit,
marker,
sort,
// ...!!dates && {
// 'dates': dateFn()
// }
}
},
/**
* PD分离
* @param {String} psd PD
* @param {String} pilipala chicken
*
* @return {String} 返回分离后的字符串
*/
bilibole(psd, pilipala) {
let bilibole = crypto.createHash('md5').update(`${psd}:${pilipala}`).digest('hex');
return `${pilipala}${bilibole}`;
},
/**
* @name stream 转buffer
*
* @param {files} stream 文件流
* @param {Function} fn 文件处理函数
*
* @return {String:Buffer} 返回Buferr 可用函数对Buffer进行处理后返回
*/
async stream2buf(stream, fn) {
try {
const parts = await toArray(stream);
const buf = Buffer.concat(parts);
return !!fn ? fn(buf) : buf
} catch (err) {
await sendToWormhole(stream);
throw err;
};
},
/**
* len 随机位数 默认6位
* @param {Number} len 随机数字长度 默认为6
*/
roundCode: (len = 6) => {
let pwd = "";
for (let idx = 0; idx < len; idx++) {
let seed = parseInt(Math.random() * 9);
pwd += seed;
}
return pwd;
},
/**
* 获取token函数
* @param {String} type token类型 ['BAIDUAip','WEWork','WeiXin','DirectAdmin','openstack']
*/
async TokenFn(type) {
const {
ctx,
config
} = this;
let datas;
const DirectAdmin = config.vhost.DirectAdmin;
const wechatApi = config.wechatApi;
const openstackApi = config.openstack;
const tokenMapFn = async () => {
const maps = {
// 百度token
'BAIDUAip': await ctx.curl('aip.baidubce.com/oauth/2.0/token', {
method: 'POST',
dataType: 'json',
data: {
'grant_type': 'client_credentials',
'client_id': config.baiduAIP.API_KEY,
'client_secret': config.baiduAIP.SECRET_KEY
}
}),
// 企业微信token
'WEWork': await ctx.curl('https://qyapi.weixin.qq.com/cgi-bin/gettoken', {
dataType: 'json',
data: {
'corpid': config.weWork.corpid,
'corpsecret': config.weWork.corpsecret
}
}),
// 微信token
'WeiXin': await ctx.curl('https://api.weixin.qq.com/cgi-bin/token', {
dataType: 'json',
data: {
'grant_type': 'client_credential',
'appid': wechatApi.appId,
'secret': wechatApi.appSecret
}
}),
// // DA token
// 'DirectAdmin': await ctx.curl(`${DirectAdmin.server}:${DirectAdmin.port}/CMD_LOGIN`, {
// method: 'POST',
// data: {
// 'username': DirectAdmin.username,
// 'password': DirectAdmin.password
// }
// }),
// openstack token
'openstack': await ctx.curl(`${openstackApi.uri}:5000/v3/auth/tokens?nocatalog`, {
method: 'POST',
dataType: 'json',
headers: {
'Content-Type': 'application/json',
},
content: `{
"auth": {
"identity": {
"methods": ["password"],
"password": {
"user": {
"name": "${openstackApi.name}",
"password": "${openstackApi.password}",
"domain": { "name": "Default" }
}
}
},
"scope": {
"project": {
"domain": {
"id": "default"
},
"name": "admin"
}
}
}
}`,
})
};
return maps[type]
};
const setResult = async () => {
if (type !== 'DirectAdmin') {
const data = await tokenMapFn();
return {
tokenValue: type === 'openstack' ? data.headers['x-subject-token'] : data.data.access_token,
tokenType: type,
tokenExpiresIn: type === 'openstack' ? new Date(data.data.token.expires_at) - 0 - 60 * 1000 : data.data.expires_in * 1000 + (new Date() - 0)
}
} else {
const cookies = (await tokenMapFn()).res.headers['set-cookie'][0].split(';');
return {
tokenValue: cookies[0],
tokenType: 'DirectAdmin',
tokenExpiresIn: (new Date(cookies[2].split('=')[1]) - 0) + 8 * 60 * 60 * 1000
}
}
};
const setToken = async () => {
const saveToken = await ctx.model.Tokens.findOneAndUpdate({
'tokenType': type
}, { ...(await setResult())
}, {
upsert: true,
new: true
});
datas = saveToken.tokenValue;
};
const tokens = await ctx.model.Tokens.findOne({
'tokenType': type
});
!!tokens && tokens.tokenExpiresIn > (new Date - 0) ? datas = tokens.tokenValue : await setToken();
return {
data: datas
}
},
/**
* mongo报错处理
*/
mongoError(error) {
const errorMap = {
'11000': ''
}
},
/**
* 函数柯里化
*/
curry: (fn, arity = fn.length, ...args) => arity <= args.length ? fn(...args) : curry.bind(null, fn, arity, ...args),
/**
* moment mix
* @param {String} scope [/day/week/month/quarter/year] 日/周/月/季度/年
* @int {Number} int 数值
*/
// 当前时间
getThisMoment: (scope) => {
return {
startDate: moment().startOf(scope).format(),
endDate: moment().format()
}
},
// 之前
getPrevMoment: (scope) => {
return {
startDate: moment().week(moment()[scope]() - 1).startOf(scope).format(),
endDate: moment().week(moment()[scope]() - 1).endOf(scope).format()
}
},
// 时间差 type [add,subtract]
subtractMoment: (int, scope, type = "subtract", withFormat = true, fn) => {
const dates = moment()[type](int, scope);
return !fn ? (withFormat ? dates.format() : dates) : fn(dates);
},
/**
* @name 格式化时间
* @param {String} formater 格式化输出标准 YYYY/MM/DD/HH/mm/ss/SSS
* @param {Date} dates 日期
* @param {Boolean} utc UTC
*/

formatDate: ({
formater,
dates
} = {}, utc = false) => {
const DATE = moment(!!dates ? dates : new Date());
return utc ? DATE.utc().format(!!formater && formater) : DATE.format(!!formater && formater)
},
// 生成时间数组
getDayAll: (begin, end) => {
let dateAllArr = new Array();
let ab = moment(begin).format("YYYY-MM-DD").split("-");
const ae = moment(end).format("YYYY-MM-DD").split("-");
const db = new Date();
db.setUTCFullYear(ab[0], ab[1] - 1, ab[2]);
const de = new Date();
de.setUTCFullYear(ae[0], ae[1] - 1, ae[2]);
const unixDb = db.getTime();
const unixDe = de.getTime();
for (let k = unixDb; k <= unixDe;) {
dateAllArr.push(new Date(parseInt(k)).format().toString());
k = k + 24 * 60 * 60 * 1000;
}
return dateAllArr;
},
getCTX: () => app.createAnonymousContext(),
// 发送短信,邮件,企业微信通知
async sendWarning(ctx, content, _id, compName) {
const auth = ctx.state.user;
const {
compEmail,
compPhone
} = await ctx.model.Company.findOne({
'_id': _id
}, {
'compEmail': 1,
'compPhone': 1
});
// await ctx.service.wework.message.send({ totag: 2, content: content.replace(/您/g, `${compName}`) });
await ctx.service.sms.svipwang.create(compPhone, content);
await ctx.service.sms.mail.sendMailDefault({
subject: '系统消息通知',
to: compEmail,
html: content
});
},
// 清理paramsFN()
deleteUndefined: (object) => {
for (let key in object) {
key !== 'detail' && delete object[(object[key] === undefined || object[key] === '' || object[key] === '[]') && key]
}
return object
},
/******************************************************************************************/
/**
* class DistributedEdit extends mix(class1, class2)
*/
// mixins
mix: (...mixins) => {
class Mix {}

for (let mixin of mixins) {
copyProperties(Mix.prototype, mixin); // 拷贝实例属性
copyProperties(Mix.prototype, Reflect.getPrototypeOf(mixin)); // 拷贝原型属性
}

return Mix;
},
// mixinsParams
copyProperties: (target, source) => {
for (let key of Reflect.ownKeys(source)) {
if (key !== "constructor" &&
key !== "prototype" &&
key !== "name"
) {
let desc = Object.getOwnPropertyDescriptor(source, key);
Object.defineProperty(target, key, desc);
}
}
},
/***************end****************/
/**
* 货币计算 mix
* @param {String} method [add/+,sub/-,mul/*,div//] 加减乘除
* @param {Array} money 需要计算的数字【前,后】
*/
decimalCash: (method = "add", money) => {
return new Decimal(money[0])[method](new Decimal(money[1])).toNumber()
},
// decimaFN: () => { return this.decimalCash() },
// decimalAdd: () => { return this.decimaFN('add') },
// decimalSub: () => { return this.decimaFN('sub') },
// decimalMul: () => { return this.decimaFN('mul') },
// decimalDiv: () => { return this.decimaFN('div') },
/****************end***************/
/**
* MONGO type转换
* @param {Array} ARR 字符串字典
* @param {Number} value 值
* @param {Boolean} hasZero 是否从0开始
*/
NumToStr(ARR, value, hasZero = false) {
return `${value}|${ARR[hasZero ? value : value - 1]}`
},
/**
* @name mongoDB update数组参数
*
*/
objToQuery(OBJ, KEY) {
return Object.keys(OBJ).reduce((a, b) => {
a[`${KEY}.$.${b}`] = OBJ[b];
return a
}, {});
},
/**
* @name 线转树
* @param {Array} ARR 需要处理的数组
* @param {String} keyName 作为分类的字段
*/
Array2Object: (ARR, keyName) => {
return ARR.reduce((a, b) => {
const keys = b[keyName];
if (a[keys] === undefined) a[keys] = [];
a[keys].push(b);
return a;
}, {})
},
/**
* @name 字典排序
*/
OBJKeySort: (obj) => {
const newkey = Object.keys(obj).sort();
const newObj = {};
for (var i = 0; i < newkey.length; i++) {
newObj[newkey[i]] = obj[newkey[i]];
}
return newObj;
},
/**
* @name 字母升序
*
* @param {String} chars 字母
* @param {Number} num 进几位
*
*/
nextChars: (chars, num, type = 'lower') => {
const char = chars.toLowerCase();
const isChar = /^[a-zA-Z]*$/.test(char);
const cx = char.charCodeAt(0);
const CHARS = (!!isChar && cx + num < 123) ? String.fromCharCode(char.charCodeAt(0) + num) : false;
return !!CHARS ? type === 'upper' ? CHARS.toUpperCase() : CHARS : 'Params Error'
},
/**
* @name 扁平化数组 fr : _.Array
*
* @param {Array} arr 数组
*
*/
deepFlatten: arr => _Array.flatten(arr),
/**
* @name 集合排序 fr : _.Collection
*
* @param {String|Function|Array|Object} 排序字段
* @param {String} asc|desc 排序方式
*
*/
orderBy: (arr, iteratees, order = 'asc') => _Collection.orderBy(arr, iteratees, order),
/**
* @name 隐藏手机号中间4位
*
*@param {String} telphone 手机号码
*/
hidePhone: (telphone) => {
const reg = /^(\d{3})\d{4}(\d{4})$/;
return telphone.replace(reg, `$1****$2`)
}
}; 查看全部
const crypto = require('crypto');
const toArray = require('stream-to-array');
const sendToWormhole = require('stream-wormhole');
const moment = require('moment');
const Decimal = require('decimal');
const _Array = require('lodash/array')
const _Collection = require('lodash/collection')
Date.prototype.format = function () {
let s = '';
s += this.getFullYear() + '-';
s += (this.getMonth() + 1) + "-";
s += this.getDate();
return (s);
};
module.exports = {
/**
* GET querys 分离函数
* @param {String} pages 分页 [上一页最后一个数据的_id,分页数量] 分页数量默认为10
* @param {String} fields 需要展示的字段 a,b,c,d
* @param {String} unFields 不需要展示的字段 a,b,c,d
* @param {String} querys 查询条件 如 a=1&b=2&c=3
* @param {String} orderBy 排序字段
* @param {String} sort 排序方式【1:正序,-1:倒序】(必须与orderBy同时出现)
* @param {String} dates 时间范围/时间查询 [开始时间,结束时间]/查询时间
*
* @version 1.0.1
* 2018/10/17 修改返回值类型可直接供mongoose使用
*
* @example
* GET /user/?orderBy=compName&sort=-1&pages=20,1&fields=compName,compAuth,compEmail&unFields=status&dates=2018-8-5&name=lopo
* index(){
* const {ctx} = this;
* const {queryParamFn} = ctx.helper;
* ctx.body = queryParamFn(ctx.query);
* }
*
* @return {Object}
* {
* querys: { name: 'lopo' },
* select: { compName: 1, compAuth: 1, compEmail: 1, status: 0 },
* pages: { marker: '1', limit: '20' },
* sort: { compName: '-1' },
* dates: '2018-8-5'
* }
*/
queryParamFn(querys, db = false) {
const mapFN = (str) => (field, val) => str[field].split(',').reduce((a, b) => {
a[b] = val;
return a
}, {});
let strToArr = mapFN(querys);
const sort = !!querys.sort ? JSON.parse(querys.sort) : {};
// const dates = querys.dates || [];
const fields = !!querys.fields ? strToArr('fields', 1) : {};
const unFields = !!querys.unFields ? strToArr('unFields', 0) : {};
const limit = !!querys.limit ? querys.limit * 1 : 10;
const marker = querys.marker || ''
const dateFn = () => {
if (!!dates) {
const ARR = dates.split(',');
return ARR.length === 2 ? {
'$gte': ARR[1],
'$lte': ARR[0]
} : `${ARR}`
}
}
delete querys.pages;
delete querys.fields;
delete querys.unFields;
delete querys.sort;
delete querys.dates;
delete querys.marker;
delete querys.limit;
delete querys._;
delete querys._type;
return {
'querys': {
...querys,
} || {},
'select': {
...fields,
...unFields
},
limit,
marker,
sort,
// ...!!dates && {
// 'dates': dateFn()
// }
}
},
/**
* PD分离
* @param {String} psd PD
* @param {String} pilipala chicken
*
* @return {String} 返回分离后的字符串
*/
bilibole(psd, pilipala) {
let bilibole = crypto.createHash('md5').update(`${psd}:${pilipala}`).digest('hex');
return `${pilipala}${bilibole}`;
},
/**
* @name stream 转buffer
*
* @param {files} stream 文件流
* @param {Function} fn 文件处理函数
*
* @return {String:Buffer} 返回Buferr 可用函数对Buffer进行处理后返回
*/
async stream2buf(stream, fn) {
try {
const parts = await toArray(stream);
const buf = Buffer.concat(parts);
return !!fn ? fn(buf) : buf
} catch (err) {
await sendToWormhole(stream);
throw err;
};
},
/**
* len 随机位数 默认6位
* @param {Number} len 随机数字长度 默认为6
*/
roundCode: (len = 6) => {
let pwd = "";
for (let idx = 0; idx < len; idx++) {
let seed = parseInt(Math.random() * 9);
pwd += seed;
}
return pwd;
},
/**
* 获取token函数
* @param {String} type token类型 ['BAIDUAip','WEWork','WeiXin','DirectAdmin','openstack']
*/
async TokenFn(type) {
const {
ctx,
config
} = this;
let datas;
const DirectAdmin = config.vhost.DirectAdmin;
const wechatApi = config.wechatApi;
const openstackApi = config.openstack;
const tokenMapFn = async () => {
const maps = {
// 百度token
'BAIDUAip': await ctx.curl('aip.baidubce.com/oauth/2.0/token', {
method: 'POST',
dataType: 'json',
data: {
'grant_type': 'client_credentials',
'client_id': config.baiduAIP.API_KEY,
'client_secret': config.baiduAIP.SECRET_KEY
}
}),
// 企业微信token
'WEWork': await ctx.curl('https://qyapi.weixin.qq.com/cgi-bin/gettoken', {
dataType: 'json',
data: {
'corpid': config.weWork.corpid,
'corpsecret': config.weWork.corpsecret
}
}),
// 微信token
'WeiXin': await ctx.curl('https://api.weixin.qq.com/cgi-bin/token', {
dataType: 'json',
data: {
'grant_type': 'client_credential',
'appid': wechatApi.appId,
'secret': wechatApi.appSecret
}
}),
// // DA token
// 'DirectAdmin': await ctx.curl(`${DirectAdmin.server}:${DirectAdmin.port}/CMD_LOGIN`, {
// method: 'POST',
// data: {
// 'username': DirectAdmin.username,
// 'password': DirectAdmin.password
// }
// }),
// openstack token
'openstack': await ctx.curl(`${openstackApi.uri}:5000/v3/auth/tokens?nocatalog`, {
method: 'POST',
dataType: 'json',
headers: {
'Content-Type': 'application/json',
},
content: `{
"auth": {
"identity": {
"methods": ["password"],
"password": {
"user": {
"name": "${openstackApi.name}",
"password": "${openstackApi.password}",
"domain": { "name": "Default" }
}
}
},
"scope": {
"project": {
"domain": {
"id": "default"
},
"name": "admin"
}
}
}
}`,
})
};
return maps[type]
};
const setResult = async () => {
if (type !== 'DirectAdmin') {
const data = await tokenMapFn();
return {
tokenValue: type === 'openstack' ? data.headers['x-subject-token'] : data.data.access_token,
tokenType: type,
tokenExpiresIn: type === 'openstack' ? new Date(data.data.token.expires_at) - 0 - 60 * 1000 : data.data.expires_in * 1000 + (new Date() - 0)
}
} else {
const cookies = (await tokenMapFn()).res.headers['set-cookie'][0].split(';');
return {
tokenValue: cookies[0],
tokenType: 'DirectAdmin',
tokenExpiresIn: (new Date(cookies[2].split('=')[1]) - 0) + 8 * 60 * 60 * 1000
}
}
};
const setToken = async () => {
const saveToken = await ctx.model.Tokens.findOneAndUpdate({
'tokenType': type
}, { ...(await setResult())
}, {
upsert: true,
new: true
});
datas = saveToken.tokenValue;
};
const tokens = await ctx.model.Tokens.findOne({
'tokenType': type
});
!!tokens && tokens.tokenExpiresIn > (new Date - 0) ? datas = tokens.tokenValue : await setToken();
return {
data: datas
}
},
/**
* mongo报错处理
*/
mongoError(error) {
const errorMap = {
'11000': ''
}
},
/**
* 函数柯里化
*/
curry: (fn, arity = fn.length, ...args) => arity <= args.length ? fn(...args) : curry.bind(null, fn, arity, ...args),
/**
* moment mix
* @param {String} scope [/day/week/month/quarter/year] 日/周/月/季度/年
* @int {Number} int 数值
*/
// 当前时间
getThisMoment: (scope) => {
return {
startDate: moment().startOf(scope).format(),
endDate: moment().format()
}
},
// 之前
getPrevMoment: (scope) => {
return {
startDate: moment().week(moment()[scope]() - 1).startOf(scope).format(),
endDate: moment().week(moment()[scope]() - 1).endOf(scope).format()
}
},
// 时间差 type [add,subtract]
subtractMoment: (int, scope, type = "subtract", withFormat = true, fn) => {
const dates = moment()[type](int, scope);
return !fn ? (withFormat ? dates.format() : dates) : fn(dates);
},
/**
* @name 格式化时间
* @param {String} formater 格式化输出标准 YYYY/MM/DD/HH/mm/ss/SSS
* @param {Date} dates 日期
* @param {Boolean} utc UTC
*/

formatDate: ({
formater,
dates
} = {}, utc = false) => {
const DATE = moment(!!dates ? dates : new Date());
return utc ? DATE.utc().format(!!formater && formater) : DATE.format(!!formater && formater)
},
// 生成时间数组
getDayAll: (begin, end) => {
let dateAllArr = new Array();
let ab = moment(begin).format("YYYY-MM-DD").split("-");
const ae = moment(end).format("YYYY-MM-DD").split("-");
const db = new Date();
db.setUTCFullYear(ab[0], ab[1] - 1, ab[2]);
const de = new Date();
de.setUTCFullYear(ae[0], ae[1] - 1, ae[2]);
const unixDb = db.getTime();
const unixDe = de.getTime();
for (let k = unixDb; k <= unixDe;) {
dateAllArr.push(new Date(parseInt(k)).format().toString());
k = k + 24 * 60 * 60 * 1000;
}
return dateAllArr;
},
getCTX: () => app.createAnonymousContext(),
// 发送短信,邮件,企业微信通知
async sendWarning(ctx, content, _id, compName) {
const auth = ctx.state.user;
const {
compEmail,
compPhone
} = await ctx.model.Company.findOne({
'_id': _id
}, {
'compEmail': 1,
'compPhone': 1
});
// await ctx.service.wework.message.send({ totag: 2, content: content.replace(/您/g, `${compName}`) });
await ctx.service.sms.svipwang.create(compPhone, content);
await ctx.service.sms.mail.sendMailDefault({
subject: '系统消息通知',
to: compEmail,
html: content
});
},
// 清理paramsFN()
deleteUndefined: (object) => {
for (let key in object) {
key !== 'detail' && delete object[(object[key] === undefined || object[key] === '' || object[key] === '[]') && key]
}
return object
},
/******************************************************************************************/
/**
* class DistributedEdit extends mix(class1, class2)
*/
// mixins
mix: (...mixins) => {
class Mix {}

for (let mixin of mixins) {
copyProperties(Mix.prototype, mixin); // 拷贝实例属性
copyProperties(Mix.prototype, Reflect.getPrototypeOf(mixin)); // 拷贝原型属性
}

return Mix;
},
// mixinsParams
copyProperties: (target, source) => {
for (let key of Reflect.ownKeys(source)) {
if (key !== "constructor" &&
key !== "prototype" &&
key !== "name"
) {
let desc = Object.getOwnPropertyDescriptor(source, key);
Object.defineProperty(target, key, desc);
}
}
},
/***************end****************/
/**
* 货币计算 mix
* @param {String} method [add/+,sub/-,mul/*,div//] 加减乘除
* @param {Array} money 需要计算的数字【前,后】
*/
decimalCash: (method = "add", money) => {
return new Decimal(money[0])[method](new Decimal(money[1])).toNumber()
},
// decimaFN: () => { return this.decimalCash() },
// decimalAdd: () => { return this.decimaFN('add') },
// decimalSub: () => { return this.decimaFN('sub') },
// decimalMul: () => { return this.decimaFN('mul') },
// decimalDiv: () => { return this.decimaFN('div') },
/****************end***************/
/**
* MONGO type转换
* @param {Array} ARR 字符串字典
* @param {Number} value 值
* @param {Boolean} hasZero 是否从0开始
*/
NumToStr(ARR, value, hasZero = false) {
return `${value}|${ARR[hasZero ? value : value - 1]}`
},
/**
* @name mongoDB update数组参数
*
*/
objToQuery(OBJ, KEY) {
return Object.keys(OBJ).reduce((a, b) => {
a[`${KEY}.$.${b}`] = OBJ[b];
return a
}, {});
},
/**
* @name 线转树
* @param {Array} ARR 需要处理的数组
* @param {String} keyName 作为分类的字段
*/
Array2Object: (ARR, keyName) => {
return ARR.reduce((a, b) => {
const keys = b[keyName];
if (a[keys] === undefined) a[keys] = [];
a[keys].push(b);
return a;
}, {})
},
/**
* @name 字典排序
*/
OBJKeySort: (obj) => {
const newkey = Object.keys(obj).sort();
const newObj = {};
for (var i = 0; i < newkey.length; i++) {
newObj[newkey[i]] = obj[newkey[i]];
}
return newObj;
},
/**
* @name 字母升序
*
* @param {String} chars 字母
* @param {Number} num 进几位
*
*/
nextChars: (chars, num, type = 'lower') => {
const char = chars.toLowerCase();
const isChar = /^[a-zA-Z]*$/.test(char);
const cx = char.charCodeAt(0);
const CHARS = (!!isChar && cx + num < 123) ? String.fromCharCode(char.charCodeAt(0) + num) : false;
return !!CHARS ? type === 'upper' ? CHARS.toUpperCase() : CHARS : 'Params Error'
},
/**
* @name 扁平化数组 fr : _.Array
*
* @param {Array} arr 数组
*
*/
deepFlatten: arr => _Array.flatten(arr),
/**
* @name 集合排序 fr : _.Collection
*
* @param {String|Function|Array|Object} 排序字段
* @param {String} asc|desc 排序方式
*
*/
orderBy: (arr, iteratees, order = 'asc') => _Collection.orderBy(arr, iteratees, order),
/**
* @name 隐藏手机号中间4位
*
*@param {String} telphone 手机号码
*/
hidePhone: (telphone) => {
const reg = /^(\d{3})\d{4}(\d{4})$/;
return telphone.replace(reg, `$1****$2`)
}
};

MongoDB 数据类型 转

mongodblopo1983 发表了文章 • 0 个评论 • 220 次浏览 • 2018-12-26 16:46 • 来自相关话题

Double 1 双精度浮点数 - 此类型用于存储浮点值
String 2 字符串 - 这是用于存储数据的最常用的数据类型。MongoDB中的字符串必须为UTF-8
Object 3 对象 - 此数据类型用于嵌入式文档
Array 4 数组 - 此类型用于将数组或列表或多个值存储到一个键中
Binary data 5 二进制数据 - 此数据类型用于存储二进制数据
Undefined 6 已废弃
Object id 7 对象ID - 此数据类型用于存储文档的ID
Boolean 8 布尔类型 - 此类型用于存储布尔值(true / false)值
Date 9 日期 - 此数据类型用于以UNIX时间格式存储当前日期或时间。可以通过创建日期对象并将日,月,年的日期进行指定自己需要的日期时间
Null 10 Null - 此类型用于存储Null值
Regular 11 正则表达式 - 此数据类型用于存储正则表达式
JavaScript 13 代码 - 此数据类型用于将JavaScript代码存储到文档中
Symbol 14 符号 - 该数据类型与字符串相同;但是,通常保留用于使用特定符号类型的语言
JavaScript (with scope) 15 代码 - 此数据类型用于将带作用域的JavaScript代码存储到文档中
32-bit integer 16 32位整型 - 此类型用于存储数值
Timestamp 17 时间戳 - 当文档被修改或添加时,可以方便地进行录制
64-bit integer 18 64位整型 - 此类型用于存储数值
Min key 255 最小键 - 此类型用于将值与最小BSON元素进行比较
Max key 127 最大键 - 此类型用于将值与最大BSON元素进行比较
---------------------
作者:_抱歉打扰了
来源:CSDN
原文:https://blog.csdn.net/qq_33661 ... 16603
版权声明:本文为博主原创文章,转载请附上博文链接! 查看全部
Double    1     双精度浮点数 - 此类型用于存储浮点值
String 2 字符串 - 这是用于存储数据的最常用的数据类型。MongoDB中的字符串必须为UTF-8
Object 3 对象 - 此数据类型用于嵌入式文档
Array 4 数组 - 此类型用于将数组或列表或多个值存储到一个键中
Binary data 5 二进制数据 - 此数据类型用于存储二进制数据
Undefined 6 已废弃
Object id 7 对象ID - 此数据类型用于存储文档的ID
Boolean 8 布尔类型 - 此类型用于存储布尔值(true / false)值
Date 9 日期 - 此数据类型用于以UNIX时间格式存储当前日期或时间。可以通过创建日期对象并将日,月,年的日期进行指定自己需要的日期时间
Null 10 Null - 此类型用于存储Null值
Regular 11 正则表达式 - 此数据类型用于存储正则表达式
JavaScript 13 代码 - 此数据类型用于将JavaScript代码存储到文档中
Symbol 14 符号 - 该数据类型与字符串相同;但是,通常保留用于使用特定符号类型的语言
JavaScript (with scope) 15 代码 - 此数据类型用于将带作用域的JavaScript代码存储到文档中
32-bit integer 16 32位整型 - 此类型用于存储数值
Timestamp 17 时间戳 - 当文档被修改或添加时,可以方便地进行录制
64-bit integer 18 64位整型 - 此类型用于存储数值
Min key 255 最小键 - 此类型用于将值与最小BSON元素进行比较
Max key 127 最大键 - 此类型用于将值与最大BSON元素进行比较
---------------------
作者:_抱歉打扰了
来源:CSDN
原文:https://blog.csdn.net/qq_33661 ... 16603
版权声明:本文为博主原创文章,转载请附上博文链接!

openstack gnocchi 服务器硬件监控 (基于Eggjs实现)

openstacklopo1983 发表了文章 • 0 个评论 • 418 次浏览 • 2018-12-22 02:51 • 来自相关话题

'use strict';

const ServerIndex = require('../index')
/**
* @name 获取监控数据
* @param {*} _ucid 用户id
* @param {*} server_id 服务器id
* @param {*} type 聚合类型
* @param {*} metric 聚合数据
* @description type和metric 子属关系如下
*
* instance?["vcpus", "memory", "disk.root.size", "compute.instance.booting.time", "cpu_util", "disk.ephemeral.size", "cpu.delta", "cpu", "memory.usage"]
*
* instance_network_interface ["network.outgoing.packets.rate", "network.incoming.bytes.rate", "network.outgoing.bytes.rate", "network.incoming.packets", "network.incoming.packets.rate", "network.outgoing.bytes", "network.incoming.bytes", "network.outgoing.packets"]
*
* instance_disk ["disk.device.read.bytes.rate", "disk.device.write.requests", "disk.device.write.bytes.rate", "disk.device.write.requests.rate", "disk.device.read.bytes", "disk.device.read.requests", "disk.device.read.requests.rate", "disk.device.write.bytes"]
*
*/
class ResourceService extends ServerIndex {
constructor(ctx) {
super(ctx);
this.actions = ':8041/v1/';
}
async getMeasures(_ucid, { server_id, type, metric } = {}) {
const ctx = this.ctx;
const { subtractMoment } = ctx.helper;
try {
const metricIDs = await this.getmetricID(_ucid, server_id, type, metric);
const getDATASFn = metricIDs.map(async e => {
return {
name: e.name,
datas: await this.OSAJax(`${this.actions}metric/${e.id}/measures`, {
body: { start: subtractMoment(1, 'hour') }
})
}
})
const datas = (await Promise.all(getDATASFn)).reduce((a, b) => {
a[`${b['name']}`] = b['datas'];
return a
}, {})
return { data: datas };
} catch (error) {
console.log(error)
return error
}
}
async getmetricID(_ucid, server_id, type, metric) {
const metrics = metric.split(',')
try {
const DATAS = await this.OSAJax(`${this.actions}search/resource/generic/`, {
_ucid,
method: 'POST',
body: {
"and": [
{
"like": {
"original_resource_id": `%${server_id}%`
}
},
{
"=": {
"type": type
}
}
]
}
});
const { code } = DATAS;
if (!code && !!DATAS.length) {
const MAPS = DATAS[0].metrics
return metrics.reduce((a, b) => { a.push({ name: b, id: MAPS[b] }); return a }, )
} else {
return undefined
}
} catch (error) {
return error
}
}
}

module.exports = ResourceService;[/b]?'use strict';
const Service = require('egg').Service;
//
class indexService extends Service {
constructor(ctx) {
super(ctx);
this.OPTokenMODEL = ctx.model.Openstack.Token;
this.OPUserMODEL = ctx.model.Openstack.Identity.User;
this.OBJKeySort = ctx.helper.OBJKeySort;
const { uri } = this.config.openstack;
this.uri = `${uri}`;
this.CurlOpt = async (method, full, token) => {
return {
'dataType': 'json',
'headers': {
...((!full && !token) && { 'X-Auth-Token': (await ctx.helper.TokenFn('openstack')).data }) || !!token && { 'X-Auth-Token': token },
'Content-Type': 'application/json',
},
'method': method,
...method !== 'DELETE' && { 'data': {} },
'timeout': 60000
}
}
};
/**
* 基础库
*
* @param {Object} body 需要提交的参数
* @param {String} method GET POST DELETE PUT PATCH
* @param {String} params url 参数
* @param {Boolean} full 是否显示完整参数
* @param {String} _ucid 用户ID Optional
*/
async OSAJax(params, { body = {}, method = "GET", full = false, status = false, _ucid } = {}) {
const ctx = this.ctx;
const opt = await this.CurlOpt(method, full, !!_ucid && await this.getUserToken(_ucid));
method !== 'DELETE' && Object.assign(opt.data, { ...!!body ? body : '' });
try {
const result = await ctx.curl(`${this.uri}${!!params ? params : ''}`, opt);
return !!full && result || !!status && result.status || result.data;
} catch (error) {
return error
}
}
}
module.exports = indexService; 查看全部
gnocchi-logo.png
'use strict';

const ServerIndex = require('../index')
/**
* @name 获取监控数据
* @param {*} _ucid 用户id
* @param {*} server_id 服务器id
* @param {*} type 聚合类型
* @param {*} metric 聚合数据
* @description type和metric 子属关系如下
*
* instance?["vcpus", "memory", "disk.root.size", "compute.instance.booting.time", "cpu_util", "disk.ephemeral.size", "cpu.delta", "cpu", "memory.usage"]
*
* instance_network_interface ["network.outgoing.packets.rate", "network.incoming.bytes.rate", "network.outgoing.bytes.rate", "network.incoming.packets", "network.incoming.packets.rate", "network.outgoing.bytes", "network.incoming.bytes", "network.outgoing.packets"]
*
* instance_disk ["disk.device.read.bytes.rate", "disk.device.write.requests", "disk.device.write.bytes.rate", "disk.device.write.requests.rate", "disk.device.read.bytes", "disk.device.read.requests", "disk.device.read.requests.rate", "disk.device.write.bytes"]
*
*/
class ResourceService extends ServerIndex {
constructor(ctx) {
super(ctx);
this.actions = ':8041/v1/';
}
async getMeasures(_ucid, { server_id, type, metric } = {}) {
const ctx = this.ctx;
const { subtractMoment } = ctx.helper;
try {
const metricIDs = await this.getmetricID(_ucid, server_id, type, metric);
const getDATASFn = metricIDs.map(async e => {
return {
name: e.name,
datas: await this.OSAJax(`${this.actions}metric/${e.id}/measures`, {
body: { start: subtractMoment(1, 'hour') }
})
}
})
const datas = (await Promise.all(getDATASFn)).reduce((a, b) => {
a[`${b['name']}`] = b['datas'];
return a
}, {})
return { data: datas };
} catch (error) {
console.log(error)
return error
}
}
async getmetricID(_ucid, server_id, type, metric) {
const metrics = metric.split(',')
try {
const DATAS = await this.OSAJax(`${this.actions}search/resource/generic/`, {
_ucid,
method: 'POST',
body: {
"and": [
{
"like": {
"original_resource_id": `%${server_id}%`
}
},
{
"=": {
"type": type
}
}
]
}
});
const { code } = DATAS;
if (!code && !!DATAS.length) {
const MAPS = DATAS[0].metrics
return metrics.reduce((a, b) => { a.push({ name: b, id: MAPS[b] }); return a }, )
} else {
return undefined
}
} catch (error) {
return error
}
}
}

module.exports = ResourceService;[/b]
?
'use strict';
const Service = require('egg').Service;
//
class indexService extends Service {
constructor(ctx) {
super(ctx);
this.OPTokenMODEL = ctx.model.Openstack.Token;
this.OPUserMODEL = ctx.model.Openstack.Identity.User;
this.OBJKeySort = ctx.helper.OBJKeySort;
const { uri } = this.config.openstack;
this.uri = `${uri}`;
this.CurlOpt = async (method, full, token) => {
return {
'dataType': 'json',
'headers': {
...((!full && !token) && { 'X-Auth-Token': (await ctx.helper.TokenFn('openstack')).data }) || !!token && { 'X-Auth-Token': token },
'Content-Type': 'application/json',
},
'method': method,
...method !== 'DELETE' && { 'data': {} },
'timeout': 60000
}
}
};
/**
* 基础库
*
* @param {Object} body 需要提交的参数
* @param {String} method GET POST DELETE PUT PATCH
* @param {String} params url 参数
* @param {Boolean} full 是否显示完整参数
* @param {String} _ucid 用户ID Optional
*/
async OSAJax(params, { body = {}, method = "GET", full = false, status = false, _ucid } = {}) {
const ctx = this.ctx;
const opt = await this.CurlOpt(method, full, !!_ucid && await this.getUserToken(_ucid));
method !== 'DELETE' && Object.assign(opt.data, { ...!!body ? body : '' });
try {
const result = await ctx.curl(`${this.uri}${!!params ? params : ''}`, opt);
return !!full && result || !!status && result.status || result.data;
} catch (error) {
return error
}
}
}
module.exports = indexService;

怎么渲染axios获取的数据呢?谢谢大佬

VUElopo1983 回复了问题 • 2 人关注 • 1 个回复 • 300 次浏览 • 2018-12-17 13:11 • 来自相关话题

MongoDB导入导出以及数据库备份

mongodblopo1983 发表了文章 • 0 个评论 • 273 次浏览 • 2018-12-15 04:16 • 来自相关话题

导出

1.概念
mongoDB中的mongoexport工具可以把一个collection导出成JSON格式或CSV格式的文件。可以通过参数指定导出的数据项,也可以根据指定的条件导出数据。
2、语法mongoexport -d dbname -c collectionname -o file --type json/csv -f field
参数说明:
-d :数据库名-c :collection名-o :输出的文件名--type : 输出的格式,默认为json-f :输出的字段,如果-type为csv,则需要加上-f "字段名"
示例mongoexport -d mongotest -c users -o /home/export/mongoDB/users.json --type json -f "_id,user_id,user_name,age,status"
-----------------------------------------------------------------------------------------------------------------

导入

1.语法mongoimport -d dbname -c collectionname --file filename --headerline --type json/csv -f field
参数说明:
-d :数据库名-c :collection名--type :导入的格式默认json-f :导入的字段名--headerline :如果导入的格式是csv,则可以使用第一行的标题作为导入的字段--file :要导入的文件

2.示例mongoimport -d mongotest -c users --file /home/mongodump/articles.json --type json
-----------------------------------------------------------------------------------------------------------------

备份


1.语法mongodump -h dbhost -d dbname -o dbdirectory
参数说明:
-h: MongDB所在服务器地址,例如:127.0.0.1,当然也可以指定端口号:127.0.0.1:27017-d: 需要备份的数据库实例,例如:test-o: 备份的数据存放位置,例如:/home/mongodump/,当然该目录需要提前建立,这个目录里面存放该数据库实例的备份数据。
2.示例sudo rm -rf /home/momgodump/
sudo mkdir -p /home/momgodump
sudo mongodump -h 192.168.17.129:27017 -d itcast -o /home/mongodump/
-----------------------------------------------------------------------------------------------------------------


恢复

1.语法mongorestore -h dbhost -d dbname --dir dbdirectory
参数说明:
-h: MongoDB所在服务器地址-d: 需要恢复的数据库实例,例如:test,当然这个名称也可以和备份时候的不一样,比如test2--dir: 备份数据所在位置,例如:/home/mongodump/itcast/--drop: 恢复的时候,先删除当前数据,然后恢复备份的数据。就是说,恢复后,备份后添加修改的数据都会被删除,慎用!
2.示例mongorestore -h 192.168.17.129:27017 -d itcast_restore --dir /home/mongodump/itcast/ 查看全部
导出

1.概念

mongoDB中的mongoexport工具可以把一个collection导出成JSON格式或CSV格式的文件。可以通过参数指定导出的数据项,也可以根据指定的条件导出数据。


2、语法
mongoexport -d dbname -c collectionname -o file --type json/csv -f field

参数说明:
  • -d :数据库名
  • -c :collection名
  • -o :输出的文件名
  • --type : 输出的格式,默认为json
  • -f :输出的字段,如果-type为csv,则需要加上-f "字段名"

示例
mongoexport -d mongotest -c users -o /home/export/mongoDB/users.json --type json -f  "_id,user_id,user_name,age,status"

-----------------------------------------------------------------------------------------------------------------

导入

1.语法
mongoimport -d dbname -c collectionname --file filename --headerline --type json/csv -f field

参数说明:
  • -d :数据库名
  • -c :collection名
  • --type :导入的格式默认json
  • -f :导入的字段名
  • --headerline :如果导入的格式是csv,则可以使用第一行的标题作为导入的字段
  • --file :要导入的文件


2.示例
mongoimport -d mongotest -c users --file /home/mongodump/articles.json --type json

-----------------------------------------------------------------------------------------------------------------

备份


1.语法
mongodump -h dbhost -d dbname -o dbdirectory

参数说明:
  • -h: MongDB所在服务器地址,例如:127.0.0.1,当然也可以指定端口号:127.0.0.1:27017
  • -d: 需要备份的数据库实例,例如:test
  • -o: 备份的数据存放位置,例如:/home/mongodump/,当然该目录需要提前建立,这个目录里面存放该数据库实例的备份数据。

2.示例
sudo rm -rf /home/momgodump/
sudo mkdir -p /home/momgodump
sudo mongodump -h 192.168.17.129:27017 -d itcast -o /home/mongodump/

-----------------------------------------------------------------------------------------------------------------


恢复

1.语法
mongorestore -h dbhost -d dbname --dir dbdirectory

参数说明:
  • -h: MongoDB所在服务器地址
  • -d: 需要恢复的数据库实例,例如:test,当然这个名称也可以和备份时候的不一样,比如test2
  • --dir: 备份数据所在位置,例如:/home/mongodump/itcast/
  • --drop: 恢复的时候,先删除当前数据,然后恢复备份的数据。就是说,恢复后,备份后添加修改的数据都会被删除,慎用!

2.示例
mongorestore -h 192.168.17.129:27017 -d itcast_restore --dir /home/mongodump/itcast/

招聘Node主程序

回复

求职招聘Reiayanami 发起了问题 • 1 人关注 • 0 个回复 • 328 次浏览 • 2018-10-22 09:25 • 来自相关话题

queries 分离函数 适用 mongoose

Nodejslopo1983 发表了文章 • 0 个评论 • 305 次浏览 • 2018-10-17 21:34 • 来自相关话题

? // app/extend/helper.js
/**
* GET querys 分离函数
* @param {String} pages 分页 [上一页最后一个数据的_id,分页数量] 分页数量默认为10
* @param {String} fields 需要展示的字段 a,b,c,d
* @param {String} unFields 不需要展示的字段 a,b,c,d
* @param {String} querys 查询条件 如 a=1&b=2&c=3
* @param {String} orderBy 排序字段
* @param {String} sort 排序方式【1:正序,-1:倒序】(必须与prderBy同时出现)
* @param {String} dates 时间范围/时间查询 [开始时间,结束时间]/查询时间
*
* @version 1.0.1
* 2018/10/17 修改返回值类型可直接供mongoose使用
*
* @example
* GET /user/?orderBy=compName&sort=-1&pages=20,1&fields=compName,compAuth,compEmail&unFields=status&dates=2018-8-5&name=lopo
* @return {Object}
* {
* querys: { name: 'lopo' },
* select: { compName: 1, compAuth: 1, compEmail: 1, status: 0 },
* pages: { marker: '1', limit: '20' },
* sort: { compName: '-1' },
* dates: '2018-8-5'
* }
*/
queryParamFn(querys) {
const mapFN = (str) => (field, val) => str[field].split(',').reduce((a, b) => { a<strong> = val; return a }, {});
let strToArr = mapFN(querys);
const pages = !!querys.pages && querys.pages;
const orderBy = !!querys.orderBy && querys.orderBy;
const sort = !!querys.sort && querys.sort;
const dates = !!querys.dates && querys.dates;
const fields = !!querys.fields ? strToArr('fields', 1) : {};
const unFields = !!querys.unFields ? strToArr('unFields', 0) : {};
const dateFn = () => {
if (!!dates) {
const ARR = dates.split(',');
return ARR.length === 2 ? { '$gte': ARR[1], '$lte': ARR[0] } : `${ARR}`
}
}
const sortFn = () => {
let OBJ = {};
if (!!orderBy) {
OBJ[orderBy] = sort;
return OBJ
}
return OBJ || {}
}
delete querys.pages;
delete querys.fields;
delete querys.unFields;
delete querys.orderBy;
delete querys.sort;
delete querys.dates;
return {
'querys': querys || {},
'select': { ...fields, ...unFields },
...(!!pages && {
'pages': {
'marker': pages.split(',')[1] || undefined,
'limit': pages.split(',')[0] || 10
}
}),
...!!orderBy && {
'sort': sortFn()
},
...!!dates && {
'dates': dateFn()
}
}
} 查看全部
?
    // app/extend/helper.js
/**
* GET querys 分离函数
* @param {String} pages 分页 [上一页最后一个数据的_id,分页数量] 分页数量默认为10
* @param {String} fields 需要展示的字段 a,b,c,d
* @param {String} unFields 不需要展示的字段 a,b,c,d
* @param {String} querys 查询条件 如 a=1&b=2&c=3
* @param {String} orderBy 排序字段
* @param {String} sort 排序方式【1:正序,-1:倒序】(必须与prderBy同时出现)
* @param {String} dates 时间范围/时间查询 [开始时间,结束时间]/查询时间
*
* @version 1.0.1
* 2018/10/17 修改返回值类型可直接供mongoose使用
*
* @example
* GET /user/?orderBy=compName&sort=-1&pages=20,1&fields=compName,compAuth,compEmail&unFields=status&dates=2018-8-5&name=lopo
* @return {Object}
* {
* querys: { name: 'lopo' },
* select: { compName: 1, compAuth: 1, compEmail: 1, status: 0 },
* pages: { marker: '1', limit: '20' },
* sort: { compName: '-1' },
* dates: '2018-8-5'
* }
*/
queryParamFn(querys) {
const mapFN = (str) => (field, val) => str[field].split(',').reduce((a, b) => { a<strong> = val; return a }, {});
let strToArr = mapFN(querys);
const pages = !!querys.pages && querys.pages;
const orderBy = !!querys.orderBy && querys.orderBy;
const sort = !!querys.sort && querys.sort;
const dates = !!querys.dates && querys.dates;
const fields = !!querys.fields ? strToArr('fields', 1) : {};
const unFields = !!querys.unFields ? strToArr('unFields', 0) : {};
const dateFn = () => {
if (!!dates) {
const ARR = dates.split(',');
return ARR.length === 2 ? { '$gte': ARR[1], '$lte': ARR[0] } : `${ARR}`
}
}
const sortFn = () => {
let OBJ = {};
if (!!orderBy) {
OBJ[orderBy] = sort;
return OBJ
}
return OBJ || {}
}
delete querys.pages;
delete querys.fields;
delete querys.unFields;
delete querys.orderBy;
delete querys.sort;
delete querys.dates;
return {
'querys': querys || {},
'select': { ...fields, ...unFields },
...(!!pages && {
'pages': {
'marker': pages.split(',')[1] || undefined,
'limit': pages.split(',')[0] || 10
}
}),
...!!orderBy && {
'sort': sortFn()
},
...!!dates && {
'dates': dateFn()
}
}
}

mongo TTL 索引

mongodblopo1983 发表了文章 • 0 个评论 • 293 次浏览 • 2018-10-15 23:48 • 来自相关话题

TTL索引是MongoDB中一种特殊的索引, 可以支持文档在一定时间之后自动过期删除,目前TTL索引只能在单字段上建立,
并且字段类型必须是date类型或者包含有date类型的数组(如果数组中包含多个date类型字段,则取最早时间为过期时间)
官网介绍链接:https://docs.mongodb.com/v3.2/core/index-ttl/
?
①TTL索引是单字段索引,混合索引不支持TTL,并且也会忽略expireAfterSeconds属性②在_id 主键上不能建立TTL索引③在capped collection中不能建立TTL索引,因为MongoDB不能从capped collection中删除文档④你不能使用createIndex()去更改已经存在的TTL索引的expireAfterSeconds值,如果想更改expireAfterSeconds,可以使用collMod命令,否则你只能删除索引,然后重建了⑤你不能在已有索引的字段上再创建TTL索引了,如果你想把非TTL索引改为TTL索引,那就只能删除重建索引了
?
?db.getCollection('smscodes').ensureIndex({'expiresIn':1},{"expireAfterSeconds":60*1})
MongoDB 权威指南的语法 错误 列为:expireAfterSecs 貌似新版的 应该为如上代码 查看全部
TTL索引是MongoDB中一种特殊的索引, 可以支持文档在一定时间之后自动过期删除,目前TTL索引只能在单字段上建立,
并且字段类型必须是date类型或者包含有date类型的数组(如果数组中包含多个date类型字段,则取最早时间为过期时间)

官网介绍链接:https://docs.mongodb.com/v3.2/core/index-ttl/


?
  • ①TTL索引是单字段索引,混合索引不支持TTL,并且也会忽略expireAfterSeconds属性
  • ②在_id 主键上不能建立TTL索引
  • ③在capped collection中不能建立TTL索引,因为MongoDB不能从capped collection中删除文档
  • ④你不能使用createIndex()去更改已经存在的TTL索引的expireAfterSeconds值,如果想更改expireAfterSeconds,可以使用collMod命令,否则你只能删除索引,然后重建了
  • ⑤你不能在已有索引的字段上再创建TTL索引了,如果你想把非TTL索引改为TTL索引,那就只能删除重建索引了

?
?
db.getCollection('smscodes').ensureIndex({'expiresIn':1},{"expireAfterSeconds":60*1})

MongoDB 权威指南的语法 错误 列为:expireAfterSecs 貌似新版的 应该为如上代码

mongo 插入多维数组 唯一 $ne+$push

mongodblopo1983 发表了文章 • 0 个评论 • 260 次浏览 • 2018-09-28 18:22 • 来自相关话题

mongo $addToSet 用于处理普通数组不重复的问题
但是遇到 多维的就会出现问题
?
故只能通过$ne+$push的方式来解决
?
代码如下(egg2.x mongoose5.x)

Schemaconst AllPsListSchema = new Schema({
// 分类中文名称
psName: {
type: String,
required: [true, '服务名称不能为空'],
unique: true,
},
// 分类英文名称
psEnName: {
type: String,
},
// 服务状态 [0/停用,1/正常,2/维护,3/下线]
psState: {
type: Number,
default: 1
},
// 服务子类
psChild: [{
// 产品/服务名称
name: {
type: String,
required: [true, '服务/产品名称不能为空'],
unique: true
},
// 产品/服务英文名称
enName: {
type: String,
required: [true, '服务/产品英文名称不能为空'],
unique: true
},
// 服务状态 [0/停用,1/正常,2/维护,3/下线]
state: {
type: Number,
default: 1
},
// [0/删除,1/锁定,2/正常] 1/锁定意思为不可删除,系统保留
status: {
type: Number,
default: 2
}
}],
// [0:删除,1:锁定,2:正常] 1/锁定意思为不可删除,系统保留
status: {
type: Number,
default: 2
}
}, {
timestamps: {
createdAt: 'created',
updatedAt: 'updated'
}
})
statics
? creatPSChild: async function (_id, bodys) {
const result = await this.findOneAndUpdate({ _id, 'psChild.name': { $ne: bodys.name }, 'psChild.enName': { $ne: bodys.enName } }, {
$push: {
'psChild': bodys
}
});
return { data: result }
} 查看全部
mongo $addToSet 用于处理普通数组不重复的问题
但是遇到 多维的就会出现问题
?
故只能通过$ne+$push的方式来解决
?
代码如下(egg2.x mongoose5.x)

Schema
const AllPsListSchema = new Schema({
// 分类中文名称
psName: {
type: String,
required: [true, '服务名称不能为空'],
unique: true,
},
// 分类英文名称
psEnName: {
type: String,
},
// 服务状态 [0/停用,1/正常,2/维护,3/下线]
psState: {
type: Number,
default: 1
},
// 服务子类
psChild: [{
// 产品/服务名称
name: {
type: String,
required: [true, '服务/产品名称不能为空'],
unique: true
},
// 产品/服务英文名称
enName: {
type: String,
required: [true, '服务/产品英文名称不能为空'],
unique: true
},
// 服务状态 [0/停用,1/正常,2/维护,3/下线]
state: {
type: Number,
default: 1
},
// [0/删除,1/锁定,2/正常] 1/锁定意思为不可删除,系统保留
status: {
type: Number,
default: 2
}
}],
// [0:删除,1:锁定,2:正常] 1/锁定意思为不可删除,系统保留
status: {
type: Number,
default: 2
}
}, {
timestamps: {
createdAt: 'created',
updatedAt: 'updated'
}
})

statics
?
        creatPSChild: async function (_id, bodys) {
const result = await this.findOneAndUpdate({ _id, 'psChild.name': { $ne: bodys.name }, 'psChild.enName': { $ne: bodys.enName } }, {
$push: {
'psChild': bodys
}
});
return { data: result }
}