state.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 goWechatAuth = (wxAppId: string, urlString?: string) => {
  56. // 开发环境
  57. if (import.meta.env.DEV) {
  58. const replaceUrl =
  59. `https://online.lexiaoya.cn/getWxCode?appid=${
  60. wxAppId || 'wx8654c671631cfade'
  61. }&state=STATE&redirect_uri=` +
  62. encodeURIComponent(urlString || window.location.href);
  63. window.location.replace(replaceUrl);
  64. }
  65. // 生产环境
  66. if (import.meta.env.PROD) {
  67. goAuth(wxAppId, urlString);
  68. }
  69. };
  70. const goAuth = (wxAppId: string, urlString?: string) => {
  71. // 用户授权
  72. const urlNow = encodeURIComponent(urlString || window.location.href);
  73. // console.log(urlNow, 'urlNow');
  74. const scope = 'snsapi_base'; //snsapi_userinfo //静默授权 用户无感知
  75. const appid = wxAppId || 'wx8654c671631cfade';
  76. 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`;
  77. window.location.replace(url);
  78. };
  79. /**
  80. * @description 支付宝授权-会根据环境去判断
  81. * @param wxAppId
  82. * @param urlString 回调链接【默认当前页面地址】
  83. * @returns void
  84. */
  85. export const goAliAuth = (alipayAppId: string, urlString?: string) => {
  86. // 支付宝授权
  87. const urlNow = encodeURIComponent(urlString || window.location.href);
  88. const appid = alipayAppId || '2021004100630808';
  89. // 开发环境
  90. if (import.meta.env.DEV) {
  91. let url = `https://kt.colexiu.com/getAliCode?app_id=${appid}&state=STATE&redirect_uri=${urlNow}`;
  92. window.location.replace(url);
  93. }
  94. // 生产环境
  95. if (import.meta.env.PROD) {
  96. alipayAuth(alipayAppId, urlString);
  97. }
  98. };
  99. const alipayAuth = (alipayAppId: string, urlString?: string) => {
  100. // 用户授权
  101. const urlNow = encodeURIComponent(urlString || window.location.href);
  102. const scope = 'auth_base'; //snsapi_userinfo //静默授权 用户无感知
  103. const appid = alipayAppId || '2021004100630808';
  104. // 判断是否是线上
  105. let url = `https://openauth.alipay.com/oauth2/publicAppAuthorize.htm?app_id=${appid}&redirect_uri=${urlNow}&response_type=auth_code&scope=${scope}&state=STATE`;
  106. window.location.replace(url);
  107. };