state.ts 4.2 KB

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