util.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. var dictUtils = {
  2. // 处理状态
  3. dealStateStr: {
  4. 0: "尚未处理",
  5. 1: "处理成功",
  6. 2: "处理失败"
  7. },
  8. getDealStateStr: function(state) {
  9. return this.dealStateStr[state]
  10. }
  11. }
  12. function formatTime(time) {
  13. if (typeof time !== 'number' || time < 0) {
  14. return time
  15. }
  16. var hour = parseInt(time / 3600)
  17. time = time % 3600
  18. var minute = parseInt(time / 60)
  19. time = time % 60
  20. var second = time
  21. return ([hour, minute, second]).map(function(n) {
  22. n = n.toString()
  23. return n[1] ? n : '0' + n
  24. }).join(':')
  25. }
  26. function formatLocation(longitude, latitude) {
  27. if (typeof longitude === 'string' && typeof latitude === 'string') {
  28. longitude = parseFloat(longitude)
  29. latitude = parseFloat(latitude)
  30. }
  31. longitude = longitude.toFixed(2)
  32. latitude = latitude.toFixed(2)
  33. return {
  34. longitude: longitude.toString().split('.'),
  35. latitude: latitude.toString().split('.')
  36. }
  37. }
  38. var dateUtils = {
  39. UNITS: {
  40. '年': 31557600000,
  41. '月': 2629800000,
  42. '天': 86400000,
  43. '小时': 3600000,
  44. '分钟': 60000,
  45. '秒': 1000
  46. },
  47. humanize: function(milliseconds) {
  48. var humanize = '';
  49. for (var key in this.UNITS) {
  50. if (milliseconds >= this.UNITS[key]) {
  51. humanize = Math.floor(milliseconds / this.UNITS[key]) + key + '前';
  52. break;
  53. }
  54. }
  55. return humanize || '刚刚';
  56. },
  57. format: function(dateStr) {
  58. var date = this.parse(dateStr)
  59. var diff = Date.now() - date.getTime();
  60. if (diff < this.UNITS['天']) {
  61. return this.humanize(diff);
  62. }
  63. var _format = function(number) {
  64. return (number < 10 ? ('0' + number) : number);
  65. };
  66. return date.getFullYear() + '/' + _format(date.getMonth() + 1) + '/' + _format(date.getDate()) + '-' +
  67. _format(date.getHours()) + ':' + _format(date.getMinutes());
  68. },
  69. parse: function(str) { //将"yyyy-mm-dd HH:MM:ss"格式的字符串,转化为一个Date对象
  70. var a = str.split(/[^0-9]/);
  71. return new Date(a[0], a[1] - 1, a[2], a[3], a[4], a[5]);
  72. }
  73. };
  74. var mobileUtils = {
  75. // 移动
  76. isChinaMobile: /^134[0-8][0-9]{7}$|^(13[5-9]|14[4|7|8]|15[0|1|2|4|7|8|9]|16[5]|17[8]|18[2|3|4|7|8]|19[5|8])[0-9]{8}$/,
  77. // 联通
  78. isChinaUnion: /^(13[0-2]|14[0|5|6]|15[5-6]|16[6-7]|17[5-6]|18[5-6])[0-9]{8}$/,
  79. // 电信
  80. isChinaTelcom: /^(133|14[1|9]|15[3]|16[2]|17[3|7]|18[0|1|9]|19[1|3|9])[0-9]{8}|(1349)[0-9]{7}$/,
  81. // 虚拟运营商
  82. isVNO: /^(17[0-1])[0-9]{8}$/,
  83. checkMobile: function(telphone) {
  84. if (telphone.length > 0) {
  85. telphone = telphone.trim();
  86. } else {
  87. return this.setReturnJson(false, '请输入手机号');
  88. }
  89. if (telphone.length !== 11) {
  90. return this.setReturnJson(false, '手机号码长度不符');
  91. }
  92. if (this.isChinaMobile.test(telphone)) {
  93. return this.setReturnJson(true, '移动', {
  94. name: 'ChinaMobile'
  95. });
  96. } else if (this.isChinaUnion.test(telphone)) {
  97. return this.setReturnJson(true, '联通', {
  98. name: 'ChinaUnion'
  99. });
  100. } else if (this.isChinaTelcom.test(telphone)) {
  101. return this.setReturnJson(true, '电信', {
  102. name: 'ChinaTelcom'
  103. });
  104. } else if (this.isVNO.test(telphone)) {
  105. return this.setReturnJson(true, '虚拟运营商', {
  106. name: 'VNO'
  107. });
  108. } else {
  109. return this.setReturnJson(false, '手机号码不正确');
  110. }
  111. },
  112. setReturnJson: function(status, msg, data) {
  113. if (typeof status !== 'boolean' && typeof status !== 'number') {
  114. status = false;
  115. }
  116. if (typeof msg !== 'string') {
  117. msg = '';
  118. }
  119. return {
  120. 'status': status,
  121. 'msg': msg,
  122. 'data': data
  123. };
  124. }
  125. };
  126. var fileUtils = {
  127. // 获取(路径+)文件名后缀
  128. getFileExt(fileName) {
  129. if (fileName == '') return ''
  130. var index= fileName.lastIndexOf(".") // 获取最后一个.的位置
  131. if (index >= 0)
  132. return fileName.substring(index + 1)
  133. else
  134. return ''
  135. },
  136. isTypeImage(fileName) {
  137. let ext = this.getFileExt(fileName)
  138. if (ext == '')
  139. return false
  140. return [ 'png', 'jpg', 'jpeg', 'bmp', 'gif', 'webp', 'psd', 'svg', 'tiff' ].indexOf(ext.toLowerCase()) !== -1;
  141. }
  142. }
  143. export {
  144. dictUtils,
  145. formatTime,
  146. formatLocation,
  147. dateUtils,
  148. mobileUtils,
  149. fileUtils
  150. }