新增发送周期

master
戴余标 2 years ago
parent 306a273a8c
commit a7ec1b63c2

@ -0,0 +1,103 @@
import 'dart:io';
import 'package:flustars/flustars.dart';
import 'package:path_provider/path_provider.dart';
import 'package:project_telephony/model/send_number_model.dart';
import 'package:sqflite/sqflite.dart';
import 'package:path/path.dart';
class DBManager {
///
final String _dbName = "dbName";
///
final int _version = 1;
static final DBManager _instance = DBManager._();
factory DBManager() {
return _instance;
}
DBManager._();
static Database? _db;
Future<Database> get db async {
return _db ??= await _initDB();
}
///
Future<Database> _initDB() async {
Directory directory = await getApplicationDocumentsDirectory();
String path = join(directory.path, _dbName);
return await openDatabase(
path,
version: _version,
onCreate: _onCreate,
onUpgrade: _onUpgrade,
);
}
///
Future _onCreate(Database db, int version) async {
const String sql = """
CREATE TABLE SendNumber(
id INTEGER primary key AUTOINCREMENT,
sendNumber TEXT,
createdAt datetime
)
""";
return await db.execute(sql);
}
///
Future _onUpgrade(Database db, int oldVersion, int newVersion) async {}
///
Future saveData(SendNumber sendNumber) async {
Database database = await db;
return await database.insert("SendNumber", sendNumber.toJson());
}
/// 使SQL
Future saveDataBySQL(SendNumber sendNumber) async {
const String sql = """
INSERT INTO SendNumber(sendNumber,createdAt) values(?,?)
""";
Database database = await db;
return await database.rawInsert(sql, [sendNumber.sendNumber, sendNumber.createdAt]);
}
///
Future<List<SendNumber>?> findAll() async {
Database? database = await db;
List<Map<String, Object?>> result = await database.query("SendNumber");
if (result.isNotEmpty) {
return result.map((e) => SendNumber.fromJson(e)).toList();
} else {
return [];
}
}
///
Future<List<SendNumber>?> find(String sendNumber) async {
Database database = await db;
List<Map<String, Object?>> result =
await database.query("SendNumber", where: "sendNumber=?", whereArgs: [sendNumber]);
if (result.isNotEmpty) {
return result.map((e) => SendNumber.fromJson(e)).toList();
} else {
return [];
}
}
///
Future<int> update(SendNumber sendNumber) async {
Database database = await db;
sendNumber.createdAt = DateUtil.formatDate(DateTime.now());
int count =
await database.update("SendNumber", sendNumber.toJson(), where: "sendNumber=?", whereArgs: [sendNumber.sendNumber]);
return count;
}
}

