From e612e60361c3431a6577bb74a58ec6efd25e287f Mon Sep 17 00:00:00 2001 From: guozhigq Date: Wed, 10 May 2023 00:35:24 +0800 Subject: [PATCH] =?UTF-8?q?mod:=20=E7=94=A8=E6=88=B7=E4=BF=A1=E6=81=AF?= =?UTF-8?q?=E6=B8=B2=E6=9F=93=E3=80=81=E9=80=80=E5=87=BA=E7=99=BB=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/http/api.dart | 3 + lib/http/init.dart | 19 +- lib/http/user.dart | 11 + lib/main.dart | 2 + lib/models/user/info.dart | 27 +- lib/models/user/stat.dart | 17 ++ lib/pages/main/controller.dart | 35 ++- lib/pages/main/view.dart | 26 +- lib/pages/mine/controller.dart | 62 ++++- lib/pages/mine/view.dart | 398 ++++++++++++++++++++---------- lib/pages/setting/controller.dart | 23 ++ lib/pages/setting/index.dart | 4 + lib/pages/setting/view.dart | 36 +++ lib/pages/webview/controller.dart | 9 +- lib/router/app_pages.dart | 5 +- lib/utils/cookie.dart | 1 - lib/utils/storage.dart | 24 ++ pubspec.lock | 128 ++++++++++ pubspec.yaml | 3 + 19 files changed, 674 insertions(+), 159 deletions(-) create mode 100644 lib/models/user/stat.dart create mode 100644 lib/pages/setting/controller.dart create mode 100644 lib/pages/setting/index.dart create mode 100644 lib/pages/setting/view.dart create mode 100644 lib/utils/storage.dart diff --git a/lib/http/api.dart b/lib/http/api.dart index 62e7047c..32abb1b6 100644 --- a/lib/http/api.dart +++ b/lib/http/api.dart @@ -23,4 +23,7 @@ class Api { // 获取用户信息 static const String userInfo = '/x/web-interface/nav'; + + // 获取当前用户状态 + static const String userStatOwner = '/x/web-interface/nav/stat'; } diff --git a/lib/http/init.dart b/lib/http/init.dart index b5008a60..87ab8c59 100644 --- a/lib/http/init.dart +++ b/lib/http/init.dart @@ -12,6 +12,7 @@ import 'package:dio_cookie_manager/dio_cookie_manager.dart'; class Request { static final Request _instance = Request._internal(); + static late CookieManager cookieManager; factory Request() => _instance; @@ -31,11 +32,9 @@ class Request { ignoreExpires: true, storage: FileStorage(cookiePath), ); - - dio.interceptors.add(CookieManager(cookieJar)); - - var cookie = await CookieManager(cookieJar) - .cookieJar + cookieManager = CookieManager(cookieJar); + dio.interceptors.add(cookieManager); + var cookie = await cookieManager.cookieJar .loadForRequest(Uri.parse(HttpString.baseUrl)); if (cookie.isEmpty) { try { @@ -46,6 +45,16 @@ class Request { } } + // 移除cookie + static removeCookie() async { + await cookieManager.cookieJar + .saveFromResponse(Uri.parse(HttpString.baseUrl), []); + await cookieManager.cookieJar + .saveFromResponse(Uri.parse(HttpString.baseApiUrl), []); + cookieManager.cookieJar.deleteAll(); + dio.interceptors.add(cookieManager); + } + /* * config it and create */ diff --git a/lib/http/user.dart b/lib/http/user.dart index 77964f08..8f8881ab 100644 --- a/lib/http/user.dart +++ b/lib/http/user.dart @@ -1,6 +1,7 @@ import 'package:pilipala/http/api.dart'; import 'package:pilipala/http/init.dart'; import 'package:pilipala/models/user/info.dart'; +import 'package:pilipala/models/user/stat.dart'; class UserHttp { static Future userStat({required int mid}) async { @@ -17,6 +18,16 @@ class UserHttp { if (res.data['code'] == 0) { UserInfoData data = UserInfoData.fromJson(res.data['data']); return {'status': true, 'data': data}; + } else { + return {'status': false, 'msg': res.data['message']}; + } + } + + static Future userStatOwner() async { + var res = await Request().get(Api.userStatOwner); + if (res.data['code'] == 0) { + UserStat data = UserStat.fromJson(res.data['data']); + return {'status': true, 'data': data}; } else { return {'status': false}; } diff --git a/lib/main.dart b/lib/main.dart index f572a7a7..f853f4d7 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -5,9 +5,11 @@ import 'package:dynamic_color/dynamic_color.dart'; import 'package:pilipala/http/init.dart'; import 'package:pilipala/router/app_pages.dart'; import 'package:pilipala/pages/main/view.dart'; +import 'package:pilipala/utils/storage.dart'; void main() async { WidgetsFlutterBinding.ensureInitialized(); + await GStrorage.init(); await Request.setCookie(); runApp(const MyApp()); } diff --git a/lib/models/user/info.dart b/lib/models/user/info.dart index ce8b641c..b51c68f0 100644 --- a/lib/models/user/info.dart +++ b/lib/models/user/info.dart @@ -29,7 +29,7 @@ class UserInfoData { bool? isLogin; int? emailVerified; String? face; - Map? levelInfo; + LevelInfo? levelInfo; int? mid; int? mobileVerified; int? money; @@ -55,7 +55,9 @@ class UserInfoData { isLogin = json['isLogin'] ?? false; emailVerified = json['email_verified']; face = json['face']; - levelInfo = json['level_info']; + levelInfo = json['level_info'] != null + ? LevelInfo.fromJson(json['level_info']) + : LevelInfo(); mid = json['mid']; mobileVerified = json['mobile_verified']; money = json['money']; @@ -78,3 +80,24 @@ class UserInfoData { shopUrl = json['shop_url']; } } + +class LevelInfo { + LevelInfo({ + this.currentLevel, + this.currentMin, + this.currentExp, + this.nextExp, + }); + + int? currentLevel; + int? currentMin; + int? currentExp; + int? nextExp; + + LevelInfo.fromJson(Map json) { + currentLevel = json['current_level']; + currentMin = json['current_min']; + currentExp = json['current_exp']; + nextExp = json['next_exp']; + } +} diff --git a/lib/models/user/stat.dart b/lib/models/user/stat.dart new file mode 100644 index 00000000..0b56a499 --- /dev/null +++ b/lib/models/user/stat.dart @@ -0,0 +1,17 @@ +class UserStat { + UserStat({ + this.following, + this.follower, + this.dynamicCount, + }); + + int? following; + int? follower; + int? dynamicCount; + + UserStat.fromJson(Map json) { + following = json['following']; + follower = json['follower']; + dynamicCount = json['dynamic_count']; + } +} diff --git a/lib/pages/main/controller.dart b/lib/pages/main/controller.dart index 2206af59..752ff713 100644 --- a/lib/pages/main/controller.dart +++ b/lib/pages/main/controller.dart @@ -1,9 +1,12 @@ import 'package:flutter/cupertino.dart'; import 'package:get/get.dart'; import 'package:flutter/material.dart'; +import 'package:hive/hive.dart'; +import 'package:pilipala/common/widgets/network_img_layer.dart'; import 'package:pilipala/pages/home/view.dart'; import 'package:pilipala/pages/hot/view.dart'; import 'package:pilipala/pages/mine/view.dart'; +import 'package:pilipala/utils/storage.dart'; class MainController extends GetxController { List pages = [ @@ -11,7 +14,7 @@ class MainController extends GetxController { const HotPage(), const MinePage(), ]; - List navigationBars = [ + RxList navigationBars = [ { // 'icon': const Icon(Icons.home_outlined), // 'selectedIcon': const Icon(Icons.home), @@ -51,5 +54,33 @@ class MainController extends GetxController { ), 'label': "我的", } - ]; + ].obs; + + @override + void onInit() { + super.onInit(); + readuUserFace(); + } + + // 设置头像 + readuUserFace() async { + Box user = GStrorage.user; + if (user.get(UserBoxKey.userFace) != null) { + navigationBars.last['icon'] = + navigationBars.last['selectedIcon'] = NetworkImgLayer( + width: 25, + height: 25, + type: 'avatar', + src: user.get(UserBoxKey.userFace), + ); + navigationBars.last['label'] = '我'; + } + } + + // 重置 + resetLast() { + navigationBars.last['icon'] = const Icon(Icons.person_outline); + navigationBars.last['selectedIcon'] = const Icon(Icons.person); + navigationBars.last['label'] = '我的'; + } } diff --git a/lib/pages/main/view.dart b/lib/pages/main/view.dart index e1503de8..bce9bbc2 100644 --- a/lib/pages/main/view.dart +++ b/lib/pages/main/view.dart @@ -20,7 +20,7 @@ class _MainAppState extends State with SingleTickerProviderStateMixin { late AnimationController? _animationController; late Animation? _fadeAnimation; late Animation? _slideAnimation; - int selectedIndex = 0; + int selectedIndex = 2; int? _lastSelectTime; //上次点击时间 @override @@ -111,17 +111,19 @@ class _MainAppState extends State with SingleTickerProviderStateMixin { ), ), ), - bottomNavigationBar: NavigationBar( - elevation: 1, - destinations: _mainController.navigationBars.map((e) { - return NavigationDestination( - icon: e['icon'], - selectedIcon: e['selectedIcon'], - label: e['label'], - ); - }).toList(), - selectedIndex: selectedIndex, - onDestinationSelected: (value) => setIndex(value), + bottomNavigationBar: Obx( + () => NavigationBar( + elevation: 1, + destinations: _mainController.navigationBars.map((e) { + return NavigationDestination( + icon: e['icon'], + selectedIcon: e['selectedIcon'], + label: e['label'], + ); + }).toList(), + selectedIndex: selectedIndex, + onDestinationSelected: (value) => setIndex(value), + ), ), ); } diff --git a/lib/pages/mine/controller.dart b/lib/pages/mine/controller.dart index 5c560ebc..62a62438 100644 --- a/lib/pages/mine/controller.dart +++ b/lib/pages/mine/controller.dart @@ -1,17 +1,69 @@ +import 'package:flutter_smart_dialog/flutter_smart_dialog.dart'; import 'package:get/get.dart'; +import 'package:hive/hive.dart'; import 'package:pilipala/http/user.dart'; import 'package:pilipala/models/user/info.dart'; +import 'package:pilipala/models/user/stat.dart'; +import 'package:pilipala/pages/main/controller.dart'; +import 'package:pilipala/utils/storage.dart'; class MineController extends GetxController { - UserInfoData? userInfo; + // 用户信息 头像、昵称、lv + Rx userInfo = UserInfoData().obs; + // 用户状态 动态、关注、粉丝 + Rx userStat = UserStat().obs; + Box user = GStrorage.user; + RxBool userLogin = false.obs; - @override - void onInit() { - super.onInit(); - // queryUserInfo(); + onLogin() { + Get.toNamed( + '/webview', + parameters: { + 'url': 'https://passport.bilibili.com/h5-app/passport/login', + 'type': 'login', + 'pageTitle': '登录bilibili', + }, + ); } Future queryUserInfo() async { var res = await UserHttp.userInfo(); + if (res['status']) { + if (res['data'].isLogin) { + userInfo.value = res['data']; + user.put(UserBoxKey.userName, res['data'].uname); + user.put(UserBoxKey.userFace, res['data'].face); + user.put(UserBoxKey.userMid, res['data'].mid); + user.put(UserBoxKey.userLogin, true); + userLogin.value = true; + Get.find().readuUserFace(); + } else { + resetUserInfo(); + } + } else { + resetUserInfo(); + // SmartDialog.showToast(res['msg']); + } + await queryUserStatOwner(); + return res; + } + + Future queryUserStatOwner() async { + var res = await UserHttp.userStatOwner(); + if (res['status']) { + userStat.value = res['data']; + } + return res; + } + + Future resetUserInfo() async { + userInfo.value = UserInfoData(); + userStat.value = UserStat(); + await user.delete(UserBoxKey.userName); + await user.delete(UserBoxKey.userFace); + await user.delete(UserBoxKey.userMid); + await user.delete(UserBoxKey.userLogin); + userLogin.value = false; + Get.find().resetLast(); } } diff --git a/lib/pages/mine/view.dart b/lib/pages/mine/view.dart index 54796281..7976659f 100644 --- a/lib/pages/mine/view.dart +++ b/lib/pages/mine/view.dart @@ -1,8 +1,8 @@ import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; -import 'package:font_awesome_flutter/font_awesome_flutter.dart'; import 'package:get/get.dart'; import 'package:pilipala/common/constants.dart'; +import 'package:pilipala/common/widgets/network_img_layer.dart'; import 'controller.dart'; class MinePage extends StatefulWidget { @@ -22,18 +22,18 @@ class _MinePageState extends State { title: null, actions: [ IconButton( - onPressed: () {}, - icon: const Icon( - CupertinoIcons.moon, + onPressed: () { + Get.changeThemeMode(ThemeMode.dark); + }, + icon: Icon( + Get.theme == ThemeData.light() + ? CupertinoIcons.moon + : CupertinoIcons.sun_max, size: 22, ), - // icon: const Icon( - // CupertinoIcons.sun_max, - // size: 22, - // ), ), IconButton( - onPressed: () {}, + onPressed: () => Get.toNamed('/setting'), icon: const Icon( CupertinoIcons.slider_horizontal_3, ), @@ -43,7 +43,8 @@ class _MinePageState extends State { ), body: RefreshIndicator( onRefresh: () async { - await Future.delayed(const Duration(seconds: 2)); + await _mineController.queryUserInfo(); + await _mineController.queryUserStatOwner(); }, child: LayoutBuilder( builder: (context, constraint) { @@ -53,127 +54,19 @@ class _MinePageState extends State { height: constraint.maxHeight, child: Column( children: [ - InkWell( - onTap: () { - Get.toNamed( - '/webview', - parameters: { - 'url': - 'https://passport.bilibili.com/h5-app/passport/login', - 'type': 'login', - 'pageTitle': '登录bilibili', - }, - ); + FutureBuilder( + future: _mineController.queryUserInfo(), + builder: (context, snapshot) { + if (snapshot.connectionState == ConnectionState.done) { + if (snapshot.data['status']) { + return Obx(() => userInfoBuild()); + } else { + return userInfoBuild(); + } + } else { + return userInfoBuild(); + } }, - child: Padding( - padding: const EdgeInsets.only(top: 10, bottom: 10), - child: Row( - children: [ - const SizedBox(width: 20), - ClipOval( - child: Container( - width: 75, - height: 75, - color: Theme.of(context) - .colorScheme - .onInverseSurface, - child: Center( - child: - Image.asset('assets/images/loading.png'), - ), - ), - ), - const SizedBox(width: 14), - Text( - '点击登录', - style: Theme.of(context).textTheme.titleMedium, - ), - ], - ), - ), - ), - const SizedBox(height: 10), - Padding( - padding: const EdgeInsets.only(left: 12, right: 12), - child: LayoutBuilder( - builder: (context, constraints) { - TextStyle style = TextStyle( - fontSize: Theme.of(context) - .textTheme - .titleMedium! - .fontSize, - color: Theme.of(context).colorScheme.primary, - fontWeight: FontWeight.bold); - return SizedBox( - height: constraints.maxWidth / 3 * 0.6, - child: GridView.count( - primary: false, - padding: const EdgeInsets.all(0), - crossAxisCount: 3, - childAspectRatio: 1.67, - children: [ - InkWell( - onTap: () {}, - borderRadius: StyleString.mdRadius, - child: Column( - mainAxisAlignment: MainAxisAlignment.center, - children: [ - Text('-', style: style), - const SizedBox(height: 8), - Text( - '动态', - style: Theme.of(context) - .textTheme - .labelMedium, - ), - ], - ), - ), - InkWell( - onTap: () {}, - borderRadius: StyleString.mdRadius, - child: Column( - mainAxisAlignment: MainAxisAlignment.center, - children: [ - Text( - '50', - style: style, - ), - const SizedBox(height: 8), - Text( - '关注', - style: Theme.of(context) - .textTheme - .labelMedium, - ), - ], - ), - ), - InkWell( - onTap: () {}, - borderRadius: StyleString.mdRadius, - child: Column( - mainAxisAlignment: MainAxisAlignment.center, - children: [ - Text( - '-', - style: style, - ), - const SizedBox(height: 8), - Text( - '粉丝', - style: Theme.of(context) - .textTheme - .labelMedium, - ), - ], - ), - ), - ], - ), - ); - }, - ), ), const SizedBox(height: 20), Padding( @@ -224,6 +117,251 @@ class _MinePageState extends State { ), ); } + + Widget userInfoBuild() { + return Column( + children: [ + const SizedBox(height: 5), + GestureDetector( + onTap: () => _mineController.onLogin(), + child: ClipOval( + child: Container( + width: 85, + height: 85, + color: Theme.of(context).colorScheme.onInverseSurface, + child: Center( + child: _mineController.userInfo.value.face != null + ? NetworkImgLayer( + src: _mineController.userInfo.value.face, + width: 85, + height: 85) + : Image.asset('assets/images/loading.png'), + ), + ), + ), + ), + const SizedBox(height: 10), + Row( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + Text( + _mineController.userInfo.value.uname ?? '点击头像登录', + style: Theme.of(context).textTheme.titleMedium, + ), + const SizedBox(width: 4), + Image.asset( + 'assets/images/lv/lv${_mineController.userInfo.value.levelInfo != null ? _mineController.userInfo.value.levelInfo!.currentLevel : '0'}.png', + height: 10, + ), + ], + ), + const SizedBox(height: 5), + Row( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + Text.rich(TextSpan(children: [ + TextSpan( + text: '硬币: ', + style: + TextStyle(color: Theme.of(context).colorScheme.outline)), + TextSpan( + text: (_mineController.userInfo.value.money ?? 'pilipala') + .toString(), + style: + TextStyle(color: Theme.of(context).colorScheme.primary)), + ])) + ], + ), + const SizedBox(height: 5), + if (_mineController.userInfo.value.levelInfo != null) ...[ + LayoutBuilder( + builder: (context, BoxConstraints box) { + return SizedBox( + width: box.maxWidth, + height: 24, + child: Stack( + children: [ + Positioned( + top: 0, + right: 0, + child: SizedBox( + height: 22, + width: box.maxWidth * + (1 - + (_mineController + .userInfo.value.levelInfo!.currentExp! / + _mineController + .userInfo.value.levelInfo!.nextExp!)), + child: Center( + child: Text( + (_mineController + .userInfo.value.levelInfo!.nextExp! - + _mineController + .userInfo.value.levelInfo!.currentExp!) + .toString(), + style: TextStyle( + color: Theme.of(context).colorScheme.primary, + fontSize: 12, + ), + ), + ), + ), + ), + ], + ), + ); + }, + ), + LayoutBuilder( + builder: (context, BoxConstraints box) { + return Container( + width: box.maxWidth, + height: 1, + clipBehavior: Clip.hardEdge, + decoration: BoxDecoration( + borderRadius: BorderRadius.circular(4), + color: Theme.of(context).colorScheme.onInverseSurface, + ), + child: Stack( + children: [ + Positioned( + top: 0, + left: 0, + bottom: 0, + child: Container( + width: box.maxWidth * + (_mineController + .userInfo.value.levelInfo!.currentExp! / + _mineController + .userInfo.value.levelInfo!.nextExp!), + height: 1, + decoration: BoxDecoration( + borderRadius: BorderRadius.circular(4), + color: Theme.of(context).colorScheme.primary, + ), + ), + ), + ], + ), + ); + }, + ), + ], + const SizedBox(height: 30), + Padding( + padding: const EdgeInsets.only(left: 12, right: 12), + child: LayoutBuilder( + builder: (context, constraints) { + TextStyle style = TextStyle( + fontSize: Theme.of(context).textTheme.titleMedium!.fontSize, + color: Theme.of(context).colorScheme.primary, + fontWeight: FontWeight.bold); + return SizedBox( + height: constraints.maxWidth / 3 * 0.6, + child: GridView.count( + primary: false, + padding: const EdgeInsets.all(0), + crossAxisCount: 3, + childAspectRatio: 1.67, + children: [ + InkWell( + onTap: () {}, + borderRadius: StyleString.mdRadius, + child: Column( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + AnimatedSwitcher( + duration: const Duration(milliseconds: 400), + transitionBuilder: + (Widget child, Animation animation) { + return ScaleTransition( + scale: animation, child: child); + }, + child: Text( + (_mineController.userStat.value.dynamicCount ?? + '-') + .toString(), + key: ValueKey(_mineController + .userStat.value.dynamicCount + .toString()), + style: style), + ), + const SizedBox(height: 8), + Text( + '动态', + style: Theme.of(context).textTheme.labelMedium, + ), + ], + ), + ), + InkWell( + onTap: () {}, + borderRadius: StyleString.mdRadius, + child: Column( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + AnimatedSwitcher( + duration: const Duration(milliseconds: 400), + transitionBuilder: + (Widget child, Animation animation) { + return ScaleTransition( + scale: animation, child: child); + }, + child: Text( + (_mineController.userStat.value.following ?? + '-') + .toString(), + key: ValueKey(_mineController + .userStat.value.following + .toString()), + style: style), + ), + const SizedBox(height: 8), + Text( + '关注', + style: Theme.of(context).textTheme.labelMedium, + ), + ], + ), + ), + InkWell( + onTap: () {}, + borderRadius: StyleString.mdRadius, + child: Column( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + AnimatedSwitcher( + duration: const Duration(milliseconds: 400), + transitionBuilder: + (Widget child, Animation animation) { + return ScaleTransition( + scale: animation, child: child); + }, + child: Text( + (_mineController.userStat.value.follower ?? '-') + .toString(), + key: ValueKey(_mineController + .userStat.value.follower + .toString()), + style: style), + ), + const SizedBox(height: 8), + Text( + '粉丝', + style: Theme.of(context).textTheme.labelMedium, + ), + ], + ), + ), + ], + ), + ); + }, + ), + ), + ], + ); + } } class ActionItem extends StatelessWidget { diff --git a/lib/pages/setting/controller.dart b/lib/pages/setting/controller.dart new file mode 100644 index 00000000..af6b6026 --- /dev/null +++ b/lib/pages/setting/controller.dart @@ -0,0 +1,23 @@ +import 'package:get/get.dart'; +import 'package:hive/hive.dart'; +import 'package:pilipala/http/init.dart'; +import 'package:pilipala/pages/mine/controller.dart'; +import 'package:pilipala/utils/storage.dart'; +import 'package:flutter_cache_manager/flutter_cache_manager.dart'; + +class SettingController extends GetxController { + Box user = GStrorage.user; + RxBool userLogin = false.obs; + + @override + void onInit() { + super.onInit(); + userLogin.value = user.get(UserBoxKey.userLogin) ?? false; + } + + loginOut() async { + await Request.removeCookie(); + await Get.find().resetUserInfo(); + userLogin.value = user.get(UserBoxKey.userLogin) ?? false; + } +} diff --git a/lib/pages/setting/index.dart b/lib/pages/setting/index.dart new file mode 100644 index 00000000..30fa06b3 --- /dev/null +++ b/lib/pages/setting/index.dart @@ -0,0 +1,4 @@ +library setting; + +export './controller.dart'; +export './view.dart'; diff --git a/lib/pages/setting/view.dart b/lib/pages/setting/view.dart new file mode 100644 index 00000000..63321afa --- /dev/null +++ b/lib/pages/setting/view.dart @@ -0,0 +1,36 @@ +import 'package:flutter/material.dart'; +import 'package:get/get.dart'; +import 'package:pilipala/pages/setting/index.dart'; + +class SettingPage extends StatefulWidget { + const SettingPage({super.key}); + + @override + State createState() => _SettingPageState(); +} + +class _SettingPageState extends State { + final SettingController _settingController = Get.put(SettingController()); + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: AppBar( + title: const Text('设置'), + ), + body: Column( + children: [ + Obx( + () => Visibility( + visible: _settingController.userLogin.value, + child: ListTile( + onTap: () => _settingController.loginOut(), + dense: false, + title: const Text('退出登录'), + ), + ), + ), + ], + ), + ); + } +} diff --git a/lib/pages/webview/controller.dart b/lib/pages/webview/controller.dart index b8d591cc..eee80c02 100644 --- a/lib/pages/webview/controller.dart +++ b/lib/pages/webview/controller.dart @@ -1,6 +1,7 @@ import 'package:flutter_smart_dialog/flutter_smart_dialog.dart'; import 'package:get/get.dart'; import 'package:pilipala/http/constants.dart'; +import 'package:pilipala/http/init.dart'; import 'package:pilipala/http/user.dart'; import 'package:pilipala/pages/mine/index.dart'; import 'package:pilipala/utils/cookie.dart'; @@ -21,6 +22,12 @@ class WebviewController extends GetxController { pageTitle = Get.parameters['pageTitle']!; webviewInit(); + if (type == 'login') { + controller.clearCache(); + controller.clearLocalStorage(); + WebViewCookieManager().clearCookies(); + controller.setUserAgent(Request().headerUa('mob')); + } } webviewInit() { @@ -49,7 +56,7 @@ class WebviewController extends GetxController { if (result['status'] && result['data'].isLogin) { SmartDialog.showToast('登录成功'); Get.find().userInfo = result['data']; - // Get.back(); + Get.back(); } } catch (e) { print(e); diff --git a/lib/router/app_pages.dart b/lib/router/app_pages.dart index 47cd8bbe..753ba8a0 100644 --- a/lib/router/app_pages.dart +++ b/lib/router/app_pages.dart @@ -4,6 +4,7 @@ import 'package:pilipala/pages/hot/index.dart'; import 'package:pilipala/pages/preview/index.dart'; import 'package:pilipala/pages/video/detail/index.dart'; import 'package:pilipala/pages/webview/index.dart'; +import 'package:pilipala/pages/setting/index.dart'; class Routes { static final List getPages = [ @@ -16,6 +17,8 @@ class Routes { // 图片预览 GetPage(name: '/preview', page: () => const ImagePreview()), // - GetPage(name: '/webview', page: () => const WebviewPage()) + GetPage(name: '/webview', page: () => const WebviewPage()), + // 设置 + GetPage(name: '/setting', page: () => const SettingPage()), ]; } diff --git a/lib/utils/cookie.dart b/lib/utils/cookie.dart index 42a2cb5d..8d5c891b 100644 --- a/lib/utils/cookie.dart +++ b/lib/utils/cookie.dart @@ -2,7 +2,6 @@ import 'dart:io'; import 'package:cookie_jar/cookie_jar.dart'; import 'package:pilipala/http/init.dart'; import 'package:pilipala/utils/utils.dart'; -import 'package:dio_cookie_manager/dio_cookie_manager.dart'; class SetCookie { static onSet(List cookiesList, String url) async { diff --git a/lib/utils/storage.dart b/lib/utils/storage.dart new file mode 100644 index 00000000..24374f36 --- /dev/null +++ b/lib/utils/storage.dart @@ -0,0 +1,24 @@ +import 'package:hive/hive.dart'; +import 'package:path_provider/path_provider.dart'; + +class GStrorage { + static late final Box user; + + static Future init() async { + final dir = await getApplicationDocumentsDirectory(); + final path = dir.path; + Hive.init('$path/hive'); + user = await Hive.openBox('user'); + } +} + +// 约定 key +class UserBoxKey { + static const String userName = 'userName'; + // 头像 + static const String userFace = 'userFace'; + // mid + static const String userMid = 'userMid'; + // 登录状态 + static const String userLogin = 'userLogin'; +} diff --git a/pubspec.lock b/pubspec.lock index 45514cc2..10b7569f 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -1,6 +1,22 @@ # Generated by pub # See https://dart.dev/tools/pub/glossary#lockfile packages: + _fe_analyzer_shared: + dependency: transitive + description: + name: _fe_analyzer_shared + sha256: "8880b4cfe7b5b17d57c052a5a3a8cc1d4f546261c7cc8fbd717bd53f48db0568" + url: "https://pub.dev" + source: hosted + version: "59.0.0" + analyzer: + dependency: transitive + description: + name: analyzer + sha256: a89627f49b0e70e068130a36571409726b04dab12da7e5625941d2c8ec278b96 + url: "https://pub.dev" + source: hosted + version: "5.11.1" args: dependency: transitive description: @@ -25,6 +41,14 @@ packages: url: "https://pub.dev" source: hosted version: "2.1.1" + build: + dependency: transitive + description: + name: build + sha256: "43865b79fbb78532e4bff7c33087aa43b1d488c4fdef014eaef568af6d8016dc" + url: "https://pub.dev" + source: hosted + version: "2.4.0" cached_network_image: dependency: "direct main" description: @@ -89,6 +113,14 @@ packages: url: "https://pub.dev" source: hosted version: "1.2.4" + convert: + dependency: transitive + description: + name: convert + sha256: "0f08b14755d163f6e2134cb58222dd25ea2a2ee8a195e53983d57c075324d592" + url: "https://pub.dev" + source: hosted + version: "3.1.1" cookie_jar: dependency: "direct main" description: @@ -121,6 +153,14 @@ packages: url: "https://pub.dev" source: hosted version: "1.0.5" + dart_style: + dependency: transitive + description: + name: dart_style + sha256: f4f1f73ab3fd2afcbcca165ee601fe980d966af6a21b5970c6c9376955c528ad + url: "https://pub.dev" + source: hosted + version: "2.3.1" dbus: dependency: transitive description: @@ -288,6 +328,38 @@ packages: url: "https://pub.dev" source: hosted version: "4.6.5" + glob: + dependency: transitive + description: + name: glob + sha256: "4515b5b6ddb505ebdd242a5f2cc5d22d3d6a80013789debfbda7777f47ea308c" + url: "https://pub.dev" + source: hosted + version: "2.1.1" + hive: + dependency: "direct main" + description: + name: hive + sha256: "8dcf6db979d7933da8217edcec84e9df1bdb4e4edc7fc77dbd5aa74356d6d941" + url: "https://pub.dev" + source: hosted + version: "2.2.3" + hive_flutter: + dependency: "direct main" + description: + name: hive_flutter + sha256: dca1da446b1d808a51689fb5d0c6c9510c0a2ba01e22805d492c73b68e33eecc + url: "https://pub.dev" + source: hosted + version: "1.1.0" + hive_generator: + dependency: "direct dev" + description: + name: hive_generator + sha256: "65998cc4d2cd9680a3d9709d893d2f6bb15e6c1f92626c3f1fa650b4b3281521" + url: "https://pub.dev" + source: hosted + version: "2.0.0" http: dependency: transitive description: @@ -344,6 +416,14 @@ packages: url: "https://pub.dev" source: hosted version: "2.0.1" + logging: + dependency: transitive + description: + name: logging + sha256: "04094f2eb032cbb06c6f6e8d3607edcfcb0455e2bb6cbc010cb01171dcb64e6d" + url: "https://pub.dev" + source: hosted + version: "1.1.1" matcher: dependency: transitive description: @@ -392,6 +472,14 @@ packages: url: "https://pub.dev" source: hosted version: "1.0.2" + package_config: + dependency: transitive + description: + name: package_config + sha256: "1c5b77ccc91e4823a5af61ee74e6b972db1ef98c2ff5a18d3161c982a55448bd" + url: "https://pub.dev" + source: hosted + version: "2.1.0" path: dependency: transitive description: @@ -528,6 +616,14 @@ packages: url: "https://pub.dev" source: hosted version: "4.2.4" + pub_semver: + dependency: transitive + description: + name: pub_semver + sha256: "40d3ab1bbd474c4c2328c91e3a7df8c6dd629b79ece4c4bd04bee496a224fb0c" + url: "https://pub.dev" + source: hosted + version: "2.1.4" rxdart: dependency: transitive description: @@ -557,6 +653,22 @@ packages: description: flutter source: sdk version: "0.0.99" + source_gen: + dependency: transitive + description: + name: source_gen + sha256: b20e191de6964e98032573cecb1d2b169d96ba63fdb586d24dcd1003ba7e94f6 + url: "https://pub.dev" + source: hosted + version: "1.3.0" + source_helper: + dependency: transitive + description: + name: source_helper + sha256: "3b67aade1d52416149c633ba1bb36df44d97c6b51830c2198e934e3fca87ca1f" + url: "https://pub.dev" + source: hosted + version: "1.3.3" source_span: dependency: transitive description: @@ -725,6 +837,14 @@ packages: url: "https://pub.dev" source: hosted version: "0.3.3" + watcher: + dependency: transitive + description: + name: watcher + sha256: "6a7f46926b01ce81bfc339da6a7f20afbe7733eff9846f6d6a5466aa4c6667c0" + url: "https://pub.dev" + source: hosted + version: "1.0.2" webview_cookie_manager: dependency: "direct main" description: @@ -789,6 +909,14 @@ packages: url: "https://pub.dev" source: hosted version: "6.2.2" + yaml: + dependency: transitive + description: + name: yaml + sha256: "75769501ea3489fca56601ff33454fe45507ea3bfb014161abc3b43ae25989d5" + url: "https://pub.dev" + source: hosted + version: "3.1.2" sdks: dart: ">=2.19.6 <3.0.0" flutter: ">=3.7.0" diff --git a/pubspec.yaml b/pubspec.yaml index 63778e94..a856f809 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -53,6 +53,8 @@ dependencies: # 存储 path_provider: ^2.0.14 + hive: ^2.2.3 + hive_flutter: ^1.1.0 # 设备信息 device_info_plus: ^8.2.0 @@ -84,6 +86,7 @@ dev_dependencies: # git: # url: https://github.com/nvi9/flutter_launcher_icons.git # ref: e045d40 + hive_generator: ^2.0.0 flutter_icons: android: true