| | |
| | | import com.example.firstapp.core.Core |
| | | import com.example.firstapp.database.entity.Code |
| | | import com.example.firstapp.database.entity.Msg |
| | | import com.example.firstapp.database.repository.KeywordRepository |
| | | import com.example.firstapp.database.service.RetrofitClient |
| | | import com.example.firstapp.entity.Rule |
| | | import kotlinx.coroutines.CoroutineScope |
| | | import kotlinx.coroutines.Dispatchers |
| | | import kotlinx.coroutines.launch |
| | | import java.text.SimpleDateFormat |
| | | import java.time.LocalDateTime |
| | | import java.time.ZoneId |
| | | import java.time.format.DateTimeFormatter |
| | | import java.util.Date |
| | | import java.util.Locale |
| | | |
| | |
| | | |
| | | @RequiresApi(Build.VERSION_CODES.O) |
| | | override fun onReceive(context: Context, intent: Intent) { |
| | | |
| | | // 检查广播的 Action 是否为短信接收 |
| | | if (Telephony.Sms.Intents.SMS_RECEIVED_ACTION == intent.action) { |
| | | |
| | | CoroutineScope(Dispatchers.IO).launch { |
| | | try { |
| | | val response = RetrofitClient.apiService.getSecurityList(); |
| | | // 这里需要将response.data存放到变量中 |
| | | if(response.code===200){ |
| | | securityKeywordsList = response.data.map { it.keyword } |
| | | Log.d("SmsReceiver", "securityKeywordsList: $securityKeywordsList") |
| | | } |
| | | }catch (e: Exception){ |
| | | Log.d("SmsReceiver", "Error: ${e.message}") |
| | | } |
| | | } |
| | | |
| | | // 获取短信内容 |
| | | val bundle: Bundle? = intent.extras |
| | | bundle?.let { |
| | | val pdus = it.get("pdus") as Array<*> |
| | |
| | | messageBody.append(messages[i]?.messageBody) |
| | | } |
| | | |
| | | // 输出短信内容到控制台 |
| | | val msg = Msg(0, "1111", "111111", messageBody.toString(),1, "111", 1, 1) |
| | | // 保存原始短信 |
| | | val msg = Msg(0, "1111", "111111", messageBody.toString(), 1, "111", 1, 1) |
| | | val msgId = Core.msg.insert(msg) |
| | | |
| | | // 这里需要查看消息是否含有securityKeywordsList这个列表中的关键词,如果含有,则不进行下一步操作 |
| | |
| | | |
| | | Log.d("SmsReceiver", "运行到正则匹配") |
| | | |
| | | // 这里我要写个数组,并创建个对象存放一些内容,如这个对象的属性有匹配内容,正则表达式,并循环遍历 |
| | | val ruleList = mutableListOf( |
| | | Rule("快递","京东","\\d{6}"), |
| | | Rule("快递","菜鸟驿站","\\d{1,2}-\\d{1,2}-\\d{4}"), |
| | | // 银行规则使用默认正则,实际匹配时会使用 BANK_PATTERNS 中的模式 |
| | | Rule("财务", "中国银行", "账单金额[::](\\d+\\.?\\d*).*还款日[::](\\d{1,2})日"), |
| | | Rule("财务", "工商银行", "账单金额[::](\\d+\\.?\\d*).*还款日[::](\\d{1,2})日"), |
| | | Rule("财务", "建设银行", "账单金额[::](\\d+\\.?\\d*).*还款日[::](\\d{1,2})日"), |
| | | Rule("财务", "信用卡", "账单[¥¥](\\d+\\.?\\d*).*还款日(\\d{2})月(\\d{2})日"), |
| | | Rule("财务", "花呗", "本月花呗账单(\\d+\\.?\\d*)元.*还款日[期是::](\\d{1,2})[日号]") |
| | | ) |
| | | |
| | | // 调用API处理短信 |
| | | CoroutineScope(Dispatchers.IO).launch { |
| | | Log.d("SmsReceiver", "CoroutineScope started") |
| | | // 获取最新的关键词配置 |
| | | val keywords = Core.keyword.getKeywords() |
| | | Log.d("keywords", keywords.toString()) |
| | | keywords.forEach { keyword -> |
| | | ruleList.add( |
| | | Rule( |
| | | keyword.type, |
| | | keyword.keyword, |
| | | "\\d{1,2}-\\d{1,2}-\\d{4}" |
| | | ) |
| | | ) |
| | | } |
| | | Log.d("RuleList", ruleList.toString()) |
| | | try { |
| | | val response = |
| | | RetrofitClient.apiService.processSms(mapOf("content" to messageBody.toString())) |
| | | |
| | | for (rule in ruleList) { |
| | | val code = if (rule.type == "财务") { |
| | | // 对信用卡账单使用特殊的提取逻辑 |
| | | val regex = rule.pattern.toRegex() |
| | | val matchResult = regex.find(messageBody.toString()) |
| | | matchResult?.let { |
| | | val amount = it.groupValues[1] // 账单金额 |
| | | amount |
| | | } |
| | | } else { |
| | | rule.extractCodeFromMessage(messageBody.toString()) |
| | | } |
| | | |
| | | if (code !== null) { |
| | | if (response.status == "success") { |
| | | // 获取当前时间 |
| | | val currentTime = LocalDateTime.now() |
| | | val date = Date.from(currentTime.atZone(ZoneId.systemDefault()).toInstant()) |
| | | val date = |
| | | Date.from(currentTime.atZone(ZoneId.systemDefault()).toInstant()) |
| | | val sdf = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()) |
| | | val createtime = sdf.format(date) |
| | | |
| | | // 对于信用卡账单,使用还款日作为time字段 |
| | | val time = if (rule.type == "财务") { |
| | | rule.extractDueDate(messageBody.toString()) ?: createtime |
| | | } else { |
| | | createtime |
| | | // 根据不同类型处理数据 |
| | | when (response.data.category) { |
| | | "快递" -> { |
| | | val code = Code( |
| | | id = 0, |
| | | category = response.data.category, |
| | | categoryId = 1, // 1-快递类型 |
| | | typeId = 1, //暂时没有根据type分类 |
| | | ruleId = 1, //1-快递类型 |
| | | msgId = msgId, |
| | | createTime = createtime, |
| | | oneLevel = response.data.details.post ?: "", |
| | | secondLevel = response.data.details.company ?: "", |
| | | code = response.data.details.pickupCode ?: "", |
| | | pickup = 0, // 0-未取件,1-已取件 |
| | | pickupTime = "", // 取件时间为空 |
| | | overTime = "", // 超时时间为空,暂时没有这块处理逻辑 |
| | | address = response.data.details.address ?: "", |
| | | remarks = response.data.details.time ?: "", |
| | | ) |
| | | if(code.oneLevel!="" && code.secondLevel!="" && code.code!="") { |
| | | Core.code.insert(code) |
| | | } |
| | | } |
| | | |
| | | "还款" -> { |
| | | val code = Code( |
| | | id = 0, |
| | | category = response.data.category, |
| | | categoryId = 2, // 2-还款类型 |
| | | typeId = 1, //暂时没有根据type分类 |
| | | ruleId = 2, //1-还款类型 |
| | | msgId = msgId, |
| | | createTime = createtime, |
| | | oneLevel = response.data.details.type ?: "", |
| | | secondLevel = response.data.details.bank ?: "", |
| | | code = response.data.details.amount ?: "", |
| | | pickup = 0, // 0-未取件,1-已取件 |
| | | pickupTime = "", // 取件时间为空 |
| | | overTime = response.data.details.date |
| | | ?: "", // 超时时间为空,暂时没有这块处理逻辑 |
| | | address = response.data.details.address ?: "", |
| | | remarks = "最小还款金额" + response.data.details.min_amount + "还款卡号" + response.data.details.number |
| | | ?: "", |
| | | ) |
| | | if(code.oneLevel!="" && code.secondLevel!="" && code.code!="") { |
| | | Core.code.insert(code) |
| | | } |
| | | } |
| | | |
| | | "收入" -> { |
| | | val code = Code( |
| | | id = 0, |
| | | category = response.data.category, |
| | | categoryId = 3, // 3-收入类型 |
| | | typeId = 1, //暂时没有根据type分类 |
| | | ruleId = 2, //1-还款类型 |
| | | msgId = msgId, |
| | | createTime = createtime, |
| | | oneLevel = response.data.details.bank ?: "", |
| | | secondLevel = response.data.details.bank ?: "", |
| | | code = response.data.details.amount ?: "", |
| | | pickup = 0, // 0-未取件,1-已取件 |
| | | pickupTime = "", // 取件时间为空 |
| | | overTime = response.data.details.datetime |
| | | ?: "", // 超时时间为空,暂时没有这块处理逻辑 |
| | | address = response.data.details.address ?: "", |
| | | remarks = "余额" + response.data.details.balance ?: "", |
| | | ) |
| | | if(code.oneLevel!="" && code.secondLevel!="" && code.code!="") { |
| | | Core.code.insert(code) |
| | | } |
| | | } |
| | | } |
| | | |
| | | val existingCode = Core.code.queryByTypeAndCodeAndDate(rule.content, code, createtime) |
| | | if (existingCode == null) { |
| | | val codeEntity = Code( |
| | | 0, |
| | | rule.type, |
| | | 1, |
| | | rule.content, |
| | | 1, |
| | | 1, |
| | | msgId, |
| | | code, |
| | | createtime, |
| | | rule.content, // 银行名称作为name |
| | | 0, |
| | | time, // 还款日期 |
| | | "" |
| | | ) |
| | | Core.code.insert(codeEntity) |
| | | Log.d("SMS_DEBUG", "新短信已保存到数据库") |
| | | |
| | | // 发送广播通知数据已更新 |
| | | //"com.example.firstapp.DATA_UPDATED" 是一个自定义的广播 Action,相当于一个标识符或者说是一个频道名称。这个名称是我们自己定义的,通常使用应用的包名作为前缀,以避免与其他应用的广播冲突。 |
| | | val updateIntent = Intent("com.example.firstapp.DATA_UPDATED") |
| | | context.sendBroadcast(updateIntent) |
| | | Log.d("SMS_DEBUG", "发送数据更新广播") |
| | | }else{ |
| | | Log.d("SmsReceiver", "Received SMS code: 已存在相同记录,不保存") |
| | | } |
| | | }else{ |
| | | Log.d("SmsReceiver", "Received SMS code: 没有匹配到内容") |
| | | // 发送广播通知数据已更新 |
| | | val updateIntent = Intent("com.example.firstapp.DATA_UPDATED") |
| | | context.sendBroadcast(updateIntent) |
| | | Log.d("SMS_DEBUG", "新短信已保存到数据库") |
| | | } |
| | | } catch (e: Exception) { |
| | | Log.e("SmsReceiver", "Error processing SMS", e) |
| | | } |
| | | } |
| | | |
| | | |
| | | // kotlin 怎么创建一个类 |
| | | // for (rule in ruleList) { |
| | | // val code = rule.extractCodeFromMessage(messageBody.toString()) |
| | | // |
| | | // if (code!==null) { |
| | | // Log.d("SmsReceiver", "Received SMS code: ${code}") |
| | | // |
| | | // |
| | | // // 获取当前时间 |
| | | // val currentTime = LocalDateTime.now() |
| | | // // 加2小时 |
| | | // val futureTime = currentTime.plusHours(2) |
| | | // // 定义时间格式 |
| | | // val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss") |
| | | // // 转换为字符串 |
| | | // val overtime = futureTime.format(formatter) |
| | | // // 封装成一个Code对象,并保存在数据库中 |
| | | // val code = Code(0, rule.type,1, rule.content,1, 1, msgId, code, overtime) |
| | | // Core.code.insert(code) |
| | | // Log.d("SMS_DEBUG", "新短信已保存到数据库") |
| | | // // 发送广播通知数据已更新 |
| | | // //"com.example.firstapp.DATA_UPDATED" 是一个自定义的广播 Action,相当于一个标识符或者说是一个频道名称。这个名称是我们自己定义的,通常使用应用的包名作为前缀,以避免与其他应用的广播冲突。 |
| | | // val updateIntent = Intent("com.example.firstapp.DATA_UPDATED") |
| | | // context.sendBroadcast(updateIntent) |
| | | // Log.d("SMS_DEBUG", "发送数据更新广播") |
| | | // }else{ |
| | | // Log.d("SmsReceiver", "Received SMS code: 没有匹配到内容") |
| | | // } |
| | | // } |
| | | |
| | | } |
| | | } |
| | | } |