index.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import store from 'store'
  2. import { getQuery } from "./queryString";
  3. import { storeData } from "../store";
  4. /** 获取浏览器信息 */
  5. export const browser = () => {
  6. const u = navigator.userAgent;
  7. const isAndroid = /(?:Android)/.test(u);
  8. const isFireFox = /(?:Firefox)/.test(u);
  9. function isIpadFun() {
  10. const ua = window.navigator.userAgent;
  11. let IsIPad = false;
  12. if (/ipad/i.test(ua)) {
  13. IsIPad = true;
  14. }
  15. // iPad from IOS13
  16. const macApp = ua.match(/Macintosh/i) != null;
  17. if (macApp) {
  18. // need to distinguish between Macbook and iPad
  19. const canvas = document.createElement('canvas');
  20. if (canvas != null) {
  21. const context: any =
  22. canvas.getContext('webgl') || canvas.getContext('experimental-webgl');
  23. if (context) {
  24. const info = context.getExtension('WEBGL_debug_renderer_info');
  25. if (info) {
  26. const renderer = context.getParameter(info.UNMASKED_RENDERER_WEBGL);
  27. if (renderer.indexOf('Apple') != -1) IsIPad = true;
  28. }
  29. }
  30. }
  31. }
  32. return IsIPad;
  33. }
  34. return {
  35. trident: u.indexOf("Trident") > -1, //IE内核
  36. presto: u.indexOf("Presto") > -1, //opera内核
  37. webKit: u.indexOf("AppleWebKit") > -1, //苹果、谷歌内核
  38. gecko: u.indexOf("Gecko") > -1 && u.indexOf("KHTML") == -1, //火狐内核
  39. mobile: !!u.match(/AppleWebKit.*Mobile.*/), //是否为移动终端
  40. ios: !!u.match(/Mac OS X/) || /(iPhone|iPad|iPod|iOS)/i.test(u), //ios终端
  41. android: u.indexOf("Android") > -1 || u.indexOf("Adr") > -1, //判断是否是 android终端
  42. iPhone: u.indexOf("ORCHESTRAAPPI") > -1, //是否为iPhone或者QQHD浏览器
  43. isApp:
  44. u.includes("DAYAAPPA") ||
  45. u.includes("DAYAAPPI") ||
  46. u.includes("COLEXIUAPPA") ||
  47. u.includes("COLEXIUAPPI") ||
  48. u.includes("ORCHESTRAAPPI") ||
  49. u.includes("ORCHESTRAAPPA"),
  50. isTeacher: u.indexOf("ORCHESTRATEACHER") > -1 || u.includes("COLEXIUTEACHER"),
  51. isStudent: u.indexOf("ORCHESTRASTUDENT") > -1 || u.includes("COLEXIUSTUDENT"),
  52. isSchool: u.indexOf("ORCHESTRASCHOOL") > -1,
  53. iPad: u.indexOf("iPad") > -1, //是否iPad
  54. isTablet:
  55. /(?:iPad|PlayBook)/.test(u) ||
  56. (isAndroid && !/(?:Mobile)/.test(u)) ||
  57. (isFireFox && /(?:Tablet)/.test(u)) ||
  58. isIpadFun(),
  59. webApp: u.indexOf("Safari") == -1, //是否web应该程序,没有头部与底部
  60. weixin: u.indexOf("MicroMessenger") > -1, //是否微信 (2015-01-22新增)
  61. alipay: u.indexOf("AlipayClient") > -1, //是否支付宝
  62. huawei: !!u.match(/huawei/i) || !!u.match(/honor/i),
  63. xiaomi: !!u.match(/mi\s/i) || !!u.match(/redmi/i) || !!u.match(/mix/i),
  64. };
  65. };
  66. /** uuid */
  67. export const getRandomKey = () => {
  68. const key = "" + Date.now() + Math.floor(Math.random() * 1000000);
  69. return key;
  70. };
  71. export const AuthorizationKey = "AUTHORIZATION";
  72. /** 设置token */
  73. export const setToken = (value: any) => {
  74. sessionStorage.setItem(AuthorizationKey, value);
  75. };
  76. /** 获取token */
  77. export const getToken = () => {
  78. return sessionStorage.getItem(AuthorizationKey) || "";
  79. };
  80. /** 设置全局通信 */
  81. export const setGlobalData = (_key: string, _value: any) => {
  82. if (!_key || !_value) return;
  83. const GYM = (window as any).GYM || {};
  84. GYM[_key] = _value;
  85. (window as any).GYM = GYM;
  86. };
  87. const BEHAVIORIDKEY = "BEHAVIORID";
  88. /** 设置 behaviorId */
  89. export const setBehaviorId = (value: any) => {
  90. localStorage.setItem(BEHAVIORIDKEY, value);
  91. };
  92. /** 获取 behaviorId */
  93. export const getBehaviorId = () => {
  94. return localStorage.getItem(BEHAVIORIDKEY);
  95. };
  96. const campIdKey = "CAMPID";
  97. /** 设置 训练营ID */
  98. export const setCampId = (value: any) => {
  99. sessionStorage.setItem(campIdKey, value);
  100. };
  101. /** 获取 训练营ID */
  102. export const getCampId = () => {
  103. return sessionStorage.getItem(campIdKey) || '';
  104. };
  105. // 秒转分
  106. export const getSecondRPM = (second: number, type?: string) => {
  107. if (isNaN(second)) return "00:00";
  108. let h = Math.floor((second / 60 / 60) % 24);
  109. let m = Math.floor((second / 60) % 60);
  110. let s = Math.floor(second % 60);
  111. if (type === "cn") {
  112. return `${h > 0 ? h.toString().padStart(2, "0") + "时" : ""}${m.toString().padStart(2, "0")}分${s.toString().padStart(2, "0")}秒`;
  113. } else {
  114. return `${h > 0 ? h.toString().padStart(2, "0") + ":" : ""}${m.toString().padStart(2, "0")}:${s.toString().padStart(2, "0")}`;
  115. }
  116. };
  117. const SPEEDKEY = 'speeds'
  118. /** 设置曲谱速度 */
  119. export const setStorageSpeed = (id: any, speed: number) => {
  120. const speeds = store.get(SPEEDKEY) || {}
  121. speeds[id] = speed
  122. store.set(SPEEDKEY, speeds)
  123. }
  124. /** 获取曲谱速度 */
  125. export const getStorageSpeed = (id: any) => {
  126. const speeds = store.get(SPEEDKEY) || {}
  127. console.log('初始速度', speeds)
  128. return speeds[id] || 0
  129. }
  130. /** 管乐迷区分业务接口 */
  131. export const getRequestHostname = () => {
  132. const query: any = getQuery();
  133. let blankOld = ['mteadev.dayaedu.com', 'mteatest.dayaedu.com', 'mteaonline.dayaedu.com']
  134. let blank = ["dev.gym.lexiaoya.cn/accompany-teacher/", "test.gym.lexiaoya.cn/accompany-teacher/", "gym.lexiaoya.cn/accompany-teacher/"];
  135. let webBlankOld = ["mandev.dayaedu.com", "mantest.dayaedu.com", "manonline.dayaedu.com", "test.dayaedu.com", "online.dayaedu.com"];
  136. let webBlank = ["dev.gym.lexiaoya.cn/accompany-web/", "test.gym.lexiaoya.cn/accompany-web/", "gym.lexiaoya.cn/accompany-web/"];
  137. const host = location.hostname + location.pathname
  138. if (blank.includes(host) || blankOld.includes(location.hostname) || query.systemType === "teacher" || storeData.isTeacher) {
  139. return "/api-teacher";
  140. } else if (webBlank.includes(host) || webBlankOld.includes(location.hostname) || query.systemType === "web") {
  141. return "/api-web";
  142. }
  143. return "/api-student";
  144. };
  145. /** debounce */
  146. export const debounce = (fn: Function, ms = 0) => {
  147. let timeoutId: number | undefined;
  148. return function(...args: any[]) {
  149. clearTimeout(timeoutId)
  150. // @ts-ignore
  151. timeoutId = setTimeout(() => fn.apply(this, args), ms);
  152. }
  153. }