mirror of
https://github.com/casdoor/casdoor.git
synced 2025-07-03 04:10:20 +08:00
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:
@ -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)
|
||||
}
|
||||
|
@ -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 {
|
||||
|
@ -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)
|
||||
}
|
Reference in New Issue
Block a user