fix auth page redirect

This commit is contained in:
2024-04-05 23:18:43 +08:00
parent 2a2895802f
commit 759a9f2c4e
6 changed files with 322 additions and 48 deletions

View File

@@ -49,6 +49,7 @@ export type ForumModel = {
createAt: number
isAdmin: boolean
isMember: boolean
}
export type ForumSectionModel = {
id: number

View File

@@ -3,7 +3,24 @@ import { AccountInfo } from '@/api/api-models'
import { QueryGetAPI, QueryPostAPI } from '@/api/query'
import { ACCOUNT_API_URL, TURNSTILE_KEY } from '@/data/constants'
import { useLocalStorage } from '@vueuse/core'
import { FormInst, FormItemInst, FormItemRule, FormRules, NAlert, NButton, NCard, NCountdown, NDivider, NForm, NFormItem, NInput, NSpace, NTabPane, NTabs, useMessage } from 'naive-ui'
import {
FormInst,
FormItemInst,
FormItemRule,
FormRules,
NAlert,
NButton,
NCard,
NCountdown,
NDivider,
NForm,
NFormItem,
NInput,
NSpace,
NTabPane,
NTabs,
useMessage,
} from 'naive-ui'
import { onUnmounted, ref } from 'vue'
import VueTurnstile from 'vue-turnstile'
@@ -87,7 +104,11 @@ const loginRules: FormRules = {
],
}
function validatePasswordStartWith(rule: FormItemRule, value: string): boolean {
return !!registerModel.value.password && registerModel.value.password.startsWith(value) && registerModel.value.password.length >= value.length
return (
!!registerModel.value.password &&
registerModel.value.password.startsWith(value) &&
registerModel.value.password.length >= value.length
)
}
function validatePasswordSame(rule: FormItemRule, value: string): boolean {
return value === registerModel.value.password
@@ -157,7 +178,9 @@ function onLoginButtonClick() {
}
async function onForgetPassword() {
canSendForgetPassword.value = false
await QueryGetAPI(ACCOUNT_API_URL + 'reset-password', { email: inputForgetPasswordValue.value }, [['Turnstile', token.value]])
await QueryGetAPI(ACCOUNT_API_URL + 'reset-password', { email: inputForgetPasswordValue.value }, [
['Turnstile', token.value],
])
.then(async (data) => {
if (data.code == 200) {
message.success('已发送密码重置链接到你的邮箱, 请检查')
@@ -193,17 +216,31 @@ onUnmounted(() => {
<NAlert type="warning"> 你已经登录 </NAlert>
</template>
<template v-else>
<NTabs v-model:value="selectedTab" size="large" animated pane-wrapper-style="margin: 0 -4px" pane-style="padding-left: 4px; padding-right: 4px; box-sizing: border-box;" style="min-width: 300px">
<NTabs
v-model:value="selectedTab"
size="large"
animated
pane-wrapper-style="margin: 0 -4px"
pane-style="padding-left: 4px; padding-right: 4px; box-sizing: border-box;"
style="min-width: 300px"
>
<NTabPane name="login" tab="登陆">
<NForm ref="formRef" :rules="loginRules" :model="loginModel">
<NFormItem path="account" label="用户名或邮箱">
<NInput v-model:value="loginModel.account" />
</NFormItem>
<NFormItem path="password" label="密码">
<NInput v-model:value="loginModel.password" type="password" @input="onPasswordInput" @keydown.enter="onLoginButtonClick" />
<NInput
v-model:value="loginModel.password"
type="password"
@input="onPasswordInput"
@keydown.enter="onLoginButtonClick"
/>
</NFormItem>
</NForm>
<NButton text secondary style="margin-left: 5px; color: gray" @click="onForgetPasswordClick"> 忘记密码 </NButton>
<NButton text secondary style="margin-left: 5px; color: gray" @click="onForgetPasswordClick">
忘记密码
</NButton>
<NSpace vertical justify="center" align="center">
<NButton :loading="isLoading" type="primary" size="large" @click="onLoginButtonClick"> 登陆 </NButton>
</NSpace>
@@ -211,27 +248,43 @@ onUnmounted(() => {
<NTabPane name="register" tab="注册">
<NForm ref="formRef" :rules="registerRules" :model="registerModel">
<NFormItem path="username" label="用户名">
<NInput v-model:value="registerModel.username" />
<NInput v-model:value="registerModel.username" placeholder="输入一个用户名, 不允许纯数字" />
</NFormItem>
<NFormItem path="email" label="邮箱">
<NInput v-model:value="registerModel.email" />
<NInput v-model:value="registerModel.email" placeholder="就是邮箱, 没收到的话请检查垃圾箱" />
</NFormItem>
<NFormItem path="password" label="密码">
<NInput v-model:value="registerModel.password" type="password" @input="onPasswordInput" @keydown.enter.prevent />
<NInput
v-model:value="registerModel.password"
type="password"
@input="onPasswordInput"
@keydown.enter.prevent
placeholder="输入密码, 需要包含英文和数字"
/>
</NFormItem>
<NFormItem ref="rPasswordFormItemRef" first path="reenteredPassword" label="重复密码">
<NInput v-model:value="registerModel.reenteredPassword" :disabled="!registerModel.password" type="password" @keydown.enter="onRegisterButtonClick" />
<NInput
v-model:value="registerModel.reenteredPassword"
:disabled="!registerModel.password"
type="password"
@keydown.enter="onRegisterButtonClick"
placeholder="再次输入密码"
/>
</NFormItem>
</NForm>
<NSpace vertical justify="center" align="center">
<NButton :loading="!token || isLoading" type="primary" size="large" @click="onRegisterButtonClick"> 注册 </NButton>
<NButton :loading="!token || isLoading" type="primary" size="large" @click="onRegisterButtonClick">
注册
</NButton>
</NSpace>
</NTabPane>
<NTabPane v-if="isForgetPassword" name="forget" tab="忘记密码">
<NInput placeholder="请输入邮箱" v-model:value="inputForgetPasswordValue" maxlength="64" />
<NDivider />
<NSpace vertical justify="center" align="center">
<NButton :loading="!token || !canSendForgetPassword" type="primary" size="large" @click="onForgetPassword"> 提交 </NButton>
<NButton :loading="!token || !canSendForgetPassword" type="primary" size="large" @click="onForgetPassword">
提交
</NButton>
<NCountdown v-if="!canSendForgetPassword" :duration="60000" @finish="canSendForgetPassword = true" />
</NSpace>
</NTabPane>

View File

@@ -0,0 +1,64 @@
<script setup lang="ts">
import { UserBasicInfo } from '@/api/api-models'
import { QueryGetAPI } from '@/api/query'
import { USER_API_URL } from '@/data/constants'
import { getUserAvatarUrl } from '@/Utils'
import { useDebounceFn } from '@vueuse/core'
import { NAvatar, NCard, NDescriptions, NDescriptionsItem, NDivider, NEmpty, NFlex, NSpin, NTag, NText } from 'naive-ui'
import { onMounted, ref, watch } from 'vue'
const props = defineProps<{
user: string | number
}>()
const userInfo = defineModel<UserBasicInfo | undefined>('userInfo')
const currentUser = ref<UserBasicInfo>({} as UserBasicInfo)
const isLoading = ref(false)
const deBounce = useDebounceFn((newValue: string | number) => {
getUserInfo(newValue)
}, 666)
watch(
() => props.user,
(newValue) => {
deBounce(newValue)
},
)
async function getUserInfo(user: string | number) {
try {
if (!props.user) {
currentUser.value = {} as UserBasicInfo
return
}
isLoading.value = true
const data = await QueryGetAPI<UserBasicInfo>(USER_API_URL + 'basic/' + user)
if (data.code == 200) {
currentUser.value = data.data
} else {
currentUser.value = {} as UserBasicInfo
}
} finally {
isLoading.value = false
userInfo.value = currentUser.value
}
}
onMounted(() => {
getUserInfo(props.user)
})
</script>
<template>
<NSpin :show="isLoading">
<NCard size="small">
<NEmpty v-if="!currentUser.id" :description="user ? (isLoading ? '加载中' : '未找到用户') : '未选择用户'" />
<NFlex v-else align="center">
<NAvatar round :src="getUserAvatarUrl(currentUser.id)" :img-props="{ referrerpolicy: 'no-referrer' }" />
<NDivider vertical />
<NText>
{{ currentUser.name }}
</NText>
</NFlex>
</NCard>
</NSpin>
</template>

View File

@@ -1,6 +1,6 @@
<script setup lang="ts">
import { useAccount } from '@/api/account'
import { QueryPostAPI } from '@/api/query'
import { QueryGetAPI, QueryPostAPI } from '@/api/query'
import { useForumStore } from '@/store/useForumStore'
import {
DataTableColumns,
@@ -19,6 +19,7 @@ import {
NModal,
NSelect,
NSpin,
NSwitch,
NTabPane,
NTabs,
NTag,
@@ -26,11 +27,12 @@ import {
useMessage,
} from 'naive-ui'
import { h, ref } from 'vue'
import { ForumModel, ForumUserModel } from '@/api/models/forum'
import { ForumModel, ForumUserLevels, ForumUserModel } from '@/api/models/forum'
import { FORUM_API_URL } from '@/data/constants'
// @ts-ignore
import Agreement from '@/document/EnableForumAgreement.md'
import { UserBasicInfo } from '@/api/api-models'
import UserBasicInfoCard from '@/components/UserBasicInfoCard.vue'
const useForum = useForumStore()
const accountInfo = useAccount()
@@ -46,11 +48,38 @@ const create_Name = ref('')
const create_Description = ref('')
const showAddAdminModal = ref(false)
const inputUser = ref<UserBasicInfo>({} as UserBasicInfo)
const addAdminName = ref()
const currentAdminInfo = ref<UserBasicInfo>()
const showBanModal = ref(false)
const addBanName = ref()
const currentBanUserInfo = ref<UserBasicInfo>()
const paginationSetting = { defaultPageSize: 20, showSizePicker: true, pageSizes: [20, 50, 100] }
const levels = [
{
label: '1. 所有人',
value: ForumUserLevels.Guest,
},
{
label: '2. 已注册用户',
value: ForumUserLevels.User,
},
{
label: '3. 已加入的用户',
value: ForumUserLevels.Member,
},
{
label: '4. 加入并绑定B站的用户',
value: ForumUserLevels.AuthedMember,
},
{
label: '5. 仅管理员',
value: ForumUserLevels.Admin,
},
]
async function createForum() {
if (!readedAgreement.value) {
message.warning('请先阅读并同意服务协议')
@@ -79,6 +108,9 @@ async function createForum() {
async function SwitchForum(owner: number) {
currentForum.value = (await useForum.GetForumInfo(owner)) ?? ({} as ForumModel)
}
async function refreshForumInfo() {
currentForum.value = (await useForum.GetForumInfo(currentForum.value.owner.id)) ?? ({} as ForumModel)
}
const defaultColumns: DataTableColumns<ForumUserModel> = [
{
title: '名称',
@@ -125,13 +157,11 @@ const memberColumns: DataTableColumns<ForumUserModel> = [
{
text: true,
type: 'success',
onClick: () =>
useForum.ConfirmApply(currentForum.value.owner.id, row.id).then((success) => {
if (success) message.success('操作成功')
currentForum.value.applying = currentForum.value.applying.filter((u) => u.id != row.id)
}),
onClick: () => {
banUser(row.id)
},
},
{ default: () => '通过申请' },
{ default: () => '封禁' },
)
},
},
@@ -147,13 +177,9 @@ const banColumns: DataTableColumns<ForumUserModel> = [
{
text: true,
type: 'success',
onClick: () =>
useForum.ConfirmApply(currentForum.value.owner.id, row.id).then((success) => {
if (success) message.success('操作成功')
currentForum.value.applying = currentForum.value.applying.filter((u) => u.id != row.id)
}),
onClick: () => unbanUser(row.id),
},
{ default: () => '通过申请' },
{ default: () => '解禁' },
)
},
},
@@ -169,28 +195,28 @@ const adminColumns: DataTableColumns<ForumUserModel> = [
NButton,
{
text: true,
type: 'success',
onClick: () =>
useForum.ConfirmApply(currentForum.value.owner.id, row.id).then((success) => {
if (success) message.success('操作成功')
currentForum.value.applying = currentForum.value.applying.filter((u) => u.id != row.id)
}),
type: 'error',
onClick: () => {
removeAdmin(row.id)
},
},
{ default: () => '通过申请' },
{ default: () => ' 取消管理员' },
)
},
},
]
async function addAdmin() {
if (!currentForum.value.id) return
try {
const data = await QueryPostAPI<ForumModel>(FORUM_API_URL + 'manage/add-admin', {
forum: currentForum.value.id,
user: addAdminName.value,
const data = await QueryGetAPI<ForumModel>(FORUM_API_URL + 'manage/add-admin', {
forum: currentForum.value.owner.id,
id: currentAdminInfo.value?.id,
})
if (data.code == 200) {
message.success('操作成功')
message.success('已添加 ' + currentAdminInfo.value?.name + ' 为管理员')
refreshForumInfo()
addAdminName.value = ''
showAddAdminModal.value = false
} else {
message.error('操作失败: ' + data.message)
}
@@ -198,6 +224,65 @@ async function addAdmin() {
message.error('操作失败: ' + err)
}
}
async function removeAdmin(id: number) {
try {
const data = await QueryGetAPI<ForumModel>(FORUM_API_URL + 'manage/del-admin', {
forum: currentForum.value.owner.id,
id,
})
if (data.code == 200) {
message.success('已取消管理员权限')
refreshForumInfo()
} else {
message.error('操作失败: ' + data.message)
}
} catch (err) {
message.error('操作失败: ' + err)
}
}
async function banUser(id: number) {
try {
const data = await QueryGetAPI<ForumModel>(FORUM_API_URL + 'manage/ban', {
forum: currentForum.value.owner.id,
id: currentBanUserInfo.value?.id,
})
if (data.code == 200) {
message.success('已封禁用户')
refreshForumInfo()
} else {
message.error('操作失败: ' + data.message)
}
} catch (err) {
message.error('操作失败: ' + err)
}
}
async function unbanUser(id: number) {
try {
const data = await QueryGetAPI<ForumModel>(FORUM_API_URL + 'manage/unban', {
forum: currentForum.value.owner.id,
id,
})
if (data.code == 200) {
message.success('已解禁')
refreshForumInfo()
} else {
message.error('操作失败: ' + data.message)
}
} catch (err) {
message.error('操作失败: ' + err)
}
}
async function updateForumSettings() {
try {
const data = await QueryPostAPI(FORUM_API_URL + 'manage/update-setting', currentForum.value.settings)
if (data.code == 200) {
} else {
message.error('修改失败: ' + data.message)
}
} catch (err) {
message.error('修改失败: ' + err)
}
}
</script>
<template>
@@ -256,14 +341,52 @@ async function addAdmin() {
<NDescriptionsItem label="管理员数量"> {{ currentForum.admins?.length ?? 0 }} </NDescriptionsItem>
</NDescriptions>
<NDivider> 设置 </NDivider>
<NDescriptions class="setting" label-placement="left">
<NDescriptionsItem label="加入需要进行管理员同意">
<NCheckbox v-model:checked="currentForum.settings.requireApply" @update:checked="updateForumSettings" />
</NDescriptionsItem>
<NDescriptionsItem v-if="currentForum.settings.requireApply" label="仅允许已绑定账号的用户申请">
<NCheckbox
v-model:checked="currentForum.settings.requireAuthedToJoin"
@update:checked="updateForumSettings"
/>
</NDescriptionsItem>
<NDescriptionsItem label="允许发布帖子">
<NCheckbox v-model:checked="currentForum.settings.allowPost" @update:checked="updateForumSettings" />
</NDescriptionsItem>
<NDescriptionsItem label="允许谁查看讨论区">
<NSelect
v-model:value="currentForum.settings.allowedViewerLevel"
:options="levels"
@update:value="updateForumSettings"
style="min-width: 200px"
/>
</NDescriptionsItem>
<NDescriptionsItem label="允许谁发布帖子">
<NSelect
v-model:value="currentForum.settings.allowedPostLevel"
:options="levels"
@update:value="updateForumSettings"
style="min-width: 200px"
/>
</NDescriptionsItem>
</NDescriptions>
</NTabPane>
<NTabPane tab="成员" name="member">
<NDivider> 申请 </NDivider>
<NDivider> 申请加入 </NDivider>
<NDataTable :columns="applyingColumns" :data="currentForum.applying" :pagination="paginationSetting" />
<NDivider> 管理员 </NDivider>
<NFlex>
<NButton @click="showAddAdminModal = true" size="small" type="primary"> 添加管理员 </NButton>
<NButton
@click="showAddAdminModal = true"
size="small"
type="primary"
:disabled="currentForum.owner.id != accountInfo.id"
>
添加管理员
</NButton>
</NFlex>
<br />
<NDataTable :columns="adminColumns" :data="currentForum.admins" :pagination="paginationSetting" />
<template v-if="currentForum.settings.requireApply">
<NDivider> 成员 </NDivider>
@@ -274,6 +397,10 @@ async function addAdmin() {
/>
</template>
<NDivider> 封禁用户 </NDivider>
<NFlex>
<NButton @click="showBanModal = true" size="small" type="primary"> 封禁用户 </NButton>
</NFlex>
<br />
<NDataTable :columns="banColumns" :data="currentForum.blackList" :pagination="paginationSetting" />
</NTabPane>
</NTabs>
@@ -288,7 +415,27 @@ async function addAdmin() {
<Agreement />
</NModal>
<NModal v-model:show="showAddAdminModal" preset="card" title="添加管理员" style="width: 600px; max-width: 90vw">
<UserBasicInfoCard :user="addAdminName" @update:user-info="(v) => (currentAdminInfo = v)" />
<br />
<NInput v-model:value="addAdminName" placeholder="请输入用户名或VTsuruId" />
<NButton @click="addAdmin" type="primary"> 添加 </NButton>
<NDivider />
<NButton @click="addAdmin" type="primary" :disabled="!currentAdminInfo?.id"> 添加 </NButton>
</NModal>
<NModal v-model:show="showBanModal" preset="card" title="封禁用户" style="width: 600px; max-width: 90vw">
<UserBasicInfoCard :user="addBanName" @update:user-info="(v) => (currentBanUserInfo = v)" />
<br />
<NInput v-model:value="addBanName" placeholder="请输入用户名或VTsuruId" />
<NDivider />
<NButton @click="banUser(currentBanUserInfo!.id)" type="warning" :disabled="!currentBanUserInfo?.id">
封禁
</NButton>
</NModal>
</template>
<style>
.setting .n-descriptions-table-content {
display: flex !important;
align-items: center !important;
}
</style>

View File

@@ -160,11 +160,11 @@ const renderOption = ({ node, option }: { node: any; option: SelectOption }) =>
)
}
function gotoAuthPage() {
if (!accountInfo.value?.biliUserAuthInfo) {
/*if (!accountInfo.value?.biliUserAuthInfo) {
message.error('你尚未进行 Bilibili 认证, 请前往面板进行认证和绑定')
return
}
/*useAuthStore()
useAuthStore()
.setCurrentAuth(accountInfo.value?.biliUserAuthInfo.token)
.then(() => {
NavigateToNewTab('/bili-user')

View File

@@ -24,12 +24,14 @@ import { onMounted, onUnmounted, ref } from 'vue'
import VueTurnstile from 'vue-turnstile'
import ForumPreviewItem from './ForumPreviewItem.vue'
import ForumCommentItem from './ForumCommentItem.vue'
import { useAccount } from '@/api/account'
const { biliInfo, userInfo } = defineProps<{
// eslint-disable-next-line @typescript-eslint/no-explicit-any
biliInfo: any | undefined
userInfo: UserInfo | undefined
}>()
const accountInfo = useAccount()
const token = ref('')
const turnstile = ref()
const editor = ref()
@@ -107,11 +109,15 @@ onUnmounted(() => {
forumInfo.settings.allowedViewerLevel > forumInfo.level
"
>
<NAlert type="warning"> 你需要成为成员才能访问 </NAlert>
<NAlert type="warning"> 你需要成为成员才能访问 {{ forumInfo.name }} </NAlert>
<br />
<NAlert v-if="forumInfo.isApplied" type="success"> 已申请, 正在等待管理员审核 </NAlert>
<NCard v-else title="加入">
加入 {{ forumInfo.name }}
<NButton type="primary" @click="ApplyToForum" :loading="useForum.isLoading">
<NCard v-else title="加入该讨论区">
<NAlert v-if="!accountInfo.id" type="error"> 需要登录后才能够加入 </NAlert>
<NAlert v-else-if="forumInfo.settings.requireApply" type="warning"> 申请需要审核 </NAlert>
<NAlert v-else type="success"> 该讨论区可直接加入 </NAlert>
<NDivider />
<NButton type="primary" @click="ApplyToForum" :loading="useForum.isLoading" :disabled="!accountInfo.id">
{{ forumInfo.settings.requireApply ? '申请' : '' }}加入
</NButton>
</NCard>
@@ -129,6 +135,9 @@ onUnmounted(() => {
<NCard style="max-width: 300px">
<NFlex vertical>
<NButton @click="showPostTopicModal = true"> 发布话题 </NButton>
<NCard v-if="forumInfo.isAdmin" size="small" title="管理员">
</NCard>
</NFlex>
</NCard>
<NList bordered style="flex: 1" size="small" hoverable clickable>