vehicle.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. package utils
  2. import (
  3. "fmt"
  4. "regexp"
  5. "strconv"
  6. "strings"
  7. "time"
  8. "unicode/utf8"
  9. "gd_auth_check/common.in/jsonrpc2"
  10. )
  11. const (
  12. VINLEN = 17
  13. PLATELEN = 6
  14. PLATENEWLEN = 7
  15. SF = "京津冀晋蒙辽吉黑沪苏浙皖闽赣鲁豫鄂湘粤桂琼渝川贵云藏陕甘青宁新"
  16. )
  17. var RUNMODE = "prod"
  18. func SetRunmode(runMode string) {
  19. RUNMODE = runMode
  20. }
  21. var vinMap = map[string]int{
  22. "0": 0,
  23. "1": 1,
  24. "2": 2,
  25. "3": 3,
  26. "4": 4,
  27. "5": 5,
  28. "6": 6,
  29. "7": 7,
  30. "8": 8,
  31. "9": 9,
  32. "A": 1,
  33. "B": 2,
  34. "C": 3,
  35. "D": 4,
  36. "E": 5,
  37. "F": 6,
  38. "G": 7,
  39. "H": 8,
  40. "J": 1,
  41. "K": 2,
  42. "L": 3,
  43. "M": 4,
  44. "N": 5,
  45. "P": 7,
  46. "R": 9,
  47. "S": 2,
  48. "T": 3,
  49. "U": 4,
  50. "V": 5,
  51. "W": 6,
  52. "X": 7,
  53. "Y": 8,
  54. "Z": 9,
  55. }
  56. var vinMatrix = []int{8, 7, 6, 5, 4, 3, 2, 10, 0, 9, 8, 7, 6, 5, 4, 3, 2}
  57. func checkVin(vin string) bool {
  58. var oldVin9 int32
  59. total := 0
  60. for i, v := range vin {
  61. if i == 8 {
  62. oldVin9 = v
  63. continue
  64. }
  65. r := rune(v)
  66. vinB := string(r)
  67. //fmt.Println("r:", string(r), vinMap[string(r)])
  68. if vinB == "O" || vinB == "I"{
  69. return false
  70. }
  71. total = total + vinMatrix[i]*vinMap[vinB]
  72. }
  73. left := total % 11
  74. newVin9 := ""
  75. if left == 10 {
  76. newVin9 = "X"
  77. } else {
  78. newVin9 = fmt.Sprintf("%d", left)
  79. }
  80. r := rune(oldVin9)
  81. if newVin9 == string(r) {
  82. return true
  83. } else {
  84. return false
  85. }
  86. }
  87. func CheckVinFormat(vin string) (string, error) {
  88. vin = strings.TrimSpace(vin)
  89. vin = strings.ToUpper(vin)
  90. /*if len(vin) != VINLEN {
  91. return vin, jsonrpc2.NewJsonError(1102, "请求参数格式不对,车架号不为17位")
  92. }*/
  93. vinLen := len(vin)
  94. strconv.Itoa(vinLen)
  95. r, _ := regexp.Compile(`[0-9A-Z]{` + strconv.Itoa(vinLen) + `}`)
  96. if r.MatchString(vin) {
  97. if vinLen == 17 && strings.HasPrefix(vin, "L") {
  98. check := checkVin(vin)
  99. if check {
  100. return vin, nil
  101. } else {
  102. return vin, jsonrpc2.NewJsonError(1102, "vin码错误")
  103. }
  104. }
  105. return vin, nil
  106. }
  107. return vin, jsonrpc2.NewJsonError(1102, "请求参数格式不对,车架号包含特殊字符")
  108. }
  109. func CheckVinFormat17Bit(vin string) (string, error) {
  110. vin = strings.TrimSpace(vin)
  111. vin = strings.ToUpper(vin)
  112. if len(vin) != VINLEN {
  113. return vin, jsonrpc2.NewJsonError(1102, "请求参数格式不对,车架号不为17位")
  114. }
  115. r, _ := regexp.Compile(`[0-9A-Z]{17}`)
  116. if r.MatchString(vin) {
  117. return vin, nil
  118. }
  119. return vin, jsonrpc2.NewJsonError(1102, "请求参数格式不对,车架号包含特殊字符")
  120. }
  121. func IsCommonPlateType(plateType string) bool {
  122. switch plateType {
  123. case "51":
  124. return true
  125. case "52":
  126. return true
  127. case "01":
  128. return true
  129. case "02":
  130. return true
  131. default:
  132. return false
  133. }
  134. }
  135. func isUnknowPlateType(plateType string) bool {
  136. switch plateType {
  137. case "02":
  138. return false
  139. case "03":
  140. return false
  141. case "04":
  142. return false
  143. case "15":
  144. return false
  145. case "16":
  146. return false
  147. case "23":
  148. return false
  149. case "24":
  150. return false
  151. case "26":
  152. return false
  153. case "27":
  154. return false
  155. default:
  156. return true
  157. }
  158. }
  159. func getPlateType(plateNo string) string {
  160. switch {
  161. case strings.HasPrefix(plateNo, "使"):
  162. return "03"
  163. case strings.HasSuffix(plateNo, "使"):
  164. return "03"
  165. // 领馆汽车
  166. case strings.HasSuffix(plateNo, "领"):
  167. return "04"
  168. case strings.HasSuffix(plateNo, "挂"):
  169. return "15"
  170. case strings.HasSuffix(plateNo, "学"):
  171. return "16"
  172. case strings.HasSuffix(plateNo, "警"):
  173. return "23"
  174. case strings.HasSuffix(plateNo, "港"):
  175. return "26"
  176. case strings.HasSuffix(plateNo, "澳"):
  177. return "27"
  178. default:
  179. return "02"
  180. }
  181. }
  182. func isSupportPlateType(plateType string) bool {
  183. switch plateType {
  184. case "01":
  185. return true
  186. case "02":
  187. return true
  188. case "03":
  189. return true
  190. case "04":
  191. return true
  192. case "05":
  193. return true
  194. case "06":
  195. return true
  196. case "07":
  197. return true
  198. case "08":
  199. return true
  200. case "09":
  201. return true
  202. case "10":
  203. return true
  204. case "11":
  205. return true
  206. case "12":
  207. return true
  208. case "13":
  209. return true
  210. case "14":
  211. return true
  212. case "15":
  213. return true
  214. case "16":
  215. return true
  216. case "17":
  217. return true
  218. case "20":
  219. return true
  220. case "21":
  221. return true
  222. case "22":
  223. return true
  224. case "23":
  225. return true
  226. case "24":
  227. return true
  228. case "25":
  229. return true
  230. case "26":
  231. return true
  232. case "27":
  233. return true
  234. case "51":
  235. return true
  236. case "52":
  237. return true
  238. case "99":
  239. return true
  240. default:
  241. return false
  242. }
  243. return false
  244. }
  245. func isContainSpecialCharactor(plateNumber []byte) bool {
  246. if strings.Contains(string(plateNumber), "I") || strings.Contains(string(plateNumber), "O") {
  247. return true
  248. }
  249. if getPlateType(string(plateNumber)) != "02" {
  250. return false
  251. }
  252. for _, v := range plateNumber {
  253. if (v >= 48 && v <= 57) || (v >= 65 && v <= 90) || (v >= 97 && v <= 122) {
  254. continue
  255. }
  256. return true
  257. }
  258. return false
  259. }
  260. func CheckPlateNoFormat(plateNo *string, plateType string) (string, error) {
  261. *plateNo = strings.TrimSpace(*plateNo)
  262. *plateNo = strings.ToUpper(*plateNo)
  263. if RUNMODE != "prod" {
  264. if strings.HasPrefix(*plateNo, "测") {
  265. return plateType, nil
  266. }
  267. }
  268. if plateType != "" {
  269. if !isSupportPlateType(plateType) {
  270. return "", jsonrpc2.NewJsonError(1103, "参数错误,不支持的车牌类型")
  271. }
  272. }
  273. // 判断车牌号码合法性
  274. sf, plateNumber := ParsePlate(*plateNo)
  275. if len(sf) == 0 {
  276. if strings.HasSuffix(*plateNo, "使") {
  277. return "03", nil
  278. }
  279. return plateType, jsonrpc2.NewJsonError(1102, "请求参数格式不对,车牌号码格式不正确")
  280. } else if sf != "使" {
  281. if !strings.Contains(SF, sf) {
  282. return plateType, jsonrpc2.NewJsonError(1102, "请求参数格式不对,不支持的省份")
  283. }
  284. }
  285. // 检查车牌是否包含特殊字符
  286. if isContainSpecialCharactor([]byte(plateNumber)) {
  287. return plateType, jsonrpc2.NewJsonError(1102, "请求参数格式不对,车牌号码包含特殊字符")
  288. }
  289. plateLen := utf8.RuneCountInString(plateNumber)
  290. if plateType == "" && plateLen == PLATENEWLEN {
  291. if plateNumber[1] > 64 && plateNumber[1] < 91 {
  292. plateType = "52"
  293. } else if plateNumber[PLATENEWLEN-1] > 64 && plateNumber[PLATENEWLEN-1] < 91 {
  294. plateType = "51"
  295. } else {
  296. return plateType, jsonrpc2.NewJsonError(1102, "请求参数格式不对,新能源汽车车牌格式不正确")
  297. }
  298. }
  299. count := 0
  300. for _, v := range plateNumber {
  301. if 64 < v && v < 91 {
  302. count++
  303. }
  304. }
  305. if strings.HasPrefix(*plateNo, "使") || strings.HasSuffix(*plateNo, "使") {
  306. if plateType == "" {
  307. return "03", nil
  308. } else {
  309. if plateType != "03" {
  310. return "", jsonrpc2.NewJsonError(1102, "请求参数格式不对,车牌和车牌类型不匹配")
  311. }
  312. return plateType, nil
  313. }
  314. } else if strings.HasSuffix(*plateNo, "领") {
  315. if plateType == "" {
  316. return "04", nil
  317. } else {
  318. if plateType != "04" {
  319. return "", jsonrpc2.NewJsonError(1102, "请求参数格式不对,车牌和车牌类型不匹配")
  320. }
  321. return plateType, nil
  322. }
  323. } else if plateType == "51" || plateType == "52" {
  324. if count > 3 {
  325. return plateType, jsonrpc2.NewJsonError(1102, "请求参数格式不对,新能源车牌号码超过3个字母")
  326. } else if plateLen != PLATENEWLEN {
  327. return plateType, jsonrpc2.NewJsonError(1102, "请求参数格式不对,新能源车牌号码长度不正确")
  328. } else {
  329. if plateNumber[1] > 64 && plateNumber[1] < 91 {
  330. if plateType != "52" {
  331. return plateType, jsonrpc2.NewJsonError(1102, "请求参数格式不对,新能源车牌号码和车牌类型不匹配")
  332. }
  333. } else if plateNumber[PLATENEWLEN-1] > 64 && plateNumber[PLATENEWLEN-1] < 91 {
  334. if plateType != "51" {
  335. return plateType, jsonrpc2.NewJsonError(1102, "请求参数格式不对,新能源车牌号码和车牌类型不匹配")
  336. }
  337. } else {
  338. return plateType, jsonrpc2.NewJsonError(1102, "请求参数格式不对,新能源汽车车牌格式不正确")
  339. }
  340. }
  341. } else {
  342. if count > 3 {
  343. return plateType, jsonrpc2.NewJsonError(1102, "请求参数格式不对,车牌号码超过3个字母")
  344. } else if plateLen != PLATELEN {
  345. return plateType, jsonrpc2.NewJsonError(1102, "请求参数格式不对,车牌号码长度不正确")
  346. }
  347. }
  348. if strings.Contains("0123456789", plateNumber[0:1]) {
  349. return plateType, jsonrpc2.NewJsonError(1102, "请求参数格式不对,号牌号码第一位不能为数字")
  350. }
  351. if plateType == "51" || plateType == "52" {
  352. return plateType, nil
  353. }
  354. if plateType != "" && isUnknowPlateType(plateType) {
  355. return plateType, nil
  356. }
  357. plateTypeNew := getPlateType(*plateNo)
  358. if plateType != "" && plateTypeNew != plateType {
  359. return plateType, jsonrpc2.NewJsonError(1102, "请求参数格式不对,车牌号码和车牌类型不匹配")
  360. }
  361. return plateTypeNew, nil
  362. }
  363. func GetPlateTypeByColor(plateNo, plateColor string) string {
  364. switch plateColor {
  365. case "0":
  366. // 蓝色
  367. return "02"
  368. case "1":
  369. //黄色 01 ,15,16
  370. if strings.HasSuffix(plateNo, "挂") {
  371. return "15"
  372. } else if strings.HasSuffix(plateNo, "学") {
  373. return "16"
  374. }
  375. return "01"
  376. case "2":
  377. // 黑色 03,04,26,27
  378. if strings.HasSuffix(plateNo, "使") || strings.HasPrefix(plateNo, "使") {
  379. return "03"
  380. } else if strings.HasSuffix(plateNo, "领") {
  381. return "04"
  382. } else if strings.HasSuffix(plateNo, "港") {
  383. return "26"
  384. } else if strings.HasSuffix(plateNo, "澳") {
  385. return "27"
  386. }
  387. return ""
  388. case "3":
  389. //白色
  390. return "23"
  391. case "4":
  392. //渐变绿色
  393. return "52"
  394. case "5":
  395. //黄绿渐变色
  396. return "51"
  397. case "6":
  398. //蓝白渐变色
  399. return ""
  400. case "9":
  401. //未确定
  402. return ""
  403. default:
  404. return ""
  405. }
  406. }
  407. func GetPlateColorByPlateType(plateType string) string {
  408. switch plateType {
  409. case "01":
  410. return "1"
  411. case "02":
  412. return "0"
  413. case "03":
  414. return "2"
  415. case "04":
  416. return "2"
  417. case "08":
  418. return "0"
  419. case "09":
  420. return "2"
  421. case "10":
  422. return "2"
  423. case "13":
  424. return "1"
  425. case "15":
  426. return "1"
  427. case "16":
  428. return "1"
  429. case "17":
  430. return "1"
  431. case "23":
  432. return "3"
  433. case "24":
  434. return "3"
  435. case "26":
  436. return "2"
  437. case "27":
  438. return "2"
  439. case "51":
  440. return "5"
  441. case "52":
  442. return "4"
  443. default:
  444. return ""
  445. }
  446. }
  447. var (
  448. idCertMatrix = []int{7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2}
  449. idCertVerifyCode = []byte("10X98765432")
  450. socialCreditMatrix = []int{1, 3, 9, 27, 19, 26, 16, 17, 20, 29, 25, 13, 8, 24, 10, 30, 28}
  451. socialCreditMap = map[int]int{'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9,
  452. 'A': 10, 'B': 11, 'C': 12, 'D': 13, 'E': 14, 'F': 15, 'G': 16, 'H': 17, 'J': 18,
  453. 'K': 19, 'L': 20, 'M': 21, 'N': 22, 'P': 23, 'Q': 24, 'R': 25, 'T': 26, 'U': 27,
  454. 'W': 28, 'X': 29, 'Y': 30}
  455. socialCreditMapKeys = []int{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'T', 'U', 'W', 'X', 'Y'}
  456. socialCreditMapValues = []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30}
  457. )
  458. func CheckIDCert(str string) (realIdCert string, res bool) {
  459. defer func() {
  460. if r := recover(); r != nil {
  461. res = false
  462. }
  463. }()
  464. idCert := strings.ToUpper(str)
  465. idCert = strings.TrimSpace(idCert)
  466. ret := idCert
  467. if len(idCert) != 18 {
  468. return "", false
  469. }
  470. year, err := strconv.Atoi(idCert[6:10])
  471. if err != nil {
  472. return "", false
  473. }
  474. if !(1900 < year && year < 2100) {
  475. return "", false
  476. }
  477. check := 0
  478. lastLetter := 0
  479. for index, value := range []byte(idCert) {
  480. if index == 17 {
  481. lastLetter = int(value)
  482. break
  483. }
  484. if !(value >= 48 && value <= 57) {
  485. return "", false
  486. }
  487. v := value - 48
  488. check = check + idCertMatrix[index]*int(v)
  489. }
  490. if !((lastLetter >= 48 && lastLetter <= 57) || lastLetter == 'X') {
  491. return "", false
  492. }
  493. timeLayout := "20060102" //转化所需模板
  494. loc, _ := time.LoadLocation("Local") //重要:获取时区
  495. _, err = time.ParseInLocation(timeLayout, idCert[6:14], loc) //使用模板在对应时区转化为time.time类型
  496. if err != nil {
  497. return "", false
  498. }
  499. verifyCode := int(idCertVerifyCode[check%11])
  500. if lastLetter == verifyCode {
  501. return ret, true
  502. }
  503. return "", false
  504. }