新增模拟未登录推荐,独立推荐设置,新增accesskey风控警告,统一推荐逻辑

This commit is contained in:
orz12
2024-01-20 15:14:52 +08:00
parent 4673f6dc5b
commit 41ddeab41a
9 changed files with 210 additions and 181 deletions

View File

@ -46,7 +46,9 @@ class VideoHttp {
setting.get(SettingBoxKey.blackMidsList, defaultValue: [-1]);
for (var i in res.data['data']['item']) {
//过滤掉live与ad以及拉黑用户
if (i['goto'] == 'av' && !blackMidsList.contains(i['owner']['mid'])) {
if (i['goto'] == 'av' &&
(i['owner'] != null &&
!blackMidsList.contains(i['owner']['mid']))) {
list.add(RecVideoItemModel.fromJson(i));
}
}
@ -59,7 +61,9 @@ class VideoHttp {
}
}
static Future rcmdVideoListApp({int? ps, required int freshIdx}) async {
// 添加额外的loginState变量模拟未登录状态
static Future rcmdVideoListApp(
{bool loginStatus = true, required int freshIdx}) async {
try {
var res = await Request().get(
Api.recommendListApp,
@ -72,9 +76,11 @@ class VideoHttp {
'device_name': 'vivo',
'pull': freshIdx == 0 ? 'true' : 'false',
'appkey': Constants.appKey,
'access_key': localCache
.get(LocalCacheKey.accessKey, defaultValue: {})['value'] ??
''
'access_key': loginStatus
? (localCache.get(LocalCacheKey.accessKey,
defaultValue: {})['value'] ??
'')
: ''
},
);
if (res.data['code'] == 0) {
@ -92,7 +98,7 @@ class VideoHttp {
}
return {'status': true, 'data': list};
} else {
return {'status': false, 'data': [], 'msg': ''};
return {'status': false, 'data': [], 'msg': res.data['message']};
}
} catch (err) {
return {'status': false, 'data': [], 'msg': err.toString()};

View File

@ -1,7 +1,7 @@
// 首页推荐类型
enum RcmdType { web, app }
enum RcmdType { web, app, notLogin }
extension RcmdTypeExtension on RcmdType {
String get values => ['web', 'app'][index];
String get labels => ['web端', 'app端'][index];
String get values => ['web', 'app', 'notLogin'][index];
String get labels => ['web端', 'app端', '模拟未登录'][index];
}

View File

@ -9,8 +9,8 @@ import 'package:pilipala/utils/storage.dart';
class RcmdController extends GetxController {
final ScrollController scrollController = ScrollController();
int _currentPage = 0;
RxList<RecVideoItemAppModel> appVideoList = <RecVideoItemAppModel>[].obs;
RxList<RecVideoItemModel> webVideoList = <RecVideoItemModel>[].obs;
// RxList<RecVideoItemAppModel> appVideoList = <RecVideoItemAppModel>[].obs;
// RxList<RecVideoItemModel> webVideoList = <RecVideoItemModel>[].obs;
bool isLoadingMore = true;
OverlayEntry? popupDialog;
Box recVideo = GStrorage.recVideo;
@ -18,6 +18,7 @@ class RcmdController extends GetxController {
RxInt crossAxisCount = 2.obs;
late bool enableSaveLastData;
late String defaultRcmdType = 'web';
late RxList<dynamic> videoList;
@override
void onInit() {
@ -37,85 +38,59 @@ class RcmdController extends GetxController {
setting.get(SettingBoxKey.enableSaveLastData, defaultValue: false);
defaultRcmdType =
setting.get(SettingBoxKey.defaultRcmdType, defaultValue: 'web');
if (defaultRcmdType == 'web'){
videoList = <RecVideoItemModel>[].obs;
} else {
videoList = <RecVideoItemAppModel>[].obs;
}
}
// 获取推荐
Future queryRcmdFeed(type) async {
print(defaultRcmdType);
if (defaultRcmdType == 'app') {
return await queryRcmdFeedApp(type);
}
if (defaultRcmdType == 'web') {
return await queryRcmdFeedWeb(type);
}
}
// 获取app端推荐
Future queryRcmdFeedApp(type) async {
if (isLoadingMore == false) {
return;
}
if (type == 'onRefresh') {
_currentPage = 0;
}
var res = await VideoHttp.rcmdVideoListApp(
freshIdx: _currentPage,
);
late final Map<String,dynamic> res;
switch (defaultRcmdType) {
case 'app': case 'notLogin':
res = await VideoHttp.rcmdVideoListApp(
loginStatus: defaultRcmdType != 'notLogin',
freshIdx: _currentPage,
);
break;
default: //'web'
res = await VideoHttp.rcmdVideoList(
freshIdx: _currentPage,
ps: 20,
);
}
if (res['status']) {
if (type == 'init') {
if (appVideoList.isNotEmpty) {
appVideoList.addAll(res['data']);
if (videoList.isNotEmpty) {
videoList.addAll(res['data']);
} else {
appVideoList.value = res['data'];
videoList.value = res['data'];
}
} else if (type == 'onRefresh') {
if (enableSaveLastData) {
appVideoList.insertAll(0, res['data']);
videoList.insertAll(0, res['data']);
} else {
appVideoList.value = res['data'];
videoList.value = res['data'];
}
} else if (type == 'onLoad') {
appVideoList.addAll(res['data']);
videoList.addAll(res['data']);
}
recVideo.put('cacheList', res['data']);
_currentPage += 1;
}
isLoadingMore = false;
return res;
}
// 获取web端推荐
Future queryRcmdFeedWeb(type) async {
if (isLoadingMore == false) {
return;
}
if (type == 'onRefresh') {
_currentPage = 0;
}
var res = await VideoHttp.rcmdVideoList(
ps: 20,
freshIdx: _currentPage,
);
if (res['status']) {
if (type == 'init') {
if (webVideoList.isNotEmpty) {
webVideoList.addAll(res['data']);
} else {
webVideoList.value = res['data'];
}
} else if (type == 'onRefresh') {
if (enableSaveLastData) {
webVideoList.insertAll(0, res['data']);
} else {
webVideoList.value = res['data'];
}
} else if (type == 'onLoad') {
webVideoList.addAll(res['data']);
if (defaultRcmdType != 'web') {
recVideo.put('cacheList', res['data']);
}
_currentPage += 1;
} else {
Get.snackbar('提示', res['msg']);
}
isLoadingMore = false;
return res;
}
// 下拉刷新
@ -129,7 +104,7 @@ class RcmdController extends GetxController {
queryRcmdFeed('onLoad');
}
// 返回顶部并刷新
// 返回顶部
void animateToTop() async {
if (scrollController.offset >=
MediaQuery.of(Get.context!).size.height * 5) {

View File

@ -91,54 +91,18 @@ class _RcmdPageState extends State<RcmdPage>
SliverPadding(
padding:
const EdgeInsets.fromLTRB(0, StyleString.safeSpace, 0, 0),
sliver: FutureBuilder(
future: _futureBuilderFuture,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
Map data = snapshot.data as Map;
if (data['status']) {
return Platform.isAndroid || Platform.isIOS
? Obx(
() => contentGrid(
_rcmdController,
_rcmdController.defaultRcmdType == 'web'
? _rcmdController.webVideoList
: _rcmdController.appVideoList),
)
: SliverLayoutBuilder(
builder: (context, boxConstraints) {
return Obx(
() => contentGrid(
_rcmdController,
_rcmdController.defaultRcmdType == 'web'
? _rcmdController.webVideoList
: _rcmdController.appVideoList),
);
});
} else {
return HttpError(
errMsg: data['msg'],
fn: () {
setState(() {
_futureBuilderFuture =
_rcmdController.queryRcmdFeed('init');
});
},
);
}
} else {
// 缓存数据
// if (_rcmdController.videoList.isNotEmpty) {
// return contentGrid(
// _rcmdController, _rcmdController.videoList);
// }
// // 骨架屏
// else {
return contentGrid(_rcmdController, []);
// }
}
},
),
sliver: Obx(() {
// 使用Obx来监听数据的变化
if (_rcmdController.isLoadingMore) {
// 如果正在加载,则显示骨架屏
return contentGrid(_rcmdController, []);
} else {
// 显示视频列表
return contentGrid(
_rcmdController,
_rcmdController.videoList);
}
}),
),
LoadingMore(ctr: _rcmdController)
],

View File

@ -1,9 +1,7 @@
import 'package:flutter/material.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:hive/hive.dart';
import 'package:pilipala/http/member.dart';
import 'package:pilipala/models/common/dynamics_type.dart';
import 'package:pilipala/models/common/rcmd_type.dart';
import 'package:pilipala/models/common/reply_sort_type.dart';
import 'package:pilipala/pages/setting/widgets/select_dialog.dart';
import 'package:pilipala/utils/storage.dart';
@ -20,23 +18,16 @@ class ExtraSetting extends StatefulWidget {
class _ExtraSettingState extends State<ExtraSetting> {
Box setting = GStrorage.setting;
static Box localCache = GStrorage.localCache;
late dynamic defaultRcmdType;
late dynamic defaultReplySort;
late dynamic defaultDynamicType;
late dynamic enableSystemProxy;
late String defaultSystemProxyHost;
late String defaultSystemProxyPort;
Box userInfoCache = GStrorage.userInfo;
var userInfo;
bool userLogin = false;
var accessKeyInfo;
@override
void initState() {
super.initState();
// 首页默认推荐类型
defaultRcmdType =
setting.get(SettingBoxKey.defaultRcmdType, defaultValue: 'web');
// 默认优先显示最新评论
defaultReplySort =
setting.get(SettingBoxKey.replySortType, defaultValue: 0);
@ -49,9 +40,6 @@ class _ExtraSettingState extends State<ExtraSetting> {
localCache.get(LocalCacheKey.systemProxyHost, defaultValue: '');
defaultSystemProxyPort =
localCache.get(LocalCacheKey.systemProxyPort, defaultValue: '');
userInfo = userInfoCache.get('userInfoCache');
userLogin = userInfo != null;
accessKeyInfo = localCache.get(LocalCacheKey.accessKey, defaultValue: null);
}
// 设置代理
@ -159,12 +147,6 @@ class _ExtraSettingState extends State<ExtraSetting> {
setKey: SettingBoxKey.enableSearchWord,
defaultVal: true,
),
const SetSwitchItem(
title: '推荐动态',
subTitle: '是否在推荐内容中展示动态',
setKey: SettingBoxKey.enableRcmdDynamic,
defaultVal: true,
),
const SetSwitchItem(
title: '快速收藏',
subTitle: '点按收藏至默认,长按选择文件夹',
@ -177,50 +159,6 @@ class _ExtraSettingState extends State<ExtraSetting> {
setKey: SettingBoxKey.enableWordRe,
defaultVal: false,
),
const SetSwitchItem(
title: '首页推荐刷新',
subTitle: '下拉刷新时保留上次内容',
setKey: SettingBoxKey.enableSaveLastData,
defaultVal: false,
),
ListTile(
dense: false,
title: Text('首页推荐类型', style: titleStyle),
subtitle: Text(
'当前使用「$defaultRcmdType端」推荐',
style: subTitleStyle,
),
onTap: () async {
String? result = await showDialog(
context: context,
builder: (context) {
return SelectDialog<String>(
title: '推荐类型',
value: defaultRcmdType,
values: RcmdType.values.map((e) {
return {'title': e.labels, 'value': e.values};
}).toList(),
);
},
);
if (result != null) {
if (result == 'app') {
// app端推荐需要access_key
if (accessKeyInfo == null) {
if (!userLogin) {
SmartDialog.showToast('请先登录');
return;
}
await MemberHttp.cookieToKey();
}
}
defaultRcmdType = result;
setting.put(SettingBoxKey.defaultRcmdType, result);
SmartDialog.showToast('下次启动时生效');
setState(() {});
}
},
),
const SetSwitchItem(
title: '启用ai总结',
subTitle: '视频详情页开启ai总结',

View File

@ -0,0 +1,137 @@
import 'package:flutter/material.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:hive/hive.dart';
import 'package:pilipala/http/member.dart';
import 'package:pilipala/models/common/rcmd_type.dart';
import 'package:pilipala/pages/setting/widgets/select_dialog.dart';
import 'package:pilipala/utils/storage.dart';
import 'widgets/switch_item.dart';
class RecommendSetting extends StatefulWidget {
const RecommendSetting({super.key});
@override
State<RecommendSetting> createState() => _RecommendSettingState();
}
class _RecommendSettingState extends State<RecommendSetting> {
Box setting = GStrorage.setting;
static Box localCache = GStrorage.localCache;
late dynamic defaultRcmdType;
Box userInfoCache = GStrorage.userInfo;
late dynamic userInfo;
bool userLogin = false;
late dynamic accessKeyInfo;
@override
void initState() {
super.initState();
// 首页默认推荐类型
defaultRcmdType =
setting.get(SettingBoxKey.defaultRcmdType, defaultValue: 'web');
userInfo = userInfoCache.get('userInfoCache');
userLogin = userInfo != null;
accessKeyInfo = localCache.get(LocalCacheKey.accessKey, defaultValue: null);
}
@override
Widget build(BuildContext context) {
TextStyle titleStyle = Theme.of(context).textTheme.titleMedium!;
TextStyle subTitleStyle = Theme.of(context)
.textTheme
.labelMedium!
.copyWith(color: Theme.of(context).colorScheme.outline);
return Scaffold(
appBar: AppBar(
centerTitle: false,
titleSpacing: 0,
title: Text(
'推荐设置',
style: Theme.of(context).textTheme.titleMedium,
),
),
body: ListView(
children: [
const SetSwitchItem(
title: '推荐动态',
subTitle: '是否在推荐内容中展示动态',
setKey: SettingBoxKey.enableRcmdDynamic,
defaultVal: true,
),
const SetSwitchItem(
title: '首页推荐刷新',
subTitle: '下拉刷新时保留上次内容',
setKey: SettingBoxKey.enableSaveLastData,
defaultVal: false,
),
ListTile(
dense: false,
title: Text('首页推荐类型', style: titleStyle),
subtitle: Text(
'当前使用「$defaultRcmdType端」推荐',
style: subTitleStyle,
),
onTap: () async {
String? result = await showDialog(
context: context,
builder: (context) {
return SelectDialog<String>(
title: '推荐类型',
value: defaultRcmdType,
values: RcmdType.values.map((e) {
return {'title': e.labels, 'value': e.values};
}).toList(),
);
},
);
if (result != null) {
if (result == 'app') {
// app端推荐需要access_key
if (accessKeyInfo == null) {
if (!userLogin) {
SmartDialog.showToast('请先登录');
return;
}
// 显示一个确认框,告知用户可能会导致账号被风控
SmartDialog.show(
animationType: SmartAnimationType.centerFade_otherSlide,
builder: (context) {
return AlertDialog(
title: const Text('提示'),
content: const Text(
'使用app端推荐需获取access_key有小概率触发风控导致账号退出在官方app重新登录即可解除是否继续'),
actions: [
TextButton(
onPressed: () {
result = null;
SmartDialog.dismiss();
},
child: const Text('取消'),
),
TextButton(
onPressed: () async {
SmartDialog.dismiss();
await MemberHttp.cookieToKey();
},
child: const Text('确定'),
),
],
);
});
}
}
if (result != null) {
defaultRcmdType = result;
setting.put(SettingBoxKey.defaultRcmdType, result);
SmartDialog.showToast('下次启动时生效');
setState(() {});
}
}
},
),
],
),
);
}
}

View File

@ -24,6 +24,11 @@ class SettingPage extends StatelessWidget {
dense: false,
title: const Text('隐私设置'),
),
ListTile(
onTap: () => Get.toNamed('/recommendSetting'),
dense: false,
title: const Text('推荐设置'),
),
ListTile(
onTap: () => Get.toNamed('/playSetting'),
dense: false,

View File

@ -37,6 +37,7 @@ import '../pages/setting/pages/color_select.dart';
import '../pages/setting/pages/display_mode.dart';
import '../pages/setting/pages/font_size_select.dart';
import '../pages/setting/pages/play_speed_set.dart';
import '../pages/setting/recommend_setting.dart';
import '../pages/setting/play_setting.dart';
import '../pages/setting/privacy_setting.dart';
import '../pages/setting/style_setting.dart';
@ -100,7 +101,8 @@ class Routes {
// 二级回复
CustomGetPage(
name: '/replyReply', page: () => const VideoReplyReplyPanel()),
// 推荐设置
CustomGetPage(name: '/recommendSetting', page: () => const RecommendSetting()),
// 播放设置
CustomGetPage(name: '/playSetting', page: () => const PlaySetting()),
// 外观设置

View File

@ -120,17 +120,19 @@ class SettingBoxKey {
/// 隐私
blackMidsList = 'blackMidsList',
/// 推荐
enableRcmdDynamic = 'enableRcmdDynamic',
defaultRcmdType = 'defaultRcmdType',
enableSaveLastData = 'enableSaveLastData',
/// 其他
autoUpdate = 'autoUpdate',
defaultRcmdType = 'defaultRcmdType',
replySortType = 'replySortType',
defaultDynamicType = 'defaultDynamicType',
enableHotKey = 'enableHotKey',
enableQuickFav = 'enableQuickFav',
enableWordRe = 'enableWordRe',
enableSearchWord = 'enableSearchWord',
enableRcmdDynamic = 'enableRcmdDynamic',
enableSaveLastData = 'enableSaveLastData',
enableSystemProxy = 'enableSystemProxy',
enableAi = 'enableAi';