@ -31,6 +31,7 @@ class API {
static _Pay pay = _Pay(); static _Pay pay = _Pay();
static _Exclude exclude = _Exclude(); static _Exclude exclude = _Exclude();
static _Content content = _Content(); static _Content content = _Content();
static _Send send = _Send();
} }
class _App { class _App {
@ -56,16 +57,16 @@ class _App {
String get trialVip => '/app/user/trialVip'; String get trialVip => '/app/user/trialVip';
/// ///
String get updateSelect =>'/app/update/select'; String get updateSelect => '/app/update/select';
/// ///
String get updateAdd => '/manage/update/add'; String get updateAdd => '/manage/update/add';
/// ///
String get psdAdd=>'/app/register'; String get psdAdd => '/app/register';
/// ///
String get psdLogin=>'/app/loginByPwd'; String get psdLogin => '/app/loginByPwd';
} }
class _Content { class _Content {
@ -122,5 +123,13 @@ class _Exclude {
String get find => '/app/exclude/find'; String get find => '/app/exclude/find';
/// ///
String get findAll =>'/app/exclude/findAll'; String get findAll => '/app/exclude/findAll';
}
class _Send {
///
String get add => '/app/send/add';
///
String get select => '/send/select';
} }

@ -0,0 +1,27 @@
import 'dart:convert';
SendNumber sendNumberFromJson(String str) => SendNumber.fromJson(json.decode(str));
String sendNumberToJson(SendNumber data) => json.encode(data.toJson());
class SendNumber {
SendNumber({
this.sendNumber,
this.createdAt,
});
SendNumber.fromJson(dynamic json) {
sendNumber = json['sendNumber'];
createdAt = json['createdAt'];
}
String? sendNumber;
String? createdAt;
Map<String, dynamic> toJson() {
final map = <String, dynamic>{};
map['sendNumber'] = sendNumber;
map['createdAt'] = createdAt;
return map;
}
}

@ -2,29 +2,34 @@ import 'dart:async';
import 'dart:ui'; import 'dart:ui';
import 'package:call_log/call_log.dart'; import 'package:call_log/call_log.dart';
import 'package:flustars/flustars.dart';
import 'package:flutter/cupertino.dart'; import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_background_service/flutter_background_service.dart'; import 'package:flutter_background_service/flutter_background_service.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart'; import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:project_telephony/model/send_number_model.dart';
import 'package:shared_preferences/shared_preferences.dart'; import 'package:shared_preferences/shared_preferences.dart';
import 'package:telephony/telephony.dart'; import 'package:telephony/telephony.dart';
import '../../base/DBManager.dart';
import '../user/user_page.dart';
const nId = "my_foreground"; const nId = "my_foreground";
const notificationId = 888; const notificationId = 888;
Future<void> initializeService() async { Future<void> initializeService() async {
final service = FlutterBackgroundService(); final service = FlutterBackgroundService();
const AndroidNotificationChannel channel = AndroidNotificationChannel( const AndroidNotificationChannel channel = AndroidNotificationChannel(
nId, // id nId, // id
'我的前台服务', // title '我的前台服务', // title
description: description: '此通道用于重要通知。', // description
'此通道用于重要通知。', // description
importance: Importance.low, // importance must be at low or higher level importance: Importance.low, // importance must be at low or higher level
); );
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin(); FlutterLocalNotificationsPlugin();
await flutterLocalNotificationsPlugin await flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation< .resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>() AndroidFlutterLocalNotificationsPlugin>()
?.createNotificationChannel(channel); ?.createNotificationChannel(channel);
await service.configure( await service.configure(
iosConfiguration: IosConfiguration( iosConfiguration: IosConfiguration(
@ -43,7 +48,8 @@ Future<void> initializeService() async {
autoStart: true, autoStart: true,
isForegroundMode: true, isForegroundMode: true,
notificationChannelId: nId, // this must match with notification channel you created above. notificationChannelId: nId,
// this must match with notification channel you created above.
initialNotificationTitle: 'AWESOME SERVICE', initialNotificationTitle: 'AWESOME SERVICE',
initialNotificationContent: 'Initializing', initialNotificationContent: 'Initializing',
foregroundServiceNotificationId: notificationId, foregroundServiceNotificationId: notificationId,
@ -62,7 +68,7 @@ bool onIosBackground(ServiceInstance service) {
void onStart(ServiceInstance service) async { void onStart(ServiceInstance service) async {
DartPluginRegistrant.ensureInitialized(); DartPluginRegistrant.ensureInitialized();
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin(); FlutterLocalNotificationsPlugin();
service.on("stopService").listen((event) { service.on("stopService").listen((event) {
service.stopSelf(); service.stopSelf();
}); });
@ -70,6 +76,7 @@ void onStart(ServiceInstance service) async {
int flag = 0; int flag = 0;
String? phoneNum = ""; String? phoneNum = "";
String callState; String callState;
bool fff=false;
Timer.periodic(const Duration(seconds: 1), (timer) async { Timer.periodic(const Duration(seconds: 1), (timer) async {
// //
@ -77,97 +84,113 @@ void onStart(ServiceInstance service) async {
// //
CallState state = await Telephony.instance.callState; CallState state = await Telephony.instance.callState;
callState = state.name; callState = state.name;
List<String> noNumberList = prefs.getStringList("specified") ??[]; List<String> noNumberList = prefs.getStringList("specified") ?? [];
flutterLocalNotificationsPlugin.show( flutterLocalNotificationsPlugin.show(
notificationId, notificationId,
'短信帮手', '短信帮手',
'$callState ${DateTime.now().second}', '$callState ${DateTime.now().second}',
const NotificationDetails( const NotificationDetails(
android: AndroidNotificationDetails( android: AndroidNotificationDetails(
nId, nId,
'我的前台服务', '我的前台服务',
icon: 'ic_bg_service_small', icon: 'ic_bg_service_small',
ongoing: true, ongoing: true,
), ),
), ),
); );
// //
String content = prefs.getString("refSms") ?? "现在有事,等会回电"; String content = prefs.getString("refSms") ?? "现在有事,等会回电";
// //
int? numberSet = prefs.getInt("numIndex") ?? 1; int? numberSet = prefs.getInt("numIndex") ?? 1;
int? dayTimes =prefs.getInt("dayTimes")??0;
print("号码设置$numberSet 发送内容$content 指定不发送$noNumberList"); print("号码设置$numberSet 发送内容$content 指定不发送$noNumberList");
// print(flag); // print(flag);
if (callState == "IDLE") { if (callState == "IDLE") {
if (flag != 0) { if (flag != 0) {
print(dayTimes);
final Iterable<CallLogEntry> entry = await CallLog.query(); final Iterable<CallLogEntry> entry = await CallLog.query();
phoneNum = entry.first.number; phoneNum = entry.first.number;
// print(phoneNum); var phone = SendNumber(
switch (numberSet) { sendNumber: phoneNum,
case 0: createdAt: DateUtil.formatDate(DateTime.now()),
);
if(!noNumberList.contains(phoneNum)){ var s = await DBManager().find(phoneNum!);
print("所有都发+号码设置$numberSet+发送内容$content"); if (s!.isEmpty) {
Telephony.backgroundInstance await DBManager().saveData(phone);
.sendSms(to: phoneNum!, message: content); fff=true;
flag = 0; }else if(DateTime.parse(s.first.createdAt!)
}else{ .add(Duration(days: dayTimes))
print("指定不发送"); .isBefore(DateTime.now())) {
flag = 0; await DBManager().update(phone);
} fff=true;
break; }
case 1: if(fff){
if (flag > 0) { switch (numberSet) {
// print("来电拒接/未接"); case 0:
// print("${phoneNum!}:${ref!}"); if (!noNumberList.contains(phoneNum)) {
if(!noNumberList.contains(phoneNum)) { print("所有都发+号码设置$numberSet+发送内容$content");
print("来电拒接+号码设置$numberSet+发送内容$content");
Telephony.backgroundInstance
.sendSms(to: phoneNum!, message: content);
flag = 0;
}else{
print("指定不发送");
flag = 0;
}
// print("发送成功");
} else if (flag == -1) {
// print("来电接听");
// print("${phoneNum!}:$content");
if(!noNumberList.contains(phoneNum)) {
print("来电接听+号码设置$numberSet+发送内容$content");
Telephony.backgroundInstance Telephony.backgroundInstance
.sendSms(to: phoneNum!, message: content); .sendSms(to: phoneNum!, message: content);
flag = 0; flag = 0;
}else{ } else {
print("指定不发送"); print("指定不发送");
flag = 0; flag = 0;
} }
} break;
case 1:
break; if (flag > 0) {
case 2: // print("来电拒接/未接");
if(flag==-2){ // print("${phoneNum!}:${ref!}");
if (entry.first.duration! > 0) { if (!noNumberList.contains(phoneNum)) {
// print("${phoneNum!}:$content"); print("来电拒接+号码设置$numberSet+发送内容$content");
if(!noNumberList.contains(phoneNum)) {
print("去电接听+号码设置$numberSet+发送内容$content");
Telephony.backgroundInstance Telephony.backgroundInstance
.sendSms(to: phoneNum!, message: content); .sendSms(to: phoneNum!, message: content);
flag = 0;
} else {
print("指定不发送");
flag = 0;
} }
} else { // print("发送成功");
} else if (flag == -1) {
// print("来电接听");
// print("${phoneNum!}:$content"); // print("${phoneNum!}:$content");
if(!noNumberList.contains(phoneNum)) { if (!noNumberList.contains(phoneNum)) {
print("去电挂断+号码设置$numberSet+发送内容$content"); print("来电接听+号码设置$numberSet+发送内容$content");
Telephony.backgroundInstance Telephony.backgroundInstance
.sendSms(to: phoneNum!, message: content); .sendSms(to: phoneNum!, message: content);
}else{ flag = 0;
} else {
print("指定不发送"); print("指定不发送");
flag = 0; flag = 0;
} }
} }
} break;
flag = 0; case 2:
break; if (flag == -2) {
if (entry.first.duration! > 0) {
// print("${phoneNum!}:$content");
if (!noNumberList.contains(phoneNum)) {
print("去电接听+号码设置$numberSet+发送内容$content");
Telephony.backgroundInstance
.sendSms(to: phoneNum!, message: content);
}
} else {
// print("${phoneNum!}:$content");
if (!noNumberList.contains(phoneNum)) {
print("去电挂断+号码设置$numberSet+发送内容$content");
Telephony.backgroundInstance
.sendSms(to: phoneNum!, message: content);
} else {
print("指定不发送");
flag = 0;
}
}
}
flag = 0;
break;
}
fff=false;
} }
} }
} else if (callState == "RINGING") { } else if (callState == "RINGING") {

@ -76,13 +76,14 @@ class _PhoneSetPageState extends State<PhoneSetPage> {
}, },
]; ];
List cycleList = [ List cycleList = [
'每天发送', '发送无限制',
"一天内发送一次", "一天内发送一次",
"三天内发送一次", "三天内发送一次",
"七天天内发送一次", "七天天内发送一次",
"十五天内发送一次", "十五天内发送一次",
"三十天内发送一次" "三十天内发送一次"
]; ];
List dayTimes=[0,1,3,7,15,30];
String cycleText = ""; String cycleText = "";
@override @override
@ -123,7 +124,7 @@ class _PhoneSetPageState extends State<PhoneSetPage> {
await SharedPreferences.getInstance(); await SharedPreferences.getInstance();
// print(prefs.getInt("numIndex")); // print(prefs.getInt("numIndex"));
select = (prefs.getInt("numIndex")) ?? 0; select = (prefs.getInt("numIndex")) ?? 0;
cycleText = prefs.getString("cycle") ?? "每天发送"; cycleText = prefs.getString("cycle") ?? "发送无限制";
UserTool.userProvider.viewLoading(); UserTool.userProvider.viewLoading();
_getRequests(); _getRequests();
@ -388,6 +389,7 @@ class _PhoneSetPageState extends State<PhoneSetPage> {
BotToast.showText(text: "成功"); BotToast.showText(text: "成功");
prefs.setString( prefs.setString(
"cycle", cycleList[index]); "cycle", cycleList[index]);
prefs.setInt('dayTimes', dayTimes[index]);
BotToast.showText( BotToast.showText(
text: cycleList[index]); text: cycleList[index]);
Navigator.pop(context); Navigator.pop(context);

@ -93,7 +93,7 @@ final FocusNode verifyNode=FocusNode();
filled: true, filled: true,
isDense: true, isDense: true,
fillColor: Colors.transparent , fillColor: Colors.transparent ,
hintText: widget.content.isNotEmpty ? "" : "请输入所需短信", hintText: UserTool.userProvider.userInfo.tag.isNotEmpty ? UserTool.userProvider.userInfo.tag : "请输入所需短信",
hintStyle: TextStyle( hintStyle: TextStyle(
color: widget.content != "" color: widget.content != ""
? const Color(0xFF333333) ? const Color(0xFF333333)
@ -122,7 +122,7 @@ final FocusNode verifyNode=FocusNode();
service.startService(); service.startService();
}); });
} }
// UserTool.userProvider.updateUserInfo(); UserTool.userProvider.updateUserInfo();
// UserTool.userProvider.updateConSms(); // UserTool.userProvider.updateConSms();
Get.back(); Get.back();
} else { } else {

@ -1035,6 +1035,20 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "2.0.3" version: "2.0.3"
sqflite:
dependency: "direct main"
description:
name: sqflite
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.3+1"
sqflite_common:
dependency: transitive
description:
name: sqflite_common
url: "https://pub.dartlang.org"
source: hosted
version: "2.4.0"
stack_trace: stack_trace:
dependency: transitive dependency: transitive
description: description:
@ -1270,4 +1284,4 @@ packages:
version: "3.1.1" version: "3.1.1"
sdks: sdks:
dart: ">=2.17.0 <3.0.0" dart: ">=2.17.0 <3.0.0"
flutter: ">=2.10.0" flutter: ">=3.0.0"

@ -91,9 +91,11 @@ dependencies:
flutter_update_dialog: ^2.0.0 flutter_update_dialog: ^2.0.0
# http # http
dio: ^4.0.6 dio: ^4.0.6
# 版本 # 版本
flutter_xupdate: ^2.0.3 flutter_xupdate: ^2.0.3
path_provider: ^2.0.11 path_provider: ^2.0.11
# 本地数据库
sqflite: ^2.0.3+1
## 生成适配器 ## 生成适配器
# hive_generator: ^1.1.3 # hive_generator: ^1.1.3
# # jdk # # jdk

Loading…
Cancel
Save