173 lines
4.8 KiB
Dart
173 lines
4.8 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:easy_debounce/easy_throttle.dart';
|
|
import 'package:flustars/flustars.dart';
|
|
import 'package:get/get.dart';
|
|
import 'package:permission_handler/permission_handler.dart';
|
|
|
|
import '../dio_cilent/kt_apis.dart';
|
|
import '../dio_cilent/kt_request.dart';
|
|
import '../kt_model/kt_register_bean.dart';
|
|
import '../kt_pages/kt_mine/logic.dart';
|
|
import '../kt_pages/kt_routes.dart';
|
|
import 'kt_keys.dart';
|
|
|
|
class KtUserUtil {
|
|
static final KtUserUtil _instance = KtUserUtil._internal();
|
|
|
|
factory KtUserUtil() => _instance;
|
|
|
|
KtUserUtil._internal();
|
|
|
|
String? get token => SpUtil.getString(KtKeys.token);
|
|
|
|
Timer? timer;
|
|
|
|
register({bool toHome = true}) async {
|
|
try {
|
|
ApiResponse res = await HttpClient().request(KtApis.register);
|
|
if (res.success) {
|
|
KtRegisterBean data = KtRegisterBean.fromJson(res.data);
|
|
SpUtil.putString(KtKeys.token, data.token ?? '');
|
|
|
|
HttpClient().setAuthToken(data.token ?? '');
|
|
if (toHome) Get.offNamed(KtRoutes.home);
|
|
KtUserUtil().enterAppPost();
|
|
// FirebaseCommon.reportFirebaseToken();
|
|
Get.put(KtMineLogic());
|
|
Get.find<KtMineLogic>().getUserInfo();
|
|
|
|
return Future.value(true);
|
|
}
|
|
if (toHome) Get.offNamed(KtRoutes.home);
|
|
return Future.value(false);
|
|
} catch (e) {
|
|
if (toHome) Get.offNamed(KtRoutes.home);
|
|
return Future.value(false);
|
|
}
|
|
}
|
|
|
|
//每十分钟执行一次
|
|
void startOnline() {
|
|
enterAppPost();
|
|
cancelTimer();
|
|
timer = Timer.periodic(Duration(minutes: 10), (timer) {
|
|
onLinePost();
|
|
});
|
|
}
|
|
|
|
void cancelTimer() {
|
|
timer?.cancel();
|
|
}
|
|
|
|
//进入app上报
|
|
enterAppPost({int isOpenNotice = 0}) {
|
|
if (token == null) return;
|
|
if (SpUtil.containsKey(KtKeys.token) ?? false) {
|
|
EasyThrottle.throttle('enterAppPost', Duration(seconds: 1), () async {
|
|
await HttpClient().request(
|
|
KtApis.enterTheApp,
|
|
data: {'is_open_notice': isOpenNotice},
|
|
);
|
|
});
|
|
// await HttpClient().request(KtApis.enterTheApp, data: {"is_open_notice": isOpenNotice});
|
|
}
|
|
}
|
|
|
|
//在线上报
|
|
onLinePost() async {
|
|
if (token == null) return;
|
|
|
|
EasyThrottle.throttle('onLinePost', Duration(seconds: 1), () async {
|
|
await HttpClient().request(
|
|
KtApis.onLine,
|
|
data: {'PostAuthorization': token ?? ''},
|
|
);
|
|
});
|
|
}
|
|
|
|
//离线上报
|
|
offLinePost() async {
|
|
if (token == null) return;
|
|
|
|
EasyThrottle.throttle('offline', Duration(seconds: 1), () async {
|
|
await HttpClient().request(
|
|
KtApis.leaveApp,
|
|
data: {'PostAuthorization': token ?? ''},
|
|
);
|
|
});
|
|
}
|
|
|
|
//上报通知权限
|
|
void reportNotify() async {
|
|
if (token == null) return;
|
|
final permissionStatus = await Permission.notification.status;
|
|
if (permissionStatus.isDenied || permissionStatus.isPermanentlyDenied) {
|
|
await HttpClient().request(
|
|
KtApis.uploadNoticeStatus,
|
|
data: {'is_open_notice': 0},
|
|
);
|
|
} else if (permissionStatus.isGranted) {
|
|
await HttpClient().request(
|
|
KtApis.uploadNoticeStatus,
|
|
data: {'is_open_notice': 1},
|
|
);
|
|
}
|
|
}
|
|
|
|
// 上报firebase token
|
|
reportFirebaseToken(String token) async {
|
|
await HttpClient().request(
|
|
KtApis.reportFirebaseToken,
|
|
data: {"fcm_token": token},
|
|
);
|
|
}
|
|
|
|
// 上报firebase 消息
|
|
sendMessageReport(int id, String title) async {
|
|
await HttpClient().request(
|
|
KtApis.sendMessageReport,
|
|
data: {"message_id": id, "title": title},
|
|
);
|
|
}
|
|
|
|
// 上报错误信息
|
|
reportErrorEvent(
|
|
String eventName,
|
|
String eventKey, {
|
|
String? errMsg,
|
|
String? type,
|
|
String? orderCode,
|
|
String? transactionId,
|
|
num shortPlayId = 0,
|
|
num shortPlayVideoId = 0,
|
|
Map<String, dynamic>? extendData,
|
|
Map<String, dynamic>? payData,
|
|
}) async {
|
|
String? userId = Get.put(KtMineLogic()).state.userInfo.customerId;
|
|
|
|
Map<String, dynamic> params = {
|
|
"event_name": eventName,
|
|
"event_key": eventKey,
|
|
"userId": userId,
|
|
"short_play_id": shortPlayId,
|
|
"short_play_video_id": shortPlayVideoId,
|
|
};
|
|
|
|
if (type != null) params.putIfAbsent('type', () => type);
|
|
if (orderCode != null) params.putIfAbsent('order_code', () => orderCode);
|
|
if (payData != null) params.putIfAbsent('pay_data', () => payData);
|
|
if (transactionId != null)
|
|
params.putIfAbsent('transaction_id', () => transactionId);
|
|
if (extendData != null) params.addAll(extendData);
|
|
params.putIfAbsent('error_msg', () => errMsg);
|
|
HttpClient().request(KtApis.reportEvent, data: params);
|
|
}
|
|
|
|
static String payCallback = 'pay_callback';
|
|
static String payError = 'pay_error';
|
|
static String payRestore = 'pay_restore';
|
|
static String payPlatformTimeout = 'pay_platform_timeout';
|
|
static String videoError = 'video_error';
|
|
}
|