util.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. 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-----'
  2. var publicKey =' -----BEGIN PUBLIC KEY-----MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCljXx8lXvdS9cxFXEvcC1m54cdU2O6yv4QNFFsfCjdn0hnwyydOfRY4oV0VB2apzG7X5FiPyxY8yj9acr43cqkH/zGSm+8CRWhRE0O197TBCj/ywnLz5KLm8745znQUVrfqWfAoC9JJjZG4ugVmEbsSQR1Bpks0USkvNWlLc1oKwIDAQAB-----END PUBLIC KEY-----'
  3. var RSA = require('wx_rsa.js')
  4. /**
  5. * 日期时间格式化
  6. * 2018/01/01 23:59:59
  7. */
  8. const formatTime = date => {
  9. const year = date.getFullYear()
  10. const month = date.getMonth() + 1
  11. const day = date.getDate()
  12. const hour = date.getHours()
  13. const minute = date.getMinutes()
  14. const second = date.getSeconds()
  15. return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
  16. }
  17. /**
  18. * 密码加密
  19. *
  20. */
  21. const formatEncrypt = (res = '') => {
  22. var input_rsa = res;
  23. var encrypt_rsa = new RSA.RSAKey();
  24. encrypt_rsa = RSA.KEYUTIL.getKey(publicKey);
  25. var encStr = encrypt_rsa.encrypt(input_rsa)
  26. encStr = RSA.hex2b64(encStr);
  27. return encStr
  28. }
  29. /**
  30. * 日期时间格式化
  31. * 2018-01-01 23:59:59
  32. */
  33. const formatTime2 = date => {
  34. const year = date.getFullYear()
  35. const month = date.getMonth() + 1
  36. const day = date.getDate()
  37. const hour = date.getHours()
  38. const minute = date.getMinutes()
  39. const second = date.getSeconds()
  40. return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')
  41. }
  42. /**
  43. * 对个位数前补零
  44. */
  45. const formatNumber = n => {
  46. n = n.toString()
  47. return n[1] ? n : '0' + n
  48. }
  49. /**
  50. * 日期格式化
  51. * 2018-01-01
  52. */
  53. const formatDate = date => {
  54. const year = date.getFullYear()
  55. const month = date.getMonth() + 1
  56. const day = date.getDate()
  57. const hour = date.getHours()
  58. const minute = date.getMinutes()
  59. const second = date.getSeconds()
  60. return [year, month, day].map(formatNumber).join('-')
  61. }
  62. /**
  63. * 时间格式化
  64. * 23:59:59
  65. */
  66. const formatTimeTwo = date => {
  67. const hour = date.getHours()
  68. const minute = date.getMinutes()
  69. const second = date.getSeconds()
  70. return [hour, minute, second].map(formatNumber).join(':')
  71. }
  72. /**
  73. * 路径格式化
  74. */
  75. const formatUrl = (url, obj) => {
  76. let temp = []
  77. let fn = (obj) => {
  78. let temp = []
  79. for (let i in obj) {
  80. if (obj[i] != null && typeof obj[i] == 'object') {
  81. temp.push(`${i}=${JSON.stringify(obj[i])}`)
  82. } else {
  83. temp.push(`${i}=${obj[i]}`)
  84. }
  85. }
  86. return temp.join('&')
  87. }
  88. return url.indexOf('?') < 0 ? url + '?' + fn(obj) : url + '&' + fn(obj)
  89. }
  90. /**
  91. * 函数节流和函数抖动。
  92. */
  93. function throttle(fn, gapTime) {
  94. if (gapTime == null || gapTime == undefined) {
  95. gapTime = 1500
  96. }
  97. let _lastTime = null
  98. // 返回新的函数
  99. return function () {
  100. let _nowTime = + new Date()
  101. if (_nowTime - _lastTime > gapTime || !_lastTime) {
  102. fn.apply(this, arguments) //将this和参数传给原函数
  103. _lastTime = _nowTime
  104. }
  105. }
  106. }
  107. /**
  108. *时间戳转天数。
  109. */
  110. function getDateDiff(dateTimeStamp) {
  111. var result;
  112. var minute = 1000 * 60;
  113. var hour = minute * 60;
  114. var day = hour * 24;
  115. var halfamonth = day * 15;
  116. var month = day * 30;
  117. var now = new Date().getTime();
  118. var diffValue = (dateTimeStamp * 1000)-now;
  119. if (diffValue < 0) {
  120. return 0;
  121. }
  122. var dayC = diffValue/ day;
  123. if (dayC >0) {
  124. result = "" + dayC;
  125. }
  126. return result;
  127. }
  128. /**
  129. *时间戳。
  130. */
  131. function formatDateZyb(timestamp, format) {
  132. const formateArr = ['Y', 'M', 'D', 'h', 'm', 's'];
  133. let returnArr = [];
  134. let date = new Date(timestamp*1000);
  135. let year = date.getFullYear()
  136. let month = date.getMonth() + 1
  137. let day = date.getDate()
  138. let hour = date.getHours()
  139. let minute = date.getMinutes()
  140. let second = date.getSeconds()
  141. returnArr.push(year, month, day);
  142. returnArr = returnArr.map(formatNumber);
  143. for (var i in returnArr) {
  144. format = format.replace(formateArr[i], returnArr[i]);
  145. }
  146. return format;
  147. }
  148. module.exports = {
  149. formatTime,
  150. formatTime2,
  151. formatUrl,
  152. formatDate,
  153. formatTimeTwo,
  154. formatNumber,
  155. throttle,
  156. getDateDiff,
  157. formatDateZyb,
  158. formatEncrypt
  159. }