Blame view

lib/app/splash_page.dart 6.73 KB
062f0df2   liangchengyou   feat:登录模块代码提交
1
  import 'dart:async';
cc3b183a   liangchengyou   feat:ios启动图/logo
2
  import 'dart:io';
a4d8eaa2   Key   feat: 登录时账户有效性校验
3
  import 'dart:math';
062f0df2   liangchengyou   feat:登录模块代码提交
4
  
1892df31   Key   优化接口调用
5
  import 'package:flutter/foundation.dart';
2a29701f   liangchengyou   feat:提交代码
6
  import 'package:flutter/material.dart';
a4d8eaa2   Key   feat: 登录时账户有效性校验
7
8
  import 'package:flutter/services.dart';
  import 'package:limiting_direction_csx/limiting_direction_csx.dart';
27dad3d1   吴启风   feat:集成友盟统计
9
  import 'package:umeng_common_sdk/umeng_common_sdk.dart';
28f20da9   吴启风   feat:针对apple审核对支付...
10
  import 'package:wow_english/common/core/app_config_helper.dart';
94342c3f   Key   feat: user util
11
  import 'package:wow_english/common/core/user_util.dart';
062f0df2   liangchengyou   feat:登录模块代码提交
12
  import 'package:wow_english/common/extension/string_extension.dart';
a4d8eaa2   Key   feat: 登录时账户有效性校验
13
  import 'package:wow_english/common/request/config.dart';
e55bbdf3   Key   fixed: aliyun oss...
14
  import 'package:wow_english/common/request/dao/user_dao.dart';
94342c3f   Key   feat: user util
15
  import 'package:wow_english/models/user_entity.dart';
2a29701f   liangchengyou   feat:提交代码
16
  import 'package:wow_english/route/route.dart';
0e314ad5   吴启风   feat:开屏页播放背景音乐wel...
17
  import 'package:wow_english/utils/audio_player_util.dart';
a4d8eaa2   Key   feat: 登录时账户有效性校验
18
  import 'package:wow_english/utils/log_util.dart';
94342c3f   Key   feat: user util
19
  import 'package:wow_english/utils/sp_util.dart';
062f0df2   liangchengyou   feat:登录模块代码提交
20
  
2a9895f6   吴启风   feat:首次启动隐私协议弹窗
21
  import '../common/core/app_consts.dart';
42f15f6c   吴启风   feat:模块选择持久化&模块主题...
22
  import '../common/core/module_cache.dart';
2a9895f6   吴启风   feat:首次启动隐私协议弹窗
23
24
  import '../common/widgets/webview_dialog.dart';
  
