Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 6c10da6

Browse files
committedJan 24, 2024
将AccountService()写成了工厂方法,结果跪了,改回来
1 parent 7202850 commit 6c10da6

18 files changed

+37
-38
lines changed
 

‎lib/account_manager/account_service.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import 'package:shared_preferences/shared_preferences.dart';
77
/// 这个类就像一个 "GetxController",它共享相同的生命周期 ("onInit()"、"onReady()"、"onClose()") 。 但里面没有 "逻辑"。它只是通知GetX的依赖注入系统,这个子类不能从内存中删除。
88
/// 所以这对保持你的 "服务 "总是可以被Get.find()获取到并保持运行是超级有用的。比如 ApiService,StorageService,CacheService。
99
class AccountService extends GetxService {
10-
1110
final _kLastLoginUserName = "kLastLoginUserName";
1211

1312
final _kLastLoginPassword = "kLastLoginPassword";
@@ -26,7 +25,7 @@ class AccountService extends GetxService {
2625

2726
var userInfo = "等级 -- 排名 -- 积分 --";
2827

29-
factory AccountService() => AccountService();
28+
static AccountService get find => Get.find<AccountService>();
3029

3130
Future<SharedPreferences> get userDefine async =>
3231
await SharedPreferences.getInstance();

‎lib/base/base_controller.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ mixin ResponseStatusMixin on GetxController {
2323
/// 本质上GetMiddleware的中间件已经做了这个事情,详细看LoginMiddleware,但是如何去拦截点击事情还没想好
2424
mixin CheckIsLoginMixin on BaseController {
2525
bool checkIsLogin() {
26-
if (!AccountService().isLogin) {
26+
if (!AccountService.find.isLogin) {
2727
Get.toNamed(Routes.login);
2828
}
2929

30-
return AccountService().isLogin;
30+
return AccountService.find.isLogin;
3131
}
3232
}

‎lib/http_client/request_client.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ final _dio = Dio(
2727
baseUrl: Api.baseUrl,
2828
connectTimeout: timeout,
2929
receiveTimeout: timeout,
30-
headers: {HttpHeaders.cookieHeader: AccountService().cookieHeaderValue},
30+
headers: {HttpHeaders.cookieHeader: AccountService.find.cookieHeaderValue},
3131
),
3232
).addPlugins;
3333

‎lib/http_util/http_util.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ abstract class HttpUtils {
5555
}
5656

5757
static Options getCookieHeaderOptions() {
58-
final value = AccountService().cookieHeaderValue;
58+
final value = AccountService.find.cookieHeaderValue;
5959
Options options = Options(headers: {HttpHeaders.cookieHeader: value});
6060
return options;
6161
}

‎lib/main.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import 'dart:io';
22
import 'package:flutter/material.dart';
33
import 'package:flutter/services.dart';
4+
45
import 'package:get/get.dart';
5-
import 'package:getx_study/account_manager/account_service.dart';
66

77
import 'package:getx_study/my_app.dart';
8+
import 'package:getx_study/account_manager/account_service.dart';
89
import 'package:getx_study/example_app/stream_app.dart';
910
import 'package:getx_study/example_app/get_x_app.dart';
1011
import 'package:getx_study/example_app/rx_dart_app.dart';

‎lib/my_app.dart

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,9 @@ class MyApp extends StatelessWidget {
4848
}
4949

5050
/// 悼念模式
51-
void _changeColorModel({required Color color, required Widget widget }) {
52-
ColorFiltered(
53-
colorFilter: ColorFilter.mode(color, BlendMode.color),
54-
child: widget
55-
);
51+
/// 将这个包裹在GetCupertinoApp外层即可,具体颜色一把使用grey
52+
Widget _changeColorModel({required Color color, required Widget widget}) {
53+
return ColorFiltered(
54+
colorFilter: ColorFilter.mode(color, BlendMode.color), child: widget);
5655
}
5756
}

‎lib/pages/launch/welcome_page.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class WelcomePage extends StatelessWidget {
5656
),
5757
child: const Text("点击进入"),
5858
onPressed: () {
59-
AccountService().saveNotFirstLaunch();
59+
AccountService.find.saveNotFirstLaunch();
6060
Get.offAllNamed(Routes.main);
6161
},
6262
),

‎lib/pages/my/controller/get_user_info_mixin.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ mixin GetUserInfoMixin
1212
final userInfo =
1313
"等级 ${response.data?.level ?? "--"} 排名 ${response.data?.rank ?? "--"} 积分 ${response.data?.coinCount ?? "--"}";
1414
this.userInfo = userInfo;
15-
AccountService().userInfo = userInfo;
15+
AccountService.find.userInfo = userInfo;
1616
return userInfo;
1717
}
1818
}

