util.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. import CryptoJS from '@/common/crypto-js.js'
  2. function formatTime(time) {
  3. if (typeof time !== 'number' || time < 0) {
  4. return time
  5. }
  6. var hour = parseInt(time / 3600)
  7. time = time % 3600
  8. var minute = parseInt(time / 60)
  9. time = time % 60
  10. var second = time
  11. return ([hour, minute, second]).map(function(n) {
  12. n = n.toString()
  13. return n[1] ? n : '0' + n
  14. }).join(':')
  15. }
  16. // 回显数据字典
  17. const selectDictLabel = (datas, value) => {
  18. var actions = [];
  19. if (!datas) {
  20. return;
  21. }
  22. Object.keys(datas).some((key) => {
  23. if (datas[key].dictValue && datas[key].dictValue == value) {
  24. actions.push(datas[key].dictLabel);
  25. return true;
  26. } else {
  27. if (datas[key].id == value) {
  28. actions.push(datas[key].typeName);
  29. return true;
  30. }
  31. }
  32. })
  33. return actions.join('');
  34. }
  35. function formatLocation(longitude, latitude) {
  36. if (typeof longitude === 'string' && typeof latitude === 'string') {
  37. longitude = parseFloat(longitude)
  38. latitude = parseFloat(latitude)
  39. }
  40. longitude = longitude.toFixed(2)
  41. latitude = latitude.toFixed(2)
  42. return {
  43. longitude: longitude.toString().split('.'),
  44. latitude: latitude.toString().split('.')
  45. }
  46. }
  47. //获取两日期之间日期列表函数
  48. function getdiffdate(stime, etime) {
  49. //初始化日期列表,数组
  50. var diffdate = new Array();
  51. var i = 0;
  52. //开始日期小于等于结束日期,并循环
  53. while (stime <= etime) {
  54. diffdate[i] = stime;
  55. //获取开始日期时间戳
  56. var stime_ts = new Date(stime).getTime();
  57. //增加一天时间戳后的日期
  58. var next_date = stime_ts + (24 * 60 * 60 * 1000);
  59. //拼接年月日,这里的月份会返回(0-11),所以要+1
  60. var next_dates_y = new Date(next_date).getFullYear() + '-';
  61. var next_dates_m = (new Date(next_date).getMonth() + 1 < 10) ? '0' + (new Date(next_date).getMonth() + 1) +
  62. '-' : (new Date(next_date).getMonth() + 1) + '-';
  63. var next_dates_d = (new Date(next_date).getDate() < 10) ? '0' + new Date(next_date).getDate() : new Date(
  64. next_date).getDate();
  65. stime = next_dates_y + next_dates_m + next_dates_d;
  66. //增加数组key
  67. i++;
  68. }
  69. return diffdate;
  70. }
  71. /**
  72. * 计算两个日期之间的天数
  73. * @param dateString1 开始日期 yyyy-MM-dd
  74. * @param dateString2 结束日期 yyyy-MM-dd
  75. * @returns {number} 如果日期相同 返回一天 开始日期大于结束日期,返回0
  76. */
  77. const getDaysBetween = (dateString1, dateString2) => {
  78. let startDate = Date.parse(dateString1);
  79. let endDate = Date.parse(dateString2);
  80. if (startDate > endDate) {
  81. return 0;
  82. }
  83. if (startDate == endDate) {
  84. return 1;
  85. }
  86. let days = (endDate - startDate) / (1 * 24 * 60 * 60 * 1000);
  87. return days.toFixed(0);
  88. }
  89. /**
  90. * 乘法函数,用来得到精确的乘法结果
  91. * @param {Object} arg1
  92. * @param {Object} arg2
  93. */
  94. const accMul = (arg1, arg2) => {
  95. var m = 0;
  96. m += deal(arg1);
  97. m += deal(arg2);
  98. var r1 = Number(arg1.toString().replace('.', ''));
  99. var r2 = Number(arg2.toString().replace('.', ''));
  100. return (r1 * r2) / Math.pow(10, m);
  101. }
  102. /**
  103. * 加法函数,用来得到精确的加法结果
  104. * @param {Object} arg1
  105. * @param {Object} arg2
  106. */
  107. const accAdd = (arg1, arg2) => {
  108. var r1 = deal(arg1);
  109. var r2 = deal(arg2);
  110. var m = Math.pow(10, Math.max(r1, r2));
  111. return (arg1 * m + arg2 * m) / m;
  112. }
  113. const hasPermi = (permi) => {
  114. let list = uni.getStorageSync('permissions');
  115. let has = list.filter(item => item == permi);
  116. return has.length > 0 ? true : false;
  117. }
  118. /**
  119. * 除法函数,用来得到精确的除法结果
  120. * @param {Object} arg1
  121. * @param {Object} arg2
  122. */
  123. const accDiv = (arg1, arg2) => {
  124. var t1 = deal(arg1);
  125. var t2 = deal(arg2);
  126. var r1 = Number(arg1.toString().replace(".", ""))
  127. var r2 = Number(arg2.toString().replace(".", ""))
  128. return (r1 / r2) * Math.pow(10, t2 - t1);
  129. }
  130. /**
  131. * 求小数点后的数据长度
  132. */
  133. const deal = (arg) => {
  134. var t = 0;
  135. try {
  136. t = arg.toString().split('.')[1].length;
  137. } catch (e) {}
  138. return t;
  139. }
  140. /**
  141. * 判断 有效日期不能小于今天
  142. */
  143. const dateCompare = (beginDate, endDate) => {
  144. var beginArrayDate = beginDate.split('-');
  145. var endArrayDate = endDate.split('-')
  146. if (parseInt(endArrayDate[0], 10) > parseInt(beginArrayDate[0], 10)) return true;
  147. if (parseInt(endArrayDate[0], 10) == parseInt(beginArrayDate[0], 10)) {
  148. if (parseInt(endArrayDate[1], 10) > parseInt(beginArrayDate[1], 10)) return true;
  149. else {
  150. if (parseInt(endArrayDate[1], 10) == parseInt(beginArrayDate[1], 10)) {
  151. if (parseInt(endArrayDate[2], 10) >= parseInt(beginArrayDate[2], 10)) return true;
  152. }
  153. }
  154. }
  155. return false;
  156. }
  157. /**
  158. * 获取日期
  159. * day 获取日期
  160. * time 获取时间
  161. */
  162. const getDate = (obj = 'day') => {
  163. const date = new Date();
  164. let year = date.getFullYear();
  165. let month = date.getMonth() + 1;
  166. let day = date.getDate();
  167. let Hours = date.getHours();
  168. let Minutes = date.getMinutes();
  169. month = month > 9 ? month : '0' + month;
  170. day = day > 9 ? day : '0' + day;
  171. Hours = Hours > 9 ? Hours : '0' + Hours;
  172. Minutes = Minutes > 9 ? Minutes : '0' + Minutes;
  173. if (obj == 'day') {
  174. return `${year}-${month}-${day}`;
  175. }
  176. if (obj == 'lm') {
  177. month = date.getMonth();
  178. month = month > 9 ? month : '0' + month;
  179. return `${year}-${month}-${day}`;
  180. }
  181. if (obj == 'time') {
  182. return `${year}-${month}-${day}` + ' ' + Hours + ':' + Minutes;
  183. }
  184. }
  185. /**
  186. * 格式化时间
  187. */
  188. const format = (obj) => {
  189. const date = new Date(obj);
  190. let year = date.getFullYear();
  191. let month = date.getMonth() + 1;
  192. let day = date.getDate();
  193. let Hours = date.getHours();
  194. let Minutes = date.getMinutes();
  195. month = month > 9 ? month : '0' + month;
  196. day = day > 9 ? day : '0' + day;
  197. Hours = Hours > 9 ? Hours : '0' + Hours;
  198. Minutes = Minutes > 9 ? Minutes : '0' + Minutes;
  199. return `${year}-${month}-${day}` + ' ' + Hours + ':' + Minutes;
  200. }
  201. // 将时间戳转化为指定时间
  202. // way:方式(1=年月日,2=年月日时分秒)默认1, 也可以指定格式:yyyy-MM-dd HH:mm:ss
  203. const forDate = (inputTime, way) => {
  204. var date = new Date(inputTime);
  205. var y = date.getFullYear();
  206. var m = date.getMonth() + 1;
  207. m = m < 10 ? ('0' + m) : m;
  208. var d = date.getDate();
  209. d = d < 10 ? ('0' + d) : d;
  210. var h = date.getHours();
  211. h = h < 10 ? ('0' + h) : h;
  212. var minute = date.getMinutes();
  213. var second = date.getSeconds();
  214. minute = minute < 10 ? ('0' + minute) : minute;
  215. second = second < 10 ? ('0' + second) : second;
  216. var ms = date.getMilliseconds();
  217. way = way || 1;
  218. // way == 1 年月日
  219. if (way === 1) {
  220. return y + '-' + m + '-' + d;
  221. }
  222. // way == 1 年月日时分秒
  223. if (way === 2) {
  224. return y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + second;
  225. }
  226. // way == 具体格式 标准格式: yyyy-MM-dd HH:mm:ss
  227. if (typeof way == 'string') {
  228. return way.replace("yyyy", y).replace("MM", m).replace("dd", d).replace("HH", h).replace("mm", minute)
  229. .replace("ss", second).replace("ms", ms);
  230. }
  231. return y + '-' + m + '-' + d;
  232. }
  233. const encrypt = (word) => {
  234. let key = CryptoJS.enc.Utf8.parse("abcdefgabcdefg12");
  235. let srcs = CryptoJS.enc.Utf8.parse(word);
  236. let encrypted = CryptoJS.AES.encrypt(srcs, key, {
  237. mode: CryptoJS.mode.ECB,
  238. padding: CryptoJS.pad.Pkcs7
  239. });
  240. return encrypted.toString();
  241. }
  242. module.exports = {
  243. formatTime: formatTime,
  244. formatLocation: formatLocation,
  245. accMul: accMul,
  246. accAdd: accAdd,
  247. accDiv: accDiv,
  248. dateCompare: dateCompare,
  249. getDate: getDate,
  250. getDaysBetween: getDaysBetween,
  251. getdiffdate: getdiffdate,
  252. selectDictLabel: selectDictLabel,
  253. hasPermi: hasPermi,
  254. forDate:forDate,
  255. encrypt:encrypt
  256. }