state.ts 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. ossUploadUrl: 'https://ks3-cn-beijing.ksyuncs.com/',
  19. musicCertStatus: false as boolean, // 是否音乐认证
  20. openLiveStatus: false as boolean // 是否开通直播
  21. })
  22. // 预览上传到oss的地址
  23. export const getOssUploadUrl = (bucket: string) => {
  24. const tmpBucket = bucket || 'gyt'
  25. return `https://${tmpBucket}.ks3-cn-beijing.ksyuncs.com/`
  26. }
  27. export const setLoginInit = () => {
  28. state.user.status = 'init'
  29. state.user.data = null
  30. }
  31. export const setLogin = (data: any) => {
  32. state.user.status = 'login'
  33. state.user.data = data
  34. }
  35. export const setLogout = () => {
  36. state.user.status = 'logout'
  37. state.user.data = null
  38. }
  39. export const setLoginError = () => {
  40. state.user.status = 'error'
  41. state.user.data = null
  42. }
  43. // 用于处理跳转地址,如果是在app内,则打开一个新的webview, 否则跳转连接
  44. export const openDefaultWebView = (url?: string, callBack?: any) => {
  45. if (browser().isApp) {
  46. postMessage({
  47. api: 'openWebView',
  48. content: {
  49. url,
  50. orientation: 1,
  51. isHideTitle: false
  52. }
  53. })
  54. } else {
  55. callBack && callBack()
  56. }
  57. }
  58. /**
  59. * @description 微信授权-会根据环境去判断
  60. * @param wxAppId
  61. * @param urlString 回调链接【默认当前页面地址】
  62. * @returns void
  63. */
  64. export const goWechatAuth = (wxAppId: string, urlString?: string) => {
  65. // 开发环境
  66. if (import.meta.env.DEV) {
  67. const replaceUrl =
  68. `https://online.lexiaoya.cn/getWxCode?appid=${wxAppId || 'wx8654c671631cfade'}&state=STATE&redirect_uri=` +
  69. encodeURIComponent(urlString || window.location.href)
  70. window.location.replace(replaceUrl)
  71. }
  72. // 生产环境
  73. if (import.meta.env.PROD) {
  74. goAuth(wxAppId, urlString)
  75. }
  76. }
  77. const goAuth = (wxAppId: string, urlString?: string) => {
  78. // 用户授权
  79. console.log(urlString || window.location.href, 'urlString || window.location.href')
  80. const urlNow = encodeURIComponent(urlString || window.location.href)
  81. console.log(urlNow, 'urlNow')
  82. const scope = 'snsapi_base' //snsapi_userinfo //静默授权 用户无感知
  83. const appid = wxAppId || 'wx8654c671631cfade'
  84. 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`
  85. window.location.replace(url)
  86. }