util.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. // 回显数据字典
  16. const selectDictLabel = (datas, value) => {
  17. var actions = [];
  18. if (!datas) {
  19. return;
  20. }
  21. Object.keys(datas).some((key) => {
  22. if (datas[key].dictValue && datas[key].dictValue == value) {
  23. actions.push(datas[key].dictLabel);
  24. return true;
  25. } else {
  26. if (datas[key].id == value) {
  27. actions.push(datas[key].typeName);
  28. return true;
  29. }
  30. }
  31. })
  32. return actions.join('');
  33. }
  34. function formatLocation(longitude, latitude) {
  35. if (typeof longitude === 'string' && typeof latitude === 'string') {
  36. longitude = parseFloat(longitude)
  37. latitude = parseFloat(latitude)
  38. }
  39. longitude = longitude.toFixed(2)
  40. latitude = latitude.toFixed(2)
  41. return {
  42. longitude: longitude.toString().split('.'),
  43. latitude: latitude.toString().split('.')
  44. }
  45. }
  46. //获取两日期之间日期列表函数
  47. function getdiffdate(stime, etime) {
  48. //初始化日期列表,数组
  49. var diffdate = new Array();
  50. var i = 0;
  51. //开始日期小于等于结束日期,并循环
  52. while (stime <= etime) {
  53. diffdate[i] = stime;
  54. //获取开始日期时间戳
  55. var stime_ts = new Date(stime).getTime();
  56. //增加一天时间戳后的日期
  57. var next_date = stime_ts + (24 * 60 * 60 * 1000);
  58. //拼接年月日,这里的月份会返回(0-11),所以要+1
  59. var next_dates_y = new Date(next_date).getFullYear() + '-';
  60. var next_dates_m = (new Date(next_date).getMonth() + 1 < 10) ? '0' + (new Date(next_date).getMonth() + 1) +
  61. '-' : (new Date(next_date).getMonth() + 1) + '-';
  62. var next_dates_d = (new Date(next_date).getDate() < 10) ? '0' + new Date(next_date).getDate() : new Date(
  63. next_date).getDate();
  64. stime = next_dates_y + next_dates_m + next_dates_d;
  65. //增加数组key
  66. i++;
  67. }
  68. return diffdate;
  69. }
  70. /**
  71. * 计算两个日期之间的天数
  72. * @param dateString1 开始日期 yyyy-MM-dd
  73. * @param dateString2 结束日期 yyyy-MM-dd
  74. * @returns {number} 如果日期相同 返回一天 开始日期大于结束日期,返回0
  75. */
  76. const getDaysBetween = (dateString1, dateString2) => {
  77. let startDate = Date.parse(dateString1);
  78. let endDate = Date.parse(dateString2);
  79. if (startDate > endDate) {
  80. return 0;
  81. }
  82. if (startDate == endDate) {
  83. return 1;
  84. }
  85. let days = (endDate - startDate) / (1 * 24 * 60 * 60 * 1000);
  86. return days.toFixed(0);
  87. }
  88. /**
  89. * 乘法函数,用来得到精确的乘法结果
  90. * @param {Object} arg1
  91. * @param {Object} arg2
  92. */
  93. const accMul = (arg1, arg2) => {
  94. var m = 0;
  95. m += deal(arg1);
  96. m += deal(arg2);
  97. var r1 = Number(arg1.toString().replace('.', ''));
  98. var r2 = Number(arg2.toString().replace('.', ''));
  99. return (r1 * r2) / Math.pow(10, m);
  100. }
  101. /**
  102. * 加法函数,用来得到精确的加法结果
  103. * @param {Object} arg1
  104. * @param {Object} arg2
  105. */
  106. const accAdd = (arg1, arg2) => {
  107. var r1 = deal(arg1);
  108. var r2 = deal(arg2);
  109. var m = Math.pow(10, Math.max(r1, r2));
  110. return (arg1 * m + arg2 * m) / m;
  111. }
  112. const hasPermi = (permi) => {
  113. let list = uni.getStorageSync('permissions');
  114. let has = list.filter(item => item == permi);
  115. return has.length > 0 ? true : false;
  116. }
  117. /**
  118. * 除法函数,用来得到精确的除法结果
  119. * @param {Object} arg1
  120. * @param {Object} arg2
  121. */
  122. const accDiv = (arg1, arg2) => {
  123. var t1 = deal(arg1);
  124. var t2 = deal(arg2);
  125. var r1 = Number(arg1.toString().replace(".", ""))
  126. var r2 = Number(arg2.toString().replace(".", ""))
  127. return (r1 / r2) * Math.pow(10, t2 - t1);
  128. }
  129. /**
  130. * 求小数点后的数据长度
  131. */
  132. const deal = (arg) => {
  133. var t = 0;
  134. try {
  135. t = arg.toString().split('.')[1].length;
  136. } catch (e) {}
  137. return t;
  138. }
  139. /**
  140. * 判断 有效日期不能小于今天
  141. */
  142. const dateCompare = (beginDate, endDate) => {
  143. var beginArrayDate = beginDate.split('-');
  144. var endArrayDate = endDate.split('-')
  145. if (parseInt(endArrayDate[0], 10) > parseInt(beginArrayDate[0], 10)) return true;
  146. if (parseInt(endArrayDate[0], 10) == parseInt(beginArrayDate[0], 10)) {
  147. if (parseInt(endArrayDate[1], 10) > parseInt(beginArrayDate[1], 10)) return true;
  148. else {
  149. if (parseInt(endArrayDate[1], 10) == parseInt(beginArrayDate[1], 10)) {
  150. if (parseInt(endArrayDate[2], 10) >= parseInt(beginArrayDate[2], 10)) return true;
  151. }
  152. }
  153. }
  154. return false;
  155. }
  156. /**
  157. * 获取日期
  158. * day 获取日期
  159. * time 获取时间
  160. */
  161. const getDate = (obj = 'day') => {
  162. const date = new Date();
  163. let year = date.getFullYear();
  164. let month = date.getMonth() + 1;
  165. let day = date.getDate();
  166. let Hours = date.getHours();
  167. let Minutes = date.getMinutes();
  168. month = month > 9 ? month : '0' + month;
  169. day = day > 9 ? day : '0' + day;
  170. Hours = Hours > 9 ? Hours : '0' + Hours;
  171. Minutes = Minutes > 9 ? Minutes : '0' + Minutes;
  172. if (obj == 'day') {
  173. return `${year}-${month}-${day}`;
  174. }
  175. if (obj == 'lm') {
  176. month = date.getMonth();
  177. month = month > 9 ? month : '0' + month;
  178. return `${year}-${month}-${day}`;
  179. }
  180. if (obj == 'time') {
  181. return `${year}-${month}-${day}` + ' ' + Hours + ':' + Minutes;
  182. }
  183. }
  184. /**
  185. * 格式化时间
  186. */
  187. const format = (obj) => {
  188. const date = new Date(obj);
  189. let year = date.getFullYear();
  190. let month = date.getMonth() + 1;
  191. let day = date.getDate();
  192. let Hours = date.getHours();
  193. let Minutes = date.getMinutes();
  194. month = month > 9 ? month : '0' + month;
  195. day = day > 9 ? day : '0' + day;
  196. Hours = Hours > 9 ? Hours : '0' + Hours;
  197. Minutes = Minutes > 9 ? Minutes : '0' + Minutes;
  198. return `${year}-${month}-${day}` + ' ' + Hours + ':' + Minutes;
  199. }
  200. module.exports = {
  201. formatTime: formatTime,
  202. formatLocation: formatLocation,
  203. accMul: accMul,
  204. accAdd: accAdd,
  205. accDiv: accDiv,
  206. dateCompare: dateCompare,
  207. getDate: getDate,
  208. getDaysBetween: getDaysBetween,
  209. getdiffdate: getdiffdate,
  210. selectDictLabel: selectDictLabel,
  211. hasPermi:hasPermi
  212. }