check.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package check
  4. import (
  5. pb_v1 "adm-task/pb/v1"
  6. "context"
  7. "fmt"
  8. "regexp"
  9. "strconv"
  10. "strings"
  11. "time"
  12. "unicode"
  13. "unicode/utf8"
  14. "google.golang.org/grpc/status"
  15. )
  16. const (
  17. VINLEN = 17
  18. PLATELEN = 6
  19. PLATENEWLEN = 7
  20. SF = "京津冀晋蒙辽吉黑沪苏浙皖闽赣鲁豫鄂湘粤桂琼渝川贵云藏陕甘青宁新"
  21. )
  22. var RUNMODE = "prod"
  23. func SetRunmode(runMode string) {
  24. RUNMODE = runMode
  25. }
  26. var vinMap = map[string]int{
  27. "0": 0,
  28. "1": 1,
  29. "2": 2,
  30. "3": 3,
  31. "4": 4,
  32. "5": 5,
  33. "6": 6,
  34. "7": 7,
  35. "8": 8,
  36. "9": 9,
  37. "A": 1,
  38. "B": 2,
  39. "C": 3,
  40. "D": 4,
  41. "E": 5,
  42. "F": 6,
  43. "G": 7,
  44. "H": 8,
  45. "J": 1,
  46. "K": 2,
  47. "L": 3,
  48. "M": 4,
  49. "N": 5,
  50. "P": 7,
  51. "R": 9,
  52. "S": 2,
  53. "T": 3,
  54. "U": 4,
  55. "V": 5,
  56. "W": 6,
  57. "X": 7,
  58. "Y": 8,
  59. "Z": 9,
  60. }
  61. var vinMatrix = []int{8, 7, 6, 5, 4, 3, 2, 10, 0, 9, 8, 7, 6, 5, 4, 3, 2}
  62. func checkVin(vin string) bool {
  63. var oldVin9 int32
  64. total := 0
  65. for i, v := range vin {
  66. if i == 8 {
  67. oldVin9 = v
  68. continue
  69. }
  70. r := rune(v)
  71. total = total + vinMatrix[i]*vinMap[string(r)]
  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, status.Error(20003, "请求参数格式不对,车架号不为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, status.Error(20003, "vin码错误")
  103. }
  104. }
  105. return vin, nil
  106. }
  107. return vin, status.Error(20003, "请求参数格式不对,车架号包含特殊字符")
  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, status.Error(20003, "请求参数格式不对,车架号不为17位")
  114. }
  115. r, _ := regexp.Compile(`[0-9A-Z]{17}`)
  116. if r.MatchString(vin) {
  117. return vin, nil
  118. }
  119. return vin, status.Error(20003, "请求参数格式不对,车架号包含特殊字符")
  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 ParsePlate(plate string) (string, string) {
  261. var index int
  262. for i, r := range plate {
  263. if !unicode.Is(unicode.Scripts["Han"], r) {
  264. index = i
  265. break
  266. }
  267. }
  268. sf := plate[0:index]
  269. hphm := plate[index:]
  270. return sf, hphm
  271. }
  272. func CheckPlateNoFormat(plateNo *string, plateType string) (string, error) {
  273. *plateNo = strings.TrimSpace(*plateNo)
  274. *plateNo = strings.ToUpper(*plateNo)
  275. if RUNMODE != "prod" {
  276. if strings.HasPrefix(*plateNo, "测") {
  277. return plateType, nil
  278. }
  279. }
  280. if plateType != "" {
  281. if !isSupportPlateType(plateType) {
  282. return "", status.Error(20003, "参数错误,不支持的车牌类型")
  283. }
  284. }
  285. // 判断车牌号码合法性
  286. sf, plateNumber := ParsePlate(*plateNo)
  287. if len(sf) == 0 {
  288. if strings.HasSuffix(*plateNo, "使") {
  289. return "03", nil
  290. }
  291. return plateType, status.Error(20003, "请求参数格式不对,车牌号码格式不正确")
  292. } else if sf != "使" {
  293. if !strings.Contains(SF, sf) {
  294. return plateType, status.Error(20003, "请求参数格式不对,不支持的省份")
  295. }
  296. }
  297. // 检查车牌是否包含特殊字符
  298. if isContainSpecialCharactor([]byte(plateNumber)) {
  299. return plateType, status.Error(20003, "请求参数格式不对,车牌号码包含特殊字符")
  300. }
  301. plateLen := utf8.RuneCountInString(plateNumber)
  302. if plateType == "" && plateLen == PLATENEWLEN {
  303. if plateNumber[1] > 64 && plateNumber[1] < 91 {
  304. plateType = "52"
  305. } else if plateNumber[PLATENEWLEN-1] > 64 && plateNumber[PLATENEWLEN-1] < 91 {
  306. plateType = "51"
  307. } else {
  308. return plateType, status.Error(20003, "请求参数格式不对,新能源汽车车牌格式不正确")
  309. }
  310. }
  311. count := 0
  312. for _, v := range plateNumber {
  313. if 64 < v && v < 91 {
  314. count++
  315. }
  316. }
  317. if strings.HasPrefix(*plateNo, "使") || strings.HasSuffix(*plateNo, "使") {
  318. if plateType == "" {
  319. return "03", nil
  320. } else {
  321. if plateType != "03" {
  322. return "", status.Error(20003, "请求参数格式不对,车牌和车牌类型不匹配")
  323. }
  324. return plateType, nil
  325. }
  326. } else if strings.HasSuffix(*plateNo, "领") {
  327. if plateType == "" {
  328. return "04", nil
  329. } else {
  330. if plateType != "04" {
  331. return "", status.Error(20003, "请求参数格式不对,车牌和车牌类型不匹配")
  332. }
  333. return plateType, nil
  334. }
  335. } else if plateType == "51" || plateType == "52" {
  336. if count > 3 {
  337. return plateType, status.Error(20003, "请求参数格式不对,新能源车牌号码超过3个字母")
  338. } else if plateLen != PLATENEWLEN {
  339. return plateType, status.Error(20003, "请求参数格式不对,新能源车牌号码长度不正确")
  340. } else {
  341. if plateNumber[1] > 64 && plateNumber[1] < 91 {
  342. if plateType != "52" {
  343. return plateType, status.Error(20003, "请求参数格式不对,新能源车牌号码和车牌类型不匹配")
  344. }
  345. } else if plateNumber[PLATENEWLEN-1] > 64 && plateNumber[PLATENEWLEN-1] < 91 {
  346. if plateType != "51" {
  347. return plateType, status.Error(20003, "请求参数格式不对,新能源车牌号码和车牌类型不匹配")
  348. }
  349. } else {
  350. return plateType, status.Error(20003, "请求参数格式不对,新能源汽车车牌格式不正确")
  351. }
  352. }
  353. } else {
  354. if count > 3 {
  355. return plateType, status.Error(20003, "请求参数格式不对,车牌号码超过3个字母")
  356. } else if plateLen != PLATELEN {
  357. return plateType, status.Error(20003, "请求参数格式不对,车牌号码长度不正确")
  358. }
  359. }
  360. if strings.Contains("0123456789", plateNumber[0:1]) {
  361. return plateType, status.Error(20003, "请求参数格式不对,号牌号码第一位不能为数字")
  362. }
  363. if plateType == "51" || plateType == "52" {
  364. return plateType, nil
  365. }
  366. if plateType != "" && isUnknowPlateType(plateType) {
  367. return plateType, nil
  368. }
  369. plateTypeNew := getPlateType(*plateNo)
  370. if plateType != "" && plateTypeNew != plateType {
  371. return plateType, status.Error(20003, "请求参数格式不对,车牌号码和车牌类型不匹配")
  372. }
  373. // 不返回默认
  374. return "", nil
  375. }
  376. func GetPlateTypeByColor(plateNo, plateColor string) string {
  377. switch plateColor {
  378. case "0":
  379. // 蓝色
  380. return "02"
  381. case "1":
  382. //黄色 01 ,15,16
  383. if strings.HasSuffix(plateNo, "挂") {
  384. return "15"
  385. } else if strings.HasSuffix(plateNo, "学") {
  386. return "16"
  387. }
  388. return "01"
  389. case "2":
  390. // 黑色 03,04,26,27
  391. if strings.HasSuffix(plateNo, "使") || strings.HasPrefix(plateNo, "使") {
  392. return "03"
  393. } else if strings.HasSuffix(plateNo, "领") {
  394. return "04"
  395. } else if strings.HasSuffix(plateNo, "港") {
  396. return "26"
  397. } else if strings.HasSuffix(plateNo, "澳") {
  398. return "27"
  399. }
  400. return ""
  401. case "3":
  402. //白色
  403. return "23"
  404. case "4":
  405. //渐变绿色
  406. return "52"
  407. case "5":
  408. //黄绿渐变色
  409. return "51"
  410. case "6":
  411. //蓝白渐变色
  412. return ""
  413. case "9":
  414. //未确定
  415. return ""
  416. default:
  417. return ""
  418. }
  419. }
  420. func GetPlateColorByPlateType(plateType string) string {
  421. switch plateType {
  422. case "01":
  423. return "1"
  424. case "02":
  425. return "0"
  426. case "03":
  427. return "2"
  428. case "04":
  429. return "2"
  430. case "08":
  431. return "0"
  432. case "09":
  433. return "2"
  434. case "10":
  435. return "2"
  436. case "13":
  437. return "1"
  438. case "15":
  439. return "1"
  440. case "16":
  441. return "1"
  442. case "17":
  443. return "1"
  444. case "23":
  445. return "3"
  446. case "24":
  447. return "3"
  448. case "26":
  449. return "2"
  450. case "27":
  451. return "2"
  452. case "51":
  453. return "5"
  454. case "52":
  455. return "4"
  456. default:
  457. return ""
  458. }
  459. }
  460. var (
  461. idCertMatrix = []int{7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2}
  462. idCertVerifyCode = []byte("10X98765432")
  463. socialCreditMatrix = []int{1, 3, 9, 27, 19, 26, 16, 17, 20, 29, 25, 13, 8, 24, 10, 30, 28}
  464. socialCreditMap = map[int]int{'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9,
  465. 'A': 10, 'B': 11, 'C': 12, 'D': 13, 'E': 14, 'F': 15, 'G': 16, 'H': 17, 'J': 18,
  466. 'K': 19, 'L': 20, 'M': 21, 'N': 22, 'P': 23, 'Q': 24, 'R': 25, 'T': 26, 'U': 27,
  467. 'W': 28, 'X': 29, 'Y': 30}
  468. 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'}
  469. 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}
  470. )
  471. func CheckIDCert(str string) (realIdCert string, res bool) {
  472. defer func() {
  473. if r := recover(); r != nil {
  474. res = false
  475. }
  476. }()
  477. idCert := strings.ToUpper(str)
  478. idCert = strings.TrimSpace(idCert)
  479. ret := idCert
  480. if len(idCert) != 18 {
  481. return "", false
  482. }
  483. year, err := strconv.Atoi(idCert[6:10])
  484. if err != nil {
  485. return "", false
  486. }
  487. if !(1900 < year && year < 2100) {
  488. return "", false
  489. }
  490. check := 0
  491. lastLetter := 0
  492. for index, value := range []byte(idCert) {
  493. if index == 17 {
  494. lastLetter = int(value)
  495. break
  496. }
  497. if !(value >= 48 && value <= 57) {
  498. return "", false
  499. }
  500. v := value - 48
  501. check = check + idCertMatrix[index]*int(v)
  502. }
  503. if !((lastLetter >= 48 && lastLetter <= 57) || lastLetter == 'X') {
  504. return "", false
  505. }
  506. timeLayout := "20060102" //转化所需模板
  507. loc, _ := time.LoadLocation("Local") //重要:获取时区
  508. _, err = time.ParseInLocation(timeLayout, idCert[6:14], loc) //使用模板在对应时区转化为time.time类型
  509. if err != nil {
  510. return "", false
  511. }
  512. verifyCode := int(idCertVerifyCode[check%11])
  513. if lastLetter == verifyCode {
  514. return ret, true
  515. }
  516. return "", false
  517. }
  518. func CheckFormat(ctx context.Context, req *pb_v1.CheckFormatRequest) (reply *pb_v1.CheckFormatReply, err error) {
  519. reply = &pb_v1.CheckFormatReply{}
  520. if req.Vin != "" {
  521. reply.Vin, err = CheckVinFormat(req.Vin)
  522. if err != nil {
  523. return reply, err
  524. }
  525. }
  526. if req.Idcert != "" {
  527. idcert, isOk := CheckIDCert(req.Idcert)
  528. if !isOk {
  529. return reply, status.Error(20003, "请求参数格式不对,身份证错误")
  530. }
  531. reply.Idcert = idcert
  532. }
  533. if req.PlateNo != "" {
  534. reply.PlateType, err = CheckPlateNoFormat(&req.PlateNo, req.PlateType)
  535. if err != nil {
  536. return reply, err
  537. }
  538. reply.PlateNo = req.PlateNo
  539. }
  540. return reply, nil
  541. }