use-login.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import { hrefToTarget, isMobile, isWechat } from 'billd-utils';
  2. import { createApp } from 'vue';
  3. import { fetchQQLogin } from '@/api/qqUser';
  4. import { fullLoading } from '@/components/FullLoading';
  5. import {
  6. QQ_CLIENT_ID,
  7. QQ_OAUTH_URL,
  8. QQ_REDIRECT_URI,
  9. WECHAT_GZH_APPID,
  10. WECHAT_GZH_OAUTH_URL,
  11. WECHAT_REDIRECT_URI,
  12. } from '@/constant';
  13. import LoginModalCpt from '@/hooks/loginModal/index.vue';
  14. import { PlatformEnum } from '@/interface';
  15. import { useAppStore } from '@/store/app';
  16. import { useUserStore } from '@/store/user';
  17. import { clearLoginInfo, setLoginInfo } from '@/utils/cookie';
  18. import { getToken } from '@/utils/localStorage/user';
  19. const app = createApp(LoginModalCpt);
  20. const container = document.createElement('div');
  21. // @ts-ignore
  22. const instance: ComponentPublicInstance<InstanceType<typeof LoginModalCpt>> =
  23. app.mount(container);
  24. document.body.appendChild(container);
  25. const POSTMESSAGE_TYPE = [PlatformEnum.qqLogin];
  26. export async function handleQQLogin(e) {
  27. const { type, data } = e.data;
  28. if (!POSTMESSAGE_TYPE.includes(type)) return;
  29. console.log('收到消息', type, data);
  30. const userStore = useUserStore();
  31. const appStore = useAppStore();
  32. try {
  33. switch (type) {
  34. case PlatformEnum.qqLogin: {
  35. const res = await fetchQQLogin(data);
  36. if (res.code === 200) {
  37. window.$message.success('登录成功!');
  38. fullLoading({
  39. loading: false,
  40. });
  41. }
  42. userStore.setToken(res.data);
  43. userStore.getUserInfo();
  44. appStore.showLoginModal = false;
  45. break;
  46. }
  47. }
  48. } catch (error) {
  49. console.log(error);
  50. } finally {
  51. clearLoginInfo();
  52. }
  53. }
  54. export function loginTip(show = false) {
  55. const token = getToken();
  56. instance.show = show;
  57. const appStore = useAppStore();
  58. if (!token) {
  59. window.$message.warning('请先登录!');
  60. // instance.show = true;
  61. appStore.showLoginModal = true;
  62. return false;
  63. }
  64. return true;
  65. }
  66. export function loginMessage() {
  67. window.addEventListener('message', handleQQLogin);
  68. }
  69. export function useQQLogin() {
  70. fullLoading({
  71. loading: true,
  72. showMask: true,
  73. content: 'qq登录...',
  74. style: { color: 'white' },
  75. });
  76. const url = (state: string) =>
  77. `${QQ_OAUTH_URL}/authorize?response_type=code&client_id=${QQ_CLIENT_ID}&redirect_uri=${QQ_REDIRECT_URI}&scope=get_user_info,get_vip_info,get_vip_rich_info&state=${state}`;
  78. let loginInfo = JSON.stringify({
  79. isMobile: false,
  80. createTime: +new Date(),
  81. env: 'qq',
  82. dev: process.env.NODE_ENV === 'development',
  83. });
  84. if (isMobile()) {
  85. loginInfo = JSON.stringify({ ...JSON.parse(loginInfo), isMobile: true });
  86. setLoginInfo(loginInfo);
  87. hrefToTarget(url(window.btoa(loginInfo)));
  88. } else {
  89. setLoginInfo(loginInfo);
  90. window.open(
  91. url(window.btoa(loginInfo)),
  92. 'qq_login_window',
  93. 'toolbar=yes,location=no,directories=no,status=no,menubar=no,scrollbars=no,titlebar=no,toolbar=no,resizable=no,copyhistory=yes, width=918, height=609,top=250,left=400'
  94. );
  95. }
  96. }
  97. export const useWechatLogin = (qrData: { platform; exp; login_id }) => {
  98. const redirectUri = encodeURIComponent(WECHAT_REDIRECT_URI);
  99. const flag = isWechat();
  100. if (flag) {
  101. window.$message.error('请在微信打开!');
  102. return;
  103. }
  104. const loginInfo = JSON.stringify({
  105. isMobile: false,
  106. createTime: +new Date(),
  107. env: 'wechat',
  108. dev: process.env.NODE_ENV === 'development',
  109. qr_platform: qrData.platform,
  110. qr_exp: qrData.exp,
  111. qr_login_id: qrData.login_id,
  112. });
  113. setLoginInfo(loginInfo);
  114. const stateRes = window.btoa(loginInfo);
  115. // https://developers.weixin.qq.com/doc/oplatform/Website_App/WeChat_Login/Wechat_Login.html
  116. const url = `${WECHAT_GZH_OAUTH_URL}appid=${WECHAT_GZH_APPID}&redirect_uri=${redirectUri}&scope=snsapi_userinfo&response_type=code&state=${stateRes}`;
  117. hrefToTarget(url);
  118. };