From e5815a5a8c0d29267032aba02e86011fd36a1877 Mon Sep 17 00:00:00 2001 From: datang Date: Wed, 24 Aug 2022 18:07:52 +0800 Subject: [PATCH] aaaaaa --- lib/providers/user_provider.dart | 42 +++++++++++++++++++++++--------- lib/ui/home/call.dart | 30 +++++++++++------------ lib/ui/user/user_page.dart | 11 ++++----- 3 files changed, 50 insertions(+), 33 deletions(-) diff --git a/lib/providers/user_provider.dart b/lib/providers/user_provider.dart index 12f0d8e..cf36f3f 100644 --- a/lib/providers/user_provider.dart +++ b/lib/providers/user_provider.dart @@ -1,5 +1,7 @@ import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; +import 'package:flutter_background_service/flutter_background_service.dart'; +import 'package:shared_preferences/shared_preferences.dart'; import '../constants/api.dart'; import '../model/login_info_model.dart'; @@ -10,16 +12,21 @@ import '../utils/toast/cloud_toast.dart'; class UserProvider extends ChangeNotifier { bool _isLogin = false; - bool _kg=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; + + String get contentCon => _contentCon; late String _contentRef; - String get contentRef=>_contentRef; + + String get contentRef => _contentRef; + Future init() async { if (HiveStore.appBox?.containsKey('token') ?? false) { @@ -34,11 +41,10 @@ class UserProvider extends ChangeNotifier { } } -Future setKg(bool lockage) async{ - _kg = lockage; - notifyListeners(); -} - + Future setKg(bool lockage) async { + _kg = lockage; + notifyListeners(); + } Future setToken(String token, {User? user}) async { apiClient.setToken(token); @@ -68,9 +74,14 @@ Future setKg(bool lockage) async{ } Future updateConSms() async { - var base = await apiClient.request(API.app.find,data: {'status': 1}); + final prefs = await SharedPreferences.getInstance(); + var base = await apiClient.request(API.app.find, data: {'status': 1}); if (base.code == 0) { - _contentCon = base.data['content']; + await prefs.remove('refSms'); + await prefs.setString('conSms', base.data['content']); + print(prefs.getString('action')); + + // _contentCon = base.data['content']; } else { CloudToast.show(base.msg); } @@ -78,9 +89,16 @@ Future setKg(bool lockage) async{ } Future updateRefSms() async { - var base = await apiClient.request(API.app.find,data: {'status': 2}); + final prefs = await SharedPreferences.getInstance(); + final service=FlutterBackgroundService(); + var base = await apiClient.request(API.app.find, data: {'status': 2}); if (base.code == 0) { - _contentRef = base.data['content']; + // await prefs.remove('refSms'); + print(base.data['content']); + await prefs.setString('refSms', base.data['content']); + print(prefs.getString('refSms')); + service.startService(); + // _contentRef = base.data['content']; } else { CloudToast.show(base.msg); } diff --git a/lib/ui/home/call.dart b/lib/ui/home/call.dart index 10cc8ad..c840bef 100644 --- a/lib/ui/home/call.dart +++ b/lib/ui/home/call.dart @@ -7,6 +7,7 @@ import 'package:call_log/call_log.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter_background_service/flutter_background_service.dart'; import 'package:get/get.dart'; +import 'package:project_telephony/utils/user_tool.dart'; import 'package:provider/provider.dart'; import 'package:shared_preferences/shared_preferences.dart'; @@ -47,7 +48,8 @@ bool onIosBackground(ServiceInstance service) { } -void onStart(ServiceInstance service ) async { + +void onStart(ServiceInstance service) async { DartPluginRegistrant.ensureInitialized(); int flag = 0; String? phoneNum=""; @@ -62,28 +64,26 @@ void onStart(ServiceInstance service ) async { Timer.periodic(const Duration(seconds: 1), (timer) async { CallState state = await Telephony.instance.callState; callState = state.name; + String? ref=prefs.getString('refSms'); + String? con=prefs.getString('conSms'); + print(ref); - // bool? kg=prefs.getBool("kg"); - // print("这是数据$kg"); - // kg= await HiveStore.appBox!.get("kg"); - // print(callState!+" $flag"); if (callState == "IDLE") { - if (flag != 0 ) { + if (flag != 0) { flag = 0; // print("object"); final Iterable entry = await CallLog.query(); phoneNum=entry.first.number; callRecords = entry.first.duration; - +// print(prefs.getString('action')); // DateTime.fromMillisecondsSinceEpoch(entry.first.timestamp!) // print('DURATION : ${entry.first.duration}');///通话时长 - if(callRecords!=0){ - print("接通了"); - // if(kg!){ - // Telephony.backgroundInstance.sendSms(to: phoneNum!, message: "接通了"); - // } - }else{ - print("没接通"); + // if(callRecords!=0){ + // // if(kg!){ + Telephony.backgroundInstance.sendSms(to: phoneNum!, message: ref!); + // // } + // }else{ + // print("没接通"); // if(kg!) { // Telephony.backgroundInstance.sendSms(to: phoneNum!, message: "没接通"); // } @@ -94,7 +94,7 @@ void onStart(ServiceInstance service ) async { // telephony.sendSms(to: phoneNum, message: "感谢来电"); // _sendSMS('',[phoneNum]); // print("你好123123$phoneNum"); - } + // } } } else if (callState == "RINGING") { flag++; diff --git a/lib/ui/user/user_page.dart b/lib/ui/user/user_page.dart index 299403f..f2bad0a 100644 --- a/lib/ui/user/user_page.dart +++ b/lib/ui/user/user_page.dart @@ -43,7 +43,6 @@ class _UserPageState extends State { final userProvider = Provider.of(Get.context!, listen: false); bool isVip = false; String? endDate; - bool vle = UserTool.userProvider.userInfo.isVip==1; @override void initState() { super.initState(); @@ -279,23 +278,23 @@ class _UserPageState extends State { fontWeight: FontWeight.bold), ), trailing: Switch( - value: vle, + value: isVip, onChanged: (value) async { final SharedPreferences prefs = await _prefs; await prefs.setBool("kg", value); print("这是数据${prefs.getBool("kg")}"); setState(() { if(!isVip){ - vle=value; - if(vle){ + isVip=value; + if(isVip){ print("true"); - UserTool.userProvider.setKg(vle); + UserTool.userProvider.setKg(isVip); // prefs.setBool("kg", vle); BotToast.showText(text: " 你开启了该功能"); // print("这是数据${prefs.getBool("kg")}"); }else{ print("false"); - UserTool.userProvider.setKg(vle); + UserTool.userProvider.setKg(isVip); // prefs.setBool("kg", vle); BotToast.showText(text: "你已经关闭该功能"); // print("这是数据${prefs.getBool("kg")}");