state.ts 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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=${wxAppId || 'wx8654c671631cfade'}&state=STATE&redirect_uri=` +
  70. encodeURIComponent(urlString || window.location.href)
  71. window.location.replace(replaceUrl)
  72. }
  73. // 生产环境
  74. if (import.meta.env.PROD) {
  75. goAuth(wxAppId, urlString)
  76. }
  77. }
  78. const goAuth = (wxAppId: string, urlString?: string) => {
  79. // 用户授权
  80. console.log(urlString || window.location.href, 'urlString || window.location.href')
  81. const urlNow = encodeURIComponent(urlString || window.location.href)
  82. console.log(urlNow, 'urlNow')
  83. const scope = 'snsapi_base' //snsapi_userinfo //静默授权 用户无感知
  84. const appid = wxAppId || 'wx8654c671631cfade'
  85. 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`
  86. window.location.replace(url)
  87. }