util.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. import moment from 'moment' // 时间库
  2. //获取年月日
  3. const formatTime = (date) => {
  4. const year = date.getFullYear()
  5. const month = date.getMonth() + 1
  6. const day = date.getDate()
  7. const hour = date.getHours()
  8. const minute = date.getMinutes()
  9. const second = date.getSeconds()
  10. return {
  11. year: year,
  12. month: [year, month].map(formatNumber).join('-'),
  13. day: [year, month, day].map(formatNumber).join('-'),
  14. month_tow: month,
  15. allDate: [year, month, day].map(formatNumber).join('-') + " " + hour + ":" + minute
  16. }
  17. }
  18. const formatNumber = n => {
  19. n = n.toString()
  20. return n[1] ? n : '0' + n
  21. }
  22. //删除数组的某一项
  23. const arrRemoveObj = (array, obj) => {
  24. let length = array.length;
  25. for (let i = 0; i < length; i++) {
  26. if (array[i] === obj) {
  27. if (i === 0) {
  28. array.shift();
  29. return array;
  30. } else if (i === length - 1) {
  31. array.pop();
  32. return array;
  33. } else {
  34. array.splice(i, 1);
  35. return array;
  36. }
  37. }
  38. }
  39. }
  40. //获取对应积分类型
  41. const getTypeItem = (arr, id) => {
  42. var item = arr.filter(element => {
  43. return typeof (id) == 'string' ? element.code == id : element.id == id
  44. });
  45. return item[0]
  46. }
  47. // 获取唯一标识(uid)
  48. const generateUUID = () => {
  49. var d = new Date().getTime();
  50. var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
  51. var r = (d + Math.random() * 16) % 16 | 0;
  52. d = Math.floor(d / 16);
  53. return (c == 'x' ? r : (r & 0x3 | 0x8)).toString(16);
  54. });
  55. return uuid;
  56. };
  57. // 根据传入的时间戳返回日期
  58. const returnStr = (time) => {
  59. if(!time){
  60. return ''
  61. }
  62. let date = `${time}000`
  63. let res = moment(Number(date)).format('YYYY-MM-DD HH:mm');
  64. return fnTime(res);
  65. };
  66. function fnTime(time) {
  67. let staer = time.slice(0, 11);
  68. let ptime = new Date(time).getTime()
  69. const twentyFourHours = 24 * 60 * 60 * 1000;
  70. const fortyEightHours = 24 * 60 * 60 * 1000 * 2;
  71. const date = new Date();
  72. const year = date.getFullYear();
  73. const month = date.getMonth() + 1;
  74. const day = date.getDate();
  75. const today = `${year}-${month}-${day}`;
  76. const todayTime = new Date(today).getTime();
  77. const yesterdayTime = new Date(todayTime - twentyFourHours).getTime();
  78. const lastYesterdayTime = new Date(todayTime - fortyEightHours).getTime();
  79. let day2 = moment().format('YYYY-MM-DD');
  80. if (ptime >= todayTime) {
  81. return '今天 ' + time.split(' ')[1] + ' 更新了执行计划';
  82. }
  83. else if (ptime < todayTime && yesterdayTime <= ptime) {
  84. return '昨天 ' + time.split(' ')[1] + ' 更新了执行计划';
  85. }
  86. else if (ptime < yesterdayTime && lastYesterdayTime <= ptime) {
  87. return '前天 ' + time.split(' ')[1] + ' 更新了执行计划';
  88. } else if (dateSum(day2, staer) > 30) {
  89. return '近30天无计划更新';
  90. } else {
  91. return time + ' 更新了执行计划';
  92. }
  93. }
  94. function dateSum(sDate1, sDate2) { //sDate1和sDate2是2008-12-13格式
  95. var aDate, oDate1, oDate2, iDays
  96. aDate = sDate1.split("-")
  97. oDate1 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0]) //转换为12-13-2008格式
  98. aDate = sDate2.split("-")
  99. oDate2 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0])
  100. iDays = parseInt(Math.abs(oDate1 - oDate2) / 1000 / 60 / 60 / 24) //把相差的毫秒数转换为天数
  101. return iDays
  102. }
  103. // 获取当前登录者在绩效系统中的身份
  104. const getRole=(type)=>{
  105. /* type为判断哪些角色*/
  106. var role = getCache('role')
  107. let is = false
  108. switch (type) {
  109. case 1: // 判断是否是主子管理员
  110. if (role == 'masterAdministrator' || role == 'childAdministrator') {
  111. is = true
  112. }
  113. break
  114. case 2: // 判断是否是部门管理员
  115. if (role == 'deptManager') {
  116. is = true
  117. }
  118. break
  119. case 3: // 判断是否是员工
  120. if (role == 'employee') {
  121. is = true
  122. }
  123. break
  124. case 4: // 判断是否是创始人
  125. if (role == 'creator') {
  126. is = true
  127. }
  128. break
  129. }
  130. return is
  131. }
  132. // 设置缓存
  133. const setCache = (key, val) => {
  134. dd.setStorageSync({
  135. key: key,
  136. data: val
  137. });
  138. }
  139. // 获取缓存
  140. const getCache = (key) => {
  141. return dd.getStorageSync({ key: key }).data;
  142. }
  143. // 防抖
  144. const _debounce = (fn, delay = 500) => {
  145. let timeout = null; // 创建一个标记用来存放定时器的返回值
  146. return function () {
  147. // 每当用户输入的时候把前一个setTimeout clear掉
  148. clearTimeout(timeout);
  149. // 然后又创建一个新的setTimeout,
  150. // 这样就能保证输入字符后的 interval 间隔内如果还有字符输入的话,就不会执行fn函数
  151. timeout = setTimeout(() => {
  152. // 关键在第一个参数,为了确保上下文环境为当前的this,所以不能直接用fn
  153. fn.apply(this, arguments);
  154. }, delay);
  155. }
  156. }
  157. // 节流
  158. const _throttle = (fn, interval = 500) => {
  159. var last;
  160. var timer;
  161. return function () {
  162. var th = this;
  163. var args = arguments;
  164. var now = +new Date();
  165. if (last && now - last < interval) {
  166. clearTimeout(timer);
  167. timer = setTimeout(function () { //用户最后一次点击时间间隔小于设置时间执行
  168. last = now;
  169. fn.apply(th, args);
  170. }, interval);
  171. } else {
  172. last = now;
  173. fn.apply(th, args);
  174. }
  175. }
  176. }
  177. module.exports = {
  178. getRole,
  179. _debounce,
  180. _throttle,
  181. setCache,
  182. getCache,
  183. returnStr,
  184. formatTime,
  185. arrRemoveObj,
  186. getTypeItem,
  187. generateUUID
  188. }