state.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import { reactive } from 'vue';
  2. import { browser } 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. navBarHeight: 0, // 状态栏高度
  11. ossUploadUrl: 'https://ks3-cn-beijing.ksyuncs.com/'
  12. });
  13. // 预览上传到oss的地址
  14. export const getOssUploadUrl = (bucket: string) => {
  15. const tmpBucket = bucket || 'gym';
  16. return `https://${tmpBucket}.ks3-cn-beijing.ksyuncs.com/`;
  17. };
  18. export const setLoginInit = () => {
  19. state.user.status = 'init';
  20. state.user.data = null;
  21. };
  22. export const setLogin = (data: any) => {
  23. state.user.status = 'login';
  24. state.user.data = data;
  25. };
  26. export const setLogout = () => {
  27. state.user.status = 'logout';
  28. state.user.data = null;
  29. };
  30. export const setLoginError = () => {
  31. state.user.status = 'error';
  32. state.user.data = null;
  33. };
  34. // 用于处理跳转地址,如果是在app内,则打开一个新的webview, 否则跳转连接
  35. export const openDefaultWebView = (url?: string, callBack?: any) => {
  36. if (browser().isApp) {
  37. postMessage({
  38. api: 'openWebView',
  39. content: {
  40. url,
  41. orientation: 1,
  42. isHideTitle: false
  43. }
  44. });
  45. } else {
  46. callBack && callBack();
  47. }
  48. };
  49. /**
  50. * @description 微信授权-会根据环境去判断
  51. * @param wxAppId
  52. * @param urlString 回调链接【默认当前页面地址】
  53. * @returns void
  54. */
  55. export const goWechatAuthTemp = (wxAppId: string, urlString?: string) => {
  56. goAuth(wxAppId, urlString);
  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://kt.colexiu.com/getWxCode?appid=${
  69. wxAppId || 'wx8654c671631cfade'
  70. }&state=STATE&redirect_uri=` +
  71. encodeURIComponent(urlString || window.location.href);
  72. window.location.replace(replaceUrl);
  73. }
  74. // 生产环境
  75. if (import.meta.env.PROD) {
  76. goAuth(wxAppId, urlString);
  77. }
  78. };
  79. const goAuth = (wxAppId: string, urlString?: string) => {
  80. // 用户授权
  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. };
  88. /**
  89. * @description 支付宝授权-会根据环境去判断
  90. * @param wxAppId
  91. * @param urlString 回调链接【默认当前页面地址】
  92. * @returns void
  93. */
  94. export const goAliAuth = (alipayAppId: string, urlString?: string) => {
  95. // 支付宝授权
  96. const urlNow = encodeURIComponent(urlString || window.location.href);
  97. const appid = alipayAppId || '2021004100630808';
  98. // 开发环境
  99. // if (import.meta.env.DEV) {
  100. let url = `https://kt.colexiu.com/getAliCode?app_id=${appid}&state=STATE&redirect_uri=${urlNow}`;
  101. window.location.replace(url);
  102. // }
  103. // 生产环境
  104. // if (import.meta.env.PROD) {
  105. // alipayAuth(alipayAppId, urlString);
  106. // }
  107. };
  108. const alipayAuth = (alipayAppId: string, urlString?: string) => {
  109. // 用户授权
  110. const urlNow = encodeURIComponent(urlString || window.location.href);
  111. const scope = 'auth_base'; //snsapi_userinfo //静默授权 用户无感知
  112. const appid = alipayAppId || '2021004100630808';
  113. // 判断是否是线上
  114. let url = `https://openauth.alipay.com/oauth2/publicAppAuthorize.htm?app_id=${appid}&redirect_uri=${urlNow}&response_type=auth_code&scope=${scope}&state=STATE`;
  115. window.location.replace(url);
  116. };