state.ts 4.1 KB

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