收藏可用
This commit is contained in:
+74
-9
@@ -14,6 +14,8 @@ class BilibiliClient {
|
||||
private dedeUserID: string | null = null;
|
||||
private sid: string | null = null;
|
||||
private accountInfo: any | null = null;
|
||||
private buvid3: any | null = null;
|
||||
private buvid4: any | null = null;
|
||||
|
||||
// 获取请求头
|
||||
private getHeaders(): { [key: string]: string } {
|
||||
@@ -30,7 +32,7 @@ class BilibiliClient {
|
||||
|
||||
// 构建Cookie字符串
|
||||
private getCookieString(): string {
|
||||
return `SESSDATA=${this.sessData}; bili_jct=${this.biliJct}; DedeUserID=${this.dedeUserID}; sid=${this.sid}`;
|
||||
return `SESSDATA=${this.sessData}; bili_jct=${this.biliJct}; DedeUserID=${this.dedeUserID}; sid=${this.sid}; buvid3=${this.buvid3}; buvid4=${this.buvid4}`;
|
||||
}
|
||||
|
||||
// 发送GET请求的函数
|
||||
@@ -41,10 +43,29 @@ class BilibiliClient {
|
||||
responseType: 'json',
|
||||
header: this.getHeaders()
|
||||
});
|
||||
//console.log(response.data);
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
console.error(`请求失败,错误码 = ${error.code}`);
|
||||
console.error(`GET请求失败,错误码 = ${error.code}`);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
// 发送POST请求的函数
|
||||
private async postRequest(url: string, data: string, content_type: string): Promise<any> {
|
||||
var headers = this.getHeaders();
|
||||
headers["Content-Type"] = content_type
|
||||
|
||||
try {
|
||||
const response = await fetch.fetch({
|
||||
url: url,
|
||||
responseType: 'json',
|
||||
method: 'POST',
|
||||
data: data,
|
||||
header: headers
|
||||
});
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
console.error(`POST请求失败,错误码 = ${error.code}`);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
@@ -70,22 +91,33 @@ class BilibiliClient {
|
||||
async updateAccountInfo(): Promise<boolean> {
|
||||
const accoutInfo = await this.getRequest("https://api.bilibili.com/x/web-interface/nav");
|
||||
this.accountInfo = accoutInfo.data.data;
|
||||
if(this.accountInfo){
|
||||
if (this.accountInfo) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// 刷新BUVID
|
||||
async updateBUVID() {
|
||||
const url = "https://api.bilibili.com/x/frontend/finger/spi";
|
||||
const response = await this.getRequest(url);
|
||||
|
||||
this.buvid3 = response.data.data.b_3
|
||||
this.buvid4 = response.data.data.b_4
|
||||
}
|
||||
|
||||
// 登录函数,使用本地存储的账号数据或通过二维码登录
|
||||
async login(send_req: boolean, interval: NodeJS.Timeout | null = null): Promise<{ success: boolean, message: string }> {
|
||||
const accountData = await this.getStoredAccountData();
|
||||
if (accountData) {
|
||||
console.log("Loaded AccountData: ", accountData)
|
||||
this.sessData = accountData.sessData;
|
||||
this.biliJct = accountData.biliJct;
|
||||
this.dedeUserID = accountData.dedeUserID;
|
||||
this.sid = accountData.sid;
|
||||
console.log('使用存储的账号数据登录成功');
|
||||
await this.updateAccountInfo()
|
||||
await this.updateBUVID()
|
||||
return {
|
||||
success: true,
|
||||
message: "登录成功"
|
||||
@@ -120,6 +152,7 @@ class BilibiliClient {
|
||||
await this.storeAccountData();
|
||||
console.log('使用二维码登录并存储账号数据成功');
|
||||
await this.updateAccountInfo()
|
||||
await this.updateBUVID()
|
||||
return {
|
||||
success: true,
|
||||
message: "登录成功"
|
||||
@@ -150,9 +183,9 @@ class BilibiliClient {
|
||||
}
|
||||
|
||||
// 获取目标用户创建的所有收藏夹的基本信息
|
||||
async getUserFavouriteFolders(mid: string, type: number = 0, rid: string = null): Promise<any>{
|
||||
async getUserFavouriteFolders(mid: string, type: number = 0, rid: string = null): Promise<any> {
|
||||
let url = `https://api.bilibili.com/x/v3/fav/folder/created/list-all?up_mid=${mid}&type=${type}`;
|
||||
if(rid){
|
||||
if (rid) {
|
||||
url += `&rid=${rid}`;
|
||||
}
|
||||
const response = await this.getRequest(url);
|
||||
@@ -161,7 +194,7 @@ class BilibiliClient {
|
||||
}
|
||||
|
||||
// 获取目标收藏夹元数据
|
||||
async getFavouriteFolderMetadata(mlid: string): Promise<any>{
|
||||
async getFavouriteFolderMetadata(mlid: string): Promise<any> {
|
||||
const url = `https://api.bilibili.com/x/v3/fav/folder/info?media_id=${mlid}`;
|
||||
const response = await this.getRequest(url);
|
||||
|
||||
@@ -169,9 +202,9 @@ class BilibiliClient {
|
||||
}
|
||||
|
||||
// 获取目标收藏夹内容
|
||||
async getFavouriteFolderContent(mlid: string, pn: number, ps: number = 10, keyword: string = null): Promise<any>{
|
||||
async getFavouriteFolderContent(mlid: string, pn: number, ps: number = 10, keyword: string = null): Promise<any> {
|
||||
let url = `https://api.bilibili.com/x/v3/fav/resource/list?media_id=${mlid}&ps=${ps}&pn=${pn}`;
|
||||
if(keyword){
|
||||
if (keyword) {
|
||||
url += `&keyword=${keyword}`
|
||||
}
|
||||
const response = await this.getRequest(url);
|
||||
@@ -179,6 +212,38 @@ class BilibiliClient {
|
||||
return response.data.data
|
||||
}
|
||||
|
||||
async isVideoStaredByBVID(bvid: string): Promise<boolean> {
|
||||
const url = `https://api.bilibili.com/x/v2/fav/video/favoured?aid=${bvid}`
|
||||
const response = await this.getRequest(url);
|
||||
|
||||
return response.data.data.favoured
|
||||
}
|
||||
|
||||
async starVideoToDefaultFavFolderByBVID(bvid: string): Promise<any> {
|
||||
let defaultFolderID = 0
|
||||
const folders = await this.getUserFavouriteFolders(this.accountInfo.mid)
|
||||
folders.list.forEach(folder => {
|
||||
if(folder.title == "默认收藏夹"){
|
||||
defaultFolderID = folder.id
|
||||
}
|
||||
});
|
||||
if(defaultFolderID == 0){
|
||||
return false
|
||||
}
|
||||
this.getVideoInfoByBVID(bvid).then(async (videoInfo) => {
|
||||
const aid = videoInfo.aid;
|
||||
const url = `https://api.bilibili.com/x/v3/fav/resource/deal`
|
||||
const data = `rid=${aid}&csrf=${this.biliJct}&type=2&add_media_ids=${defaultFolderID}&del_media_ids=`
|
||||
const response = await this.postRequest(url, data, "application/x-www-form-urlencoded");
|
||||
|
||||
return response.data.code
|
||||
}).catch(error => {
|
||||
console.log("catched error: " + error)
|
||||
return false
|
||||
});
|
||||
return false
|
||||
}
|
||||
|
||||
// 退出登录(指在手表上删除存储的账号数据)
|
||||
logOut(): any {
|
||||
storage.delete({
|
||||
|
||||
@@ -21,15 +21,15 @@
|
||||
</div>
|
||||
<div class="vidtoolbar" style="margin-top: 25px">
|
||||
<div class="vidtool" style="margin-left: 0px">
|
||||
<image class="vidtoolimg" src={{GetToolBarIconSrc("like")}}></image>
|
||||
<image class="vidtoolimg" src={{likesrc}}></image>
|
||||
<text class="vidtooltext">{{this.$app.$def.tools.formatNumber(vid.stat.like)}}</text>
|
||||
</div>
|
||||
<div class="vidtool">
|
||||
<image class="vidtoolimg" src={{GetToolBarIconSrc("coin")}}></image>
|
||||
<image class="vidtoolimg" src={{coinsrc}}></image>
|
||||
<text class="vidtooltext">{{this.$app.$def.tools.formatNumber(vid.stat.coin)}}</text>
|
||||
</div>
|
||||
<div class="vidtool">
|
||||
<image class="vidtoolimg" src={{GetToolBarIconSrc("star")}}></image>
|
||||
<div class="vidtool" @click="AddToFav()">
|
||||
<image class="vidtoolimg" src={{starsrc}}></image>
|
||||
<text class="vidtooltext">{{this.$app.$def.tools.formatNumber(vid.stat.favorite)}}</text>
|
||||
</div>
|
||||
<div class="vidtool">
|
||||
@@ -47,6 +47,7 @@
|
||||
|
||||
<script>
|
||||
import router from "@system.router"
|
||||
import prompt from "@system.prompt"
|
||||
export default {
|
||||
public: {
|
||||
bvid: 1 // 视频AID
|
||||
@@ -55,30 +56,47 @@ export default {
|
||||
vid: {},
|
||||
liked: false,
|
||||
coined: false,
|
||||
stared: false
|
||||
stared: false,
|
||||
likesrc: "",
|
||||
coinsrc: "",
|
||||
starsrc: ""
|
||||
},
|
||||
RouterBack() {
|
||||
router.back()
|
||||
},
|
||||
GetToolBarIconSrc(imgName){
|
||||
switch(imgName){
|
||||
case "like":
|
||||
if(this.liked){
|
||||
return "/common/vidtool_liked.png"
|
||||
}
|
||||
return "/common/vidtool_like.png"
|
||||
case "coin":
|
||||
if(this.coined){
|
||||
return "/common/vidtool_coined.png"
|
||||
}
|
||||
return "/common/vidtool_coin.png"
|
||||
case "star":
|
||||
if(this.stared){
|
||||
return "/common/vidtool_stared.png"
|
||||
}
|
||||
return "/common/vidtool_star.png"
|
||||
default:
|
||||
return ""
|
||||
async AddToFav(){
|
||||
var result = await this.$app.$def.biliclient.starVideoToDefaultFavFolderByBVID(this.bvid)
|
||||
console.log(result)
|
||||
if(!result){
|
||||
prompt.showToast({
|
||||
message: "收藏成功"
|
||||
})
|
||||
this.UpdateVideoToolbarStatus()
|
||||
}
|
||||
else{
|
||||
prompt.showToast({
|
||||
message: "收藏失败,请重试,或重新登录后再试"
|
||||
})
|
||||
}
|
||||
this.UpdateVideoToolbarStatus()
|
||||
},
|
||||
async UpdateVideoToolbarStatus(){
|
||||
this.stared = await this.$app.$def.biliclient.isVideoStaredByBVID(this.bvid)
|
||||
|
||||
this.UpdateToolBarIconSrc()
|
||||
},
|
||||
UpdateToolBarIconSrc(){
|
||||
this.likesrc = "/common/vidtool_like.png"
|
||||
if(this.liked){
|
||||
this.likesrc = "/common/vidtool_liked.png"
|
||||
}
|
||||
this.coinsrc = "/common/vidtool_coin.png"
|
||||
if(this.coined){
|
||||
this.coinsrc = "/common/vidtool_coined.png"
|
||||
}
|
||||
this.starsrc = "/common/vidtool_star.png"
|
||||
if(this.stared){
|
||||
this.starsrc = "/common/vidtool_stared.png"
|
||||
}
|
||||
},
|
||||
async GetVideoDetail(){
|
||||
@@ -86,6 +104,7 @@ export default {
|
||||
},
|
||||
onInit(){
|
||||
this.GetVideoDetail()
|
||||
this.UpdateVideoToolbarStatus()
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
Reference in New Issue
Block a user