Skip to content

Commit 7dcfb14

Browse files
authored
Merge pull request #47 from whrss9527/main
2 parents 0193687 + 285a004 commit 7dcfb14

File tree

3 files changed

+110
-1
lines changed

3 files changed

+110
-1
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ fmt:
1111
go fmt ./...
1212

1313
vet:
14-
go vet ./..
14+
go vet ./...
1515

1616
test:
1717
go test ./... -cover -coverprofile=coverage -covermode=atomic -race -v

id_validator_basic.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package idvalidator
2+
3+
import (
4+
"errors"
5+
"strconv"
6+
"time"
7+
8+
"github.com/spf13/cast"
9+
)
10+
11+
// BasicIdInfo 不含地址码的身份证信息
12+
type BasicIdInfo struct {
13+
Birthday time.Time
14+
Constellation string
15+
ChineseZodiac string
16+
Sex int
17+
Length int
18+
CheckBit string
19+
}
20+
21+
// IsValidBasic 验证身份证号除地址信息之外的合法性
22+
func IsValidBasic(id string) bool {
23+
code, err := generateCode(id)
24+
if err != nil {
25+
return false
26+
}
27+
28+
// 检查顺序码、生日码、地址码
29+
if !checkOrderCode(code["order"]) || !checkBirthdayCode(code["birthdayCode"]) {
30+
return false
31+
}
32+
33+
// 15位身份证不含校验码
34+
if code["type"] == "15" {
35+
return true
36+
}
37+
38+
// 校验码
39+
return code["checkBit"] == generatorCheckBit(code["body"])
40+
}
41+
42+
// GetBasicInfo 获取除地址信息之外的身份证信息
43+
func GetBasicInfo(id string) (BasicIdInfo, error) {
44+
// 验证有效性
45+
if !IsValidBasic(id) {
46+
return BasicIdInfo{}, errors.New("invalid ID card number")
47+
}
48+
49+
code, _ := generateCode(id)
50+
51+
// 生日
52+
cst, _ := time.LoadLocation("Asia/Shanghai")
53+
birthday, _ := time.ParseInLocation("20060102", code["birthdayCode"], cst)
54+
55+
// 性别
56+
sex := 1
57+
order, _ := strconv.Atoi(code["order"])
58+
if (order % 2) == 0 {
59+
sex = 0
60+
}
61+
62+
// 长度
63+
length := cast.ToInt(code["type"])
64+
65+
return BasicIdInfo{
66+
Birthday: birthday,
67+
Constellation: getConstellation(code["birthdayCode"]),
68+
ChineseZodiac: getChineseZodiac(code["birthdayCode"]),
69+
Sex: sex,
70+
Length: length,
71+
CheckBit: code["checkBit"],
72+
}, nil
73+
}

id_validator_basic_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package idvalidator
2+
3+
import (
4+
"testing"
5+
)
6+
7+
// go test -v -cover -coverprofile=cover.out
8+
// go tool cover -func=cover.out
9+
// go tool cover -html=cover.out
10+
func TestIsValidBasic(t *testing.T) {
11+
errIds := []string{
12+
"44030819990110", // 号码位数不合法
13+
"440308199902301512", // 出生日期码不合法
14+
"440308199901101513", // 验证码不合法
15+
"610104620932690", // 出生日期码不合法
16+
"11010119900307867X", // 校验位不合法
17+
"TES12345678901 j", // 特殊字符格式不合法
18+
}
19+
for _, id := range errIds {
20+
if IsValidBasic(id) {
21+
t.Errorf("ID must be invalid.: %s", id)
22+
}
23+
}
24+
}
25+
26+
func TestGetInfoBasic(t *testing.T) {
27+
_, e1 := GetBasicInfo("440301197110292910")
28+
if e1 != nil {
29+
t.Errorf("`e1` must be nil.: %v", e1)
30+
}
31+
32+
_, e2 := GetBasicInfo("500154199302305886")
33+
if e2 == nil {
34+
t.Errorf("`e2` must not be nil.: %v", e2)
35+
}
36+
}

0 commit comments

Comments
 (0)