state.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. orchestraInfo: {
  11. token: '' as any,
  12. phone: '' as any,
  13. installStatus: 0 as any,
  14. nickname: '',
  15. avatar: '',
  16. unionId: 0 // 是否已关联账号
  17. } as any, // 管乐团信息
  18. projectType: 'default' as 'default' | 'tenant', // 机构端,还是默认
  19. payBackPath: '/tenant/',
  20. platformType: '' as 'STUDENT' | 'TEACHER',
  21. platformApi: '/api-student' as '/api-student' | '/api-teacher',
  22. version: '', // 版本号 例如: 1.0.0
  23. ossUploadUrl: 'https://ks3-cn-beijing.ksyuncs.com/',
  24. musicCertStatus: false as boolean, // 是否音乐认证
  25. openLiveStatus: false as boolean // 是否开通直播
  26. })
  27. // 预览上传到oss的地址
  28. export const getOssUploadUrl = (bucket: string) => {
  29. const tmpBucket = bucket || 'daya'
  30. return `https://${tmpBucket}.ks3-cn-beijing.ksyuncs.com/`
  31. }
  32. export const setLoginInit = () => {
  33. state.user.status = 'init'
  34. state.user.data = null
  35. }
  36. export const setLogin = (data: any) => {
  37. state.user.status = 'login'
  38. state.user.data = data
  39. }
  40. export const setLogout = () => {
  41. state.user.status = 'logout'
  42. state.user.data = null
  43. }
  44. export const setLoginError = () => {
  45. state.user.status = 'error'
  46. state.user.data = null
  47. }
  48. // 用于处理跳转地址,如果是在app内,则打开一个新的webview, 否则跳转连接
  49. export const openDefaultWebView = (url?: string, callBack?: any) => {
  50. if (browser().isApp) {
  51. postMessage({
  52. api: 'openWebView',
  53. content: {
  54. url,
  55. orientation: 1,
  56. isHideTitle: false
  57. }
  58. })
  59. } else {
  60. callBack && callBack()
  61. }
  62. }
  63. /**
  64. * @description 微信授权-会根据环境去判断
  65. * @param wxAppId
  66. * @param urlString 回调链接【默认当前页面地址】
  67. * @returns void
  68. */
  69. export const goWechatAuth = (wxAppId: string, urlString?: string) => {
  70. // 开发环境
  71. if (import.meta.env.DEV) {
  72. const replaceUrl =
  73. `https://kt.colexiu.com/getWxCode?appid=${
  74. wxAppId || 'wx8654c671631cfade'
  75. }&state=STATE&redirect_uri=` +
  76. encodeURIComponent(urlString || window.location.href)
  77. window.location.replace(replaceUrl)
  78. }
  79. // 生产环境
  80. if (import.meta.env.PROD) {
  81. goAuth(wxAppId, urlString)
  82. }
  83. }
  84. const goAuth = (wxAppId: string, urlString?: string) => {
  85. // 用户授权
  86. const urlNow = encodeURIComponent(urlString || window.location.href)
  87. // console.log(urlNow, 'urlNow');
  88. const scope = 'snsapi_base' //snsapi_userinfo //静默授权 用户无感知
  89. const appid = wxAppId || 'wx8654c671631cfade'
  90. 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`
  91. window.location.replace(url)
  92. }
  93. /**
  94. * @description 支付宝授权-会根据环境去判断
  95. * @param wxAppId
  96. * @param urlString 回调链接【默认当前页面地址】
  97. * @returns void
  98. */
  99. export const goAliAuth = (alipayAppId: string, urlString?: string) => {
  100. // 支付宝授权
  101. const urlNow = encodeURIComponent(urlString || window.location.href)
  102. const appid = alipayAppId || '2021004100630808'
  103. // 开发环境
  104. if (import.meta.env.DEV) {
  105. const url = `https://kt.colexiu.com/getAliCode?app_id=${appid}&state=STATE&redirect_uri=${urlNow}`
  106. window.location.replace(url)
  107. }
  108. // 生产环境
  109. if (import.meta.env.PROD) {
  110. alipayAuth(alipayAppId, urlString)
  111. }
  112. }
  113. const alipayAuth = (alipayAppId: string, urlString?: string) => {
  114. // 用户授权
  115. const urlNow = encodeURIComponent(urlString || window.location.href)
  116. const scope = 'auth_base' //snsapi_userinfo //静默授权 用户无感知
  117. const appid = alipayAppId || '2021004100630808'
  118. // 判断是否是线上
  119. const url = `https://openauth.alipay.com/oauth2/publicAppAuthorize.htm?app_id=${appid}&redirect_uri=${urlNow}&response_type=auth_code&scope=${scope}&state=STATE`
  120. window.location.replace(url)
  121. }