utils.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. import qs from 'query-string'
  2. import appState from '/src/state'
  3. import SettingState from '/src/pages/detail/setting-state'
  4. import instruments from '/src/constant/instruments'
  5. import instrumentsClassfiy from '/src/constant/instrumentsClassfiy'
  6. /**是否是开发环境 */
  7. export const getDevelopment = function () {
  8. return /(192\.168|localhost)/ig.test(location.host)
  9. }
  10. export const addZero = (num: number) => {
  11. return num < 10 ? '0' + num : num
  12. }
  13. export const formatTime = (time: number) => {
  14. const s = time % 60
  15. const m = time / 60
  16. return addZero(Math.floor(m)) + ':' + addZero(Math.floor(s))
  17. }
  18. export const browser = () => {
  19. var u = navigator.userAgent
  20. // app = navigator.appVersion;
  21. return {
  22. trident: u.indexOf('Trident') > -1, //IE内核
  23. presto: u.indexOf('Presto') > -1, //opera内核
  24. webKit: u.indexOf('AppleWebKit') > -1, //苹果、谷歌内核
  25. gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1, //火狐内核
  26. mobile: !!u.match(/AppleWebKit.*Mobile.*/), //是否为移动终端
  27. ios: !!u.match(/Mac OS X/), //ios终端
  28. android: u.indexOf('ORCHESTRAAPPA') > -1 || u.indexOf('Adr') > -1, //android终端
  29. iPhone: u.indexOf('ORCHESTRAAPPI') > -1, //是否为iPhone或者QQHD浏览器
  30. isApp:
  31. u.indexOf('ORCHESTRAAPPI') > -1 ||
  32. u.indexOf('ORCHESTRAAPPA') > -1,
  33. isTeacher: u.indexOf('ORCHESTRATEACHER') > -1,
  34. isStudent: u.indexOf('ORCHESTRASTUDENT') > -1,
  35. isSchool: u.indexOf('ORCHESTRASCHOOL') > -1,
  36. iPad: u.indexOf('iPad') > -1, //是否iPad
  37. webApp: u.indexOf('Safari') == -1, //是否web应该程序,没有头部与底部
  38. weixin: u.indexOf('MicroMessenger') > -1, //是否微信 (2015-01-22新增)
  39. huawei: !!u.match(/huawei/i) || !!u.match(/honor/i),
  40. xiaomi: !!u.match(/mi\s/i) || !!u.match(/redmi/i) || !!u.match(/mix/i),
  41. }
  42. }
  43. export const getRequestHostname = () => {
  44. let search : any = ''
  45. try {
  46. search = qs.parse(location.search)
  47. } catch (error) {}
  48. if(search?.client){
  49. return search.client === 'student' ? '/api-student' : search.client === 'teacher' ? '/api-teacher' : '/api-backend'
  50. }
  51. const browserInfo = browser();
  52. if (browserInfo.isStudent){
  53. return '/api-student'
  54. }
  55. if (browserInfo.isTeacher){
  56. return '/api-teacher'
  57. }
  58. return '/api-backend'
  59. }
  60. export const getRandomKey = () => {
  61. let key = '' + new Date().getTime() + Math.floor(Math.random() * 1000000)
  62. return key
  63. }
  64. export const findNearestNumber = (arr: number[], target: number) => {
  65. return arr.reduce((pre, curr) => {
  66. return Math.abs(pre - target) > Math.abs(curr - target) ? curr : pre
  67. })
  68. }
  69. export const toNext = (hash: string, query?: any) => {
  70. const setPrefix = (url: string): string => {
  71. if (url) {
  72. return '?' + url
  73. }
  74. return ''
  75. }
  76. const searchStr = qs.stringify({
  77. ...qs.parse(location.search),
  78. _t: new Date().getTime(),
  79. })
  80. const hashSearchStr = qs.stringify(query)
  81. const nextUrl = location.origin + location.pathname + setPrefix(searchStr) + hash + setPrefix(hashSearchStr)
  82. return nextUrl
  83. // location.replace(nextUrl)
  84. }
  85. export const noop = (): void => {}
  86. export const getImageUrl = (name: string) => {
  87. return new URL(`./dir/${name}.png`, location.origin).href
  88. }
  89. export const setTongjiTag = (event: any[]) => {
  90. if ((window as any)._czc) {
  91. ;(window as any)._czc.push(event)
  92. }
  93. }
  94. export type IPlatform = 'ANDROID' | 'IOS' | 'WEB'
  95. export const getPlatform = (): IPlatform => {
  96. const browserInfo = browser()
  97. if (browserInfo.ios) {
  98. return 'IOS'
  99. }
  100. if (browserInfo.android) {
  101. return 'ANDROID'
  102. }
  103. return 'WEB'
  104. }
  105. type IJavaScriptAnimate = {
  106. timing: (timeFraction: number) => number
  107. draw: (progress: number) => void
  108. duration: number
  109. }
  110. export function JavaScriptAnimate({ timing, draw, duration }: IJavaScriptAnimate) {
  111. let start = performance.now()
  112. requestAnimationFrame(function animate(time) {
  113. // timeFraction 从 0 增加到 1
  114. let timeFraction = (time - start) / duration
  115. if (timeFraction > 1) timeFraction = 1
  116. // 计算当前动画状态
  117. let progress = timing(timeFraction)
  118. draw(progress) // 绘制
  119. if (timeFraction < 1) {
  120. requestAnimationFrame(animate)
  121. }
  122. })
  123. }
  124. /** 格式化当前曲谱缩放比例 */
  125. export const formatZoom = (num = 1) => {
  126. const size = {
  127. small: 0.5,
  128. middle: 0.7,
  129. large: 1,
  130. }
  131. const zoom = size[SettingState.sett.scoreSize]
  132. return num * zoom
  133. }
  134. /**
  135. * 设置声部初始化信息
  136. */
  137. export const setVoiceInit = () => {
  138. const chinesePartName = { ...instruments }
  139. const MusicalInstrumentClassification = { ...instrumentsClassfiy }
  140. appState.chinesePartName = chinesePartName
  141. appState.MusicalInstrumentClassification = MusicalInstrumentClassification
  142. }
  143. export const getVoiceChinesName = (name: string | undefined) => {
  144. let viewname = name || ''
  145. if (name) {
  146. const cname = appState.chinesePartName[name]
  147. if (!cname) {
  148. const names = Object.keys(appState.chinesePartName)
  149. for (const n of names) {
  150. if (name.match(n)) {
  151. // console.table({
  152. // 输入名称: name,
  153. // 循环名称: n,
  154. // 匹配: name.match(n)
  155. // })
  156. viewname = name.replace(n, appState.chinesePartName[n])
  157. break
  158. }
  159. }
  160. } else {
  161. viewname = cname
  162. }
  163. }
  164. return viewname ? name + (name !== viewname ? ' (' + viewname + ')' : '') : ''
  165. }
  166. // 判断是否被转译
  167. export const isEncoded = (uri: string) => {
  168. uri = uri || '';
  169. return uri !== decodeURI(uri);
  170. }