Compare commits

..

1 Commits

Author SHA1 Message Date
Tim 7bd1225b27 feat: add point mall module 2025-08-17 01:23:47 +08:00
20 changed files with 97 additions and 67 deletions
@@ -7,5 +7,4 @@ import lombok.Data;
public class DiscordLoginRequest { public class DiscordLoginRequest {
private String code; private String code;
private String redirectUri; private String redirectUri;
private String inviteToken;
} }
@@ -7,5 +7,4 @@ import lombok.Data;
public class GithubLoginRequest { public class GithubLoginRequest {
private String code; private String code;
private String redirectUri; private String redirectUri;
private String inviteToken;
} }
@@ -6,5 +6,4 @@ import lombok.Data;
@Data @Data
public class GoogleLoginRequest { public class GoogleLoginRequest {
private String idToken; private String idToken;
private String inviteToken;
} }
@@ -7,5 +7,4 @@ import lombok.Data;
public class MakeReasonRequest { public class MakeReasonRequest {
private String token; private String token;
private String reason; private String reason;
private String inviteToken;
} }
@@ -9,5 +9,4 @@ public class RegisterRequest {
private String email; private String email;
private String password; private String password;
private String captcha; private String captcha;
private String inviteToken;
} }
@@ -8,5 +8,4 @@ public class TwitterLoginRequest {
private String code; private String code;
private String redirectUri; private String redirectUri;
private String codeVerifier; private String codeVerifier;
private String inviteToken;
} }
@@ -7,5 +7,4 @@ import lombok.Data;
public class VerifyRequest { public class VerifyRequest {
private String username; private String username;
private String code; private String code;
private String inviteToken;
} }
+39 -4
View File
@@ -56,6 +56,19 @@
<i class="menu-item-icon fas fa-chart-line"></i> <i class="menu-item-icon fas fa-chart-line"></i>
<span class="menu-item-text">站点统计</span> <span class="menu-item-text">站点统计</span>
</NuxtLink> </NuxtLink>
<NuxtLink
v-if="authState.loggedIn"
class="menu-item"
exact-active-class="selected"
to="/about/points"
@click="handleItemClick"
>
<i class="menu-item-icon fas fa-coins"></i>
<span class="menu-item-text">
积分商城
<span v-if="myPoint !== null" class="point-count">{{ myPoint }}</span>
</span>
</NuxtLink>
</div> </div>
<div class="menu-section"> <div class="menu-section">
@@ -130,7 +143,7 @@
<script setup> <script setup>
import { computed, onMounted, ref, watch } from 'vue' import { computed, onMounted, ref, watch } from 'vue'
import { authState } from '~/utils/auth' import { authState, fetchCurrentUser } from '~/utils/auth'
import { fetchUnreadCount, notificationState } from '~/utils/notification' import { fetchUnreadCount, notificationState } from '~/utils/notification'
import { useIsMobile } from '~/utils/screen' import { useIsMobile } from '~/utils/screen'
import { cycleTheme, ThemeMode, themeState } from '~/utils/theme' import { cycleTheme, ThemeMode, themeState } from '~/utils/theme'
@@ -147,6 +160,7 @@ const emit = defineEmits(['item-click'])
const categoryOpen = ref(true) const categoryOpen = ref(true)
const tagOpen = ref(true) const tagOpen = ref(true)
const myPoint = ref(null)
/** ✅ 用 useAsyncData 替换原生 fetch,避免 SSR+CSR 二次请求 */ /** ✅ 用 useAsyncData 替换原生 fetch,避免 SSR+CSR 二次请求 */
const { const {
@@ -191,6 +205,15 @@ const unreadCount = computed(() => notificationState.unreadCount)
const showUnreadCount = computed(() => (unreadCount.value > 99 ? '99+' : unreadCount.value)) const showUnreadCount = computed(() => (unreadCount.value > 99 ? '99+' : unreadCount.value))
const shouldShowStats = computed(() => authState.role === 'ADMIN') const shouldShowStats = computed(() => authState.role === 'ADMIN')
const loadPoint = async () => {
if (authState.loggedIn) {
const user = await fetchCurrentUser()
myPoint.value = user ? user.point : null
} else {
myPoint.value = null
}
}
const updateCount = async () => { const updateCount = async () => {
if (authState.loggedIn) { if (authState.loggedIn) {
await fetchUnreadCount() await fetchUnreadCount()
@@ -200,9 +223,15 @@ const updateCount = async () => {
} }
onMounted(async () => { onMounted(async () => {
await updateCount() await Promise.all([updateCount(), loadPoint()])
// 登录态变化时再拉一次未读数;与 useAsyncData 无关 // 登录态变化时再拉一次未读数和积分;与 useAsyncData 无关
watch(() => authState.loggedIn, updateCount) watch(
() => authState.loggedIn,
() => {
updateCount()
loadPoint()
},
)
}) })
const handleItemClick = () => { const handleItemClick = () => {
@@ -292,6 +321,12 @@ const gotoTag = (t) => {
font-weight: bold; font-weight: bold;
} }
.point-count {
margin-left: 4px;
font-size: 12px;
color: var(--primary-color);
}
.menu-item-icon { .menu-item-icon {
margin-right: 10px; margin-right: 10px;
opacity: 0.5; opacity: 0.5;
+29
View File
@@ -0,0 +1,29 @@
<template>
<div class="point-mall-page">
<p v-if="authState.loggedIn && point !== null">我的积分{{ point }}</p>
<p v-else>请先登录以查看积分</p>
</div>
</template>
<script setup>
import { onMounted, ref } from 'vue'
import { authState, fetchCurrentUser } from '~/utils/auth'
const point = ref(null)
onMounted(async () => {
if (authState.loggedIn) {
const user = await fetchCurrentUser()
point.value = user ? user.point : null
}
})
</script>
<style scoped>
.point-mall-page {
padding: 20px;
max-width: var(--page-max-width);
background-color: var(--background-color);
margin: 0 auto;
}
</style>
+3 -4
View File
@@ -9,12 +9,11 @@ import { discordExchange } from '~/utils/discord'
onMounted(async () => { onMounted(async () => {
const url = new URL(window.location.href) const url = new URL(window.location.href)
const code = url.searchParams.get('code') const code = url.searchParams.get('code')
const inviteToken = url.searchParams.get('state') const state = url.searchParams.get('state')
const result = await discordExchange(code, inviteToken, '') const result = await discordExchange(code, state, '')
if (result.needReason) { if (result.needReason) {
const q = inviteToken ? `&invite_token=${inviteToken}` : '' navigateTo(`/signup-reason?token=${result.token}`, { replace: true })
navigateTo(`/signup-reason?token=${result.token}${q}`, { replace: true })
} else { } else {
navigateTo('/', { replace: true }) navigateTo('/', { replace: true })
} }
+3 -4
View File
@@ -9,12 +9,11 @@ import { githubExchange } from '~/utils/github'
onMounted(async () => { onMounted(async () => {
const url = new URL(window.location.href) const url = new URL(window.location.href)
const code = url.searchParams.get('code') const code = url.searchParams.get('code')
const inviteToken = url.searchParams.get('state') const state = url.searchParams.get('state')
const result = await githubExchange(code, inviteToken, '') const result = await githubExchange(code, state, '')
if (result.needReason) { if (result.needReason) {
const q = inviteToken ? `&invite_token=${inviteToken}` : '' navigateTo(`/signup-reason?token=${result.token}`, { replace: true })
navigateTo(`/signup-reason?token=${result.token}${q}`, { replace: true })
} else { } else {
navigateTo('/', { replace: true }) navigateTo('/', { replace: true })
} }
+1 -4
View File
@@ -9,17 +9,14 @@ import { googleAuthWithToken } from '~/utils/google'
onMounted(async () => { onMounted(async () => {
const hash = new URLSearchParams(window.location.hash.substring(1)) const hash = new URLSearchParams(window.location.hash.substring(1))
const idToken = hash.get('id_token') const idToken = hash.get('id_token')
const inviteToken = hash.get('state')
if (idToken) { if (idToken) {
await googleAuthWithToken( await googleAuthWithToken(
idToken, idToken,
inviteToken,
() => { () => {
navigateTo('/', { replace: true }) navigateTo('/', { replace: true })
}, },
(token) => { (token) => {
const q = inviteToken ? `&invite_token=${inviteToken}` : '' navigateTo(`/signup-reason?token=${token}`, { replace: true })
navigateTo(`/signup-reason?token=${token}${q}`, { replace: true })
}, },
) )
} else { } else {
+1 -4
View File
@@ -35,7 +35,7 @@
</div> </div>
<div class="other-login-page-content"> <div class="other-login-page-content">
<div class="login-page-button" @click="loginWithGoogle"> <div class="login-page-button" @click="googleAuthorize">
<img class="login-page-button-icon" src="../assets/icons/google.svg" alt="Google Logo" /> <img class="login-page-button-icon" src="../assets/icons/google.svg" alt="Google Logo" />
<div class="login-page-button-text">Google 登录</div> <div class="login-page-button-text">Google 登录</div>
</div> </div>
@@ -115,9 +115,6 @@ const loginWithDiscord = () => {
const loginWithTwitter = () => { const loginWithTwitter = () => {
twitterAuthorize() twitterAuthorize()
} }
const loginWithGoogle = () => {
googleAuthorize()
}
</script> </script>
<style scoped> <style scoped>
+2 -6
View File
@@ -23,17 +23,14 @@ import BaseInput from '~/components/BaseInput.vue'
import { toast } from '~/main' import { toast } from '~/main'
const config = useRuntimeConfig() const config = useRuntimeConfig()
const API_BASE_URL = config.public.apiBaseUrl const API_BASE_URL = config.public.apiBaseUrl
const route = useRoute()
const reason = ref('') const reason = ref('')
const error = ref('') const error = ref('')
const isWaitingForRegister = ref(false) const isWaitingForRegister = ref(false)
const token = ref('') const token = ref('')
const inviteToken = ref('')
onMounted(async () => { onMounted(async () => {
token.value = route.query.token || '' token.value = route.query.token || ''
inviteToken.value = route.query.invite_token || ''
if (!token.value) { if (!token.value) {
await navigateTo({ path: '/signup' }, { replace: true }) await navigateTo({ path: '/signup' }, { replace: true })
} }
@@ -53,9 +50,8 @@ const submit = async () => {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
}, },
body: JSON.stringify({ body: JSON.stringify({
token: token.value, token: this.token,
reason: reason.value, reason: this.reason,
...(inviteToken.value ? { inviteToken: inviteToken.value } : {}),
}), }),
}) })
isWaitingForRegister.value = false isWaitingForRegister.value = false
+5 -14
View File
@@ -69,7 +69,7 @@
</div> </div>
<div class="other-signup-page-content"> <div class="other-signup-page-content">
<div class="signup-page-button" @click="signupWithGoogle"> <div class="signup-page-button" @click="googleAuthorize">
<img class="signup-page-button-icon" src="~/assets/icons/google.svg" alt="Google Logo" /> <img class="signup-page-button-icon" src="~/assets/icons/google.svg" alt="Google Logo" />
<div class="signup-page-button-text">Google 注册</div> <div class="signup-page-button-text">Google 注册</div>
</div> </div>
@@ -97,7 +97,6 @@ import { githubAuthorize } from '~/utils/github'
import { googleAuthorize } from '~/utils/google' import { googleAuthorize } from '~/utils/google'
import { twitterAuthorize } from '~/utils/twitter' import { twitterAuthorize } from '~/utils/twitter'
const config = useRuntimeConfig() const config = useRuntimeConfig()
const route = useRoute()
const API_BASE_URL = config.public.apiBaseUrl const API_BASE_URL = config.public.apiBaseUrl
const emailStep = ref(0) const emailStep = ref(0)
const email = ref('') const email = ref('')
@@ -110,11 +109,9 @@ const passwordError = ref('')
const code = ref('') const code = ref('')
const isWaitingForEmailSent = ref(false) const isWaitingForEmailSent = ref(false)
const isWaitingForEmailVerified = ref(false) const isWaitingForEmailVerified = ref(false)
const inviteToken = ref('')
onMounted(async () => { onMounted(async () => {
username.value = route.query.u || '' username.value = route.query.u || ''
inviteToken.value = route.query.invite_token || ''
try { try {
const res = await fetch(`${API_BASE_URL}/api/config`) const res = await fetch(`${API_BASE_URL}/api/config`)
if (res.ok) { if (res.ok) {
@@ -159,7 +156,6 @@ const sendVerification = async () => {
username: username.value, username: username.value,
email: email.value, email: email.value,
password: password.value, password: password.value,
...(inviteToken.value ? { inviteToken: inviteToken.value } : {}),
}), }),
}) })
isWaitingForEmailSent.value = false isWaitingForEmailSent.value = false
@@ -188,14 +184,12 @@ const verifyCode = async () => {
body: JSON.stringify({ body: JSON.stringify({
code: code.value, code: code.value,
username: username.value, username: username.value,
...(inviteToken.value ? { inviteToken: inviteToken.value } : {}),
}), }),
}) })
const data = await res.json() const data = await res.json()
if (res.ok) { if (res.ok) {
if (registerMode.value === 'WHITELIST') { if (registerMode.value === 'WHITELIST') {
const q = inviteToken.value ? `&invite_token=${inviteToken.value}` : '' navigateTo(`/signup-reason?token=${data.token}`, { replace: true })
navigateTo(`/signup-reason?token=${data.token}${q}`, { replace: true })
} else { } else {
toast.success('注册成功,请登录') toast.success('注册成功,请登录')
navigateTo('/login', { replace: true }) navigateTo('/login', { replace: true })
@@ -209,17 +203,14 @@ const verifyCode = async () => {
isWaitingForEmailVerified.value = false isWaitingForEmailVerified.value = false
} }
} }
const signupWithGoogle = () => {
googleAuthorize(inviteToken.value)
}
const signupWithGithub = () => { const signupWithGithub = () => {
githubAuthorize(inviteToken.value) githubAuthorize()
} }
const signupWithDiscord = () => { const signupWithDiscord = () => {
discordAuthorize(inviteToken.value) discordAuthorize()
} }
const signupWithTwitter = () => { const signupWithTwitter = () => {
twitterAuthorize(inviteToken.value) twitterAuthorize()
} }
</script> </script>
+3 -4
View File
@@ -9,12 +9,11 @@ import { twitterExchange } from '~/utils/twitter'
onMounted(async () => { onMounted(async () => {
const url = new URL(window.location.href) const url = new URL(window.location.href)
const code = url.searchParams.get('code') const code = url.searchParams.get('code')
const inviteToken = url.searchParams.get('state') const state = url.searchParams.get('state')
const result = await twitterExchange(code, inviteToken, '') const result = await twitterExchange(code, state, '')
if (result.needReason) { if (result.needReason) {
const q = inviteToken ? `&invite_token=${inviteToken}` : '' navigateTo(`/signup-reason?token=${result.token}`, { replace: true })
navigateTo(`/signup-reason?token=${result.token}${q}`, { replace: true })
} else { } else {
navigateTo('/', { replace: true }) navigateTo('/', { replace: true })
} }
+2 -2
View File
@@ -15,7 +15,7 @@ export function discordAuthorize(state = '') {
window.location.href = url window.location.href = url
} }
export async function discordExchange(code, inviteToken, reason) { export async function discordExchange(code, state, reason) {
try { try {
const config = useRuntimeConfig() const config = useRuntimeConfig()
const API_BASE_URL = config.public.apiBaseUrl const API_BASE_URL = config.public.apiBaseUrl
@@ -26,7 +26,7 @@ export async function discordExchange(code, inviteToken, reason) {
code, code,
redirectUri: `${window.location.origin}/discord-callback`, redirectUri: `${window.location.origin}/discord-callback`,
reason, reason,
inviteToken, state,
}), }),
}) })
const data = await res.json() const data = await res.json()
+2 -2
View File
@@ -15,7 +15,7 @@ export function githubAuthorize(state = '') {
window.location.href = url window.location.href = url
} }
export async function githubExchange(code, inviteToken, reason) { export async function githubExchange(code, state, reason) {
try { try {
const config = useRuntimeConfig() const config = useRuntimeConfig()
const API_BASE_URL = config.public.apiBaseUrl const API_BASE_URL = config.public.apiBaseUrl
@@ -26,7 +26,7 @@ export async function githubExchange(code, inviteToken, reason) {
code, code,
redirectUri: `${window.location.origin}/github-callback`, redirectUri: `${window.location.origin}/github-callback`,
reason, reason,
inviteToken, state,
}), }),
}) })
const data = await res.json() const data = await res.json()
+5 -10
View File
@@ -21,7 +21,7 @@ export async function googleGetIdToken() {
}) })
} }
export function googleAuthorize(state = '') { export function googleAuthorize() {
const config = useRuntimeConfig() const config = useRuntimeConfig()
const GOOGLE_CLIENT_ID = config.public.googleClientId const GOOGLE_CLIENT_ID = config.public.googleClientId
const WEBSITE_BASE_URL = config.public.websiteBaseUrl const WEBSITE_BASE_URL = config.public.websiteBaseUrl
@@ -31,23 +31,18 @@ export function googleAuthorize(state = '') {
} }
const redirectUri = `${WEBSITE_BASE_URL}/google-callback` const redirectUri = `${WEBSITE_BASE_URL}/google-callback`
const nonce = Math.random().toString(36).substring(2) const nonce = Math.random().toString(36).substring(2)
const url = `https://accounts.google.com/o/oauth2/v2/auth?client_id=${GOOGLE_CLIENT_ID}&redirect_uri=${encodeURIComponent(redirectUri)}&response_type=id_token&scope=openid%20email%20profile&nonce=${nonce}&state=${state}` const url = `https://accounts.google.com/o/oauth2/v2/auth?client_id=${GOOGLE_CLIENT_ID}&redirect_uri=${encodeURIComponent(redirectUri)}&response_type=id_token&scope=openid%20email%20profile&nonce=${nonce}`
window.location.href = url window.location.href = url
} }
export async function googleAuthWithToken( export async function googleAuthWithToken(idToken, redirect_success, redirect_not_approved) {
idToken,
inviteToken,
redirect_success,
redirect_not_approved,
) {
try { try {
const config = useRuntimeConfig() const config = useRuntimeConfig()
const API_BASE_URL = config.public.apiBaseUrl const API_BASE_URL = config.public.apiBaseUrl
const res = await fetch(`${API_BASE_URL}/api/auth/google`, { const res = await fetch(`${API_BASE_URL}/api/auth/google`, {
method: 'POST', method: 'POST',
headers: { 'Content-Type': 'application/json' }, headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ idToken, inviteToken }), body: JSON.stringify({ idToken }),
}) })
const data = await res.json() const data = await res.json()
if (res.ok && data.token) { if (res.ok && data.token) {
@@ -71,7 +66,7 @@ export async function googleAuthWithToken(
export async function googleSignIn(redirect_success, redirect_not_approved) { export async function googleSignIn(redirect_success, redirect_not_approved) {
try { try {
const token = await googleGetIdToken() const token = await googleGetIdToken()
await googleAuthWithToken(token, '', redirect_success, redirect_not_approved) await googleAuthWithToken(token, redirect_success, redirect_not_approved)
} catch { } catch {
/* ignore */ /* ignore */
} }
+2 -2
View File
@@ -42,7 +42,7 @@ export async function twitterAuthorize(state = '') {
window.location.href = url window.location.href = url
} }
export async function twitterExchange(code, inviteToken, reason) { export async function twitterExchange(code, state, reason) {
try { try {
const config = useRuntimeConfig() const config = useRuntimeConfig()
const API_BASE_URL = config.public.apiBaseUrl const API_BASE_URL = config.public.apiBaseUrl
@@ -55,7 +55,7 @@ export async function twitterExchange(code, inviteToken, reason) {
code, code,
redirectUri: `${window.location.origin}/twitter-callback`, redirectUri: `${window.location.origin}/twitter-callback`,
reason, reason,
inviteToken, state,
codeVerifier, codeVerifier,
}), }),
}) })