Skip to content

Commit

Permalink
feat(page): 完善 切换线路
Browse files Browse the repository at this point in the history
  • Loading branch information
d1y committed Apr 2, 2022
1 parent 0d086a9 commit d9705b4
Show file tree
Hide file tree
Showing 6 changed files with 278 additions and 81 deletions.
17 changes: 15 additions & 2 deletions lib/api/helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,17 @@ String createVodTypeURL(int id) {
return "/vodtype/$id.html";
}

/// [vodType] 为类型 `id`
/// [action] 为空则为全部
String createVodTypeAndTypeURL(int vodType, String action) {
return "/vodshow/$vodType---$action--------.html";
/// [page] 页数
///
/// 不要🙅🏻‍♀️使用 [createVodTypeURL] 函数来生成线路操作
String createVodTypeAndTypeURL({
required int vodType,
String action = "",
int page = 1,
}) {
return "/vodshow/$vodType---$action-----$page---.html";
}

String createVodDetailURL(String detailID) {
Expand All @@ -33,13 +41,18 @@ String createVodPlayURL(String id) {
enum PageQueryStringType {
/// 搜索
search,

/// 线路
vodtype,
}

extension SelfToString on PageQueryStringType {
String get action {
switch (this) {
case PageQueryStringType.search:
return "vodsearch";
case PageQueryStringType.vodtype:
return "vodtype";
default:
return "";
}
Expand Down
44 changes: 44 additions & 0 deletions lib/api/parse_utils.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,50 @@
import 'package:dan211/modules/vod_movie.dart';
import 'package:flutter/foundation.dart';
import 'package:html/dom.dart';

class RespPageData {
RespPageData({
this.total = -1,
this.current = -1,
this.totalPage = -1,
});

int total;
int current;
int totalPage;
}

RespPageData getRespPageData(DocumentFragment document) {
int total = -1;
int current = -1;
int totalPage = -1;

var pageTip = document.querySelector(".page_tip")?.text.trim() ?? "";
if (pageTip == "共0条数据,当前/页") {
debugPrint("搜索的内容为空");
} else {
// 共1443条数据,当前1/145页
var _pageCache = pageTip.split("条数据");
total = int.tryParse(_pageCache[0].substring(1)) ?? 0;
var _pageNumberCache1 = _pageCache[1].split(",当前")[1];
var _pageNumberCache = _pageNumberCache1.substring(
0,
_pageNumberCache1.length - 1,
);
var _pageNumberTarget = _pageNumberCache.split("/");
current = int.tryParse(_pageNumberTarget[0]) ?? 0;
totalPage = int.tryParse(_pageNumberTarget[1]) ?? 0;
debugPrint("total: $total\n");
debugPrint("current: $current\n");
debugPrint("total_page: $totalPage\n");
}
return RespPageData(
total: total,
current: current,
totalPage: totalPage,
);
}

/// 获取 `vod-type`
List<VodType> getVodType(DocumentFragment $) {
var _tags = $.querySelectorAll(".resou a");
Expand Down
74 changes: 48 additions & 26 deletions lib/api/send.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import 'dart:async';

import 'package:dan211/api/parse_utils.dart';
import 'package:dan211/modules/art_detail.dart';
import 'package:dan211/modules/vod_case.dart';
import 'package:dan211/modules/vod_detail.dart';
import 'package:dan211/modules/vod_play.dart' as vodplay;
import 'package:dan211/modules/vod_search.dart';
import 'package:dan211/share/dan_movie_card.dart';
import 'package:flutter/foundation.dart';
import 'package:html/parser.dart' as parser;
import 'package:dan211/modules/vod_movie.dart';
Expand Down Expand Up @@ -161,35 +163,55 @@ class SendHttp {
);
}).toList();

int total = -1;
int current = -1;
int totalPage = -1;

var pageTip = document.querySelector(".page_tip")?.text.trim() ?? "";
if (pageTip == "共0条数据,当前/页") {
debugPrint("搜索的内容为空");
} else {
// 共1443条数据,当前1/145页
var _pageCache = pageTip.split("条数据");
total = int.tryParse(_pageCache[0].substring(1)) ?? 0;
var _pageNumberCache1 = _pageCache[1].split(",当前")[1];
var _pageNumberCache = _pageNumberCache1.substring(
0,
_pageNumberCache1.length - 1,
);
var _pageNumberTarget = _pageNumberCache.split("/");
current = int.tryParse(_pageNumberTarget[0]) ?? 0;
totalPage = int.tryParse(_pageNumberTarget[1]) ?? 0;
debugPrint("total: $total\n");
debugPrint("current: $current\n");
debugPrint("total_page: $totalPage\n");
}
RespPageData _pageData = getRespPageData(document);

return VodSearchRespData(
data: cards,
total: total,
current: current,
totalPage: totalPage,
total: _pageData.total,
current: _pageData.current,
totalPage: _pageData.totalPage,
);
}

static Future<VodCaseRespData> getDataByVodType({
required DanMovieCardItem item,
int page = 1,
String action = "",
}) async {
String send = createVodTypeAndTypeURL(
vodType: item.id ?? -1,
page: page,
action: action,
);
var resp = await XHttp.dio.get(send);
var document = parser.parseFragment(resp.data);
RespPageData _pageData = getRespPageData(document);
List<String> tags = document
.querySelectorAll("#first_list_p a")
.map(
(e) => e.text.trim(),
)
.toList();

var listNode = document.querySelectorAll(".img-list li");

List<VodCard> cards = listNode.map((e) {
var a = e.querySelector("a");
var img = a?.querySelector("img");
var title = img?.attributes["alt"]?.trim() ?? "";
var id = a?.attributes["href"]?.split("/")[2].split(".html")[0] ?? "";
var image = img?.attributes["src"]?.trim() ?? "";
return VodCard(
cover: image,
id: id,
title: title,
);
}).toList();

return VodCaseRespData(
cards: cards,
pageData: _pageData,
tags: tags,
);
}
}
80 changes: 78 additions & 2 deletions lib/app/modules/vod_case/controllers/vod_case_controller.dart
Original file line number Diff line number Diff line change
@@ -1,27 +1,103 @@
import 'package:dan211/api/parse_utils.dart';
import 'package:dan211/api/send.dart';
import 'package:dan211/app/modules/vod_search/views/page_bar.dart';
import 'package:dan211/modules/vod_case.dart';
import 'package:dan211/share/dan_movie_card.dart';
import 'package:dan211/share/dan_movie_data.dart';
import 'package:get/get.dart';

class VodCaseController extends GetxController {
int caseIndex = 0;
int _caseIndex = 0;

int get caseIndex => _caseIndex;

set caseIndex(int newVal) {
_caseIndex = newVal;
update();
fetchData();
}

VodCaseRespData data = VodCaseRespData(
cards: [],
pageData: RespPageData(),
tags: [],
);

List<DanMovieCardItem> get shareData => danMovieShareConstData.data;

DanMovieCardItem get currCaseItem => shareData[caseIndex];

String currentTag = "全部";

bool isLoading = false;
String errorStack = "";
int page = 1;

String get _currentTagText {
/// 事实上如果是 `全部` 则为传递 ` ` 内容
if (currentTag == "全部") return "";
return currentTag;
}

changeCurrentTag(String item) {
currentTag = item;
page = 1;
update();
fetchData();
}

changeCaseIndex(DanMovieCardItem? item) {
if (item == null) return;
var _index = shareData.indexOf(item);
if (_index <= -1) return;
currentTag = "全部";
update();
caseIndex = _index;
update();
}


fetchData() async {
isLoading = true;
update();
try {
data = await SendHttp.getDataByVodType(
item: currCaseItem,
action: _currentTagText,
page: page,
);
isLoading = false;
errorStack = "";
update();
} on Exception catch (e) {
isLoading = false;
errorStack = e.toString();
update();
}
}

handlePrevAndNext(CupertinoPageBarActionType _type) async {
var _p = page;
if (_type == CupertinoPageBarActionType.prev) {
page--;
} else {
page++;
}
await fetchData();

/// 错误栈有内容回退操作
if (errorStack.isNotEmpty) {
page = _p;
}
}

beforeHook() {
caseIndex = 0;
}

@override
void onInit() {
super.onInit();
beforeHook();
}

@override
Expand Down
Loading

0 comments on commit d9705b4

Please sign in to comment.