common.js 5.9 KB

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