index.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. import store from 'store'
  2. import { getQuery } from "./queryString";
  3. /** 获取浏览器信息 */
  4. export const browser = () => {
  5. const u = navigator.userAgent;
  6. return {
  7. trident: u.indexOf("Trident") > -1, //IE内核
  8. presto: u.indexOf("Presto") > -1, //opera内核
  9. webKit: u.indexOf("AppleWebKit") > -1, //苹果、谷歌内核
  10. gecko: u.indexOf("Gecko") > -1 && u.indexOf("KHTML") == -1, //火狐内核
  11. mobile: !!u.match(/AppleWebKit.*Mobile.*/), //是否为移动终端
  12. ios: !!u.match(/Mac OS X/) || /(iPhone|iPad|iPod|iOS)/i.test(u), //ios终端
  13. android: u.indexOf("Android") > -1 || u.indexOf("Adr") > -1, //判断是否是 android终端
  14. iPhone: u.indexOf("ORCHESTRAAPPI") > -1, //是否为iPhone或者QQHD浏览器
  15. isApp:
  16. u.includes("DAYAAPPA") ||
  17. u.includes("DAYAAPPI") ||
  18. u.includes("COLEXIUAPPA") ||
  19. u.includes("COLEXIUAPPI") ||
  20. u.includes("ORCHESTRAAPPI") ||
  21. u.includes("ORCHESTRAAPPA"),
  22. isTeacher: u.indexOf("ORCHESTRATEACHER") > -1 || u.includes("COLEXIUTEACHER"),
  23. isStudent: u.indexOf("ORCHESTRASTUDENT") > -1 || u.includes("COLEXIUSTUDENT"),
  24. isSchool: u.indexOf("ORCHESTRASCHOOL") > -1,
  25. iPad: u.indexOf("iPad") > -1, //是否iPad
  26. webApp: u.indexOf("Safari") == -1, //是否web应该程序,没有头部与底部
  27. weixin: u.indexOf("MicroMessenger") > -1, //是否微信 (2015-01-22新增)
  28. alipay: u.indexOf("AlipayClient") > -1, //是否支付宝
  29. huawei: !!u.match(/huawei/i) || !!u.match(/honor/i),
  30. xiaomi: !!u.match(/mi\s/i) || !!u.match(/redmi/i) || !!u.match(/mix/i),
  31. };
  32. };
  33. /** uuid */
  34. export const getRandomKey = () => {
  35. const key = "" + Date.now() + Math.floor(Math.random() * 1000000);
  36. return key;
  37. };
  38. export const AuthorizationKey = "AUTHORIZATION";
  39. /** 设置token */
  40. export const setToken = (value: any) => {
  41. sessionStorage.setItem(AuthorizationKey, value);
  42. };
  43. /** 获取token */
  44. export const getToken = () => {
  45. return sessionStorage.getItem(AuthorizationKey) || "";
  46. };
  47. /** 设置全局通信 */
  48. export const setGlobalData = (_key: string, _value: any) => {
  49. if (!_key || !_value) return;
  50. const GYM = (window as any).GYM || {};
  51. GYM[_key] = _value;
  52. (window as any).GYM = GYM;
  53. };
  54. const BEHAVIORIDKEY = "BEHAVIORID";
  55. /** 设置 behaviorId */
  56. export const setBehaviorId = (value: any) => {
  57. localStorage.setItem(BEHAVIORIDKEY, value);
  58. };
  59. /** 获取 behaviorId */
  60. export const getBehaviorId = () => {
  61. return localStorage.getItem(BEHAVIORIDKEY);
  62. };
  63. const campIdKey = "CAMPID";
  64. /** 设置 训练营ID */
  65. export const setCampId = (value: any) => {
  66. sessionStorage.setItem(campIdKey, value);
  67. };
  68. /** 获取 训练营ID */
  69. export const getCampId = () => {
  70. return sessionStorage.getItem(campIdKey) || '';
  71. };
  72. // 秒转分
  73. export const getSecondRPM = (second: number, type?: string) => {
  74. if (isNaN(second)) return "00:00";
  75. let h = Math.floor((second / 60 / 60) % 24);
  76. let m = Math.floor((second / 60) % 60);
  77. let s = Math.floor(second % 60);
  78. if (type === "cn") {
  79. return `${h > 0 ? h.toString().padStart(2, "0") + "时" : ""}${m.toString().padStart(2, "0")}分${s.toString().padStart(2, "0")}秒`;
  80. } else {
  81. return `${h > 0 ? h.toString().padStart(2, "0") + ":" : ""}${m.toString().padStart(2, "0")}:${s.toString().padStart(2, "0")}`;
  82. }
  83. };
  84. const SPEEDKEY = 'speeds'
  85. /** 设置曲谱速度 */
  86. export const setStorageSpeed = (id: any, speed: number) => {
  87. const speeds = store.get(SPEEDKEY) || {}
  88. speeds[id] = speed
  89. store.set(SPEEDKEY, speeds)
  90. }
  91. /** 获取曲谱速度 */
  92. export const getStorageSpeed = (id: any) => {
  93. const speeds = store.get(SPEEDKEY) || {}
  94. return speeds[id] || 0
  95. }
  96. /** 返回各产品应用对应的服务接口地址 */
  97. export const matchProductApiUrl = () => {
  98. const query: any = getQuery();
  99. const apiUrls = {
  100. 'cbs': {
  101. 'dev': 'https://dev.resource.colexiu.com',
  102. 'test': 'https://test.resource.colexiu.com',
  103. 'online': 'https://mec.colexiu.com'
  104. },
  105. 'gym': {
  106. 'dev': 'https://dev.dayaedu.com',
  107. 'test': 'https://test.dayaedu.com',
  108. 'online': 'https://online.dayaedu.com'
  109. },
  110. 'colexiu': {
  111. 'dev': 'https://dev.colexiu.com/',
  112. 'test': 'https://test.colexiu.com',
  113. 'online': 'https://online.colexiu.com'
  114. },
  115. 'orchestra': {
  116. 'dev': 'https://dev.lexiaoya.cn',
  117. 'test': 'https://test.lexiaoya.cn',
  118. 'online': 'https://online.lexiaoya.cn'
  119. },
  120. 'instrument': {
  121. 'dev': 'https://dev.kt.colexiu.com',
  122. 'test': 'https://test.kt.colexiu.com',
  123. 'test2': 'https://test.lexiaoya.cn',
  124. 'online': 'https://kt.colexiu.com',
  125. // 'online': 'https://resource.colexiu.com'
  126. }
  127. }
  128. let environment: 'dev' | 'test' | 'test2' | 'online' = location.origin.includes('//dev') ? 'dev' : location.origin.includes('//test') ? 'test' : (location.origin.includes('//online') || location.origin.includes('//kt') || location.origin.includes('//mec')) ? 'online' : 'dev'
  129. if (query.isCbs) {
  130. return apiUrls.cbs[environment] + '/cbs-app'
  131. } else {
  132. const pathName = location.pathname.includes('gym') ? 'gym' : location.pathname.includes('colexiu') ? 'colexiu' : location.pathname.includes('orchestra') ? 'orchestra' : 'instrument'
  133. // 兼容课堂乐器,测试环境两个域名
  134. if (pathName === 'instrument' && environment === 'test') {
  135. environment = location.origin.includes('//test.kt') ? 'test' : 'test2'
  136. }
  137. return apiUrls[pathName][environment] + '/edu-app'
  138. }
  139. }
  140. /** debounce */
  141. export const debounce = (fn: Function, ms = 0) => {
  142. let timeoutId: number | undefined;
  143. return function(...args: any[]) {
  144. clearTimeout(timeoutId)
  145. // @ts-ignore
  146. timeoutId = setTimeout(() => fn.apply(this, args), ms);
  147. }
  148. }