import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter_background_service/flutter_background_service.dart'; import 'package:flutter_contacts/flutter_contacts.dart'; import 'package:project_telephony/utils/user_tool.dart'; import 'package:shared_preferences/shared_preferences.dart'; import '../constants/api.dart'; import '../model/hive/phone_model.dart'; 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'; import '../utils/hive_store.dart'; import '../utils/toast/cloud_toast.dart'; class UserProvider extends ChangeNotifier { bool _isLogin = false; bool _kg = false; bool get kg => _kg; bool get isLogin => _isLogin; late UserInfoModel _userInfo; UserInfoModel get userInfo => _userInfo; late String _contentCon; String get contentCon => _contentCon; late String _contentRef; String get contentRef => _contentRef; Future init() async { if (HiveStore.appBox?.containsKey('token') ?? false) { final token = HiveStore.appBox?.get('token') as String; // UserTool.phoneNumProvider.init(); // print("重置"); _isLogin = true; apiClient.setToken(token); await updateUserInfo(); // updateConSms(); // updateRefSms(); // updateCallSms(); // updateIdleSms(); return true; } else { _isLogin = false; return false; } } Future setKg(bool lockage) async { _kg = lockage; notifyListeners(); } Future setToken(String token, {User? user}) async { final prefs = await SharedPreferences.getInstance(); // List dxText=[]; apiClient.setToken(token); await HiveStore.appBox!.put('token', token); _isLogin = true; // 每次打开app更新用户信息 updateUserInfo(); prefs.setInt("numIndex", 1);// prefs.setString("refSms","现在有事,等会回电"); // _viewLoading();// // UserTool.phoneNumProvider.init(); // prefs.setStringList("dxText", [ // "祝你万事顺心", // "欢迎你的来电,祝你生活愉快", // "感谢您的来电,我们会尽快处理", // ]); // prefs.setStringList("dxText",["祝你万事顺心","欢迎你的来电,祝你生活愉快","感谢您的来电,我们会尽快处理",]); // updateConSms(); // updateRefSms(); // updateCallSms(); // updateIdleSms(); // viewLoading(); // getExclude(); } Future logout() async { final service = FlutterBackgroundService(); apiClient.clearToken(); _isLogin = false; await HiveStore.appBox?.delete('token'); HiveStore.dataBox?.delete("ml"); service.invoke("stopService"); } Future updateUserInfo() async { var base = await apiClient.request(API.app.info); if (base.code == 0) { _userInfo = UserInfoModel.fromJson(base.data); } else { CloudToast.show(base.msg); _userInfo = UserInfoModel.fail; } notifyListeners(); } Future updateConSms() async { final prefs = await SharedPreferences.getInstance(); final service = FlutterBackgroundService(); var base = await apiClient.request(API.content.find, data: {'status': 1}); if (base.code == 0) { await prefs.remove('conSms'); await prefs.setString( 'conSms', (UserTool.userProvider.userInfo.tag) == "" ? "${base.data['content']}" : "【${UserTool.userProvider.userInfo.tag}】${base.data['content']}"); service.invoke("stopService"); if (prefs.getBool('kg') ?? false) { Future.delayed(const Duration(seconds: 1), () async { service.startService(); }); } } else { CloudToast.show(base.msg); } notifyListeners(); } // Future updateRefSms() async { // 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') ?? false) { // Future.delayed(const Duration(seconds: 1), () async { // service.startService(); // }); // } // } else { // CloudToast.show(base.msg); // } // notifyListeners(); // } // // Future updateCallSms() async { // final prefs = await SharedPreferences.getInstance(); // final service = FlutterBackgroundService();//出错 // var base = await apiClient.request(API.content.find, data: {'status': 3}); // if (base.code == 0) { // await prefs.remove('callSms'); // await prefs.setString( // 'callSms', // (UserTool.userProvider.userInfo.tag) == "" // ? "${base.data['content']}" // : "【${UserTool.userProvider.userInfo.tag}】${base.data['content']}"); // service.invoke("stopService"); // if (prefs.getBool('kg') ?? false) { // Future.delayed(const Duration(seconds: 1), () async { // service.startService(); // }); // } // } else { // CloudToast.show(base.msg); // } // notifyListeners(); // } // // Future updateIdleSms() async { // final prefs = await SharedPreferences.getInstance(); // 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') ?? false) { // 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'); // prefs.setStringList('exclude', base.data); // service.invoke("stopService"); // if (prefs.getBool('kg') ?? false) { // Future.delayed(const Duration(seconds: 1), () async { // service.startService(); // }); // } // } else { // CloudToast.show(base.msg); // } // notifyListeners(); // } Future viewLoading( ) async { List numList=[]; List numbers=[]; List nameList=[]; List massList = []; List phoneList = []; final service = FlutterBackgroundService(); final SharedPreferences prefs = await SharedPreferences.getInstance(); var numberList = await ExcludeFunc.getContacts() ; List contact = await FlutterContacts.getContacts();//出错 for (var element in contact ) { final full = await FlutterContacts.getContact(element.id); if(full?.phones.length==0){ numbers.add('无'); }else{ numbers.add(full!.phones.first.number.replaceAll(" ", "")); nameList.add(full.displayName); numbers.add(full.phones.first.number); phoneList.add(PhoneNum(name: full.displayName, phone: full.phones.first.number,state: false)); // phoneList.add(PhoneNumModel(time: 0, name: full!.displayName, state: false, num: full.phones.first.number)); // numbers.add(full!.phones.first.number); } // status.add(false); } // print("numbersList$numberList"); for (int i = 0; i < numberList.length; i++) { numList.add(numberList[i].phone); } await HiveStore.dataBox!.put("pl",phoneList) ; massList.add(PhoneModel(title:"未分组联系人",time: "本机通讯录",phoneList:HiveStore.dataBox!.get("pl"),state: false)); HiveStore.dataBox?.put("ml",massList) ; prefs.setStringList("specified", numList); prefs.setStringList("addressList", numbers); prefs.setStringList("nameList",nameList ); prefs.setStringList("number", numbers); service.invoke("stopService"); print("指定不发送${prefs.getStringList("specified")}"); if (prefs.getBool('kg') ?? false) { Future.delayed(const Duration(seconds: 1), () async { service.startService(); }); } notifyListeners(); } // Future _viewLoading() async { // List? contacts; // List numbers=[]; // List nameList=[]; // final SharedPreferences prefs = await SharedPreferences.getInstance(); // contacts = await FlutterContacts.getContacts(); // for (var element in contacts) { // final full = await FlutterContacts.getContact(element.id); // if(full?.phones.length==0){ // numbers.add('无'); // }else{ // nameList.add(full!.displayName); // numbers.add(full.phones.first.number); // // phoneList.add(PhoneNumModel(time: 0, name: full!.displayName, state: false, num: full.phones.first.number)); // // numbers.add(full!.phones.first.number); // } // } // prefs.setStringList("nameList",nameList ); // prefs.setStringList("number", numbers); // // print(contacts.length); // notifyListeners(); // } }