share.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import { Toast, Dialog } from 'vant'
  2. import { postMessage } from '@/helpers/native-message'
  3. import { browser } from '@/helpers/utils'
  4. import { state } from '@/state'
  5. // share information
  6. export const shareCall = (str: string, params?: any) => {
  7. const query = {
  8. url: str,
  9. action: params?.action || 'h5', // app, h5
  10. pageTag: params?.pageTag || 1 // 页面标识
  11. // params: {}
  12. }
  13. const iosStr = encodeURIComponent(JSON.stringify(query))
  14. console.log(iosStr, 'query')
  15. if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
  16. // ColexiuStudent 唤起学生端
  17. window.location.href = `ColexiuStudent://linkUrl=${iosStr}`
  18. } else if (/(Android)/i.test(navigator.userAgent)) {
  19. window.location.href = `colexiustudent://html:8888/SplashActivity?url=${iosStr}`
  20. } else {
  21. Toast('请用手机或移动设备打开')
  22. }
  23. }
  24. export const appDownload = () => {
  25. function failed() {
  26. window.location.href = '应用商店的地址'
  27. }
  28. function transfer(cb) {
  29. window.location.href = 'app的scheme'
  30. const initialTime = +new Date()
  31. let counter = 0
  32. let waitTime = 0
  33. const checkOpen = setInterval(() => {
  34. counter++
  35. waitTime = +new Date() - initialTime
  36. if (waitTime > 3500) {
  37. clearInterval(checkOpen)
  38. cb()
  39. }
  40. if (counter < 1000) {
  41. return
  42. }
  43. }, 20)
  44. document.addEventListener('visibilitychange', () => {
  45. const isHidden = document.hidden
  46. if (isHidden) {
  47. clearInterval(checkOpen)
  48. }
  49. })
  50. }
  51. transfer(failed)
  52. }
  53. // 页面跳转原生页面
  54. export const initJumpNativePage = (url: string) => {
  55. if (browser().isApp) {
  56. if (state.platformType === 'STUDENT') {
  57. // 自动跳转到学生端视频课详情购买页
  58. window.location.replace(url)
  59. // 为了处理andoird webview的跳转问题
  60. if (browser().ios) {
  61. window.location.replace(url)
  62. } else {
  63. postMessage({
  64. api: 'openWebView',
  65. content: {
  66. url,
  67. orientation: 1,
  68. isHideTitle: false
  69. }
  70. })
  71. postMessage({ api: 'back' })
  72. }
  73. return
  74. } else if (state.platformType === 'TEACHER') {
  75. Dialog.alert({
  76. title: '提示',
  77. message: '请使用酷乐秀学生端扫码打开',
  78. confirmButtonColor: '#2dc7aa'
  79. }).then(() => {
  80. postMessage({ api: 'back' })
  81. })
  82. }
  83. } else {
  84. // 如果不在app里面则不需要唤起操作
  85. shareCall(url)
  86. }
  87. }