util.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. //获取两日期之间日期列表函数
  28. function getdiffdate(stime, etime) {
  29. //初始化日期列表,数组
  30. var diffdate = new Array();
  31. var i = 0;
  32. //开始日期小于等于结束日期,并循环
  33. while (stime <= etime) {
  34. diffdate[i] = stime;
  35. //获取开始日期时间戳
  36. var stime_ts = new Date(stime).getTime();
  37. //增加一天时间戳后的日期
  38. var next_date = stime_ts + (24 * 60 * 60 * 1000);
  39. //拼接年月日,这里的月份会返回(0-11),所以要+1
  40. var next_dates_y = new Date(next_date).getFullYear() + '-';
  41. var next_dates_m = (new Date(next_date).getMonth() + 1 < 10) ? '0' + (new Date(next_date).getMonth() + 1) +
  42. '-' : (new Date(next_date).getMonth() + 1) + '-';
  43. var next_dates_d = (new Date(next_date).getDate() < 10) ? '0' + new Date(next_date).getDate() : new Date(
  44. next_date).getDate();
  45. stime = next_dates_y + next_dates_m + next_dates_d;
  46. //增加数组key
  47. i++;
  48. }
  49. return diffdate;
  50. }
  51. /**
  52. * 计算两个日期之间的天数
  53. * @param dateString1 开始日期 yyyy-MM-dd
  54. * @param dateString2 结束日期 yyyy-MM-dd
  55. * @returns {number} 如果日期相同 返回一天 开始日期大于结束日期,返回0
  56. */
  57. const getDaysBetween = (dateString1, dateString2) => {
  58. let startDate = Date.parse(dateString1);
  59. let endDate = Date.parse(dateString2);
  60. if (startDate > endDate) {
  61. return 0;
  62. }
  63. if (startDate == endDate) {
  64. return 1;
  65. }
  66. let days = (endDate - startDate) / (1 * 24 * 60 * 60 * 1000);
  67. return days.toFixed(0);
  68. }
  69. var dateUtils = {
  70. UNITS: {
  71. '年': 31557600000,
  72. '月': 2629800000,
  73. '天': 86400000,
  74. '小时': 3600000,
  75. '分钟': 60000,
  76. '秒': 1000
  77. },
  78. humanize: function(milliseconds) {
  79. var humanize = '';
  80. for (var key in this.UNITS) {
  81. if (milliseconds >= this.UNITS[key]) {
  82. humanize = Math.floor(milliseconds / this.UNITS[key]) + key + '前';
  83. break;
  84. }
  85. }
  86. return humanize || '刚刚';
  87. },
  88. format: function(dateStr) {
  89. var date = this.parse(dateStr)
  90. var diff = Date.now() - date.getTime();
  91. if (diff < this.UNITS['天']) {
  92. return this.humanize(diff);
  93. }
  94. var _format = function(number) {
  95. return (number < 10 ? ('0' + number) : number);
  96. };
  97. return date.getFullYear() + '/' + _format(date.getMonth() + 1) + '/' + _format(date.getDay()) + '-' +
  98. _format(date.getHours()) + ':' + _format(date.getMinutes());
  99. },
  100. parse: function(str) { //将"yyyy-mm-dd HH:MM:ss"格式的字符串,转化为一个Date对象
  101. var a = str.split(/[^0-9]/);
  102. return new Date(a[0], a[1] - 1, a[2], a[3], a[4], a[5]);
  103. }
  104. };
  105. // 回显数据字典
  106. const selectDictLabel = (datas, value) => {
  107. var actions = [];
  108. Object.keys(datas).some((key) => {
  109. if (datas[key].value == ('' + value)) {
  110. actions.push(datas[key].label);
  111. return true;
  112. }
  113. })
  114. return actions.join('');
  115. }
  116. /**
  117. * 乘法函数,用来得到精确的乘法结果
  118. * @param {Object} arg1
  119. * @param {Object} arg2
  120. */
  121. const accMul = (arg1, arg2) => {
  122. var m = 0;
  123. m += deal(arg1);
  124. m += deal(arg2);
  125. var r1 = Number(arg1.toString().replace('.', ''));
  126. var r2 = Number(arg2.toString().replace('.', ''));
  127. return (r1 * r2) / Math.pow(10, m);
  128. }
  129. /**
  130. * 加法函数,用来得到精确的加法结果
  131. * @param {Object} arg1
  132. * @param {Object} arg2
  133. */
  134. const accAdd = (arg1, arg2) => {
  135. var r1 = deal(arg1);
  136. var r2 = deal(arg2);
  137. var m = Math.pow(10, Math.max(r1, r2));
  138. return (arg1 * m + arg2 * m) / m;
  139. }
  140. /**
  141. * 除法函数,用来得到精确的除法结果
  142. * @param {Object} arg1
  143. * @param {Object} arg2
  144. */
  145. const accDiv = (arg1, arg2) => {
  146. var t1 = deal(arg1);
  147. var t2 = deal(arg2);
  148. var r1 = Number(arg1.toString().replace(".", ""))
  149. var r2 = Number(arg2.toString().replace(".", ""))
  150. return (r1 / r2) * Math.pow(10, t2 - t1);
  151. }
  152. /**
  153. * 求小数点后的数据长度
  154. */
  155. const deal = (arg) => {
  156. var t = 0;
  157. try {
  158. t = arg.toString().split('.')[1].length;
  159. } catch (e) {}
  160. return t;
  161. }
  162. /**
  163. * 时间加上多少分
  164. */
  165. const dateTo = (date, minute) => {
  166. let time = new Date(date);
  167. time.setMinutes(time.getMinutes() + minute, time.getSeconds(), 0);
  168. return format(time);
  169. }
  170. /**
  171. * 判断 有效日期不能小于今天
  172. */
  173. const dateCompare = (beginDate, endDate) => {
  174. var beginArrayDate = beginDate.split('-');
  175. var endArrayDate = endDate.split('-')
  176. if (parseInt(endArrayDate[0], 10) > parseInt(beginArrayDate[0], 10)) return true;
  177. if (parseInt(endArrayDate[0], 10) == parseInt(beginArrayDate[0], 10)) {
  178. if (parseInt(endArrayDate[1], 10) > parseInt(beginArrayDate[1], 10)) return true;
  179. else {
  180. if (parseInt(endArrayDate[1], 10) == parseInt(beginArrayDate[1], 10)) {
  181. if (parseInt(endArrayDate[2], 10) >= parseInt(beginArrayDate[2], 10)) return true;
  182. }
  183. }
  184. }
  185. return false;
  186. }
  187. /**
  188. * 获取日期
  189. * day 获取日期
  190. * time 获取时间
  191. */
  192. const getDate = (obj = 'day') => {
  193. const date = new Date();
  194. let year = date.getFullYear();
  195. let month = date.getMonth() + 1;
  196. let day = date.getDate();
  197. let Hours = date.getHours();
  198. let Minutes = date.getMinutes();
  199. month = month > 9 ? month : '0' + month;
  200. day = day > 9 ? day : '0' + day;
  201. Hours = Hours > 9 ? Hours : '0' + Hours;
  202. Minutes = Minutes > 9 ? Minutes : '0' + Minutes;
  203. if (obj == 'day') {
  204. return `${year}-${month}-${day}`;
  205. }
  206. if (obj == 'time') {
  207. return `${year}-${month}-${day}` + ' ' + Hours + ':' + Minutes;
  208. }
  209. if (obj == 'year') {
  210. return `${year}`;
  211. }
  212. if (obj == 'month') {
  213. return `${month}`;
  214. }
  215. if (obj == 'ym') {
  216. return `${year}-${month}`;
  217. }
  218. }
  219. /**
  220. * 格式化时间
  221. */
  222. const format = (obj) => {
  223. const date = new Date(obj);
  224. let year = date.getFullYear();
  225. let month = date.getMonth() + 1;
  226. let day = date.getDate();
  227. let Hours = date.getHours();
  228. let Minutes = date.getMinutes();
  229. month = month > 9 ? month : '0' + month;
  230. day = day > 9 ? day : '0' + day;
  231. Hours = Hours > 9 ? Hours : '0' + Hours;
  232. Minutes = Minutes > 9 ? Minutes : '0' + Minutes;
  233. return `${year}-${month}-${day}` + ' ' + Hours + ':' + Minutes;
  234. }
  235. function convertToChinaNum(num) {
  236. var arr1 = new Array('零', '一', '二', '三', '四', '五', '六', '七', '八', '九');
  237. var arr2 = new Array('', '十', '百', '千', '万', '十', '百', '千', '亿', '十', '百', '千', '万', '十', '百', '千',
  238. '亿'); //可继续追加更高位转换值
  239. if (!num || isNaN(num)) {
  240. return "零";
  241. }
  242. var english = num.toString().split("")
  243. var result = "";
  244. for (var i = 0; i < english.length; i++) {
  245. var des_i = english.length - 1 - i; //倒序排列设值
  246. result = arr2[i] + result;
  247. var arr1_index = english[des_i];
  248. result = arr1[arr1_index] + result;
  249. }
  250. //将【零千、零百】换成【零】 【十零】换成【十】
  251. result = result.replace(/零(千|百|十)/g, '零').replace(/十零/g, '十');
  252. //合并中间多个零为一个零
  253. result = result.replace(/零+/g, '零');
  254. //将【零亿】换成【亿】【零万】换成【万】
  255. result = result.replace(/零亿/g, '亿').replace(/零万/g, '万');
  256. //将【亿万】换成【亿】
  257. result = result.replace(/亿万/g, '亿');
  258. //移除末尾的零
  259. result = result.replace(/零+$/, '')
  260. //将【零一十】换成【零十】
  261. //result = result.replace(/零一十/g, '零十');//貌似正规读法是零一十
  262. //将【一十】换成【十】
  263. result = result.replace(/^一十/g, '十');
  264. return result;
  265. }
  266. //检查重复
  267. const checkSame = (form, value, callback) => {
  268. let num = 0;
  269. form.op.forEach(item => {
  270. if (item.name == value) {
  271. num++;
  272. }
  273. });
  274. if (num > 1) {
  275. return callback(new Error('选项内容不能重复'));
  276. } else {
  277. callback();
  278. }
  279. }
  280. //检查重复
  281. const duplicate = (list) => {
  282. let tags = new Set();
  283. let obj = [];
  284. list.forEach(item => {
  285. tags.add(item.name);
  286. });
  287. Array.from(tags).forEach(item => {
  288. obj.push({
  289. name: item,
  290. check: false
  291. });
  292. });
  293. return obj;
  294. }
  295. module.exports = {
  296. formatTime: formatTime,
  297. formatLocation: formatLocation,
  298. dateUtils: dateUtils,
  299. selectDictLabel: selectDictLabel,
  300. accMul: accMul,
  301. accAdd: accAdd,
  302. accDiv: accDiv,
  303. dateCompare: dateCompare,
  304. getDate: getDate,
  305. dateTo: dateTo,
  306. cn: convertToChinaNum,
  307. getDaysBetween: getDaysBetween,
  308. getdiffdate: getdiffdate,
  309. checkSame: checkSame,
  310. duplicate:duplicate
  311. }