refactor: Refactor API imports and update API endpoints

This commit is contained in:
Hakadao
2024-04-11 17:24:01 +08:00
parent 1a3bafb284
commit d5b568c978
8 changed files with 24 additions and 17 deletions

View File

@@ -11,6 +11,7 @@ import { updateInterval } from './notify'
import { getUserID, isHomePage } from '~/utils/main'
import { settings } from '~/logic'
import emitter from '~/utils/mitt'
import API from '~/background/msg.define'
// import { useTopBarStore } from '~/stores/topBarStore'
@@ -224,7 +225,7 @@ async function getUserInfo() {
try {
const res = await browser.runtime
.sendMessage({
contentScriptQuery: 'getUserInfo',
contentScriptQuery: API.USER.GET_USER_INFO,
})
if (res.code === 0) {

View File

@@ -4,6 +4,7 @@ import { onMounted, reactive, ref, watch } from 'vue'
import type { FavoriteCategory, FavoriteResource } from '../types'
import { getUserID, isHomePage, removeHttpFromUrl, smoothScrollToTop } from '~/utils/main'
import { calcCurrentTime } from '~/utils/dataFormatter'
import API from '~/background/msg.define'
const favoriteCategories = reactive<Array<FavoriteCategory>>([])
const favoriteResources = reactive<Array<FavoriteResource>>([])
@@ -67,7 +68,7 @@ onMounted(async () => {
async function getFavoriteCategories() {
await browser.runtime
.sendMessage({
contentScriptQuery: 'getFavoriteCategories',
contentScriptQuery: API.FAVORITE.GET_FAVORITE_CATEGORIES,
up_mid: getUserID(),
})
.then((res) => {
@@ -86,7 +87,7 @@ function getFavoriteResources() {
isLoading.value = true
browser.runtime
.sendMessage({
contentScriptQuery: 'getFavoriteResources',
contentScriptQuery: API.FAVORITE.GET_FAVORITE_RESOURCES,
media_id: activatedMediaId.value,
pn: currentPageNum.value,
keyword: '',

View File

@@ -7,6 +7,7 @@ import type { HistoryItem } from '../types'
import { HistoryType } from '../types'
import { isHomePage, removeHttpFromUrl, smoothScrollToTop } from '~/utils/main'
import { calcCurrentTime } from '~/utils/dataFormatter'
import API from '~/background/msg.define'
const { t } = useI18n()
@@ -137,13 +138,13 @@ function getHistoryUrl(item: HistoryItem) {
/**
* Get history list
* @param {HistoryType} type
* @param {number} viewAt Last viewed timestamp
* @param {number} view_at Last viewed timestamp
*/
function getHistoryList(type: HistoryType, view_at = 0 as number) {
isLoading.value = true
browser.runtime
.sendMessage({
contentScriptQuery: 'getHistoryList',
contentScriptQuery: API.HISTORY.GET_HISTORY_LIST,
type,
view_at,
})

View File

@@ -7,6 +7,7 @@ import { MomentType } from '../types'
import type { MomentItem } from '../types'
import { getCSRF, getUserID, isHomePage, smoothScrollToTop } from '~/utils/main'
import { calcTimeSince } from '~/utils/dataFormatter'
import API from '~/background/msg.define'
const { t } = useI18n()
@@ -94,7 +95,7 @@ function getTopBarNewMoments(type_list: number[]) {
isLoading.value = true
browser.runtime
.sendMessage({
contentScriptQuery: 'getTopBarNewMoments',
contentScriptQuery: API.MOMENT.GET_TOP_BAR_NEW_MOMENTS,
uid: getUserID(),
type_list,
})
@@ -129,7 +130,7 @@ function getTopbarHistoryMoments(type_list: number[]) {
isLoading.value = true
browser.runtime
.sendMessage({
contentScriptQuery: 'getTopbarHistoryMoments',
contentScriptQuery: API.MOMENT.GET_TOP_BAR_HISTORY_MOMENTS,
uid: getUserID(),
type_list,
offset_dynamic_id: moments[moments.length - 1].dynamic_id_str,
@@ -155,7 +156,7 @@ function getTopbarLiveMoments(page: number) {
isLoading.value = true
browser.runtime
.sendMessage({
contentScriptQuery: 'getTopbarLiveMoments',
contentScriptQuery: API.MOMENT.GET_TOP_BAR_LIVE_MOMENTS,
page,
pagesize: 10,
})
@@ -248,7 +249,7 @@ function toggleWatchLater(aid: number) {
if (!isInWatchLater) {
browser.runtime.sendMessage({
contentScriptQuery: 'saveToWatchLater',
contentScriptQuery: API.WATCHLATER.SAVE_TO_WATCHLATER,
aid,
csrf: getCSRF(),
})
@@ -259,7 +260,7 @@ function toggleWatchLater(aid: number) {
}
else {
browser.runtime.sendMessage({
contentScriptQuery: 'removeFromWatchLater',
contentScriptQuery: API.WATCHLATER.REMOVE_FROM_WATCHLATER,
aid,
csrf: getCSRF(),
})

View File

@@ -1,6 +1,7 @@
<script setup lang="ts">
import { useI18n } from 'vue-i18n'
import { isHomePage } from '~/utils/main'
import API from '~/background/msg.define'
const { t } = useI18n()
@@ -40,7 +41,7 @@ onMounted(() => {
function getUnreadMessageCount() {
browser.runtime
.sendMessage({
contentScriptQuery: 'getUnreadMsg',
contentScriptQuery: API.NOTIFICATION.GET_UNREAD_MSG,
}).then((res) => {
if (res.code === 0) {
const resData = res.data
@@ -58,7 +59,7 @@ function getUnreadMessageCount() {
browser.runtime
.sendMessage({
contentScriptQuery: 'getUnreadDm',
contentScriptQuery: API.NOTIFICATION.GET_UNREAD_DM,
}).then((res) => {
if (res.code === 0) {
const resData = res.data

View File

@@ -4,6 +4,7 @@ import type { UserInfo, UserStat } from '../types'
import { revokeAccessKey } from '~/utils/authProvider'
import { getCSRF, getUserID, isHomePage } from '~/utils/main'
import { numFormatter } from '~/utils/dataFormatter'
import API from '~/background/msg.define'
defineProps<{
userInfo: UserInfo
@@ -31,7 +32,7 @@ const userStat = reactive<UserStat>({} as UserStat)
onMounted(() => {
browser.runtime
.sendMessage({
contentScriptQuery: 'getUserStat',
contentScriptQuery: API.USER.GET_USER_STAT,
})
.then((res) => {
if (res.code === 0)
@@ -42,7 +43,7 @@ onMounted(() => {
async function logout() {
revokeAccessKey()
browser.runtime.sendMessage({
contentScriptQuery: 'logout',
contentScriptQuery: API.AUTH.LOGOUT,
biliCSRF: getCSRF(),
}).then(() => {
location.reload()

View File

@@ -3,6 +3,7 @@ import { onMounted, reactive, ref } from 'vue'
import { calcCurrentTime } from '~/utils/dataFormatter'
import type { List as VideoItem, WatchLaterResult } from '~/models/video/watchLater'
import { isHomePage, removeHttpFromUrl } from '~/utils/main'
import API from '~/background/msg.define'
const watchLaterList = reactive<VideoItem[]>([])
const isLoading = ref<boolean>()
@@ -36,7 +37,7 @@ function getAllWatchLaterList() {
browser.runtime
.sendMessage({
contentScriptQuery: 'getAllWatchLaterList',
contentScriptQuery: API.WATCHLATER.GET_ALL_WATCHLATER_LIST,
})
.then((res: WatchLaterResult) => {
if (res.code === 0)

View File

@@ -267,7 +267,7 @@ function handleDislike() {
}
browser.runtime.sendMessage({
contentScriptQuery: 'dislikeVideo',
contentScriptQuery: API.VIDEO.DISLIKE_VIDEO,
...params,
sign: getTvSign(params),
})
@@ -297,7 +297,7 @@ function handleUndoDislike(video: AppVideoItem) {
}
browser.runtime.sendMessage({
contentScriptQuery: 'undoDislikeVideo',
contentScriptQuery: API.VIDEO.UNDO_DISLIKE_VIDEO,
...params,
sign: getTvSign(params),
}).then((res) => {