common.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. export const getRandomKey = () => {
  2. let key = '' + new Date().getTime() + Math.floor(Math.random() * 1000000)
  3. return key
  4. }
  5. const browser = () => {
  6. var u = navigator.userAgent
  7. // app = navigator.appVersion;
  8. return {
  9. trident: u.indexOf('Trident') > -1, //IE内核
  10. presto: u.indexOf('Presto') > -1, //opera内核
  11. webKit: u.indexOf('AppleWebKit') > -1, //苹果、谷歌内核
  12. gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1, //火狐内核
  13. mobile: !!u.match(/AppleWebKit.*Mobile.*/), //是否为移动终端
  14. ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端
  15. // ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端
  16. android: u.indexOf('DAYAAPPA') > -1 || u.indexOf('Adr') > -1, //android终端
  17. iPhone: u.indexOf('DAYAAPPI') > -1, //是否为iPhone或者QQHD浏览器
  18. iPad: u.indexOf('iPad') > -1, //是否iPad
  19. webApp: u.indexOf('Safari') == -1, //是否web应该程序,没有头部与底部
  20. weixin: u.indexOf('MicroMessenger') > -1, //是否微信 (2015-01-22新增)
  21. qq: u.match(/\sQQ/i) == " qq" //是否QQ
  22. }
  23. }
  24. const calcMinute = (minute) => {
  25. if (minute <= 0) {
  26. return '0分钟'
  27. }
  28. let minutes = minute % 60 // 算出分钟
  29. let hours = 0 // 小时
  30. if (minute >= 60) {
  31. hours = (minute - minutes) / 60
  32. }
  33. let text = ''
  34. if (hours) {
  35. text = hours + '小时'
  36. }
  37. if (minutes) {
  38. text += minutes + '分钟'
  39. }
  40. return text
  41. }
  42. /**
  43. * 小于10的数变成 (0x)
  44. * @param {数值} num
  45. */
  46. const getSTD = (num) => {
  47. return Number(num) >= 10 ? num : '0' + num
  48. }
  49. /**
  50. * 获取格式化的年月日
  51. * @param {日期} time
  52. * @param {不是IOS里使用(多用于接口参数)} noIos
  53. */
  54. const getYMD = (time, noIos) => {
  55. let tempDate = time || new Date()
  56. if (typeof (tempDate) == 'string') {
  57. tempDate = new Date(time.replace(/-/ig, '/'))
  58. }
  59. let t = noIos ? '-' : '/'
  60. let month = getSTD(tempDate.getMonth() + 1)
  61. let day = getSTD(tempDate.getDate())
  62. return tempDate.getFullYear() + t + month + t + day
  63. }
  64. const getDateList = () => {
  65. let getNowYear = new Date().getFullYear()
  66. let getNowMonth = new Date().getMonth() + 1
  67. const getDateList = {}
  68. for (let i = 0; i < 12; i++) {
  69. let months = []
  70. for (let j = 0; j < (i == 0 ? getNowMonth : 12); j++) {
  71. months.push((j + 1) + '月')
  72. }
  73. getDateList[(getNowYear - i) + '年'] = months // 组合数据
  74. }
  75. return getDateList
  76. }
  77. // 防抖
  78. const _debounce = (fn, delay) => {
  79. delay = delay || 200
  80. let timer
  81. return function () {
  82. let th = this
  83. let args = arguments
  84. if (timer) {
  85. clearTimeout(timer)
  86. }
  87. timer = setTimeout(function () {
  88. timer = null
  89. fn.apply(th, args)
  90. }, delay)
  91. };
  92. }
  93. // 节流
  94. const _throttle = (fn, time) => {
  95. let canRun = true
  96. return function () {
  97. let _arguments = arguments
  98. if (!canRun) return
  99. canRun = false
  100. setTimeout(() => {
  101. fn.call(this, _arguments)
  102. canRun = true
  103. }, time);
  104. }
  105. }
  106. // const _throttle = (fn, interval) => {
  107. // let last
  108. // let timer
  109. // interval = interval || 200
  110. // return function () {
  111. // let that = this
  112. // let args = arguments
  113. // let now = +new Date();
  114. // if(last && last - now < 2000){
  115. // clearTimeout(timer)
  116. // }
  117. // timer = setTimeout(function () {
  118. // fn.apply(that, args)
  119. // last = +new Date()
  120. // }, 200)
  121. // }
  122. // }
  123. // 学生地址
  124. const validStudentUrl = () => {
  125. let url = window.location.href
  126. let returnUrl = ''
  127. if (/test/.test(url)) { // test环境
  128. returnUrl = 'http://mstutest.dayaedu.com'
  129. } else if (/dev/.test(url)) { // dev 环境
  130. returnUrl = 'http://mstudev.dayaedu.com'
  131. } else if (/online/.test(url)) { //线上
  132. returnUrl = 'https://mstuonline.dayaedu.com'
  133. } else { // 默认dev环境
  134. returnUrl = 'http://mstudev.dayaedu.com'
  135. }
  136. return returnUrl
  137. }
  138. /**
  139. * 转换金额为保留2位小数
  140. * @param x
  141. * @returns 强制保留2位小数的金额
  142. */
  143. const changeTwoDecimal = (amt) => {
  144. let f_x = parseFloat(amt);
  145. if (isNaN(f_x)) {
  146. return "0.00";
  147. }
  148. f_x = Math.round(f_x * 100) / 100;
  149. let s_x = f_x.toString();
  150. let pos_decimal = s_x.indexOf('.');
  151. if (pos_decimal < 0) {
  152. pos_decimal = s_x.length;
  153. s_x += '.';
  154. }
  155. while (s_x.length <= pos_decimal + 2) {
  156. s_x += '0';
  157. }
  158. return s_x;
  159. }
  160. export {
  161. browser,
  162. calcMinute,
  163. getSTD,
  164. getYMD,
  165. getDateList,
  166. _debounce,
  167. _throttle,
  168. validStudentUrl,
  169. changeTwoDecimal
  170. }