util.js 4.0 KB

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