Files
aida_front/src/component/LoginPage/googleLogin.vue

220 lines
5.4 KiB
Vue
Raw Normal View History

2024-11-22 09:20:25 +08:00
<template>
<div class="Container">
<!-- 谷歌登录 -->
2025-09-23 16:20:21 +08:00
<div class="g_id_signin" id="g_id_signin">
</div>
<!-- <div class="icon" @click="toGmailLogin"> -->
<div class="icon">
2024-12-31 11:40:40 +08:00
<img src="@/assets/images/loginPage/gmailIcon.svg" alt="">
<span>{{ displayText }}</span>
2024-12-31 11:40:40 +08:00
</div>
2024-11-22 09:20:25 +08:00
</div>
</template>
<script>
2024-12-18 17:38:43 +08:00
import { defineComponent, ref, reactive, watch, onMounted, onBeforeUnmount, toRefs } from "vue";
2025-09-11 14:56:08 +08:00
import { Modal,message } from 'ant-design-vue';
import { useI18n } from 'vue-i18n'
2024-11-22 09:20:25 +08:00
export default defineComponent({
name: "login",
2024-12-11 16:26:36 +08:00
emits: ['googelLogin'],
2024-12-31 11:40:40 +08:00
props: {
text: {
type: String,
default: ''
2024-12-31 11:40:40 +08:00
}
},
2024-12-11 16:26:36 +08:00
setup(props, { emit }) {
2025-09-11 14:56:08 +08:00
const {t} = useI18n()
2025-09-23 16:20:21 +08:00
function decodeJWT(token) {
let base64Url = token.split(".")[1];
let base64 = base64Url.replace(/-/g, "+").replace(/_/g, "/");
let jsonPayload = decodeURIComponent(
atob(base64)
.split("")
.map(function (c) {
return "%" + ("00" + c.charCodeAt(0).toString(16)).slice(-2);
})
.join("")
);
return JSON.parse(jsonPayload);
}
2024-11-22 09:20:25 +08:00
const handleCredentialResponse = async (response) => {
// 获取回调响应的凭证数据 然后拿这个凭证给后台后台jwt进行解析获取登录信息
2025-09-23 16:20:21 +08:00
console.log("Encoded JWT ID token: " + response.credential);
const responsePayload = decodeJWT(response.credential);
console.log("Decoded JWT ID token fields:");
console.log(" Full Name: " + responsePayload.name);
console.log(" Given Name: " + responsePayload.given_name);
console.log(" Family Name: " + responsePayload.family_name);
console.log(" Unique ID: " + responsePayload.sub);
console.log(" Profile image URL: " + responsePayload.picture);
console.log(" Email: " + responsePayload.email);
2024-11-22 09:20:25 +08:00
let code = response.credential
2024-12-11 16:26:36 +08:00
emit('googelLogin',code)
2025-02-03 16:07:17 +08:00
window.isAddGmail = false
2024-11-22 09:20:25 +08:00
}
2024-12-11 16:26:36 +08:00
let data = reactive({
2024-12-18 17:38:43 +08:00
// scriptSrc:'https://apis.google.com/js/platform.js',
2024-12-11 16:26:36 +08:00
scriptSrc:'https://accounts.google.com/gsi/client',
2024-12-18 17:38:43 +08:00
script:null
2024-12-11 16:26:36 +08:00
})
2025-09-23 16:20:21 +08:00
console.log(import.meta.env.VITE_USER_NODE_ENV)
2025-09-04 13:44:30 +08:00
let GOOGLE_CLIENT_ID
if(import.meta.env.VITE_USER_NODE_ENV == 'development'){
2025-09-23 16:20:21 +08:00
GOOGLE_CLIENT_ID = '157095842121-kdd1fdf8m8nudvj9sprstb2k2prnf9e4.apps.googleusercontent.com'
2025-09-04 13:44:30 +08:00
}else{
GOOGLE_CLIENT_ID = '29310152396-nnsd3h533fld665oguu8ovrt1nukmt46.apps.googleusercontent.com'
}
2024-12-11 16:26:36 +08:00
// 使用谷歌登录的api
2024-12-18 17:38:43 +08:00
const createGmailLogin = async ()=>{
var existingScript = document.querySelector(`script[src="${data.scriptSrc}"]`);
2025-01-20 17:51:00 +08:00
if(!window.isAddGmail){
if(!existingScript){
window.isAddGmail = true
await new Promise((resolve, reject) => {
const script = document.createElement("script");
script.onload=()=>{
resolve()
}
document.body.appendChild(script);
2025-09-23 16:20:21 +08:00
script.src = data.scriptSrc
2025-01-20 17:51:00 +08:00
})
}
window.google.accounts.id.initialize({
// 主要就是填写client_id
client_id: GOOGLE_CLIENT_ID,
auto_select: false,
callback: handleCredentialResponse,
// context:"signin",
ux_mode:"popup",
itp_support:true,
2024-12-18 17:38:43 +08:00
});
2025-09-23 16:20:21 +08:00
console.log(document.querySelector('.Container #g_id_signin'))
2025-01-20 17:51:00 +08:00
window.google.accounts.id.renderButton(
2025-09-23 16:20:21 +08:00
document.querySelector('.Container #g_id_signin'),
{
type:"standard",//icon为只有一个icon
shape:"circle",
theme:"outline",
size:"large",
logo_alignment:"center",
});
2025-01-20 17:51:00 +08:00
}
2024-12-11 16:26:36 +08:00
}
2025-09-08 14:50:59 +08:00
const toGmailLogin = ()=>{
message.info(t('account.canNotUtilize'))
}
const displayText = computed(() => {
return props.text || t('Login.LoginWithGoogle')
})
2024-12-18 17:38:43 +08:00
onBeforeUnmount(()=>{
var existingScript = document.querySelector(`script[src="${data.scriptSrc}"]`);
2025-02-03 16:07:17 +08:00
if(existingScript){
existingScript.remove()
window.isAddGmail = false
}
2024-12-18 17:38:43 +08:00
})
2025-09-03 16:36:06 +08:00
onMounted(()=>{
2025-09-23 16:20:21 +08:00
createGmailLogin()
2025-09-03 16:36:06 +08:00
})
2024-11-22 09:20:25 +08:00
return {
2025-09-08 14:50:59 +08:00
toGmailLogin,
displayText,
2024-11-22 09:20:25 +08:00
}
},
})
</script>
2024-12-11 16:26:36 +08:00
<style scoped lang="less">
2024-11-22 09:20:25 +08:00
.Container{
2024-12-31 11:40:40 +08:00
position: relative;
2025-01-20 18:37:45 +08:00
border-radius: 4rem;
2025-01-20 18:39:30 +08:00
overflow: hidden;
2025-07-19 14:04:48 +08:00
background: transparent;
2025-08-22 10:27:48 +08:00
@media (max-width: 768px) {
border-radius: 2.5rem;
}
2024-12-11 16:26:36 +08:00
:deep(.nsm7Bb-HzV7m-LgbsSe-bN97Pc-sM5MNb ){
justify-content: center;
.nsm7Bb-HzV7m-LgbsSe-Bz112c{
padding: 0;
}
}
2024-12-31 11:40:40 +08:00
:deep(.nsm7Bb-HzV7m-LgbsSe.JGcpL-RbRzK){
width: 100%;
}
2025-01-20 16:28:16 +08:00
// :deep(.S9gUrf-YoZ4jf){
// *{
// width: 100%;
// height: 100%;
// }
// }
2024-12-31 11:40:40 +08:00
.icon{
// width: 40px;
width: auto;
2024-12-18 17:38:43 +08:00
display: flex;
align-items: center;
justify-content: center;
2024-12-31 11:40:40 +08:00
border-radius: 4rem;
2025-08-22 10:27:48 +08:00
padding: 1.3rem 4.6rem;
2024-12-18 17:38:43 +08:00
cursor: pointer;
2024-12-31 11:40:40 +08:00
box-sizing: border-box;
2025-01-20 18:35:17 +08:00
position: relative;
2025-07-19 14:04:48 +08:00
background: transparent;
2025-09-23 16:20:21 +08:00
pointer-events: none;
2025-11-03 16:52:43 +08:00
background-color: #F9FAFA;
2025-08-22 10:27:48 +08:00
@media (max-width: 768px) {
border-radius: 2.5rem;
padding: .32rem 1.7rem;
}
2024-12-18 17:38:43 +08:00
&:hover{
background: #f8faff;
}
img{
2024-12-31 11:40:40 +08:00
width: 3.8rem;
height: 3.8rem;
2025-08-22 10:27:48 +08:00
@media (max-width: 768px) {
width: 2.7rem;
height: 2.7rem;
}
2024-12-31 11:40:40 +08:00
}
span{
font-size: 1.4rem;
margin-left: 1.4rem;
2025-08-22 10:27:48 +08:00
@media (max-width: 768px) {
font-size: 1.2rem;
margin-left: 1rem;
}
2024-12-31 11:40:40 +08:00
}
}
.g_id_signin{
position: absolute;
2025-01-20 18:27:07 +08:00
width: 100%;
height: 100%;
2025-09-23 16:20:21 +08:00
// overflow: hidden;
2024-12-31 11:40:40 +08:00
top: 0;
left: 0;
2025-09-23 16:20:21 +08:00
// opacity: 0;
2025-07-19 14:04:48 +08:00
.S9gUrf-YoZ4jf{
2025-09-23 16:20:21 +08:00
2025-07-19 14:04:48 +08:00
}
2025-01-20 18:27:07 +08:00
:deep(.S9gUrf-YoZ4jf){
2025-01-20 16:37:04 +08:00
width: 100%;
height: 100%;
2025-09-23 16:20:21 +08:00
iframe{
zoom: 3;
}
2025-01-20 16:37:04 +08:00
}
2024-12-31 11:40:40 +08:00
}
&:hover{
.icon{
background: #f8faff;
2024-12-18 17:38:43 +08:00
}
}
2024-11-22 09:20:25 +08:00
}
</style>