const crypto = require('crypto'); const moment = require('moment'); const utils = { md5(key) { const hash = crypto.createHash('md5'); return hash.update(key).digest('hex').toUpperCase(); }, dateFormat(date) { return moment(date).format('YYYY-MM-DD HH:mm:ss'); }, async fill(data, func) { if (!data) { return; } if (Array.isArray(data)) { data = await Promise.all( data.map(async item => { return func(item); }) ); } else { data = await func(data); } return data; }, createTradeNo() { return moment().format('YYYYMMDDHHmmssS') + Math.floor(Math.random() * 1000); }, onSuccess(data, msg) { return { code: 0, data, msg, }; }, onError(msg, data) { return { code: utils[msg] || -1, msg, data, }; }, MESSAGE: { 没有权限: 1, }, fixPrice(price) { return Math.floor(price * 100) / 100; }, }; module.exports = utils;