2024-11-22 09:20:25 +08:00
|
|
|
|
<template>
|
|
|
|
|
|
<div class="Container">
|
|
|
|
|
|
<!-- 谷歌登录 -->
|
2025-01-20 18:35:17 +08:00
|
|
|
|
<div class="g_id_signin" id="g_id_signin">
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<div class="icon">
|
2024-12-31 11:40:40 +08:00
|
|
|
|
<img src="@/assets/images/loginPage/gmailIcon.svg" alt="">
|
|
|
|
|
|
<span>{{ $props.text }}</span>
|
|
|
|
|
|
</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";
|
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: 'Sign in with Google'
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
2024-12-11 16:26:36 +08:00
|
|
|
|
setup(props, { emit }) {
|
2024-11-22 09:20:25 +08:00
|
|
|
|
const handleCredentialResponse = async (response) => {
|
|
|
|
|
|
// 获取回调响应的凭证数据 然后拿这个凭证给后台,后台jwt进行解析获取登录信息
|
|
|
|
|
|
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
|
|
|
|
})
|
|
|
|
|
|
let GOOGLE_CLIENT_ID = '194770296147-njd68pm7tnapgonkj2h48mhf63n15n3f.apps.googleusercontent.com'
|
|
|
|
|
|
// let GOOGLE_CLIENT_ID = '399537927614-3sd3rs9p79doocsrff7gm5m1f3chvmn2.apps.googleusercontent.com'
|
|
|
|
|
|
// 使用谷歌登录的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.src = data.scriptSrc
|
|
|
|
|
|
script.onload=()=>{
|
|
|
|
|
|
resolve()
|
|
|
|
|
|
}
|
|
|
|
|
|
document.body.appendChild(script);
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
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-01-20 17:51:00 +08:00
|
|
|
|
window.google.accounts.id.renderButton(
|
|
|
|
|
|
document.querySelector('.Container #g_id_signin'),
|
|
|
|
|
|
{
|
2025-01-20 18:02:03 +08:00
|
|
|
|
type:"standard",//icon为只有一个icon
|
2025-01-20 17:51:00 +08:00
|
|
|
|
shape:"circle",
|
2025-01-20 18:02:03 +08:00
|
|
|
|
theme:"outline",
|
2025-01-20 17:51:00 +08:00
|
|
|
|
size:"large",
|
|
|
|
|
|
logo_alignment:"center",
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
2024-12-11 16:26:36 +08:00
|
|
|
|
}
|
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
|
|
|
|
})
|
|
|
|
|
|
createGmailLogin()
|
2024-11-22 09:20:25 +08:00
|
|
|
|
return {
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
})
|
|
|
|
|
|
</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;
|
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;
|
|
|
|
|
|
border: 1px solid #dadce0;
|
2024-12-31 11:40:40 +08:00
|
|
|
|
border-radius: 4rem;
|
|
|
|
|
|
padding: .5rem 3rem;
|
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;
|
|
|
|
|
|
background: #fff;
|
2025-01-20 18:37:45 +08:00
|
|
|
|
pointer-events: none;
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
span{
|
|
|
|
|
|
font-size: 1.4rem;
|
|
|
|
|
|
margin-left: 1.4rem;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
.g_id_signin{
|
|
|
|
|
|
position: absolute;
|
2025-01-20 18:27:07 +08:00
|
|
|
|
width: 100%;
|
|
|
|
|
|
height: 100%;
|
|
|
|
|
|
overflow: hidden;
|
2024-12-31 11:40:40 +08:00
|
|
|
|
top: 0;
|
|
|
|
|
|
left: 0;
|
2025-01-20 18:35:17 +08:00
|
|
|
|
// opacity: 0;
|
2025-01-20 18:27:07 +08:00
|
|
|
|
:deep(.S9gUrf-YoZ4jf){
|
2025-01-20 16:37:04 +08:00
|
|
|
|
width: 100%;
|
|
|
|
|
|
height: 100%;
|
|
|
|
|
|
}
|
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>
|