util.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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(test){
  70. // let wordArray = utf8.parse(test);
  71. // return base64.stringify(wordArray);
  72. // }
  73. function encodeBase64(text){
  74. let res
  75. // #ifdef MP-WEIXIN
  76. let buffer = Buffer.from(text).buffer
  77. res = uni.arrayBufferToBase64(buffer)
  78. // #endif
  79. // #ifndef MP-WEIXIN
  80. let wordArray = utf8.parse(text);
  81. res = base64.stringify(wordArray);
  82. // #endif
  83. return res
  84. }
  85. function decodeBase64(text){
  86. let buffer
  87. // #ifdef MP-WEIXIN
  88. res = uni.base64ToArrayBuffer(text)
  89. // #endif
  90. // #ifndef MP-WEIXIN
  91. res = base64.stringify(wordArray);
  92. // #endif
  93. }
  94. module.exports = {
  95. formatTime: formatTime,
  96. formatLocation: formatLocation,
  97. dateUtils: dateUtils,
  98. encodeBase64:encodeBase64,
  99. }