util.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. function formatTime(time) {
  2. if (typeof time !== 'number' || time < 0) {
  3. return time
  4. }
  5. var hour = parseInt(time / 3600)
  6. time = time % 3600
  7. var minute = parseInt(time / 60)
  8. time = time % 60
  9. var second = time
  10. return ([hour, minute, second]).map(function(n) {
  11. n = n.toString()
  12. return n[1] ? n : '0' + n
  13. }).join(':')
  14. }
  15. function formatLocation(longitude, latitude) {
  16. if (typeof longitude === 'string' && typeof latitude === 'string') {
  17. longitude = parseFloat(longitude)
  18. latitude = parseFloat(latitude)
  19. }
  20. longitude = longitude.toFixed(2)
  21. latitude = latitude.toFixed(2)
  22. return {
  23. longitude: longitude.toString().split('.'),
  24. latitude: latitude.toString().split('.')
  25. }
  26. }
  27. var dateUtils = {
  28. UNITS: {
  29. '年': 31557600000,
  30. '月': 2629800000,
  31. '天': 86400000,
  32. '小时': 3600000,
  33. '分钟': 60000,
  34. '秒': 1000
  35. },
  36. humanize: function(milliseconds) {
  37. var humanize = '';
  38. for (var key in this.UNITS) {
  39. if (milliseconds >= this.UNITS[key]) {
  40. humanize = Math.floor(milliseconds / this.UNITS[key]) + key + '前';
  41. break;
  42. }
  43. }
  44. return humanize || '刚刚';
  45. },
  46. format: function(dateStr) {
  47. var date = this.parse(dateStr)
  48. var diff = Date.now() - date.getTime();
  49. if (diff < this.UNITS['天']) {
  50. return this.humanize(diff);
  51. }
  52. var _format = function(number) {
  53. return (number < 10 ? ('0' + number) : number);
  54. };
  55. return date.getFullYear() + '/' + _format(date.getMonth() + 1) + '/' + _format(date.getDate()) + '-' +
  56. _format(date.getHours()) + ':' + _format(date.getMinutes());
  57. },
  58. parse: function(str) { //将"yyyy-mm-dd HH:MM:ss"格式的字符串,转化为一个Date对象
  59. var a = str.split(/[^0-9]/);
  60. return new Date(a[0], a[1] - 1, a[2], a[3], a[4], a[5]);
  61. }
  62. };
  63. //环境配置
  64. let config = {
  65. dev: true,
  66. test: 'http://api.work.yuandaibao.com/', // wxd03e9b0848beb6a7
  67. release: 'https://mall.churenyiliao.com/', // wxf828191f0a322e72
  68. data: {
  69. version: '2.0.7',
  70. system: 1,
  71. json: 1,
  72. }
  73. }
  74. //接口路径
  75. let path = config.dev ? config.test : config.release;
  76. config.path = path;
  77. export {
  78. formatTime,
  79. formatLocation,
  80. dateUtils,
  81. config
  82. }