‎lib/pages/my/controller/login_controller.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class LoginController
4040

4141
String message;
4242
if (response.isSuccess == true && response.data != null) {
43-
await AccountService()
43+
await AccountService.find
4444
.save(info: response.data!, isLogin: true, password: password);
4545
await getUserCoinInfo();
4646
message = "登录成功";
@@ -61,7 +61,7 @@ class LoginController
6161
// Get.back();
6262
if (response.isSuccess) {
6363
final params = {
64-
"isLogin": AccountService().isLogin,
64+
"isLogin": AccountService.find.isLogin,
6565
"userInfo": userInfo,
6666
};
6767
navigator?.pop(params);

‎lib/pages/my/controller/my_collect_controller.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ class MyCollectController
6565

6666
String message;
6767
if (model.isSuccess) {
68-
if ((AccountService().info?.collectIds ?? [])
68+
if ((AccountService.find.info?.collectIds ?? [])
6969
.contains(dataSource[index].originId)) {
70-
(AccountService().info?.collectIds ?? [])
70+
(AccountService.find.info?.collectIds ?? [])
7171
.remove(dataSource[index].originId);
7272
}
7373
dataSource.removeAt(index);

‎lib/pages/my/controller/my_controller.dart

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ class MyController
1414
为了避免这种问题,_observable_的第一次变化将总是触发一个事件,即使它包含相同的.value。
1515
如果你想删除这种行为,你可以使用: isLogin.firstRebuild = false;。
1616
*/
17-
final isLogin = AccountService().isLogin.obs;
17+
final isLogin = AccountService.find.isLogin.obs;
1818

19-
final rxUserInfo = AccountService().userInfo.obs;
19+
final rxUserInfo = AccountService.find.userInfo.obs;
2020

2121
@override
2222
void onInit() {
@@ -29,7 +29,7 @@ class MyController
2929
String message;
3030
if (response.isSuccess) {
3131
message = "登出成功";
32-
AccountService().clear();
32+
AccountService.find.clear();
3333
} else {
3434
message = "登出失败";
3535
}
@@ -38,27 +38,27 @@ class MyController
3838
message,
3939
duration: const Duration(seconds: 1),
4040
);
41-
return AccountService().isLogin;
41+
return AccountService.find.isLogin;
4242
}
4343

4444
Future<void> autoLogin() async {
45-
final username = await AccountService().getLastLoginUserName();
46-
final password = await AccountService().getLastLoginPassword();
45+
final username = await AccountService.find.getLastLoginUserName();
46+
final password = await AccountService.find.getLastLoginPassword();
4747

4848
if (username.isNotEmpty && password.isNotEmpty) {
4949
final response =
5050
await request.login(username: username, password: password);
5151

5252
String message;
5353
if (response.isSuccess == true && response.data != null) {
54-
await AccountService()
54+
await AccountService.find
5555
.save(info: response.data!, isLogin: true, password: password);
5656
message = "自动登录成功";
5757

5858
await getUserCoinInfo();
5959

60-
isLogin.value = AccountService().isLogin;
61-
rxUserInfo.value = AccountService().userInfo;
60+
isLogin.value = AccountService.find.isLogin;
61+
rxUserInfo.value = AccountService.find.userInfo;
6262
} else {
6363
message = "自动登录失败";
6464
}

‎lib/pages/my/controller/register_controller.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class RegisterController extends LoginController {
3434

3535
String message;
3636
if (response.isSuccess == true && response.data != null) {
37-
await AccountService()
37+
await AccountService.find
3838
.save(info: response.data!, isLogin: true, password: password);
3939
await getUserCoinInfo();
4040
message = "注册成功";
@@ -50,7 +50,7 @@ class RegisterController extends LoginController {
5050
if (response.isSuccess) {
5151
Future.delayed(
5252
const Duration(seconds: 0),
53-
() => navigator?.pop(AccountService().isLogin),
53+
() => navigator?.pop(AccountService.find.isLogin),
5454
);
5555
}
5656
}

‎lib/pages/my/view/my_page.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ class MyPage extends GetView<MyController> {
8383
Get.back();
8484
final result = await controller.logout();
8585
controller.rxUserInfo.value =
86-
AccountService().userInfo;
86+
AccountService.find.userInfo;
8787
controller.isLogin.value = result;
8888
},
8989
),

‎lib/pages/web/controller/web_controller.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@ class WebController extends BaseRequestController<WebRepository, Object?> {
112112

113113
String message;
114114
if (model.isSuccess) {
115-
if ((AccountService().info?.collectIds ?? []).contains(originId)) {
116-
(AccountService().info?.collectIds ?? []).remove(originId);
115+
if ((AccountService.find.info?.collectIds ?? []).contains(originId)) {
116+
(AccountService.find.info?.collectIds ?? []).remove(originId);
117117
}
118118
message = "取消收藏成功";
119119
_actionTag = _actionTag - 1;
@@ -136,7 +136,7 @@ class WebController extends BaseRequestController<WebRepository, Object?> {
136136

137137
String message;
138138
if (model.isSuccess) {
139-
(AccountService().info?.collectIds ?? []).add(originId);
139+
(AccountService.find.info?.collectIds ?? []).add(originId);
140140
message = "收藏成功";
141141
_actionTag = _actionTag + 1;
142142
_type = CollectActionType.collect;
@@ -155,7 +155,7 @@ class WebController extends BaseRequestController<WebRepository, Object?> {
155155

156156
bool isCollect(IWebLoadInfo webLoadInfo) {
157157
final collectId = _realCollectId(webLoadInfo);
158-
final collectIds = AccountService().info?.collectIds;
158+
final collectIds = AccountService.find.info?.collectIds;
159159
if (collectIds != null && collectId != null) {
160160
if (collectIds.contains(collectId)) {
161161
return true;

‎lib/pages/web/view/web_page.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class WebPage extends GetView<WebController> {
2727
if (notShowCollectIcon == "true") {
2828
isShowCollectIcon = false;
2929
} else {
30-
isShowCollectIcon = webLoadInfo.id != null && AccountService().isLogin;
30+
isShowCollectIcon = webLoadInfo.id != null && AccountService.find.isLogin;
3131
}
3232

3333
controller.flutterWebViewSetting(webLoadInfo);

‎lib/routes/login_middleware.dart renamed to ‎lib/routes/middleware/login_middleware.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class LoginMiddleware extends GetMiddleware {
99
/// 当需要执行路由的重定向时,就可以调用此函数,比如使用它实现强制登录逻辑,即没有登录时跳转登录逻辑。
1010
@override
1111
RouteSettings? redirect(String? route) {
12-
return AccountService().isLogin
12+
return AccountService.find.isLogin
1313
? super.redirect(route)
1414
: const RouteSettings(name: Routes.login);
1515
}

‎lib/routes/routes.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ import 'package:getx_study/pages/tree/view/tabs_page.dart';
2727
import 'package:getx_study/pages/tree/view/tree_page.dart';
2828
import 'package:getx_study/pages/web/binding/web_binding.dart';
2929
import 'package:getx_study/pages/web/view/web_page.dart';
30-
import 'package:getx_study/routes/login_middleware.dart';
31-
import 'package:getx_study/routes/web_middleware.dart';
30+
import 'package:getx_study/routes/middleware/login_middleware.dart';
31+
import 'package:getx_study/routes/middleware/web_middleware.dart';
3232

3333
abstract class Routes {
3434
Routes._();

0 commit comments

Comments
 (0)
Please sign in to comment.