在这里插入图片描述

📚 概述

本案例深入探讨了在 Kotlin Multiplatform (KMP) 项目中实现电话号码验证工具的完整流程。通过将 Kotlin 代码编译为 JavaScript,并在 OpenHarmony 的 ArkTS 中调用,我们展示了如何充分利用 Kotlin 的特性来进行电话号码格式识别、国际化支持和有效性检查。

电话号码验证是应用开发的重要功能,允许我们验证用户输入的电话号码、识别号码格式、支持国际化。在 KMP 项目中,我们可以利用这些特性来构建具有强大电话号码处理能力的应用。

本文将详细介绍如何在 KMP 项目中实现电话号码格式识别、国际化支持、有效性检查等核心概念。

🎯 核心概念

1. 中国手机号码识别 (China Mobile Number Recognition)

识别中国手机号码格式。

val chinaPattern = Regex("^1[3-9]\\d{9}$")

when {
    chinaPattern.matches(cleanInput) -> {
        phoneFormat = "中国手机"
        isValid = true
        country = "中国"
        phoneType = "移动"
        when {
            cleanInput.startsWith("13") -> phoneType = "移动"
            cleanInput.startsWith("14") -> phoneType = "联通"
            cleanInput.startsWith("15") -> phoneType = "移动"
            cleanInput.startsWith("16") -> phoneType = "中国联通"
            cleanInput.startsWith("17") -> phoneType = "电信"
            cleanInput.startsWith("18") -> phoneType = "移动"
            cleanInput.startsWith("19") -> phoneType = "电信"
        }
    }
}

代码解释:

  • 正则表达式验证中国手机号码格式
  • 识别运营商(移动、联通、电信)
  • 根据首位数字判断运营商类型

2. 座机号码识别 (Landline Number Recognition)

识别座机号码格式。

val landlinePattern = Regex("^0\\d{2,3}-?\\d{7,8}$")

landlinePattern.matches(cleanInput) -> {
    phoneFormat = "座机"
    isValid = true
    country = "中国"
    phoneType = "固定电话"
}

代码解释:

  • 验证座机号码格式
  • 支持带或不带分隔符
  • 识别为固定电话类型

3. E.164 国际格式识别 (E.164 International Format Recognition)

识别 E.164 国际标准格式。

val e164Pattern = Regex("^\\+[1-9]\\d{1,14}$")

e164Pattern.matches(cleanInput) -> {
    phoneFormat = "E.164"
    isValid = true
    country = "国际"
    phoneType = "国际格式"
    val countryCode = cleanInput.substring(1, 3)
    country = when (countryCode) {
        "1" -> "美国/加拿大"
        "44" -> "英国"
        "33" -> "法国"
        "49" -> "德国"
        "86" -> "中国"
        else -> "其他国家"
    }
}

代码解释:

  • 验证 E.164 格式(+开头)
  • 提取国家代码
  • 识别国家/地区

4. 国际格式识别 (International Format Recognition)

识别通用国际格式。

val internationalPattern = Regex("^\\+?[1-9]\\d{1,14}$")

internationalPattern.matches(cleanInput) -> {
    phoneFormat = "国际格式"
    isValid = true
    country = "国际"
    phoneType = "国际号码"
}

代码解释:

  • 验证国际格式
  • 支持可选的 + 前缀
  • 识别为国际号码

5. 信任度评分 (Trust Score Calculation)

计算电话号码的信任度。

var trustScore = 0
if (isValid) trustScore += 50
if (phoneFormat != "未知") trustScore += 30
if (cleanInput.isNotEmpty()) trustScore += 20

代码解释:

  • 有效格式:50分
  • 已识别格式:30分
  • 非空输入:20分

💡 实现代码详解

Kotlin 源代码

