7
0

Change the access control of some functions

This commit is contained in:
guanguans 2021-01-27 11:29:38 +08:00
джерело 332dd2a6d4
коміт 6541375966
4 змінених файлів з 70 додано та 70 видалено

@ -9,28 +9,28 @@ import (
)
// 检查ID参数
func CheckIdArgument(id string) bool {
_, err := GenerateCode(id)
func checkIdArgument(id string) bool {
_, err := generateCode(id)
return err == nil
}
// 生成数据
func GenerateCode(id string) (map[string]string, error) {
func generateCode(id string) (map[string]string, error) {
length := len(id)
if length == 15 {
return GenerateShortCode(id)
return generateShortCode(id)
}
if length == 18 {
return GenerateLongCode(id)
return generateLongCode(id)
}
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 {
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 {
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 {
return GetAddressInfo(addressCode, birthdayCode)["province"] != ""
func checkAddressCode(addressCode string, birthdayCode string) bool {
return getAddressInfo(addressCode, birthdayCode)["province"] != ""
}
// 检查出生日期码
func CheckBirthdayCode(birthdayCode string) bool {
year, _ := strconv.Atoi(Substr(birthdayCode, 0, 4))
func checkBirthdayCode(birthdayCode string) bool {
year, _ := strconv.Atoi(substr(birthdayCode, 0, 4))
if year < 1800 {
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
}

@ -11,7 +11,7 @@ import (
)
// 生成Bit码
func GeneratorCheckBit(body string) string {
func generatorCheckBit(body string) string {
// 位置加权
var posWeight [19]float64
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 := ""
for code, addressStr := range data.AddressCode {
if address == addressStr {
@ -46,45 +46,45 @@ func GeneratorAddressCode(address string) string {
}
}
classification := AddressCodeClassification(addressCode)
classification := addressCodeClassification(addressCode)
switch classification {
case "country":
// addressCode = GetRandAddressCode("\\d{4}(?!00)[0-9]{2}$")
addressCode = GetRandAddressCode("\\d{4}(?)[0-9]{2}$")
// addressCode = getRandAddressCode("\\d{4}(?!00)[0-9]{2}$")
addressCode = getRandAddressCode("\\d{4}(?)[0-9]{2}$")
case "province":
provinceCode := Substr(addressCode, 0, 2)
provinceCode := substr(addressCode, 0, 2)
// pattern := "^" + provinceCode + "\\d{2}(?!00)[0-9]{2}$"
pattern := "^" + provinceCode + "\\d{2}(?)[0-9]{2}$"
addressCode = GetRandAddressCode(pattern)
addressCode = getRandAddressCode(pattern)
case "city":
cityCode := Substr(addressCode, 0, 4)
cityCode := substr(addressCode, 0, 4)
// pattern := "^" + cityCode + "(?!00)[0-9]{2}$"
pattern := "^" + cityCode + "(?)[0-9]{2}$"
addressCode = GetRandAddressCode(pattern)
addressCode = getRandAddressCode(pattern)
}
return addressCode
}
// 地址码分类
func AddressCodeClassification(addressCode string) string {
func addressCodeClassification(addressCode string) string {
// 全国
if addressCode == "" {
return "country"
}
// 港澳台
if Substr(addressCode, 0, 1) == "8" {
if substr(addressCode, 0, 1) == "8" {
return "special"
}
// 省级
if Substr(addressCode, 2, 6) == "0000" {
if substr(addressCode, 2, 6) == "0000" {
return "province"
}
// 市级
if Substr(addressCode, 4, 6) == "00" {
if substr(addressCode, 4, 6) == "00" {
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)
var keys []string
for key := range data.AddressCode {
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)
}
}
@ -109,25 +109,25 @@ func GetRandAddressCode(pattern string) string {
}
// 生成出生日期码
func GeneratorBirthdayCode(birthday string) string {
year := DatePipelineHandle(DatePad(Substr(birthday, 0, 4), "year"), "year")
month := DatePipelineHandle(DatePad(Substr(birthday, 4, 6), "month"), "month")
day := DatePipelineHandle(DatePad(Substr(birthday, 6, 8), "day"), "day")
func generatorBirthdayCode(birthday string) string {
year := datePipelineHandle(datePad(substr(birthday, 0, 4), "year"), "year")
month := datePipelineHandle(datePad(substr(birthday, 4, 6), "month"), "month")
day := datePipelineHandle(datePad(substr(birthday, 6, 8), "day"), "day")
birthday = year + month + day
_, error := time.Parse("20060102", birthday)
// example: 195578
if error != nil {
year = DatePad(year, "year")
month = DatePad(month, "month")
day = DatePad(day, "day")
year = datePad(year, "year")
month = datePad(month, "month")
day = datePad(day, "day")
}
return year + month + day
}
// 日期处理
func DatePipelineHandle(date string, category string) string {
func datePipelineHandle(date string, category string) string {
dateInt, _ := strconv.Atoi(date)
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())
orderCode := rand.Intn(999-111) + 111
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
if category == "year" {
padLength = 4

@ -6,8 +6,8 @@ import (
"strings"
)
// 检查地址码
func GetAddressInfo(addressCode string, birthdayCode string) map[string]string {
// 获取地址信息
func getAddressInfo(addressCode string, birthdayCode string) map[string]string {
addressInfo := map[string]string{
"province": "",
"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字开头
firstCharacter := Substr(addressCode, 0, 1)
firstCharacter := substr(addressCode, 0, 1)
// 港澳台居民居住证无市级、县级信息
if firstCharacter == "8" {
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
}
// 获取省市区地址码
func GetAddress(addressCode string, birthdayCode string) string {
func getAddress(addressCode string, birthdayCode string) string {
address := ""
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] {
// if len(val) == 0 {
// continue
@ -52,9 +52,9 @@ func GetAddress(addressCode string, birthdayCode string) string {
}
// 获取星座信息
func GetConstellation(birthdayCode string) string {
monthStr := Substr(birthdayCode, 4, 6)
dayStr := Substr(birthdayCode, 6, 8)
func getConstellation(birthdayCode string) string {
monthStr := substr(birthdayCode, 4, 6)
dayStr := substr(birthdayCode, 6, 8)
month, _ := strconv.Atoi(monthStr)
day, _ := strconv.Atoi(dayStr)
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
end, _ := strconv.Atoi(Substr(birthdayCode, 0, 4))
end, _ := strconv.Atoi(substr(birthdayCode, 0, 4))
key := (end - start) % 12
return data.ChineseZodiac[key]
}
// Substr 截取字符串
func Substr(source string, start int, end int) string {
// substr 截取字符串
func substr(source string, start int, end int) string {
r := []rune(source)
length := len(r)

@ -9,7 +9,7 @@ import (
)
// 身份证信息
type IdInfo struct {
type idInfo struct {
AddressCode int
Abandoned int
Address string
@ -24,13 +24,13 @@ type IdInfo struct {
// 验证身份证号合法性
func IsValid(id string) bool {
code, err := GenerateCode(id)
code, err := generateCode(id)
if err != nil {
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
}
@ -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) {
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"])
// 地址信息
addressInfo := GetAddressInfo(code["addressCode"], code["birthdayCode"])
addressInfo := getAddressInfo(code["addressCode"], code["birthdayCode"])
var addressTree []string
for _, val := range addressInfo {
addressTree = append(addressTree, val)
@ -79,14 +79,14 @@ func GetInfo(id string) (IdInfo, error) {
// 长度
length, _ := strconv.Atoi(code["type"])
return IdInfo{
return idInfo{
AddressCode: addressCode,
Abandoned: abandoned,
Address: addressInfo["province"] + addressInfo["city"] + addressInfo["district"],
AddressTree: addressTree,
Birthday: birthday,
Constellation: GetConstellation(code["birthdayCode"]),
ChineseZodiac: GetChineseZodiac(code["birthdayCode"]),
Constellation: getConstellation(code["birthdayCode"]),
ChineseZodiac: getChineseZodiac(code["birthdayCode"]),
Sex: sex,
Length: length,
CheckBit: code["checkBit"],
@ -105,21 +105,21 @@ func FakeId() string {
// sex 性别1为男性0为女性
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 {
return addressCode + Substr(birthdayCode, 2, 8) + orderCode
return addressCode + substr(birthdayCode, 2, 8) + orderCode
}
body := addressCode + birthdayCode + orderCode
return body + GeneratorCheckBit(body)
return body + generatorCheckBit(body)
}
// 15位升级18位号码
@ -128,9 +128,9 @@ func UpgradeId(id string) (string, error) {
return "", errors.New("Not Valid ID card number.")
}
code, _ := GenerateShortCode(id)
code, _ := generateShortCode(id)
body := code["addressCode"] + code["birthdayCode"] + code["order"]
return body + GeneratorCheckBit(body), nil
return body + generatorCheckBit(body), nil
}