diff --git a/src/api/api-models.ts b/src/api/api-models.ts index 91dcaa5..eb9a2a6 100644 --- a/src/api/api-models.ts +++ b/src/api/api-models.ts @@ -103,6 +103,7 @@ export interface LotteryUserInfo { location?: string isVIP?: boolean card?: LotteryUserCardInfo + visiable: boolean } export interface LotteryUserCardInfo { name: string diff --git a/src/data/notifactions.ts b/src/data/notifactions.ts index 3c26a77..a074fd3 100644 --- a/src/data/notifactions.ts +++ b/src/data/notifactions.ts @@ -5,16 +5,14 @@ import { NotifactionInfo } from '@/api/api-models' import { useAccount } from '@/api/account' const account = useAccount() -const { data, loading, run } = useRequest( - () => { - return QueryGetAPI(NOTIFACTION_API_URL + 'get') - }, - { - errorRetryCount: 5, - pollingInterval: 5000, - pollingWhenHidden: false, - } -) +const { data, run } = useRequest(get, { + errorRetryCount: 5, + pollingInterval: 5000, + pollingWhenHidden: false, +}) +function get() { + return QueryGetAPI(NOTIFACTION_API_URL + 'get') +} export const notifactions = () => data export const GetNotifactions = () => { diff --git a/src/views/ManageLayout.vue b/src/views/ManageLayout.vue index a28779e..1752173 100644 --- a/src/views/ManageLayout.vue +++ b/src/views/ManageLayout.vue @@ -41,6 +41,20 @@ const menuOptions = [ key: 'manage-questionBox', icon: renderIcon(BookOutline), }, + { + label: () => + h( + RouterLink, + { + to: { + name: 'manage-lottery', + }, + }, + { default: () => '动态抽奖' } + ), + key: 'manage-lottery', + icon: renderIcon(BookOutline), + }, ] diff --git a/src/views/manage/LotteryView.vue b/src/views/manage/LotteryView.vue index d04c111..f4cf11a 100644 --- a/src/views/manage/LotteryView.vue +++ b/src/views/manage/LotteryView.vue @@ -2,8 +2,33 @@ import { LotteryUserInfo } from '@/api/api-models' import { QueryGetAPI } from '@/api/query' import { LOTTERY_API_URL, TURNSTILE_KEY } from '@/data/constants' -import { NButton, NTabPane, NTabs, useMessage } from 'naive-ui' -import { ref } from 'vue' +import { useLocalStorage } from '@vueuse/core' +import { randomInt } from 'crypto' +import { List } from 'linqts' +import { + NAvatar, + NButton, + NCard, + NCheckbox, + NCollapseTransition, + NCountdown, + NDivider, + NGrid, + NGridItem, + NInput, + NInputGroup, + NInputGroupLabel, + NInputNumber, + NRadioButton, + NRadioGroup, + NSpace, + NTabPane, + NTabs, + NTag, + useMessage, +} from 'naive-ui' +import { breadcrumbLight } from 'naive-ui/es/breadcrumb/styles' +import { computed, ref } from 'vue' import VueTurnstile from 'vue-turnstile' interface TempLotteryResponseModel { @@ -11,23 +36,91 @@ interface TempLotteryResponseModel { createTime: number total: number } +interface LotteryOption { + resultCount: number + lotteryType: 'single' | 'half' + needVIP: boolean +} + const message = useMessage() const token = ref() const turnstile = ref() +const defaultOption = { + resultCount: 1, + lotteryType: 'single', + needVIP: false, +} as LotteryOption +const lotteryOption = useLocalStorage('Settings.LotteryOption', defaultOption) + +const isLoading = ref(false) +const isLottering = ref(false) + +const inputDynamic = ref() +const inputDynamicId = computed(() => { + try { + var id = BigInt(inputDynamic.value ?? '') + return id + } catch { + try { + const url = new URL(inputDynamic.value ?? '') + if (url.host.endsWith('bilibili.com')) { + const sp = url.pathname.split('/') + const id = BigInt(sp.length > 1 ? sp[sp.length - 1] : sp[0]) + return id + } + } catch { + return null + } + } + return null +}) +const isCommentCountDown = ref(true) +const originCommentUsers = ref() +const originForwardUsers = ref() +const currentType = ref<'comment' | 'forward'>('comment') const commentUsers = ref() +const forwardUsers = ref() +const currentUsers = computed(() => { + switch (currentType.value) { + case 'comment': { + return commentUsers.value + } + case 'forward': { + return forwardUsers.value + } + } + return undefined +}) + +async function onGet() { + switch (currentType.value) { + case 'comment': { + await getCommentsUsers() + break + } + case 'forward': { + await getForwardUsers() + break + } + } + currentUsers.value?.users.forEach((u) => (u.visiable = true)) +} async function getCommentsUsers() { + isLoading.value = true await QueryGetAPI( LOTTERY_API_URL + 'comments', { - id: 803541974225256452n, + id: inputDynamicId.value, }, [['Turnstile', token.value]] ) .then((data) => { if (data.code == 200) { + originCommentUsers.value = data.data commentUsers.value = data.data + isCommentCountDown.value = false } else { message.error('获取用户失败: ' + data.message) } @@ -38,12 +131,135 @@ async function getCommentsUsers() { }) .finally(() => { turnstile.value?.reset() + isLoading.value = false }) } +async function getForwardUsers() { + isLoading.value = true + await QueryGetAPI( + LOTTERY_API_URL + 'forward', + { + id: inputDynamicId.value, + }, + [['Turnstile', token.value]] + ) + .then((data) => { + if (data.code == 200) { + originForwardUsers.value = data.data + forwardUsers.value = data.data + isCommentCountDown.value = false + } else { + message.error('获取用户失败: ' + data.message) + } + }) + .catch((err) => { + console.error(err) + message.error('获取失败') + }) + .finally(() => { + turnstile.value?.reset() + isLoading.value = false + }) +} +function getRandomInt(max: number) { + return Math.floor(Math.random() * max) +} +function startLottery() { + if (!isLottering.value && currentUsers.value) { + isLottering.value = true + try { + const data = currentUsers.value + const users = data.users.filter((u) => { + if (lotteryOption.value.needVIP) { + return u.isVIP == true + } + }) + switch (lotteryOption.value.lotteryType) { + case 'single': { + console.log('开始抽取单个用户') + const removed = [] as number[] + removeSingleUser() + function removeSingleUser() { + if (data.users.length > lotteryOption.value.resultCount) { + console.log(`[${data.users.length}] 移除` + data.users.splice(getRandomInt(data.users.length), 1)[0].name) + setTimeout(() => { + removeSingleUser() + }, 500) + } else { + isLottering.value = false + } + } + break + } + } + } catch (err) { + console.error(err) + message.error('发生错误') + } + } +} diff --git a/src/views/manage/QuestionBoxManageView.vue b/src/views/manage/QuestionBoxManageView.vue index ed075a8..67f1778 100644 --- a/src/views/manage/QuestionBoxManageView.vue +++ b/src/views/manage/QuestionBoxManageView.vue @@ -1,7 +1,7 @@