Certification.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import { STORAGE_KEY } from "../datas/GlobalStorage";
  2. import Loader from "../Loader";
  3. import GameM, { AUDIO_TYPE } from "../manager/GameM";
  4. import UiM, { PANEL_NAME } from "../manager/UiM";
  5. import EffectNode from "./EffectNode";
  6. /** 实名认证 */
  7. const { ccclass, property } = cc._decorator;
  8. @ccclass
  9. export default class Certification extends cc.Component {
  10. @property(cc.EditBox)
  11. editBox: cc.EditBox = null
  12. @property(cc.Node)
  13. labResult: cc.Node = null
  14. start() {
  15. }
  16. clickClose() {
  17. GameM.audioM.playEffect(AUDIO_TYPE.button)
  18. UiM.Instance.offPanel(PANEL_NAME.CertificationNode)
  19. }
  20. clickSure() {
  21. GameM.audioM.playEffect(AUDIO_TYPE.button)
  22. if (this.labResult.active) {
  23. UiM.Instance.offPanel(PANEL_NAME.CertificationNode)
  24. }
  25. else {
  26. this.validateIdCard(this.editBox.string)
  27. }
  28. }
  29. onDisable() {
  30. Loader.Inst.passAnti = true
  31. }
  32. /*
  33. * 身份证15位编码规则:dddddd yymmdd xx p
  34. * dddddd:6位地区编码
  35. * yymmdd: 出生年(两位年)月日,如:910215
  36. * xx: 顺序编码,系统产生,无法确定
  37. * p: 性别,奇数为男,偶数为女
  38. *
  39. * 身份证18位编码规则:dddddd yyyymmdd xxx y
  40. * dddddd:6位地区编码
  41. * yyyymmdd: 出生年(四位年)月日,如:19910215
  42. * xxx:顺序编码,系统产生,无法确定,奇数为男,偶数为女
  43. * y: 校验码,该位数值可通过前17位计算获得
  44. *
  45. * 前17位号码加权因子为 Wi = [ 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 ]
  46. * 验证位 Y = [ 1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2 ]
  47. * 如果验证码恰好是10,为了保证身份证是十八位,那么第十八位将用X来代替
  48. * 校验位计算公式:Y_P = mod( ∑(Ai×Wi),11 )
  49. * i为身份证号码1...17 位; Y_P为校验码Y所在校验码数组位置
  50. */
  51. validateIdCard(idCard) {
  52. //15位和18位身份证号码的正则表达式
  53. var regIdCard = /^(^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$)|(^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[Xx])$)$/;
  54. //如果通过该验证,说明身份证格式正确,但准确性还需计算
  55. if (regIdCard.test(idCard)) {
  56. if (idCard.length == 18) {
  57. var idCardWi = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2); //将前17位加权因子保存在数组里
  58. var idCardY = new Array(1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2); //这是除以11后,可能产生的11位余数、验证码,也保存成数组
  59. var idCardWiSum = 0; //用来保存前17位各自乖以加权因子后的总和
  60. for (var i = 0; i < 17; i++) {
  61. idCardWiSum += idCard.substring(i, i + 1) * idCardWi[i];
  62. }
  63. var idCardMod = idCardWiSum % 11;//计算出校验码所在数组的位置
  64. var idCardLast = idCard.substring(17);//得到最后一位身份证号码
  65. //如果等于2,则说明校验码是10,身份证号码最后一位应该是X
  66. if (idCardMod == 2) {
  67. if (idCardLast == "X" || idCardLast == "x") {
  68. this.checkShowResult()
  69. } else {
  70. Loader.Inst.PlayTip("身份证号码错误!");
  71. }
  72. } else {
  73. //用计算出的验证码与最后一位身份证号码匹配,如果一致,说明通过,否则是无效的身份证号码
  74. if (idCardLast == idCardY[idCardMod]) {
  75. this.checkShowResult()
  76. } else {
  77. Loader.Inst.PlayTip("身份证号码错误!");
  78. }
  79. }
  80. }
  81. } else {
  82. Loader.Inst.PlayTip("身份证格式不正确!");
  83. }
  84. }
  85. checkShowResult() {
  86. let year = this.editBox.string.slice(6, 10)
  87. if (Number(year) > 2003) {
  88. Loader.Inst.PlayTip("恭喜通过验证啦!");
  89. this.editBox.node.active = false
  90. this.labResult.active = true
  91. GameM.commonData.identification = 2
  92. }
  93. else {
  94. Loader.Inst.PlayTip("恭喜通过验证啦!");
  95. UiM.Instance.offPanel(PANEL_NAME.CertificationNode)
  96. GameM.commonData.identification = 1
  97. }
  98. GameM.globalStorage.setStorage(STORAGE_KEY.identification, GameM.commonData.identification)
  99. }
  100. }