feat: 添加粉丝勋章点亮功能

This commit is contained in:
magicFeirl 2025-03-15 16:00:26 +08:00
parent 189f535840
commit f7e57f2dde
4 changed files with 128 additions and 0 deletions

View File

@ -0,0 +1,56 @@
<template>
<div class="container">
<p class="title">一键点亮粉丝勋章</p>
<TextBox placeholder="直播间ID" :text="roomid" @change="handleRoomIdChange" :changeOnBlur="true"></TextBox>
<AsyncButton @click="handleKeepAliveRequest">点亮!</AsyncButton>
</div>
</template>
<script setup lang="ts">
import { TextBox, AsyncButton } from '@/ui'
import { ref } from 'vue'
import { validateRoomId, getLiveRoomId, keepAliveRequest } from './utils'
import { Toast } from '@/core/toast'
const roomid = ref(getLiveRoomId())
const handleRoomIdChange = (value: string) => {
if (validateRoomId(value)) {
roomid.value = value
} else {
roomid.value = ""
}
}
const handleKeepAliveRequest = async () => {
if (!roomid.value) {
return
}
return keepAliveRequest(roomid.value).then(() => {
Toast.success('发送点亮勋章请求成功', '提示')
}).catch(msg => {
Toast.error('勋章点亮失败,原因: ' + msg, '提示')
})
}
</script>
<style lang="scss" scoped>
@import "common";
.container {
display: flex;
flex-flow: column;
box-shadow: 0 0 0 1px #8884;
order: -2;
border-radius: 4px;
padding: 6px 6px 6px 10px;
@include v-stretch(6px);
.title {
@include semi-bold();
}
}
</style>

View File

@ -0,0 +1 @@
在直播间页面的功能面板添加一键点亮粉丝勋章功能,仅适用于有粉丝勋章且正在直播的直播间。原理:发送一个 300 次点赞的请求点亮粉丝勋章。

View File

@ -0,0 +1,16 @@
import { defineComponentMetadata } from '@/components/define'
import { getUID } from '@/core/utils'
export const component = defineComponentMetadata({
name: 'badgeKeepalive',
displayName: '一键点亮直播间粉丝勋章',
entry: () => {},
reload: none,
unload: none,
tags: [componentsTags.live],
condition: () => Boolean(getUID()),
widget: {
component: () => import('./BadgeKeepalive.vue').then(m => m.default),
},
urlInclude: ['//live.bilibili.com'],
})

View File

@ -0,0 +1,55 @@
import { postTextWithCredentials, getJsonWithCredentials } from '@/core/ajax'
import { getCsrf, getUID } from '@/core/utils'
// 获取当前直播间号
export function getLiveRoomId(): string {
const matched = location.href.match(/live.bilibili.com\/(\d+)/)
return matched ? matched[1] : ''
}
export function validateRoomId(value: string): boolean {
return /^\d+$/.test(value)
}
function validateJSON(data: object) {
if (data['code'] !== 0) {
throw data['message']
}
return data['data']
}
export async function getLiveRoomUserInfo(room_id: string) {
const data = await getJsonWithCredentials(
`https://api.live.bilibili.com/xlive/web-room/v1/index/getInfoByUser?room_id=${room_id}`,
)
return validateJSON(data)
}
export async function keepAliveRequest(room_id: string, click_time = '300') {
// 需要先获取直播间房主的 UID
const data = await getLiveRoomUserInfo(room_id)
const curr_weared = data['medal']['curr_weared']
if (!curr_weared) {
throw `暂未获得直播间 ${room_id} 的粉丝勋章`
}
const anchor_id = curr_weared['target_id']
const params = {
click_time,
room_id,
anchor_id,
uid: getUID(),
csrf: getCsrf(),
}
const baseURL =
'https://api.live.bilibili.com/xlive/app-ucenter/v1/like_info_v3/like/likeReportV3'
return validateJSON(
JSON.parse(await postTextWithCredentials(baseURL, new URLSearchParams(params))),
)
}