use-login.ts 4.2 KB

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