Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
B
book-app
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
EzijingWeb
book-app
Commits
59b05681
提交
59b05681
authored
4月 08, 2024
作者:
yueweilu
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
离线数据上传时机、随时检测网络变化
上级
af0fef44
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
83 行增加
和
11 行删除
+83
-11
global.dart
lib/global.dart
+79
-6
controller.dart
lib/pages/library/controller.dart
+4
-5
没有找到文件。
lib/global.dart
浏览文件 @
59b05681
import
'dart:convert'
;
import
'package:connectivity_plus/connectivity_plus.dart'
;
import
'package:flutter/cupertino.dart'
;
import
'package:flutter/services.dart'
;
import
'package:flutter_book/services/index.dart'
;
import
'package:flutter_book/store/index.dart'
;
import
'package:flutter_book/utils/index.dart'
;
import
'package:get/get.dart'
;
import
'apis/index.dart'
;
class
Global
{
static
Future
<
void
>
init
()
async
{
// 确保 Flutter 绑定已经初始化
WidgetsFlutterBinding
.
ensureInitialized
();
// 设置应用程序的首选屏幕方向
await
SystemChrome
.
setPreferredOrientations
([
DeviceOrientation
.
portraitUp
]);
// String documentRoot = await Tools.getDirectory();
// final InAppLocalhostServer localhostServer =
// InAppLocalhostServer(documentRoot: '$documentRoot/',shared: true);
// await localhostServer.start();
// print('本地服务器已成功启动,根目录为: $documentRoot');
// 初始化数据库
SqlManager
.
init
();
// 检测网络变化
Connectivity
().
onConnectivityChanged
.
listen
((
ConnectivityResult
result
)
{
Console
.
log
(
'网络变化--------------------------------
$result
'
);
if
(
result
!=
ConnectivityResult
.
none
&&
UserStore
.
to
.
isLogin
){
upload
();
}
});
// 监测app生命周期
// WidgetsBinding.instance?.addObserver(AppLifecycleObserver());
await
Future
.
wait
([
...
...
@@ -33,7 +41,72 @@ class Global {
});
}
static
void
upload
()
async
{
List
<
Map
<
String
,
dynamic
>>
data
=
await
SqlManager
.
queryNoUploadData
();
List
<
Map
<
String
,
dynamic
>>
finalData
=
List
<
Map
<
String
,
dynamic
>>.
from
(
data
);
Console
.
log
(
'Sql-----查询到的数据-----------------------
$data
'
);
if
(
data
.
isNotEmpty
){
for
(
int
i
=
0
;
i
<
data
.
length
;
i
++){
Map
<
String
,
dynamic
>
temp
=
data
[
i
];
// for (Map<String, dynamic> temp in data){
// 如果是笔记
if
(
temp
[
'types'
]
==
3
){
// 存储url的数组
List
<
String
>
finalAudios
=
[];
// 存储url的数组
List
<
String
>
finalImages
=
[];
Map
<
String
,
dynamic
>
contentMap
=
jsonDecode
(
temp
[
'note_content'
]);
List
<
dynamic
>
audios
=
contentMap
[
'audio'
];
if
(
audios
.
isNotEmpty
){
for
(
String
path
in
audios
){
final
url
=
await
uploadSource
(
path:
path
);
finalAudios
.
add
(
url
);
}
}
List
<
dynamic
>
images
=
contentMap
[
'image'
];
if
(
images
.
isNotEmpty
){
for
(
String
path
in
images
){
final
url
=
await
uploadSource
(
path:
path
);
finalImages
.
add
(
url
);
}
}
Map
<
String
,
dynamic
>
finalContentMap
=
{
'text'
:
contentMap
[
'text'
],
'audio'
:
finalAudios
,
'image'
:
finalImages
};
finalData
[
i
]
=
Map
<
String
,
dynamic
>.
from
(
temp
);
finalData
[
i
][
'note_content'
]
=
jsonEncode
(
finalContentMap
);
}
}
String
noteStr
=
jsonEncode
(
finalData
);
final
result
=
await
LibraryAPI
.
uploadOffline
(
node:
noteStr
);
// 上传成功后 更新数据库中的字段
if
(
result
.
isNotEmpty
)
{
// SqlManager.updateUploadStatus();
SqlManager
.
updateNotesId
(
result
);
}
}
}
// 上传文件
static
Future
<
String
>
uploadSource
({
required
String
path
})
async
{
// String result = await CommonAPI.upload(path:path,fileTypes: 'comment');
// return result;
OssTool
tool
=
OssTool
(
'zxts-comment-file'
);
final
response
=
await
tool
.
putObjectFile
(
path
);
Console
.
log
(
'------response--------------------------
${response.realUri}
'
);
return
response
.
realUri
.
toString
();
}
}
/// 生命周期类
class
AppLifecycleObserver
extends
WidgetsBindingObserver
{
@override
void
didChangeAppLifecycleState
(
AppLifecycleState
state
)
{
...
...
lib/pages/library/controller.dart
浏览文件 @
59b05681
...
...
@@ -99,12 +99,11 @@ class LibraryController extends GetxController with GetTickerProviderStateMixin{
// 测试添加数据库数据
// testInsertData();
final
result
=
await
Tools
.
checkCurrentNetStatus
();
// 有网络 并且是登录状态 的时候上传 笔记
if
(
result
&&
UserStore
.
to
.
isLogin
){
upload
();
}
// final result = await Tools.checkCurrentNetStatus();
// if (result && UserStore.to.isLogin){
// upload();
// }
super
.
onReady
();
}
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论