feat: support multiple country codes for sending SMS (#1557)

* feat: support multiple country code

* feat: improve UI

* feat: migrate accountItem

* fix: Aliyun compatible

* fix: phone validate

* fix: typo
This commit is contained in:
Yaodong Yu
2023-02-16 22:53:28 +08:00
committed by GitHub
parent 4605938f8e
commit eb72c9f273
52 changed files with 560 additions and 258 deletions

View File

@ -14,6 +14,8 @@
package util
import "sort"
func DeleteVal(values []string, val string) []string {
newValues := []string{}
for _, v := range values {
@ -23,3 +25,8 @@ func DeleteVal(values []string, val string) []string {
}
return newValues
}
func ContainsString(values []string, val string) bool {
sort.Strings(values)
return sort.SearchStrings(values, val) != len(values)
}

View File

@ -227,7 +227,7 @@ func IsChinese(str string) bool {
}
func GetMaskedPhone(phone string) string {
return getMaskedPhone(phone)
return rePhone.ReplaceAllString(phone, "$1****$2")
}
func GetMaskedEmail(email string) string {

View File

@ -17,16 +17,13 @@ package util
import (
"net/mail"
"regexp"
"github.com/nyaruka/phonenumbers"
)
var (
rePhoneCn *regexp.Regexp
rePhone *regexp.Regexp
)
var rePhone *regexp.Regexp
func init() {
// https://learnku.com/articles/31543
rePhoneCn, _ = regexp.Compile(`^1(3\d|4[5-9]|5[0-35-9]|6[2567]|7[0-8]|8\d|9[0-35-9])\d{8}$`)
rePhone, _ = regexp.Compile("(\\d{3})\\d*(\\d{4})")
}
@ -35,10 +32,19 @@ func IsEmailValid(email string) bool {
return err == nil
}
func IsPhoneCnValid(phone string) bool {
return rePhoneCn.MatchString(phone)
func IsPhoneValid(phone string, countryCode string) bool {
phoneNumber, err := phonenumbers.Parse(phone, countryCode)
if err != nil {
return false
}
return phonenumbers.IsValidNumber(phoneNumber)
}
func getMaskedPhone(phone string) string {
return rePhone.ReplaceAllString(phone, "$1****$2")
func IsPhoneAllowInRegin(countryCode string, allowRegions []string) bool {
return !ContainsString(allowRegions, countryCode)
}
func GetE164Number(phone string, countryCode string) (string, bool) {
phoneNumber, _ := phonenumbers.Parse(phone, countryCode)
return phonenumbers.Format(phoneNumber, phonenumbers.E164), phonenumbers.IsValidNumber(phoneNumber)
}