Change the access control of some functions

This commit is contained in:
guanguans 2021-01-27 11:29:38 +08:00
parent 332dd2a6d4
commit 6541375966
4 changed files with 70 additions and 70 deletions

View File

@ -9,28 +9,28 @@ import (
) )
// 检查ID参数 // 检查ID参数
func CheckIdArgument(id string) bool { func checkIdArgument(id string) bool {
_, err := GenerateCode(id) _, err := generateCode(id)
return err == nil return err == nil
} }
// 生成数据 // 生成数据
func GenerateCode(id string) (map[string]string, error) { func generateCode(id string) (map[string]string, error) {
length := len(id) length := len(id)
if length == 15 { if length == 15 {
return GenerateShortCode(id) return generateShortCode(id)
} }
if length == 18 { if length == 18 {
return GenerateLongCode(id) return generateLongCode(id)
} }
return map[string]string{}, errors.New("Invalid ID card number length.") return map[string]string{}, errors.New("Invalid ID card number length.")
} }
// 生成短数据 // 生成短数据
func GenerateShortCode(id string) (map[string]string, error) { func generateShortCode(id string) (map[string]string, error) {
if len(id) != 15 { if len(id) != 15 {
return map[string]string{}, errors.New("Invalid ID card number length.") return map[string]string{}, errors.New("Invalid ID card number length.")
} }
@ -49,7 +49,7 @@ func GenerateShortCode(id string) (map[string]string, error) {
} }
// 生成长数据 // 生成长数据
func GenerateLongCode(id string) (map[string]string, error) { func generateLongCode(id string) (map[string]string, error) {
if len(id) != 18 { if len(id) != 18 {
return map[string]string{}, errors.New("Invalid ID card number length.") return map[string]string{}, errors.New("Invalid ID card number length.")
} }
@ -67,13 +67,13 @@ func GenerateLongCode(id string) (map[string]string, error) {
} }
// 检查地址码 // 检查地址码
func CheckAddressCode(addressCode string, birthdayCode string) bool { func checkAddressCode(addressCode string, birthdayCode string) bool {
return GetAddressInfo(addressCode, birthdayCode)["province"] != "" return getAddressInfo(addressCode, birthdayCode)["province"] != ""
} }
// 检查出生日期码 // 检查出生日期码
func CheckBirthdayCode(birthdayCode string) bool { func checkBirthdayCode(birthdayCode string) bool {
year, _ := strconv.Atoi(Substr(birthdayCode, 0, 4)) year, _ := strconv.Atoi(substr(birthdayCode, 0, 4))
if year < 1800 { if year < 1800 {
return false return false
} }
@ -89,6 +89,6 @@ func CheckBirthdayCode(birthdayCode string) bool {
} }
// 检查顺序码 // 检查顺序码
func CheckOrderCode(orderCode string) bool { func checkOrderCode(orderCode string) bool {
return len(orderCode) == 3 return len(orderCode) == 3
} }

View File

@ -11,7 +11,7 @@ import (
) )
// 生成Bit码 // 生成Bit码
func GeneratorCheckBit(body string) string { func generatorCheckBit(body string) string {
// 位置加权 // 位置加权
var posWeight [19]float64 var posWeight [19]float64
for i := 2; i < 19; i++ { for i := 2; i < 19; i++ {
@ -37,7 +37,7 @@ func GeneratorCheckBit(body string) string {
} }
// 生成地址码 // 生成地址码
func GeneratorAddressCode(address string) string { func generatorAddressCode(address string) string {
addressCode := "" addressCode := ""
for code, addressStr := range data.AddressCode { for code, addressStr := range data.AddressCode {
if address == addressStr { if address == addressStr {
@ -46,45 +46,45 @@ func GeneratorAddressCode(address string) string {
} }
} }
classification := AddressCodeClassification(addressCode) classification := addressCodeClassification(addressCode)
switch classification { switch classification {
case "country": case "country":
// addressCode = GetRandAddressCode("\\d{4}(?!00)[0-9]{2}$") // addressCode = getRandAddressCode("\\d{4}(?!00)[0-9]{2}$")
addressCode = GetRandAddressCode("\\d{4}(?)[0-9]{2}$") addressCode = getRandAddressCode("\\d{4}(?)[0-9]{2}$")
case "province": case "province":
provinceCode := Substr(addressCode, 0, 2) provinceCode := substr(addressCode, 0, 2)
// pattern := "^" + provinceCode + "\\d{2}(?!00)[0-9]{2}$" // pattern := "^" + provinceCode + "\\d{2}(?!00)[0-9]{2}$"
pattern := "^" + provinceCode + "\\d{2}(?)[0-9]{2}$" pattern := "^" + provinceCode + "\\d{2}(?)[0-9]{2}$"
addressCode = GetRandAddressCode(pattern) addressCode = getRandAddressCode(pattern)
case "city": case "city":
cityCode := Substr(addressCode, 0, 4) cityCode := substr(addressCode, 0, 4)
// pattern := "^" + cityCode + "(?!00)[0-9]{2}$" // pattern := "^" + cityCode + "(?!00)[0-9]{2}$"
pattern := "^" + cityCode + "(?)[0-9]{2}$" pattern := "^" + cityCode + "(?)[0-9]{2}$"
addressCode = GetRandAddressCode(pattern) addressCode = getRandAddressCode(pattern)
} }
return addressCode return addressCode
} }
// 地址码分类 // 地址码分类
func AddressCodeClassification(addressCode string) string { func addressCodeClassification(addressCode string) string {
// 全国 // 全国
if addressCode == "" { if addressCode == "" {
return "country" return "country"
} }
// 港澳台 // 港澳台
if Substr(addressCode, 0, 1) == "8" { if substr(addressCode, 0, 1) == "8" {
return "special" return "special"
} }
// 省级 // 省级
if Substr(addressCode, 2, 6) == "0000" { if substr(addressCode, 2, 6) == "0000" {
return "province" return "province"
} }
// 市级 // 市级
if Substr(addressCode, 4, 6) == "00" { if substr(addressCode, 4, 6) == "00" {
return "city" return "city"
} }
@ -93,12 +93,12 @@ func AddressCodeClassification(addressCode string) string {
} }
// 获取随机地址码 // 获取随机地址码
func GetRandAddressCode(pattern string) string { func getRandAddressCode(pattern string) string {
mustCompile := regexp.MustCompile(pattern) mustCompile := regexp.MustCompile(pattern)
var keys []string var keys []string
for key := range data.AddressCode { for key := range data.AddressCode {
keyStr := strconv.Itoa(key) keyStr := strconv.Itoa(key)
if mustCompile.MatchString(keyStr) && Substr(keyStr, 4, 6) != "00" { if mustCompile.MatchString(keyStr) && substr(keyStr, 4, 6) != "00" {
keys = append(keys, keyStr) keys = append(keys, keyStr)
} }
} }
@ -109,25 +109,25 @@ func GetRandAddressCode(pattern string) string {
} }
// 生成出生日期码 // 生成出生日期码
func GeneratorBirthdayCode(birthday string) string { func generatorBirthdayCode(birthday string) string {
year := DatePipelineHandle(DatePad(Substr(birthday, 0, 4), "year"), "year") year := datePipelineHandle(datePad(substr(birthday, 0, 4), "year"), "year")
month := DatePipelineHandle(DatePad(Substr(birthday, 4, 6), "month"), "month") month := datePipelineHandle(datePad(substr(birthday, 4, 6), "month"), "month")
day := DatePipelineHandle(DatePad(Substr(birthday, 6, 8), "day"), "day") day := datePipelineHandle(datePad(substr(birthday, 6, 8), "day"), "day")
birthday = year + month + day birthday = year + month + day
_, error := time.Parse("20060102", birthday) _, error := time.Parse("20060102", birthday)
// example: 195578 // example: 195578
if error != nil { if error != nil {
year = DatePad(year, "year") year = datePad(year, "year")
month = DatePad(month, "month") month = datePad(month, "month")
day = DatePad(day, "day") day = datePad(day, "day")
} }
return year + month + day return year + month + day
} }
// 日期处理 // 日期处理
func DatePipelineHandle(date string, category string) string { func datePipelineHandle(date string, category string) string {
dateInt, _ := strconv.Atoi(date) dateInt, _ := strconv.Atoi(date)
switch category { switch category {
@ -155,7 +155,7 @@ func DatePipelineHandle(date string, category string) string {
} }
// 生成顺序码 // 生成顺序码
func GeneratorOrderCode(sex int) string { func generatorOrderCode(sex int) string {
rand.Seed(time.Now().Unix()) rand.Seed(time.Now().Unix())
orderCode := rand.Intn(999-111) + 111 orderCode := rand.Intn(999-111) + 111
if sex != orderCode%2 { if sex != orderCode%2 {
@ -166,7 +166,7 @@ func GeneratorOrderCode(sex int) string {
} }
// 日期补全 // 日期补全
func DatePad(date string, category string) string { func datePad(date string, category string) string {
padLength := 2 padLength := 2
if category == "year" { if category == "year" {
padLength = 4 padLength = 4

View File

@ -6,8 +6,8 @@ import (
"strings" "strings"
) )
// 检查地址码 // 获取地址信息
func GetAddressInfo(addressCode string, birthdayCode string) map[string]string { func getAddressInfo(addressCode string, birthdayCode string) map[string]string {
addressInfo := map[string]string{ addressInfo := map[string]string{
"province": "", "province": "",
"city": "", "city": "",
@ -15,29 +15,29 @@ func GetAddressInfo(addressCode string, birthdayCode string) map[string]string {
} }
// 省级信息 // 省级信息
addressInfo["province"] = GetAddress(Substr(addressCode, 0, 2)+"0000", birthdayCode) addressInfo["province"] = getAddress(substr(addressCode, 0, 2)+"0000", birthdayCode)
// 用于判断是否是港澳台居民居住证8字开头 // 用于判断是否是港澳台居民居住证8字开头
firstCharacter := Substr(addressCode, 0, 1) firstCharacter := substr(addressCode, 0, 1)
// 港澳台居民居住证无市级、县级信息 // 港澳台居民居住证无市级、县级信息
if firstCharacter == "8" { if firstCharacter == "8" {
return addressInfo return addressInfo
} }
// 市级信息 // 市级信息
addressInfo["city"] = GetAddress(Substr(addressCode, 0, 4)+"00", birthdayCode) addressInfo["city"] = getAddress(substr(addressCode, 0, 4)+"00", birthdayCode)
// 县级信息 // 县级信息
addressInfo["district"] = GetAddress(addressCode, birthdayCode) addressInfo["district"] = getAddress(addressCode, birthdayCode)
return addressInfo return addressInfo
} }
// 获取省市区地址码 // 获取省市区地址码
func GetAddress(addressCode string, birthdayCode string) string { func getAddress(addressCode string, birthdayCode string) string {
address := "" address := ""
addressCodeInt, _ := strconv.Atoi(addressCode) addressCodeInt, _ := strconv.Atoi(addressCode)
year, _ := strconv.Atoi(Substr(birthdayCode, 0, 4)) year, _ := strconv.Atoi(substr(birthdayCode, 0, 4))
for key, val := range data.AddressCodeTimeline[addressCodeInt] { for key, val := range data.AddressCodeTimeline[addressCodeInt] {
// if len(val) == 0 { // if len(val) == 0 {
// continue // continue
@ -52,9 +52,9 @@ func GetAddress(addressCode string, birthdayCode string) string {
} }
// 获取星座信息 // 获取星座信息
func GetConstellation(birthdayCode string) string { func getConstellation(birthdayCode string) string {
monthStr := Substr(birthdayCode, 4, 6) monthStr := substr(birthdayCode, 4, 6)
dayStr := Substr(birthdayCode, 6, 8) dayStr := substr(birthdayCode, 6, 8)
month, _ := strconv.Atoi(monthStr) month, _ := strconv.Atoi(monthStr)
day, _ := strconv.Atoi(dayStr) day, _ := strconv.Atoi(dayStr)
startDate := data.Constellation[month]["start_date"] startDate := data.Constellation[month]["start_date"]
@ -72,17 +72,17 @@ func GetConstellation(birthdayCode string) string {
} }
// 获取生肖信息 // 获取生肖信息
func GetChineseZodiac(birthdayCode string) string { func getChineseZodiac(birthdayCode string) string {
// 子鼠 // 子鼠
start := 1900 start := 1900
end, _ := strconv.Atoi(Substr(birthdayCode, 0, 4)) end, _ := strconv.Atoi(substr(birthdayCode, 0, 4))
key := (end - start) % 12 key := (end - start) % 12
return data.ChineseZodiac[key] return data.ChineseZodiac[key]
} }
// Substr 截取字符串 // substr 截取字符串
func Substr(source string, start int, end int) string { func substr(source string, start int, end int) string {
r := []rune(source) r := []rune(source)
length := len(r) length := len(r)

View File

@ -9,7 +9,7 @@ import (
) )
// 身份证信息 // 身份证信息
type IdInfo struct { type idInfo struct {
AddressCode int AddressCode int
Abandoned int Abandoned int
Address string Address string
@ -24,13 +24,13 @@ type IdInfo struct {
// 验证身份证号合法性 // 验证身份证号合法性
func IsValid(id string) bool { func IsValid(id string) bool {
code, err := GenerateCode(id) code, err := generateCode(id)
if err != nil { if err != nil {
return false return false
} }
// 检查顺序码、生日码、地址码 // 检查顺序码、生日码、地址码
if !CheckOrderCode(code["order"]) || !CheckBirthdayCode(code["birthdayCode"]) || !CheckAddressCode(code["addressCode"], code["birthdayCode"]) { if !checkOrderCode(code["order"]) || !checkBirthdayCode(code["birthdayCode"]) || !checkAddressCode(code["addressCode"], code["birthdayCode"]) {
return false return false
} }
@ -40,21 +40,21 @@ func IsValid(id string) bool {
} }
// 校验码 // 校验码
return code["checkBit"] == GeneratorCheckBit(code["body"]) return code["checkBit"] == generatorCheckBit(code["body"])
} }
// 获取身份证信息 // 获取身份证信息
func GetInfo(id string) (IdInfo, error) { func GetInfo(id string) (idInfo, error) {
// 验证有效性 // 验证有效性
if !IsValid(id) { if !IsValid(id) {
return IdInfo{}, errors.New("Not Valid ID card number.") return idInfo{}, errors.New("Not Valid ID card number.")
} }
code, _ := GenerateCode(id) code, _ := generateCode(id)
addressCode, _ := strconv.Atoi(code["addressCode"]) addressCode, _ := strconv.Atoi(code["addressCode"])
// 地址信息 // 地址信息
addressInfo := GetAddressInfo(code["addressCode"], code["birthdayCode"]) addressInfo := getAddressInfo(code["addressCode"], code["birthdayCode"])
var addressTree []string var addressTree []string
for _, val := range addressInfo { for _, val := range addressInfo {
addressTree = append(addressTree, val) addressTree = append(addressTree, val)
@ -79,14 +79,14 @@ func GetInfo(id string) (IdInfo, error) {
// 长度 // 长度
length, _ := strconv.Atoi(code["type"]) length, _ := strconv.Atoi(code["type"])
return IdInfo{ return idInfo{
AddressCode: addressCode, AddressCode: addressCode,
Abandoned: abandoned, Abandoned: abandoned,
Address: addressInfo["province"] + addressInfo["city"] + addressInfo["district"], Address: addressInfo["province"] + addressInfo["city"] + addressInfo["district"],
AddressTree: addressTree, AddressTree: addressTree,
Birthday: birthday, Birthday: birthday,
Constellation: GetConstellation(code["birthdayCode"]), Constellation: getConstellation(code["birthdayCode"]),
ChineseZodiac: GetChineseZodiac(code["birthdayCode"]), ChineseZodiac: getChineseZodiac(code["birthdayCode"]),
Sex: sex, Sex: sex,
Length: length, Length: length,
CheckBit: code["checkBit"], CheckBit: code["checkBit"],
@ -105,21 +105,21 @@ func FakeId() string {
// sex 性别1为男性0为女性 // sex 性别1为男性0为女性
func FakeRequireId(isEighteen bool, address string, birthday string, sex int) string { func FakeRequireId(isEighteen bool, address string, birthday string, sex int) string {
// 生成地址码 // 生成地址码
addressCode := GeneratorAddressCode(address) addressCode := generatorAddressCode(address)
// 出生日期码 // 出生日期码
birthdayCode := GeneratorBirthdayCode(birthday) birthdayCode := generatorBirthdayCode(birthday)
// 生成顺序码 // 生成顺序码
orderCode := GeneratorOrderCode(sex) orderCode := generatorOrderCode(sex)
if !isEighteen { if !isEighteen {
return addressCode + Substr(birthdayCode, 2, 8) + orderCode return addressCode + substr(birthdayCode, 2, 8) + orderCode
} }
body := addressCode + birthdayCode + orderCode body := addressCode + birthdayCode + orderCode
return body + GeneratorCheckBit(body) return body + generatorCheckBit(body)
} }
// 15位升级18位号码 // 15位升级18位号码
@ -128,9 +128,9 @@ func UpgradeId(id string) (string, error) {
return "", errors.New("Not Valid ID card number.") return "", errors.New("Not Valid ID card number.")
} }
code, _ := GenerateShortCode(id) code, _ := generateShortCode(id)
body := code["addressCode"] + code["birthdayCode"] + code["order"] body := code["addressCode"] + code["birthdayCode"] + code["order"]
return body + GeneratorCheckBit(body), nil return body + generatorCheckBit(body), nil
} }