utils.ts 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. export const browser = () => {
  2. const u = navigator.userAgent;
  3. return {
  4. trident: u.indexOf('Trident') > -1, //IE内核
  5. presto: u.indexOf('Presto') > -1, //opera内核
  6. webKit: u.indexOf('AppleWebKit') > -1, //苹果、谷歌内核
  7. gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1, //火狐内核
  8. mobile: !!u.match(/AppleWebKit.*Mobile.*/), //是否为移动终端
  9. ios: !!u.match(/Mac OS X/), //ios终端
  10. // ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端
  11. android: u.indexOf('DAYAAPPA') > -1 || u.indexOf('Adr') > -1, //android终端
  12. iPhone: u.indexOf('DAYAAPPI') > -1, //是否为iPhone或者QQHD浏览器
  13. isApp:
  14. u.indexOf('DAYAAPPI') > -1 ||
  15. u.indexOf('DAYAAPPA') > -1 ||
  16. u.indexOf('Adr') > -1,
  17. iPad: u.indexOf('iPad') > -1, //是否iPad
  18. webApp: u.indexOf('Safari') == -1, //是否web应该程序,没有头部与底部
  19. weixin: u.indexOf('MicroMessenger') > -1, //是否微信 (2015-01-22新增)
  20. alipay: u.indexOf('AlipayClient') > -1, //是否支付宝
  21. huawei: !!u.match(/huawei/i) || !!u.match(/honor/i),
  22. xiaomi: !!u.match(/mi\s/i) || !!u.match(/redmi/i) || !!u.match(/mix/i)
  23. };
  24. };
  25. export const getRandomKey = () => {
  26. const key = '' + new Date().getTime() + Math.floor(Math.random() * 1000000);
  27. return key;
  28. };
  29. /**
  30. * 删除token
  31. */
  32. export const removeAuth = () => {
  33. sessionStorage.removeItem('Authorization');
  34. };
  35. /**
  36. * 设置token
  37. * @param token
  38. * @returns {void}
  39. */
  40. export const setAuth = (token: any) => {
  41. sessionStorage.setItem('Authorization', token);
  42. };
  43. /**
  44. * 获取token
  45. */
  46. export const getAuth = () => {
  47. sessionStorage.getItem('Authorization');
  48. };
  49. export function checkPhone(phone: string) {
  50. const phoneRule =
  51. /^((13[0-9])|(14(0|[5-7]|9))|(15([0-3]|[5-9]))|(16(2|[5-7]))|(17[0-8])|(18[0-9])|(19([0-3]|[5-9])))\d{8}$/;
  52. return phoneRule.test(phone);
  53. }