use-login.ts 3.9 KB

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