fix(notify): add mobile accept ext

This commit is contained in:
马鸿飞
2023-04-26 10:35:56 +08:00
parent 3ad90db0f4
commit 7a05adea2b
3 changed files with 105 additions and 0 deletions
+35
View File
@@ -17,6 +17,7 @@ package notify
import (
"fmt"
"regexp"
"strings"
"yunion.io/x/onecloud/pkg/apis"
)
@@ -74,6 +75,40 @@ func ParseInternationalMobile(mobile string) SInternationalMobile {
}
}
// 支持接受分机号(仅保留数字)
func (im *SInternationalMobile) AcceptExtMobile() {
im.Mobile = moveAreaCode(im.Mobile)
im.Mobile = moveExtStr(im.Mobile)
}
// 对传入的手机号去除地区编号
func moveAreaCode(mobile string) string {
// 所有地区编号
allArea := `283|282|281|280|269|268|267|266|265|264|263|262|261|260|259|258|257|256|255|254|253|252|251|250|249|248|247|246|245|244|243|242|241|240|239|238|237|236|235|234|233|232|231|230|229|228|227|226|225|224|223|222|221|220|219|218|217|216|215|214|213|212|211|210|98|95|94|93|92|91|90|86|84|82|81|66|65|64|63|62|61|60|58|57|56|55|54|53|52|51|49|48|47|46|45|44|43|41|40|39|36|34|33|32|31|30|27|20|7|1`
temp := strings.Split(allArea, "|")
for _, area := range temp {
if strings.HasPrefix(mobile, "+"+area) {
mobile = mobile[1+len(area):]
break
}
}
return mobile
}
// 对传入的手机号去除分机号后缀
func moveExtStr(mobile string) string {
// 定义正则表达式,只保留数字字符串,到不为数字时结束
re := regexp.MustCompile(`^\d+`)
// 匹配正则表达式并获取原号码
match := re.FindStringSubmatch(mobile)
if match != nil {
mobile = match[0]
} else {
mobile = ""
}
return mobile
}
func (im SInternationalMobile) String() string {
if im.AreaCode == "" {
return im.Mobile
+68
View File
@@ -0,0 +1,68 @@
// Copyright 2019 Yunion
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package notify
import "testing"
func TestMobileExt(t *testing.T) {
cases := []struct {
data SInternationalMobile
want string
}{
{
SInternationalMobile{
"+8612345678901",
"+86",
},
"12345678901",
},
{
SInternationalMobile{
"+8612345678901;ext=2",
"+86",
},
"12345678901",
},
{
SInternationalMobile{
"+8812345678901",
"+86",
},
"",
},
{
SInternationalMobile{
"+8612345678901",
"",
},
"12345678901",
},
{
SInternationalMobile{
"+8612345678901;ext=2",
"",
},
"12345678901",
},
}
for _, c := range cases {
temp := c.data
temp.AcceptExtMobile()
if temp.Mobile != c.want {
t.Errorf("reset mobile err,old:%s,new:%s", c.data, temp)
}
}
}
+2
View File
@@ -142,6 +142,7 @@ func (rm *SReceiverManager) ValidateCreateData(ctx context.Context, userCred mcc
return input, httperrors.NewInputParameterError("invalid email")
}
// validate mobile
input.InternationalMobile.AcceptExtMobile()
if ok := LaxMobileRegexp.MatchString(input.InternationalMobile.Mobile); len(input.InternationalMobile.Mobile) > 0 && !ok {
return input, httperrors.NewInputParameterError("invalid mobile")
}
@@ -637,6 +638,7 @@ func (r *SReceiver) ValidateUpdateData(ctx context.Context, userCred mcclient.To
return input, httperrors.NewInputParameterError("invalid email")
}
// validate mobile
input.InternationalMobile.AcceptExtMobile()
if ok := len(input.InternationalMobile.Mobile) == 0 || LaxMobileRegexp.MatchString(input.InternationalMobile.Mobile); !ok {
return input, httperrors.NewInputParameterError("invalid mobile")
}