state.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import { reactive } from 'vue'
  2. import { browser, setAuth } from './helpers/utils'
  3. import { postMessage } from './helpers/native-message'
  4. type status = 'init' | 'login' | 'logout' | 'error'
  5. export const state = reactive({
  6. user: {
  7. status: 'init' as status,
  8. data: {} as any
  9. },
  10. platformType: 'STUDENT' as 'STUDENT' | 'TEACHER' | 'SCHOOL',
  11. clientId: {
  12. STUDENT: 'jmedu-student',
  13. TEACHER: 'jmedu-teacher',
  14. SCHOOL: 'jmedu-school'
  15. },
  16. platformApi: '/api-student' as '/api-student' | '/api-teacher' | '/api-school',
  17. version: '', // 版本号 例如: 1.0.0
  18. navBarHeight: 0, // 状态栏高度
  19. ossUploadUrl: 'https://ks3-cn-beijing.ksyuncs.com/',
  20. musicCertStatus: false as boolean, // 是否音乐认证
  21. openLiveStatus: false as boolean // 是否开通直播
  22. })
  23. // 预览上传到oss的地址
  24. export const getOssUploadUrl = (bucket: string) => {
  25. const tmpBucket = bucket || 'gyt'
  26. return `https://${tmpBucket}.ks3-cn-beijing.ksyuncs.com/`
  27. }
  28. export const setLoginInit = () => {
  29. state.user.status = 'init'
  30. state.user.data = null
  31. }
  32. export const setLogin = (data: any) => {
  33. state.user.status = 'login'
  34. state.user.data = data
  35. }
  36. export const setLogout = () => {
  37. state.user.status = 'logout'
  38. state.user.data = null
  39. }
  40. export const setLoginError = () => {
  41. state.user.status = 'error'
  42. state.user.data = null
  43. }
  44. // 用于处理跳转地址,如果是在app内,则打开一个新的webview, 否则跳转连接
  45. export const openDefaultWebView = (url?: string, callBack?: any) => {
  46. if (browser().isApp) {
  47. postMessage({
  48. api: 'openWebView',
  49. content: {
  50. url,
  51. orientation: 1,
  52. isHideTitle: false
  53. }
  54. })
  55. } else {
  56. callBack && callBack()
  57. }
  58. }
  59. /**
  60. * @description 微信授权-会根据环境去判断
  61. * @param wxAppId
  62. * @param urlString 回调链接【默认当前页面地址】
  63. * @returns void
  64. */
  65. export const goWechatAuth = (wxAppId: string, urlString?: string) => {
  66. // 开发环境
  67. if (import.meta.env.DEV) {
  68. const replaceUrl =
  69. `https://online.lexiaoya.cn/getWxCode?appid=${
  70. wxAppId || 'wx8654c671631cfade'
  71. }&state=STATE&redirect_uri=` + encodeURIComponent(urlString || window.location.href)
  72. window.location.replace(replaceUrl)
  73. }
  74. // 生产环境
  75. if (import.meta.env.PROD) {
  76. goAuth(wxAppId, urlString)
  77. }
  78. }
  79. const goAuth = (wxAppId: string, urlString?: string) => {
  80. // 用户授权
  81. console.log(urlString || window.location.href, 'urlString || window.location.href')
  82. const urlNow = encodeURIComponent(urlString || window.location.href)
  83. console.log(urlNow, 'urlNow')
  84. const scope = 'snsapi_base' //snsapi_userinfo //静默授权 用户无感知
  85. const appid = wxAppId || 'wx8654c671631cfade'
  86. const url = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${urlNow}&response_type=code&scope=${scope}&state=STATE&connect_redirect=1#wechat_redirect`
  87. window.location.replace(url)
  88. }
  89. /**
  90. * @description 支付宝授权-会根据环境去判断
  91. * @param wxAppId
  92. * @param urlString 回调链接【默认当前页面地址】
  93. * @returns void
  94. */
  95. export const goAliAuth = (alipayAppId: string, urlString?: string) => {
  96. // 支付宝授权
  97. const urlNow = encodeURIComponent(urlString || window.location.href)
  98. const appid = alipayAppId || '2021004113639386'
  99. // 开发环境
  100. if (import.meta.env.DEV) {
  101. const url = `https://online.lexiaoya.cn/getAliCode?app_id=${appid}&state=STATE&redirect_uri=${urlNow}`
  102. window.location.replace(url)
  103. }
  104. // 生产环境
  105. if (import.meta.env.PROD) {
  106. alipayAuth(alipayAppId, urlString)
  107. }
  108. }
  109. const alipayAuth = (alipayAppId: string, urlString?: string) => {
  110. // 用户授权
  111. const urlNow = encodeURIComponent(urlString || window.location.href)
  112. const scope = 'auth_base' //snsapi_userinfo //静默授权 用户无感知
  113. const appid = alipayAppId || '2021004113639386'
  114. // 判断是否是线上
  115. const url = `https://openauth.alipay.com/oauth2/publicAppAuthorize.htm?app_id=${appid}&redirect_uri=${urlNow}&response_type=auth_code&scope=${scope}&state=STATE`
  116. window.location.replace(url)
  117. }