You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

220 lines
6.7 KiB

2 years ago
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
2 years ago
import 'package:flutter_background_service/flutter_background_service.dart';
import 'package:flutter_contacts/flutter_contacts.dart';
2 years ago
import 'package:project_telephony/utils/user_tool.dart';
2 years ago
import 'package:shared_preferences/shared_preferences.dart';
2 years ago
import '../constants/api.dart';
import '../model/exclude_phone_model.dart';
2 years ago
import '../model/login_info_model.dart';
import '../model/network/api_client.dart';
import '../model/user_info_model.dart';
import '../ui/home/set/func/exclude_contacts_func.dart';
2 years ago
import '../utils/hive_store.dart';
import '../utils/toast/cloud_toast.dart';
class UserProvider extends ChangeNotifier {
bool _isLogin = false;
2 years ago
bool _kg = false;
2 years ago
bool get kg => _kg;
2 years ago
bool get isLogin => _isLogin;
late UserInfoModel _userInfo;
UserInfoModel get userInfo => _userInfo;
late String _contentCon;
2 years ago
String get contentCon => _contentCon;
late String _contentRef;
2 years ago
String get contentRef => _contentRef;
2 years ago
Future<bool> init() async {
if (HiveStore.appBox?.containsKey('token') ?? false) {
final token = HiveStore.appBox?.get('token') as String;
_isLogin = true;
apiClient.setToken(token);
await updateUserInfo();
return true;
} else {
_isLogin = false;
return false;
}
}
2 years ago
Future setKg(bool lockage) async {
_kg = lockage;
notifyListeners();
}
2 years ago
2 years ago
Future setToken(String token, {User? user}) async {
apiClient.setToken(token);
await HiveStore.appBox!.put('token', token);
_isLogin = true;
//每次打开app更新用户信息
await updateUserInfo();
updateConSms();
updateRefSms();
updateCallSms();
updateIdleSms();
viewLoading();
getExclude();
2 years ago
}
Future logout() async {
final service = FlutterBackgroundService();
2 years ago
apiClient.clearToken();
_isLogin = false;
await HiveStore.appBox?.delete('token');
service.invoke("stopService");
2 years ago
}
Future updateUserInfo() async {
var base = await apiClient.request(API.app.info);
2 years ago
if (base.code == 0) {
_userInfo = UserInfoModel.fromJson(base.data);
} else {
CloudToast.show(base.msg);
2 years ago
_userInfo = UserInfoModel.fail;
2 years ago
}
notifyListeners();
}
2 years ago
2 years ago
Future updateConSms() async {
2 years ago
final prefs = await SharedPreferences.getInstance();
2 years ago
final service = FlutterBackgroundService();
var base = await apiClient.request(API.content.find, data: {'status': 1});
if (base.code == 0) {
2 years ago
await prefs.remove('conSms');
await prefs.setString(
'conSms',
(UserTool.userProvider.userInfo.tag) == ""
? "${base.data['content']}"
: "${UserTool.userProvider.userInfo.tag}${base.data['content']}");
2 years ago
service.invoke("stopService");
if (prefs.getBool('kg')!) {
2 years ago
Future.delayed(const Duration(seconds: 1), () async {
service.startService();
});
}
} else {
CloudToast.show(base.msg);
}
notifyListeners();
}
Future updateRefSms() async {
2 years ago
final prefs = await SharedPreferences.getInstance();
final service = FlutterBackgroundService();
var base = await apiClient.request(API.content.find, data: {'status': 2});
if (base.code == 0) {
await prefs.remove('refSms');
await prefs.setString(
'refSms',
(UserTool.userProvider.userInfo.tag) == ""
? "${base.data['content']}"
: "${UserTool.userProvider.userInfo.tag}${base.data['content']}");
service.invoke("stopService");
if (prefs.getBool('kg')!) {
Future.delayed(const Duration(seconds: 1), () async {
service.startService();
});
}
} else {
CloudToast.show(base.msg);
}
notifyListeners();
}
Future updateCallSms() async {
2 years ago
final prefs = await SharedPreferences.getInstance();
final service = FlutterBackgroundService();
var base = await apiClient.request(API.content.find, data: {'status': 3});
2 years ago
if (base.code == 0) {
await prefs.remove('callSms');
2 years ago
await prefs.setString(
'callSms',
2 years ago
(UserTool.userProvider.userInfo.tag) == ""
? "${base.data['content']}"
: "${UserTool.userProvider.userInfo.tag}${base.data['content']}");
service.invoke("stopService");
if (prefs.getBool('kg')!) {
Future.delayed(const Duration(seconds: 1), () async {
service.startService();
});
}
} else {
CloudToast.show(base.msg);
}
notifyListeners();
}
Future updateIdleSms() async {
2 years ago
final prefs = await SharedPreferences.getInstance();
2 years ago
final service = FlutterBackgroundService();
var base = await apiClient.request(API.content.find, data: {'status': 4});
if (base.code == 0) {
await prefs.remove('idleSms');
await prefs.setString(
'idleSms',
(UserTool.userProvider.userInfo.tag) == ""
? "${base.data['content']}"
: "${UserTool.userProvider.userInfo.tag}${base.data['content']}");
service.invoke("stopService");
if (prefs.getBool('kg')!) {
Future.delayed(const Duration(seconds: 1), () async {
service.startService();
});
}
} else {
CloudToast.show(base.msg);
}
notifyListeners();
}
Future getExclude() async {
final prefs = await SharedPreferences.getInstance();
final service = FlutterBackgroundService();
var base = await apiClient.request(API.exclude.find);
if (base.code == 0) {
await prefs.remove('exclude');
await prefs.setStringList('exclude', base.data);
2 years ago
service.invoke("stopService");
if (prefs.getBool('kg')!) {
2 years ago
Future.delayed(const Duration(seconds: 1), () async {
service.startService();
});
}
} else {
CloudToast.show(base.msg);
}
notifyListeners();
}
Future viewLoading() async {
List<String>? numList=[];
List<String>? numbers=[];
final service = FlutterBackgroundService();
final SharedPreferences prefs = await SharedPreferences.getInstance();
var contact = await FlutterContacts.getContacts();
for (var element in contact!) {
final full = await FlutterContacts.getContact(element.id);
numbers.add(full!.phones.first.number.replaceAll(" ", ""));
// status.add(false);
}
var numberList = await ExcludeFunc.getContacts();
for (int i = 0; i < numberList.length; i++) {
numList.add(numberList[i].phone);
}
await prefs.setStringList("specified", numList);
await prefs.setStringList("addressList", numbers);
service.invoke("stopService");
if (prefs.getBool('kg')!) {
Future.delayed(const Duration(seconds: 1), () async {
service.startService();
});
}
notifyListeners();
}
2 years ago
}