util.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // #ifndef MP-WEIXIN
  2. const base64 = require("crypto-js/enc-base64");
  3. const utf8 = require("crypto-js/enc-utf8");
  4. // #endif
  5. var $protobuf = require("protobufjs/minimal");
  6. $util = $protobuf.util;
  7. function formatTime(time) {
  8. if (typeof time !== 'number' || time < 0) {
  9. return time
  10. }
  11. var hour = parseInt(time / 3600)
  12. time = time % 3600
  13. var minute = parseInt(time / 60)
  14. time = time % 60
  15. var second = time
  16. return ([hour, minute, second]).map(function (n) {
  17. n = n.toString()
  18. return n[1] ? n : '0' + n
  19. }).join(':')
  20. }
  21. function formatLocation(longitude, latitude) {
  22. if (typeof longitude === 'string' && typeof latitude === 'string') {
  23. longitude = parseFloat(longitude)
  24. latitude = parseFloat(latitude)
  25. }
  26. longitude = longitude.toFixed(2)
  27. latitude = latitude.toFixed(2)
  28. return {
  29. longitude: longitude.toString().split('.'),
  30. latitude: latitude.toString().split('.')
  31. }
  32. }
  33. var dateUtils = {
  34. UNITS: {
  35. '年': 31557600000,
  36. '月': 2629800000,
  37. '天': 86400000,
  38. '小时': 3600000,
  39. '分钟': 60000,
  40. '秒': 1000
  41. },
  42. humanize: function (milliseconds) {
  43. var humanize = '';
  44. for (var key in this.UNITS) {
  45. if (milliseconds >= this.UNITS[key]) {
  46. humanize = Math.floor(milliseconds / this.UNITS[key]) + key + '前';
  47. break;
  48. }
  49. }
  50. return humanize || '刚刚';
  51. },
  52. format: function (dateStr) {
  53. var date = this.parse(dateStr)
  54. var diff = Date.now() - date.getTime();
  55. if (diff < this.UNITS['天']) {
  56. return this.humanize(diff);
  57. }
  58. var _format = function (number) {
  59. return (number < 10 ? ('0' + number) : number);
  60. };
  61. return date.getFullYear() + '/' + _format(date.getMonth() + 1) + '/' + _format(date.getDate()) + '-' +
  62. _format(date.getHours()) + ':' + _format(date.getMinutes());
  63. },
  64. parse: function (str) { //将"yyyy-mm-dd HH:MM:ss"格式的字符串,转化为一个Date对象
  65. var a = str.split(/[^0-9]/);
  66. return new Date(a[0], a[1] - 1, a[2], a[3], a[4], a[5]);
  67. }
  68. };
  69. function encodeBase64(text){
  70. let res
  71. // #ifdef MP-WEIXIN
  72. let buffer = Buffer.from(text).buffer
  73. res = uni.arrayBufferToBase64(buffer)
  74. // #endif
  75. // #ifndef MP-WEIXIN
  76. let wordArray = utf8.parse(text);
  77. res = base64.stringify(wordArray);
  78. // #endif
  79. return res
  80. }
  81. function decodeBase64(text){
  82. let resStr
  83. // #ifdef MP-WEIXIN
  84. let buffer = uni.base64ToArrayBuffer(text)
  85. resStr = Buffer.from(buffer).toString();
  86. // #endif
  87. // #ifndef MP-WEIXIN
  88. let wordArray = base64.parse(text);
  89. resStr = utf8.stringify(wordArray)
  90. // #endif
  91. return resStr
  92. }
  93. module.exports = {
  94. formatTime: formatTime,
  95. formatLocation: formatLocation,
  96. dateUtils: dateUtils,
  97. encodeBase64:encodeBase64,
  98. decodeBase64:decodeBase64,
  99. }