a117a5a3   liangchengyou   feat:更新代码
25
26
  class SplashPage extends StatelessWidget {
    const SplashPage({super.key});
2a29701f   liangchengyou   feat:提交代码
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
  
    @override
    Widget build(BuildContext context) {
      return const TransitionView();
    }
  }
  
  class TransitionView extends StatefulWidget {
    const TransitionView({super.key});
  
    @override
    State<StatefulWidget> createState() {
      return _TransitionViewState();
    }
  }
  
  class _TransitionViewState extends State<TransitionView> {
062f0df2   liangchengyou   feat:登录模块代码提交
44
    Future startTime() async {
94342c3f   Key   feat: user util
45
46
      // 判断是否登录
      UserEntity? userEntity = UserUtil.getUser();
a4d8eaa2   Key   feat: 登录时账户有效性校验
47
48
      Log.d('当前模式:kDebugMode=$kDebugMode, kProfileMode=$kProfileMode, kReleaseMode=$kReleaseMode');
      Log.d('当前API地址:${RequestConfig.baseUrl}');
e55bbdf3   Key   fixed: aliyun oss...
49
50
51
52
      //BuildContext context = Navigator.of(context).context;
      //var currentPageName = ModalRoute.of(Navigator.of(AppRouter.context))?.settings.name;
      //Log.d('Splash读当前页面:$currentPageName');
      Log.d('Splash读本地, userEntity: $userEntity');
a4d8eaa2   Key   feat: 登录时账户有效性校验
53
      int apartInMilliseconds = 0;
8fec4713   吴启风   feat:启动请求失败阻塞优化
54
      // 获取系统配置信息
c4458dc6   吴启风   feat:启动页必要接口增加失败重试
55
      AppConfigHelper.getAppConfig();
a4d8eaa2   Key   feat: 登录时账户有效性校验
56
      // 调一下接口判断一下有效性再往下
278208b8   吴启风   feat:1、用户访问权限调整;2...
57
58
      if (userEntity != null && userEntity.token != null) {
        String token = userEntity.token!;
a4d8eaa2   Key   feat: 登录时账户有效性校验
59
        DateTime startTime = DateTime.now();
c4458dc6   吴启风   feat:启动页必要接口增加失败重试
60
        await fetchNecessaryData(token);
a4d8eaa2   Key   feat: 登录时账户有效性校验
61
        apartInMilliseconds = DateTime.now().difference(startTime).inMilliseconds;
1892df31   Key   优化接口调用
62
      }
0e314ad5   吴启风   feat:开屏页播放背景音乐wel...
63
64
      /// 开屏最低2.5s,保证视觉与音乐同步
      int duration = max(2500 - apartInMilliseconds, 0);
e55bbdf3   Key   fixed: aliyun oss...
65
      Log.d('Splash getUserInfo 耗时:${apartInMilliseconds}ms, 最终duration=$duration');
a4d8eaa2   Key   feat: 登录时账户有效性校验
66
      Timer(Duration(milliseconds: duration), () {
210d15e0   Key   fixed: 账户注销接口
67
        /*if (userEntity != null) {
08a0f5a8   liangchengyou   feat:路由方式更新
68
          pushNamedAndRemoveUntil(AppRouteName.home, (route) => false);
062f0df2   liangchengyou   feat:登录模块代码提交
69
        } else {
08a0f5a8   liangchengyou   feat:路由方式更新
70
          pushNamedAndRemoveUntil(AppRouteName.login, (route) => false);
210d15e0   Key   fixed: 账户注销接口
71
        }*/
2a9895f6   吴启风   feat:首次启动隐私协议弹窗
72
73
        bool isAggreementAccepted = AppConfigHelper.getAgreementAccepted();
        if (isAggreementAccepted) {
27dad3d1   吴启风   feat:集成友盟统计
74
          _initData();
2a9895f6   吴启风   feat:首次启动隐私协议弹窗
75
76
77
78
79
80
81
82
83
84
85
86
        } else {
          showDialog(
            context: context,
            barrierDismissible: false,
            builder: (BuildContext context) {
              return WillPopScope(
                onWillPop: () => Future.value(false),
                child: WebviewDialog(
                  title: "服务条款及隐私政策",
                  webUrl: AppConsts.userPrivacyPolicyUrl,
                  leftTap: () {
                    AppConfigHelper.saveAgreementAccepted(true);
27dad3d1   吴启风   feat:集成友盟统计
87
                    _initData();
2a9895f6   吴启风   feat:首次启动隐私协议弹窗
88
89
90
                  },
                  rightTap: () {
                    // 退出应用
258578de   吴启风   feat:修复ios上退出应用失效问题
91
                    AppConfigHelper.exitApp();
2a9895f6   吴启风   feat:首次启动隐私协议弹窗
92
93
94
95
96
97
                  },
                ),
              );
            },
          );
        }
062f0df2   liangchengyou   feat:登录模块代码提交
98
99
100
      });
    }
  
27dad3d1   吴启风   feat:集成友盟统计
101
102
103
104
105
106
    ///初始化数据
    void _initData() {
      UmengCommonSdk.initCommon("663b66b0b3362515012f4ea5", "663b66ecf32cc41105ae74b7", "official");
      pushNamedAndRemoveUntil(AppRouteName.home, (route) => false);
    }
  
8fec4713   吴启风   feat:启动请求失败阻塞优化
107
108
109
    Future<void> fetchNecessaryData(String userToken,
        {Completer<void>? completer}) async {
      completer ??= Completer<void>();
c4458dc6   吴启风   feat:启动页必要接口增加失败重试
110
111
112
113
114
115
116
117
118
      // 获取用户信息
      try {
        UserEntity? userEntity = await UserDao.getUserInfo();
        Log.d('Splash在线更新, userEntity: $userEntity');
        if (userEntity != null) {
          userEntity.token = userToken;
          Log.d('Splash重设token, userEntity: $userEntity');
          UserUtil.saveUser(userEntity);
        }
8fec4713   吴启风   feat:启动请求失败阻塞优化
119
120
        //todo 如果为null是否需要清除用户信息?
        completer.complete();
c4458dc6   吴启风   feat:启动页必要接口增加失败重试
121
      } catch (e) {
8fec4713   吴启风   feat:启动请求失败阻塞优化
122
        debugPrint('Splash获取用户信息异常:$e');
c4458dc6   吴启风   feat:启动页必要接口增加失败重试
123
124
125
126
127
128
129
130
131
132
133
134
        //异常的话弹窗提示重试
        showDialog(
          context: context,
          barrierDismissible: false,
          builder: (BuildContext context) {
            return WillPopScope(
              onWillPop: () => Future.value(false),
              child: AlertDialog(
                title: const Text('温馨提示'),
                content: const Text('网络异常,请检查网络后重试'),
                actions: <Widget>[
                  TextButton(
8fec4713   吴启风   feat:启动请求失败阻塞优化
135
136
137
138
139
140
141
                      child: const Text('退出'),
                      onPressed: () => {
                            // 关闭对话框并重试
                            Navigator.of(context).pop(),
                            AppConfigHelper.exitApp(),
                            completer?.complete(),
                          }),
c4458dc6   吴启风   feat:启动页必要接口增加失败重试
142
143
144
                  TextButton(
                    child: const Text('重试'),
                    onPressed: () async {
8fec4713   吴启风   feat:启动请求失败阻塞优化
145
146
147
148
                      // 关闭对话框并重试
                      Navigator.of(context).pop();
                      await fetchNecessaryData(userToken, completer: completer);
                      completer?.complete();
c4458dc6   吴启风   feat:启动页必要接口增加失败重试
149
150
151
152
153
154
155
156
157
                    },
                  ),
                ],
              ),
            );
          },
        );
        e.logE();
      }
8fec4713   吴启风   feat:启动请求失败阻塞优化
158
159
      // 等待completer完成,这会阻塞后续代码的执行,直到用户做出选择
      await completer.future;
c4458dc6   吴启风   feat:启动页必要接口增加失败重试
160
161
    }
  
062f0df2   liangchengyou   feat:登录模块代码提交
162
163
164
    @override
    void initState() {
      super.initState();
089ccd5c   Key   fixed: user util ...
165
166
167
168
      init();
    }
  
    void init() async {
0e314ad5   吴启风   feat:开屏页播放背景音乐wel...
169
      changeDevice();
089ccd5c   Key   fixed: user util ...
170
      await SpUtil.preInit();
42f15f6c   吴启风   feat:模块选择持久化&模块主题...
171
      ModuleCache.instance.init();
0e314ad5   吴启风   feat:开屏页播放背景音乐wel...
172
      AudioPlayerUtil.getInstance().playAudio(AudioPlayerUtilType.welcomeToWow);
062f0df2   liangchengyou   feat:登录模块代码提交
173
      startTime();
cc3b183a   liangchengyou   feat:ios启动图/logo
174
175
176
177
178
179
180
181
    }
  
    void changeDevice() async {
      ///设置设备默认方向
      WidgetsFlutterBinding.ensureInitialized();
      if (Platform.isIOS) {
        await LimitingDirectionCsx.setScreenDirection(DeviceDirectionMask.Landscape);
      } else {
a4d8eaa2   Key   feat: 登录时账户有效性校验
182
        await SystemChrome.setPreferredOrientations([DeviceOrientation.landscapeLeft, DeviceOrientation.landscapeRight]);
cc3b183a   liangchengyou   feat:ios启动图/logo
183
      }
062f0df2   liangchengyou   feat:登录模块代码提交
184
185
    }
  
2a29701f   liangchengyou   feat:提交代码
186
187
188
    @override
    Widget build(BuildContext context) {
      return Scaffold(
2a29701f   liangchengyou   feat:提交代码
189
        body: Center(
062f0df2   liangchengyou   feat:登录模块代码提交
190
191
192
193
          child: Container(
            decoration: BoxDecoration(
                image: DecorationImage(
                    image: AssetImage(
1d5315dd   liangchengyou   feat:添加字体,调整文件结构
194
                      'splash'.assetGif,
062f0df2   liangchengyou   feat:登录模块代码提交
195
                    ),
94342c3f   Key   feat: user util
196
                    fit: BoxFit.fill)),
2a29701f   liangchengyou   feat:提交代码
197
198
199
200
          ),
        ),
      );
    }
056970d8   Key   feat: api
201
  }