@@ -8,11 +8,11 @@ import (
88 "math"
99 "math/rand"
1010 "regexp"
11- "strconv"
1211 "strings"
1312 "time"
1413
1514 "github.com/guanguans/id-validator/data"
15+ "github.com/spf13/cast"
1616)
1717
1818// 生成Bit码
@@ -29,42 +29,36 @@ func generatorCheckBit(body string) string {
2929 bodyArray := strings .Split (body , "" )
3030 count := len (bodyArray )
3131 for i := 0 ; i < count ; i ++ {
32- bodySub , _ := strconv .Atoi (bodyArray [i ])
33- bodySum += bodySub * int (posWeight [18 - i ])
32+ bodySum += cast .ToInt (bodyArray [i ]) * int (posWeight [18 - i ])
3433 }
3534
3635 // 生成校验码
3736 checkBit := (12 - (bodySum % 11 )) % 11
3837 if checkBit == 10 {
3938 return "x"
4039 }
41- return strconv . Itoa (checkBit )
40+ return cast . ToString (checkBit )
4241}
4342
4443// 生成地址码
4544func generatorAddressCode (address string ) string {
4645 addressCode := ""
4746 for code , addressStr := range data .AddressCode {
4847 if address == addressStr {
49- addressCode = strconv . Itoa (code )
48+ addressCode = cast . ToString (code )
5049 break
5150 }
5251 }
5352
5453 classification := addressCodeClassification (addressCode )
5554 switch classification {
5655 case "country" :
57- // addressCode = getRandAddressCode("\\d{4}(?!00)[0-9]{2}$")
5856 addressCode = getRandAddressCode ("\\ d{4}(?)[0-9]{2}$" )
5957 case "province" :
60- provinceCode := substr (addressCode , 0 , 2 )
61- // pattern := "^" + provinceCode + "\\d{2}(?!00)[0-9]{2}$"
62- pattern := "^" + provinceCode + "\\ d{2}(?)[0-9]{2}$"
58+ pattern := "^" + substr (addressCode , 0 , 2 ) + "\\ d{2}(?)[0-9]{2}$"
6359 addressCode = getRandAddressCode (pattern )
6460 case "city" :
65- cityCode := substr (addressCode , 0 , 4 )
66- // pattern := "^" + cityCode + "(?!00)[0-9]{2}$"
67- pattern := "^" + cityCode + "(?)[0-9]{2}$"
61+ pattern := "^" + substr (addressCode , 0 , 4 ) + "(?)[0-9]{2}$"
6862 addressCode = getRandAddressCode (pattern )
6963 }
7064
@@ -102,7 +96,7 @@ func getRandAddressCode(pattern string) string {
10296 mustCompile := regexp .MustCompile (pattern )
10397 var keys []string
10498 for key := range data .AddressCode {
105- keyStr := strconv . Itoa (key )
99+ keyStr := cast . ToString (key )
106100 if mustCompile .MatchString (keyStr ) && substr (keyStr , 4 , 6 ) != "00" {
107101 keys = append (keys , keyStr )
108102 }
@@ -121,7 +115,7 @@ func generatorBirthdayCode(addressCode string, address string, birthday string)
121115 month := datePipelineHandle (datePad (substr (birthday , 4 , 6 ), "month" ), "month" )
122116 day := datePipelineHandle (datePad (substr (birthday , 6 , 8 ), "day" ), "day" )
123117
124- addressCodeInt , _ := strconv . Atoi (addressCode )
118+ addressCodeInt := cast . ToInt (addressCode )
125119 if _ , ok := data .AddressCodeTimeline [addressCodeInt ]; ok {
126120 timeLine := data .AddressCodeTimeline [addressCodeInt ]
127121 for _ , val := range timeLine {
@@ -136,13 +130,11 @@ func generatorBirthdayCode(addressCode string, address string, birthday string)
136130 }
137131 }
138132
139- yearInt , _ := strconv .Atoi (year )
140- startYearInt , _ := strconv .Atoi (startYear )
141- endYearInt , _ := strconv .Atoi (endYear )
142- if yearInt < startYearInt {
133+ yearInt := cast .ToInt (year )
134+ if yearInt < cast .ToInt (startYear ) {
143135 year = startYear
144136 }
145- if yearInt > endYearInt {
137+ if yearInt > cast . ToInt ( endYear ) {
146138 year = endYear
147139 }
148140
@@ -160,26 +152,26 @@ func generatorBirthdayCode(addressCode string, address string, birthday string)
160152
161153// 日期处理
162154func datePipelineHandle (date string , category string ) string {
163- dateInt , _ := strconv . Atoi (date )
155+ dateInt := cast . ToInt (date )
164156
165157 switch category {
166158 case "year" :
167159 nowYear := time .Now ().Year ()
168160 rand .Seed (time .Now ().Unix ())
169161 if dateInt < 1800 || dateInt > nowYear {
170162 randDate := rand .Intn (nowYear - 1950 ) + 1950
171- date = strconv . Itoa (randDate )
163+ date = cast . ToString (randDate )
172164 }
173165 case "month" :
174166 if dateInt < 1 || dateInt > 12 {
175167 randDate := rand .Intn (12 - 1 ) + 1
176- date = strconv . Itoa (randDate )
168+ date = cast . ToString (randDate )
177169 }
178170
179171 case "day" :
180172 if dateInt < 1 || dateInt > 31 {
181173 randDate := rand .Intn (28 - 1 ) + 1
182- date = strconv . Itoa (randDate )
174+ date = cast . ToString (randDate )
183175 }
184176 }
185177
@@ -194,7 +186,7 @@ func generatorOrderCode(sex int) string {
194186 orderCode --
195187 }
196188
197- return strconv . Itoa (orderCode )
189+ return cast . ToString (orderCode )
198190}
199191
200192// 日期补全
0 commit comments