var privateKey = '-----BEGIN RSA PRIVATE KEY-----MIICXgIBAAKBgQCoChRDJ6e7BTE5yYBIS + NGYBpDs7ftEematqhvMmOFcJng7qjJk + yJ1j7DCqbCD2f / BI6gTfGXASiYuO6kklZu8Pkw4HAUkaaGyhaC8Z + TMg79PPRz5hziEdFXPTdXvXudiXbI2Wi6D90ZaSwN6ZHs7Mtc5VgGK3jxS35iLm+ oAQIDAQABAoGBAI + nHi9SxUdSZwS5yBsGFSNioNFj4Eag243RvShicUXwPvxVyqGY / cvQBhODFZAsz4Dpimxsda3b5bK51fmGyK / nXraHRunWcG7cDDB0EnRpGh4LvMI5Tny + kV0v07N0kkYF+ Lig88IjyBXMAY8m97QK / Huf6MsDFo7B6maSvlmBAkEA35GXk6achryGAoUyyLSro7bI9A9 + wXWFdXoqu1 / X1sZ8taGy7saB + XEA6EQ + XHRp7rZkQ5StoBL +reDGvLJLWQJBAMBqW / F + qg1VpmV / EfYTSS0 + jliw / Ik4kKHLuD / bYK61FG80JIoxLbelB / 1ZVZ8WR0cUKgrmoo8HOggjocNTNOkCQQCYibK86CHGAF0C3TSgIj01r2H+u4 / FmVScqeT8AVG31aeDGbeHGOPXeJWg4 + cUl80rNUDFp2yrWipwInwWhSPJAkAf+ 02u9Ru0vbC7nARTP19hWs10Jm7DLBi2G9NTIdaPE2ADH8qXAZeUt6R9UrTtjVlpkgtu5mjMlynpImsHuTPJAkEAoU10QspqfxL4F44KdHjHY1btc8wb4soaLy / eAY8PLE + jpNh8jsqA8v1EqLQbYz50D / BpkJsT5W + wydTvtEE3sA ==-----END RSA PRIVATE KEY-----' var publicKey =' -----BEGIN PUBLIC KEY-----MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCljXx8lXvdS9cxFXEvcC1m54cdU2O6yv4QNFFsfCjdn0hnwyydOfRY4oV0VB2apzG7X5FiPyxY8yj9acr43cqkH/zGSm+8CRWhRE0O197TBCj/ywnLz5KLm8745znQUVrfqWfAoC9JJjZG4ugVmEbsSQR1Bpks0USkvNWlLc1oKwIDAQAB-----END PUBLIC KEY-----' var RSA = require('wx_rsa.js') /** * 日期时间格式化 * 2018/01/01 23:59:59 */ const formatTime = date => { const year = date.getFullYear() const month = date.getMonth() + 1 const day = date.getDate() const hour = date.getHours() const minute = date.getMinutes() const second = date.getSeconds() return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':') } /** * 密码加密 * */ const formatEncrypt = (res = '') => { var input_rsa = res; var encrypt_rsa = new RSA.RSAKey(); encrypt_rsa = RSA.KEYUTIL.getKey(publicKey); var encStr = encrypt_rsa.encrypt(input_rsa) encStr = RSA.hex2b64(encStr); return encStr } /** * 日期时间格式化 * 2018-01-01 23:59:59 */ const formatTime2 = date => { const year = date.getFullYear() const month = date.getMonth() + 1 const day = date.getDate() const hour = date.getHours() const minute = date.getMinutes() const second = date.getSeconds() return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':') } /** * 对个位数前补零 */ const formatNumber = n => { n = n.toString() return n[1] ? n : '0' + n } /** * 日期格式化 * 2018-01-01 */ const formatDate = date => { const year = date.getFullYear() const month = date.getMonth() + 1 const day = date.getDate() const hour = date.getHours() const minute = date.getMinutes() const second = date.getSeconds() return [year, month, day].map(formatNumber).join('-') } /** * 时间格式化 * 23:59:59 */ const formatTimeTwo = date => { const hour = date.getHours() const minute = date.getMinutes() const second = date.getSeconds() return [hour, minute, second].map(formatNumber).join(':') } /** * 路径格式化 */ const formatUrl = (url, obj) => { let temp = [] let fn = (obj) => { let temp = [] for (let i in obj) { if (obj[i] != null && typeof obj[i] == 'object') { temp.push(`${i}=${JSON.stringify(obj[i])}`) } else { temp.push(`${i}=${obj[i]}`) } } return temp.join('&') } return url.indexOf('?') < 0 ? url + '?' + fn(obj) : url + '&' + fn(obj) } /** * 函数节流和函数抖动。 */ function throttle(fn, gapTime) { if (gapTime == null || gapTime == undefined) { gapTime = 1500 } let _lastTime = null // 返回新的函数 return function () { let _nowTime = + new Date() if (_nowTime - _lastTime > gapTime || !_lastTime) { fn.apply(this, arguments) //将this和参数传给原函数 _lastTime = _nowTime } } } /** *时间戳转天数。 */ function getDateDiff(dateTimeStamp) { var result; var minute = 1000 * 60; var hour = minute * 60; var day = hour * 24; var halfamonth = day * 15; var month = day * 30; var now = new Date().getTime(); var diffValue = (dateTimeStamp * 1000)-now; if (diffValue < 0) { return 0; } var dayC = diffValue/ day; if (dayC >0) { result = "" + dayC; } return result; } /** *时间戳。 */ function formatDateZyb(timestamp, format) { const formateArr = ['Y', 'M', 'D', 'h', 'm', 's']; let returnArr = []; let date = new Date(timestamp*1000); let year = date.getFullYear() let month = date.getMonth() + 1 let day = date.getDate() let hour = date.getHours() let minute = date.getMinutes() let second = date.getSeconds() returnArr.push(year, month, day); returnArr = returnArr.map(formatNumber); for (var i in returnArr) { format = format.replace(formateArr[i], returnArr[i]); } return format; } module.exports = { formatTime, formatTime2, formatUrl, formatDate, formatTimeTwo, formatNumber, throttle, getDateDiff, formatDateZyb, formatEncrypt }