fun phoneNumberTool(phoneInput: String): String {
    return try {
        val cleanInput = phoneInput.trim()
        
        if (cleanInput.isEmpty()) {
            return "❌ 输入为空"
        }
        
        // 定义电话号码格式模式
        val chinaPattern = Regex("^1[3-9]\\d{9}$")
        val internationalPattern = Regex("^\\+?[1-9]\\d{1,14}$")
        val landlinePattern = Regex("^0\\d{2,3}-?\\d{7,8}$")
        val e164Pattern = Regex("^\\+[1-9]\\d{1,14}$")
        
        var phoneFormat = "未知"
        var isValid = false
        var country = "未知"
        var phoneType = "未知"
        
        // 识别电话号码格式
        when {
            chinaPattern.matches(cleanInput) -> {
                phoneFormat = "中国手机"
                isValid = true
                country = "中国"
                phoneType = "移动"
                when {
                    cleanInput.startsWith("13") -> phoneType = "移动"
                    cleanInput.startsWith("14") -> phoneType = "联通"
                    cleanInput.startsWith("15") -> phoneType = "移动"
                    cleanInput.startsWith("16") -> phoneType = "中国联通"
                    cleanInput.startsWith("17") -> phoneType = "电信"
                    cleanInput.startsWith("18") -> phoneType = "移动"
                    cleanInput.startsWith("19") -> phoneType = "电信"
                }
            }
            landlinePattern.matches(cleanInput) -> {
                phoneFormat = "座机"
                isValid = true
                country = "中国"
                phoneType = "固定电话"
            }
            e164Pattern.matches(cleanInput) -> {
                phoneFormat = "E.164"
                isValid = true
                country = "国际"
                phoneType = "国际格式"
                val countryCode = cleanInput.substring(1, 3)
                country = when (countryCode) {
                    "1" -> "美国/加拿大"
                    "44" -> "英国"
                    "33" -> "法国"
                    "49" -> "德国"
                    "86" -> "中国"
                    else -> "其他国家"
                }
            }
            internationalPattern.matches(cleanInput) -> {
                phoneFormat = "国际格式"
                isValid = true
                country = "国际"
                phoneType = "国际号码"
            }
            else -> {
                phoneFormat = "未知"
                isValid = false
            }
        }
        
        // 计算信任度
        var trustScore = 0
        if (isValid) trustScore += 50
        if (phoneFormat != "未知") trustScore += 30
        if (cleanInput.isNotEmpty()) trustScore += 20
        
        // 返回结果
        val status = if (isValid) "✅ 有效" else "❌ 无效"
        
        return """
            $status
            ━━━━━━━━━━━━━━━━━━━━━━━━━
            格式: $phoneFormat
            输入: $cleanInput
            国家/地区: $country
            号码类型: $phoneType
            长度: ${cleanInput.length} 位
            信任度: $trustScore/100
        """.trimIndent()
        
    } catch (e: Exception) {
        "❌ 验证失败: ${e.message}"
    }
}

🔍 支持的电话号码格式

  • 中国手机: 1[3-9]xxxxxxxxx
  • 座机: 0xx-xxxxxxx 或 0xxx-xxxxxxxx
  • E.164: +[1-9]xxxxxxxxxx
  • 国际格式: +[1-9]xxxxxxxxxx

📝 总结

Kotlin 的电话号码验证工具提供了强大的功能。通过在 KMP 项目中使用这些特性,我们可以:

  1. 格式识别:自动识别电话号码格式
  2. 国际化支持:支持多个国家的电话号码格式
  3. 运营商识别:识别中国手机运营商
  4. 有效性检查:验证电话号码有效性
  5. 简化显示:只显示关键信息

电话号码验证是应用开发的重要功能,掌握这些技能对于编写安全、可靠的代码至关重要。

欢迎加入开源鸿蒙跨平台社区:https://openharmonycrossplatform.csdn.net

Logo

开源鸿蒙跨平台开发社区汇聚开发者与厂商,共建“一次开发,多端部署”的开源生态,致力于降低跨端开发门槛,推动万物智联创新。

更多推荐