vehicle.go 12 KB

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